Hello I am kind of new to all this so, if I behave like a noob, I am sorry.
I am working on a project for my studies, in which we are trying to make a snake like lamp that moves, when people are standing close to it.
We made a working snake, on which you pull wires to move it in a certain direction.
So to achieve this pulling we use 2 servos 1 for each axis (which would pull 2 wires, so in rest the servo would be at 90 degrees and when a person is close to one side the servo will go that way by going to 0 or 180 depending on which side the person is on) as sensors, we use LDRs one on each side of the snake, so that if a person is standing in front of it the light will be blocked en the snake will "know" to go in that direction.
I am first trying to get the control of 1 servo working with one LDR, but I am having some problems. (BTW using my hand to block the LDR atm, but will of course change the value's accordingly once I get it working)
When I map the LDR value's to 0-180 and then write them to the servo, it works, but it is more advanced then what I need + it is quite sensitive so the servo will keep moving between small degrees even though the person (hand) is standing still and it is also quite hard to let it reach the 180 and 0 degrees (but of course I can make this easier by lowering the range of the mapped value's)
My code for this:
#include <Servo.h>
Servo myservo;
int ldr = A0; //input pin for sensor
int val; //variable for sensor value's
int pos = 90; //rest position
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
Serial.begin(9600);
pinMode(ldr, INPUT);
myservo.write(pos); //write rest position
}
void loop()
{
Serial.println(analogRead(ldr)); // Print the value from the sensor
val = analogRead(ldr); // Read sensor value
val= map(val, 300, 700 , 0 , 180); //map sensor value to value's of servo
myservo.write(val); //move servo to position given by the amount of light
delay(100); //wait for restart
}
So for a less advanced and better working system (in our case at least), I tried to use if/else statements, to compare the sensor value's to a threshold(=sensor value's when light is blocked by person/hand) and then write 180 or 0 degrees to the servos, but I can't seem to get it working. For now I only hooked up 1 LDR and want it to go to 180 for when the LDR is blocked and to 0 when it isn't, so that I could see if it would work.
My code for this:
#include <Servo.h>
Servo myservo;
int ldr = A0; //input pin for sensor
int val; //variable for sensor value's
int thresh = 600; //threshold LDR
int pos = 0; //rest position
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
Serial.begin(9600);
pinMode(ldr, INPUT);
myservo.write(pos); //write rest position
}
void loop()
{
Serial.println(analogRead(ldr)); // Print the value from the sensor
val = analogRead(ldr); // Read sensor vlue
if (val < thresh) { //compare threshold to sensor value
myservo.write(180); //move servo to position given by the amount of light
delay(300);
}
else if (val > thresh) { //compare threshold to sensor value
myservo.write(0); //move servo to position given by the amount of light
delay(300);
}
}
With this code the servo only resets to the the 90degrees rest position and after that, it doesn't move at all, when I put my hand in front of the LDR or not, while if I open the serial monitor, it clearly show value's below and above the threshold, so I don't know what the problem is.
Does anyone know what's wrong with the code, or has any suggestions for a better way to achieve this.
(I know LDRs aren't the perfect solution for this kind of thing, but we need to keep the costs low and it only has to show our concept, so it will be sufficient)
Thanks in advance!
Forgot to post specific hardware I am using:
- Arduino Uno R3
- 2 Blue Bird BMS-620MG servo's
- Separate power supply for the servo (6V and 1.5A)
- LDRs (with 10K resistors)