Help with my Code

First project... here is what i have done, i want the servo to turn when the motion sensor sees something (me in the morning making coffee, the cat walking by or an army of evil honey badgers) and then turn a servo (to turn on lights, make coffee or thwart said honey badgers). it should leave a light on while not in action.. i do not have my stuff yet, will probably get it tomorrow, could someone test it or tell me if they see any errors beforehand (the stuff will take a while to arrive so do not worry about being too late)

const byte ledPin = 13; // LED pin
const byte motionPin = 2; // motion detector input pin
byte senseMotion = 0; // variable to hold current state of motion detector

void setup() {
// set the digital pin directions
pinMode(ledPin, OUTPUT);
pinMode(motionPin, INPUT);
servoMain.attach(10); // servo on digital pin 10
}

void loop()
{
// Now watch for burglers
senseMotion = digitalRead(motionPin);
if (senseMotion == HIGH) { // burgler found!
servoMain.write(90); // Turn Servo Left to 90 degrees
} else { // no burgler, yet...
digitalWrite(ledPin, HIGH);
}
}

thank you,

:slight_smile:

#include <Servo.h>  
Servo servoMain;  // create servo object to control a servo

I assume that you #include the servo library and create the instance of servoMain, but I don't see it in your code.

i just noticed (i think) that and that the servo would never return to default after turning on. here is my revision... tell me if i addressed the issue you found

const byte ledPin = 13; // LED pin
const byte motionPin = 2; // motion detector input pin
byte senseMotion = 0; // variable to hold current state of motion detector
Servo servoMain; // Define our Servo

void setup() {
// set the digital pin directions
pinMode(ledPin, OUTPUT);
pinMode(motionPin, INPUT);
servoMain.attach(10); // servo on digital pin 10
}

void loop()
{
// Now watch for burglers
senseMotion = digitalRead(motionPin);
if (senseMotion == HIGH) { // burgler found!
servoMain.write(0); // Turn Servo Left to 0 degrees
delay(1000); // Wait 1 second
servoMain.write(90); // Turn Servo back to center position (90 degrees)
delay(1000); // Wait 1 second
} else { // no burgler, yet...
digitalWrite(ledPin, LOW);
}
}

you still need to include the servo library at the very beginning of your code.
#include <Servo.h>

like this?

#include <Servo.h>
const byte ledPin = 13; // LED pin
const byte motionPin = 2; // motion detector input pin
byte senseMotion = 0; // variable to hold current state of motion detector

void setup() {
// set the digital pin directions
pinMode(ledPin, OUTPUT);
pinMode(motionPin, INPUT);
}

void loop()
{
// Now watch for burglers
senseMotion = digitalRead(motionPin);
if (senseMotion == HIGH) { // burgler found!
digitalWrite(ledPin, HIGH);
} else { // no burgler, yet...
digitalWrite(ledPin, LOW);
}
}

Yes, like that. Your code should work. If not we are here all week. ; ) I hope you have a ton of fun with Arduino. A note on using servo with Arduino. You can probably drive the servo from the Arduino 5V as long as there is no load on it. If you will be using a servo to do any work you will need an external supply for the servo. 4 AA batteries works real well and will power the servo for quite a while.

no worries, that has been thought of

i am very much a hardware guy

i just got this error message

Turret_rev_1:4: error: 'Servo' does not name a type
Turret_rev_1.ino: In function 'void setup()':
Turret_rev_1:10: error: 'servoMain' was not declared in this scope
Turret_rev_1.ino: In function 'void loop()':
Turret_rev_1:18: error: 'servoMain' was not declared in this scope

when i discovered the verify button, any ideas?

scratch that last bit, i put your advice in the wrong program.... thank you

my bad

. . . . . .. . . . . . . . . . . ,.-‘”. . . . . . . . . .~., . . . . . . . .. . . . . .,.-”. . . . . . . . . . . . . . . . . .“-., . . . . .. . . . . . ..,/. . . . . . . . . . . . . . . . . . . . . . . ”:, . . . . . . . .. .,?. . . . . . . . . . . . . . . . . . . . . . . . . . .\, . . . . . . . . . /. . . . . . . . . . . . . . . . . . . . . . . . . . . . ,} . . . . . . . . ./. . . . . . . . . . . . . . . . . . . . . . . . . . ,:`^`.} . . . . . . . ./. . . . . . . . . . . . . . . . . . . . . . . . . ,:”. . . ./ . . . . . . .?. . . __. . . . . . . . . . . . . . . . . . . . :`. . . ./ . . . . . . . /__.(. . .“~-,_. . . . . . . . . . . . . . ,:`. . . .. ./ . . . . . . /(_. . ”~,_. . . ..“~,_. . . . . . . . . .,:`. . . . _/ . . . .. .{.._$;_. . .”=,_. . . .“-,_. . . ,.-~-,}, .~”; /. .. .} . . .. . .((. . .*~_. . . .”=-._. . .“;,,./`. . /” . . . ./. .. ../ . . . .. . .\`~,. . ..“~.,. . . . . . . . . ..`. . .}. . . . . . ../ . . . . . .(. ..`=-,,. . . .`. . . . . . . . . . . ..(. . . ;_,,-” . . . . . ../.`~,. . ..`-.. . . . . . . . . . . . . . ..\. . /\ . . . . . . \`~.*-,. . . . . . . . . . . . . . . . . ..|,./.....\,__ ,,_. . . . . }.>-._\. . . . . . . . . . . . . . . . . .|. . . . . . ..`=~-, . .. `=~-,_\_. . . `\,. . . . . . . . . . . . . . . . .\ . . . . . . . . . .`=~-,,.\,. . . . . . . . . . . . . . . .\ . . . . . . . . . . . . . . . . `:,, . . . . . . . . . . . . . `\. . . . . . ..__ . . . . . . . . . . . . . . . . . . .`=-,. . . . . . . . . .,%`>--==
. . . . . . . . . . . . . . . . . . . . . . . . . .,-%. . . ..`