I am trying to control a servo with a light sensor in a condition where light level changes in a very narrow amount not much change.
I need to be mapping little change of input signal to a big rotation by servo.
So I played a bit in the map function as shown below...
but I am not getting good result..serve rotates very little angle with light change...
I need advice on this appreciate help.
/*
Make a Servo Move to light change.
This example code is in the public domain.
*/
// for servo stuff we include the servo library
#include <Servo.h>
// creating a servo object
Servo myservo;
// Some Varuables we need
int ServoPin = 9;
int SensorPin = A0;
int Mover = 0 ;
// the setup routine runs once when you press reset:
void setup() {
// initialize
myservo.attach(ServoPin);
pinMode(SensorPin, INPUT);
}
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(SensorPin);
// We Map it here down to the possible range of servo moovment.
Mover = map(sensorValue,0,50,0,175);
// note normally the analog reading is 0- 1023, but i used 0-50 to get more motion from little change in light readings.
myservo.write(Mover);
}
Sketch is for a a LDR light sensitive resistor than causes the servo to rotate at any light change. But the spot where it is located takes very little light change..How to know know if light change between 0-50 or 973-1023?? great question...anyone can help to answer it...
I did not understand the comment about Mover...if I remove this line
You have Mover defined as a global variable, but it doesn't need to be a global, but it does need to be defined.
(it helps us to help you if, when you say "then the code would not compile", you actually tell us why it doesn't compile. Otherwise, we have to guess, as I have done here)
Many thanks..PaulS tip worked...using the serial print showed that light change value between 900-1023
so when I changed this line
Mover = map(sensorValue,900,1023,0,175);
Now servo responds very well to light change...
but sadly, the analog pin A0 pics noise so even when no light change, servo still shakes back and forth...
I checked the serial print...even when no light change, A0 still reading small values between 900-950
I tried to reduce servo mapping like this
Mover = map(sensorValue,0,50,30,120);
but servo still moves back and forth from picked noise..
I tried to use other analog input like A1 or A2, but also having same problem...
any advice?
You need to introduce some hysteresis into the system so that you move the servos only if the analogue input has changed by a significant amount since the last time the value was read. This will prevent minor variations on the inputs being passed on to the servos.
UKHeliBob:
You need to introduce some hysteresis into the system so that you move the servos only if the analogue input has changed by a significant amount since the last time the value was read. This will prevent minor variations on the inputs being passed on to the servos.
forgive my limited knowledge..do u have any link to understand this comment ?
Yes Paul tip worked. also found that problems disappear if we provide seperate supple for the servo s...The sudden spike in power draw once the servo moves badly disturbs the code.