I have arduino code that reads serial. It reads what servo to write to, and what pulse. I can get it to pick the servo by input of a, s, or d. But I can't get it to change pulse. How can I have 2 different types of serial input.
Current Code
int servoPin[] = {4,7};
int pos[] = {1800, 1800};
long lastPulse = 0;
int refreshTime = 20;
char val;
int i = 0;
void setup()
{
pinMode(servoPin[0], OUTPUT);
pinMode(servoPin[1], OUTPUT);
Serial.begin(9600);
}
void loop()
{
val = Serial.read();
if(val == 'a')
{
i = 0;
}
if(val == 's')
{
i = 1;
}
if (millis() - lastPulse >= refreshTime)
{
digitalWrite(servoPin[i], HIGH); // start the pulse
delayMicroseconds(pos[i]); // pulse width
digitalWrite(servoPin[i], LOW); // stop the pulse
lastPulse = millis(); // save the time of the last pulse
}
}