Servo moves inconsistent amounts (time-sensitive)

I am getting values of 0 and 1020 from the Serial Monitor. Should the 1020 be 1023 or is that not an issue? It doesn’t seem to be affecting anything…

I get 1020 also.
It is no issue, your program works as long as this value is more than 200.
It is caused by the motion sensor bit itself, when activated is sends a signal a littlebit less than 5 volt.
Exactly 5 volt would be 1023, so I think it sends 1020/1023 x 5 = 4.98 volt. No problem.
My setup works as expected now.

Here is the new code:

int motionTrigger=0;
int servoMotor= 5;

void setup() {

Serial.begin (9600);
pinMode (0, INPUT);
pinMode (5, OUTPUT);
}

void loop() {

int motionTriggerValue= analogRead(motionTrigger);
Serial.println (motionTriggerValue);

if (motionTriggerValue > 0)
{
analogWrite (servoMotor, 180);
Serial.println (“servo did move”);
delay (1000);
} else {
analogWrite (servoMotor, 0);
delay (1000);
Serial.println (“servo did not move”);
}
}

I think the only step left is finding the right value in
analogWrite (servoMotor, 180);

Finally, is there a way for the motion trigger to only output a signal for 1 or 2 seconds? I feel that 3 is a bit long, as the servo cannot return to its original position when the signal is still on.

That’s not possible, the sensor cannot be changed itself…
However you could program the servo to return quicker.
Try this alternative, it moves the servo back after 0.5 sec and then it must wait for the sensor to stop it’s signal.
:grinning:

int motionTrigger=0;
int servoMotor= 5;

void setup() {

Serial.begin (9600);
pinMode (0, INPUT);
pinMode (5, OUTPUT);
}

void loop() {

int motionTriggerValue= analogRead(motionTrigger);
Serial.println (motionTriggerValue);

if (motionTriggerValue > 0)
{
analogWrite (servoMotor, 180);delay(500);analogWrite (servoMotor, 0);
Serial.println (“servo did move”);
delay (4000);
}
}

Just experiment with the values of 4000 and 500 until it does what you want it to do. :grinning:

is there an else statement, or is that not necessary?

It works without an else statement. When the trigger value is zero the program just jumps over the if statement and starts from the beginning. it works.

Okay, I’ll test it.
The original code might also work for me though

1 Like

It worked!!!
Thank you so much for your help, @alexpikkert

1 Like

Great ! Can you show the project when it is finished ? :grinning: :bitstar:

Wait, maybe the code does need something else.

The servo is suposed to be uncovering a clock and turning back.
Instead, it covers and then automatically UNCOVERS the clock. Should I add an else statement?

Here is the new code:

int motionTrigger=0; //tells Arduino that pin 0 can also be called motionTrigger
int servoMotor= 5; //tells Arduino that pin 5 can also be called servoMotor

void setup() { //setup section, which sets up the code for the loop section

Serial.begin (9600); //begins serial connection with computer at 9600 bits per second
pinMode (0, INPUT); //tells pin motionTrigger to be an INPUT
pinMode (5, OUTPUT); //tells pin servoMotor to be an OUTPUT
} //end of setup section

void loop() { //loop section repeats over and over

int motionTriggerValue= analogRead(motionTrigger);
Serial.println (motionTriggerValue); //reads in on voltage on motionTrigger pin

if (motionTriggerValue > 1) //if motionTriggerValue is greater than 0
{
analogWrite (servoMotor, 180); //then move the servoMotor at a power of 180 (corresponds to about 3.6 volts)
delay(1500); // delay for 1500 milleseconds (1.5 seconds)
Serial.println (“servo did move”); //tells me that the servo “did move”
delay (3000); // delay for 3000 milleseconds (3.0 seconds)
} else {
analogWrite (servoMotor, 0); // moves the servoMotor at a power of 0, or 0 volts
Serial.println (“servo didn’t move”); //tells me that the servo didn’t move
} //end of “else” statement
} //end of “loop” section

If the 180 setting on your servo is the covered position for the clock then,

perhaps you just need to add another

analogWrite(servoMotor, 180); 

to the if statement to cover it back up?

I am not entirely following the code anymore, but maybe right after the delay(4000);

I would love to see the final project too :smile:

Now it’s not moving at all

int motionTrigger=0; //tells Arduino that pin 0 can also be called motionTrigger
int servoMotor= 5; //tells Arduino that pin 5 can also be called servoMotor

void setup() { //setup section, which sets up the code for the loop section

Serial.begin (9600); //begins serial connection with computer at 9600 bits per second
pinMode (0, INPUT); //tells pin motionTrigger to be an INPUT
pinMode (5, OUTPUT); //tells pin servoMotor to be an OUTPUT
} //end of setup section

void loop() { //loop section repeats over and over

int motionTriggerValue= analogRead(motionTrigger);
Serial.println (motionTriggerValue); //reads in on voltage on motionTrigger pin

if (motionTriggerValue > 1) //if motionTriggerValue is greater than 1
{
analogWrite (servoMotor, 180); //then move the servoMotor at a power of 180 (corresponds to about 3.6 volts)
delay(1500); // delay for 1500 milleseconds (1.5 seconds)
analogWrite (servoMotor, 180); //then move the servoMotor at a power of 180 (corresponds to about 3.6 volts)
Serial.println (“servo did move”); //tells me that the servo “did move”
delay (3000); // delay for 3000 milleseconds (3.0 seconds)
} //end of"if" statement
} //end of “loop” section

hmm…based on the new code you posted maybe you need to add

analogWrite(servoMotor, 0); 

after the delay(3000);

sorry, I was looking at your old code

After adding analogWrite (servoMotor, 0); I have the servo moving automatically. So now it says

if (motionTriggerValue >=1020), which I think fixed it.

1 Like

Could you just relocate the cover on the servo? That’s much easier I think… :grinning:

Thank you @alexpikkert @wilsonrl @Yuziana and @JackANDJude for all your help.

Now that the science fair is over, I wanted to let you know how it went. Again, the point
of the project is that I only want to see the clock sometimes in the night, so the servo only moves the motion trigger is activated. Here is the code (along with my notes on what the code does):

int motionTrigger=0; //tells Arduino that pin 0 can also be called motionTrigger
int servoMotor= 5; //tells Arduino that pin 5 can also be called servoMotor

void setup() { //setup section, which sets up the code for the loop section

Serial.begin (9600); //begins serial connection with computer at 9600 bits per second
pinMode (0, INPUT); //tells pin motionTrigger to be an INPUT
pinMode (5, OUTPUT); //tells pin servoMotor to be an OUTPUT
} //end of setup section

void loop() { //loop section repeats over and over

int motionTriggerValue= analogRead(motionTrigger);
Serial.println (motionTriggerValue); //reads in on voltage on motionTrigger pin

if (motionTriggerValue >=1020) //if motionTriggerValue is greater than or equal to 1020…
{
analogWrite (servoMotor, 180); //then move the servoMotor at a power of 180 (corresponds to about 3.6 volts)
delay(1500); // delay for 1500 milleseconds (1.5 seconds)
analogWrite (servoMotor, 180); //then move the servoMotor at a power of 180 (corresponds to about 3.6 volts)
Serial.println (“servo did move”); //tells me that the servo “did move”
analogWrite (servoMotor, 0); // then move the servoMotor at a power of 0 (corresponds to 0 volts)
delay (3000); // delay for 3000 milleseconds (3.0 seconds)
} //end of"if" statement
} //end of “loop” section

Here is a video: Motion Detector Clock

2 Likes

Well done! I hope everyone liked his project.
It looked like he worked really hard on it. :smiley: