Servo moves inconsistent amounts (time-sensitive)

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: