Hey all, I'm currently learning and here is the problem. Using a joystick I'm controlling 3 servos that move robotic eye, up-down, left-right, and blink(push-button). I also need a constant loop of a pulse in the forehead with a forth servo. The problem is because of the .5 sec delay (500) in the pulse loop it is slowing down the input, output of the eye servos. Is there a way to take out the delay and use another function or sort the sketch out better for this application? Thanks in advance!
The sketch:
#include <Servo.h>
//Eye movments
const int VERT = 2;
const int HORIZ = 1;
Servo lr; //servo moving eye left to right Pin 22
Servo ud; //servo moving eye up to down Pin 23
int servoVal;
//Blink
int button1 = 4; //pushbutton Pin 4
int press1 = 0;
Servo Blink;
//Center a servo
Servo centerServo; // To true center of a servo
//Pulse
Servo Pulse; //Servo name
int pos = 0;
void setup()
{
//Eye movments
Serial.begin(9600);
lr.attach(22);
ud.attach(23);
//Blink
pinMode(button1, INPUT);
Blink.attach (11);
digitalWrite(4, HIGH); //enable pullups to make pin high
//Center Servo
centerServo.attach(21);
centerServo.write(90); // sets the servo position to 90° (is half of 180°)
//Pulse
Pulse.attach(17); //Servo pin
}
void loop()
{
//Eye movments
servoVal = analogRead(HORIZ);
servoVal = map(servoVal, 0, 1023, 0, 179);
ud.write(servoVal);
servoVal = analogRead(VERT);
servoVal = map(servoVal, 0, 1023, 0, 179);
lr.write(servoVal);
// Blink
press1 = digitalRead(button1);
if (press1 == LOW)
{
Blink.write(160);
}
else {
Blink.write(0);
delay(5);
//Pulse
for (pos = 2; pos <= 20; pos += 1) { // 2 degrees to 20 degrees
// in steps of 1 degree
Pulse.write(pos); // tell servo to go to position in variable 'pos'
delay(5); // waits 15ms for the servo to reach the position
}
for (pos = 20; pos >= 1; pos -= 1) { // 20 degrees to 1 degrees
Pulse.write(pos); // tell servo to go to position in variable 'pos'
delay(15);
}
for (pos = 1; pos <= 21; pos += 1) { // 1 degrees to 21 degrees
// in steps of 1 degree
Pulse.write(pos); // tell servo to go to position in variable 'pos'
delay(5);
}
for (pos = 21; pos >= 0; pos -= 1) { // goes from 21 degrees to 0 degrees
Pulse.write(pos); // tell servo to go to position in variable 'pos'
delay(15);
}
{
if (pos = 0); pos >= 2; {pos += 1; // counts from 0 to 2
Serial.write(pos); // counts from 0 to 2
delay(500);
}
}}}