Hi, I need help for my Servo control. I use a EWTS86 sensor to
measure the angle of my head movement, then I use the following code to capture the signals from sensor in order to control the Servo turning angles.
The problem is that when everytime my movement stops, the sensor's signal will be set to 512(analog value), after map() functions, it means 90 degree. so the Servo turns to the 90 degree position. but actually what I need is that when I stop moving, the Servo should also stops too instead of going back to the 90 degree position.
Need your help.
int servopin = 13;
int readValue =0;// Serial.read();
int value = 0;
int sensor = 0;
void setup()
{
pinMode(servopin, OUTPUT); // sets the digital pin as output
Serial.begin(9600);
Serial.available();
}
void loop()
{
value = analogRead(sensor);
//Serial.println(value);
value = map(value, 0, 1023, 0, 179);
servopulse(value);
for(int i=0;i<50;i++)
{
servopulse(readValue);
}
}
void servopulse(int angle)
{
int pulsewidth=(angle*11)+500;
digitalWrite(servopin,HIGH);
delayMicroseconds(pulsewidth);
digitalWrite(servopin,LOW);
}