This is my first project with arduino and I can't seem to be able to tie the servo to activate when the PIR sensor turns on. The LED turns on with the PIR sensor though. Also I'm using the Grove Kit. Any help would be very much appreciated!
#include <Servo.h>
const int pinServo = 5; // pin of servo
const int signal = 3;
const int led = 2;
int pir = 3;
Servo groveServo;Â // create servo object to control a servo
int shaft;Â Â // variable to read the value from the analog pin
void setup() {
groveServo.attach(pinServo);
pinMode (signal, INPUT);
pinMode(led, OUTPUT);
}
void loop() {
Â
pir = digitalRead(signal);
digitalWrite(led, pir);
delay(200);
shaft = digitalRead(pir);Â
shaft = map(shaft, 0, 1023, 0, 179);Â Â
groveServo.write(shaft);
delay(15);
Â
}
The result from digitalRead() is 0 or 1, meaning the digital input is LOW or HIGH.
shaft = map(shaft, 0, 1023, 0, 179);
The statement above looks like it came from a servo control program reads from an analog input pin, which gives a result in the range 0 to 1023. It maps that range to 0 to 179 degrees for the servo.
When you try to map 0 and 1 to that range, you will get 0 and 0 as the output, so the servo will not move.
Try this instead:
shaft = digitalRead(pir);Â
if (shaft == 1)
{
  shaft = 179;
}
groveServo.write(shaft);
The servo should now move from one end position to the other depending on your digital input.
@Ray Thanks! It works if I make the digitalRead(signal) and not digitalRead(pir). So since this is digital, it doesn't allow for the option to keep the servo at the stop location until the sensor is activated once again right? Right now, it will go to the predefined stop of 179 and then return back to the original stop of 1 and not stay at 179. Maybe I need to make the loop from stopping after it reaches 179?
shaft = digitalRead(signal);Â
if (shaft == 1)
{
  shaft = 179;
}
groveServo.write(shaft);
Hackscribble: EDIT: sorry, I was getting my pirs and my signals completely mixed up
Can you clarify what you want the program to do when the PIR activates, when it deactivates and when it activates again?
Ah sorry! I'd like the servo to turn just a few notches then stay at location. Right now, the servo turns the full distance and then returns back to the original spot once the sensor turns off.
I'd like the servo to turn just a few notches then stay at location. Right now, the servo turns the full distance and then returns back to the original spot once the sensor turns off.
And once it moves to the new location, not move again (no matter what the PIR does)?
In that case, you can simplify the program.
At the top, put define statements to say what the start and end positions are for the servo:
#define SERVO_START_POS 0 // Starting position in degrees
#define SERVO_END_POS 45 // End position in degrees - change this to what you want
At the end of the setup() function, put this:
groveServo.write(SERVO_START_POS); // Make sure the servo is in the starting position
while (digitalRead(signal) == LOW)
{
// Do nothing until the PIR input goes high
}
groveServo.write(SERVO_END_POS);
Delete all the code in loop(). The code in setup() is executed once, and then the Arduino will wait until you reset it or power it off and on.