Hey there,
I have a project which is move by a servo and controlled by a push bottom and it is work very will and I am thinking to use the IR sensor rather than the push bottom using same code with a little editing , could any one help me with that ?
here is the code
#include <Servo.h>
// Set digital pin numbers: const int servoPin = 8; // The number of the Servo pin
const int buttonPin = 9; // The number of the Pushbutton pin
int buttonState = 0; // Variable for reading the pushbutton status
int directionState = 0; // Variable for reading direction of the servo
Servo myservo; // Create servo object to control a servo
int pos = 0; // Variable to store the servo position
void setup() {
myservo.attach(8); // attaches the servo on pin 8 to the servo object
pinMode(buttonPin, INPUT); // initialize the pushbutton pin as an input
}
void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
if (directionState == 0){
//The button is pushed
if (buttonState == HIGH) {
directionState = 1;// The direction for the servo is clockwise
// goes from 0 degrees to 180 degrees in steps of 1 degree
for(pos = 0; pos < 180; pos=pos+5)
{
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
}
}
} else if (directionState == 1) {
// The button is pushed
if (buttonState == HIGH) {
directionState = 0; // The direction for the servo is anti-clockwise
// goes from 180 degrees to 0 degrees in steps of 1 degree
for(pos = 180; pos>=1; pos=pos-5)
{
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
}
}
}
}
It's unclear what type of IR sensor your planning to use ?:
with analog output (intensity) .
with the digital output you can just use in the same way as your button.
The analog type you have to evaluate the intensity. Roughly it would be like this.
if (analogRead(IRpin) > 512 ) //read IR sensor on analog pin, compare analog value 0...1023, 0..5Volt
#include <Servo.h>
const int IRpin = 3; // The pinnumber of the IR sensor
int directionState = 0; // Variable for reading direction of the servo
Servo myservo; // Create servo object to control a servo
int pos = 0; // Variable to store the servo position
void setup()
{ myservo.attach(8); // attaches the servo on pin 8 to the servo object
}
void loop()
{ if (analogRead(IRpin) > 512 ) //read IR sensor on analog pin, compare analog value 0...1023, 0..5Volt
{ if (directionState == 1)
{ for(pos = 180; pos>=1; pos=pos-5)
{ myservo.write(pos);
delay(15);
}
}
else // directionState == 0
{ for(pos = 0; pos < 180; pos=pos+5)
{ myservo.write(pos);
delay(15);
}
}
directionState = ~directionState;
}//fi
}//loop
Thank you astrofrostbyte for your code and help , yes I am using Sharp 2Y0A21(Infrared Proximity Sensor), and I try your code but nothing work(the IR and the Servo), do I have to plug the IR sensor in analog port (3) or the digital one ?
Thank you!
Thank you HazardsMin for your help,
both codes are work right now because I was't attach the servo in the right port. Now, the servo just work by it self and it seems the IR sensor is not working and there is no numbers when I run Serial Monitor to check the reading. By the way I attached the IR to Analog 3.
I am looking to move the servo based on how far the person from the sensor , so it is react toward the person present.
here is the last version of code.
#include <Servo.h>
const int IRpin = 3; // The pinnumber of the IR sensor
int directionState = 0; // Variable for reading direction of the servo
Servo myservo; // Create servo object to control a servo
int pos = 0; // Variable to store the servo position
void setup()
{ myservo.attach(8); // attaches the servo on pin 8 to the servo object
}
void loop()
{ if (analogRead(IRpin) > 512 ) //read IR sensor on analog pin, compare analog value 0...1023, 0..5Volt
{ if (directionState == 1)
{ for(pos = 180; pos>=1; pos=pos-5)
{ myservo.write(pos);
delay(15);
}
}
else // directionState == 0
{ for(pos = 0; pos < 180; pos=pos+5)
{ myservo.write(pos);
delay(15);
}
}
directionState = !directionState;
}//fi
}//loop
Instead of jumping right in with both the servo and sensor, test them both individually.
Your servo will not work properly, running on the 5 volts from the arduino. You will harm the arduino if you continue to do that. Get a separate power supply around 6 volts to power the servo.
Also the best way to see if your sensor is working, is to use the serial monitor.
Add Serial.begin(9600); to setup() and use Serial.println(analogRead(IRpin)); to see the values your getting from the sensor.
I test the sharp sensor and it is work as well as the servo, but I cant figure out what is wrong with this code. All I need is have the code move the sensor based on the person present.
Yes, I edit the code and I will try to explain what I want to do:
1- A person be closer to the IR sensor.
2- if the Sensor read more than 300 (val > 300).
3- the Servo will move from 0 to 180.
4- As soon as the person there in the range of ( val >300) the servo does not move.
5- Whenever the person goes back and the Sensor read is less than 300 (val < 300).
5- The Servo will move back from 180 to 0.
6- As soon as no body there, the Servo will hold in this position.
I try to write a code for that but it is not work as I want.
Yes, I edit the code and I will try to explain what I want to do:
1- A person be closer to the IR sensor.
2- if the Sensor read more than 300 (val > 300).
3- the Servo will move from 0 to 180.
4- As soon as the person there in the range of ( val >300) the servo does not move.
5- Whenever the person goes back and the Sensor read is less than 300 (val < 300).
5- The Servo will move back from 180 to 0.
6- As soon as no body there, the Servo will hold in this position.
Ok, 2 and 6 will conflict with each other, if you don't set more limits. I don't know the sensitivity of your sensor, so I don't know how far out it can accurately take measurements. Now with your current needs, you want the servo to activate, lets say open (0 - 180), when someone is within a range of 300 "something". You also want the servo to close when the person is outside the range of 300, and you want the servo to hold its position when the person is completely gone. Does that sound right?
My questions to you are how far can your sensor accurately measure, and at what point is the person considered completely gone? The code for that is simple, you just need to get more information in order to put it together the way you want it.
if(less than 300) open up
else if(greater than 300, but less than say 500) close
else hold servo position
Thank you Dear HazardsMind for your help and replay ,
I don't know the sensitivity of your sensor, so I don't know how far out it can accurately take measurements.
the limit of the IR sharp sensor is about (34 to 587).
Now with your current needs, you want the servo to activate, lets say open (0 - 180), when someone is within a range of 300 "something". You also want the servo to close when the person is outside the range of 300, and you want the servo to hold its position when the person is completely gone. Does that sound right?
Yes, it open (0-180) when some one is within a range of 300 and grater ,and hold on this position as soon as the person still in this range, Second when the person out side the range of 300, the servo close ( 180 - 0) and hold on this position as far as no body there.
My questions to you are how far can your sensor accurately measure, and at what point is the person considered completely gone?
as i said up it is from ( 34- 587) , and I am consider any point less than 300 is considering no body there.