Hello guys, I'm having problems on my code.
#include <Servo.h>
Servo HTservo;
int ms;
int center=1500;
int six=600;
int toggleA=5;
int toggleB=10;
int toggleAstatus;
int toggleBstatus;
void setup()
{
Serial.begin(9600);
while (!Serial);
Serial.println("Servo Test");
HTservo.attach(9);
HTservo.writeMicroseconds(center);
Serial.println("Servo Adjust to:");
Serial.print(center);
Serial.print("ms");
pinMode(toggleA,INPUT_PULLUP);
pinMode(toggleB,INPUT_PULLUP);
}
void loop()
{
toggle();
}
void toggle()
{
toggleAstatus=digitalRead(toggleA);
toggleBstatus=digitalRead(toggleB);
if (Serial.available())
{
int ms = Serial.parseInt();
if (toggleAstatus == HIGH && toggleBstatus == HIGH)
{
HTservo.writeMicroseconds(ms);
Serial.println("Servo Rotate:");
Serial.print(ms);
Serial.println("ms");
}
if (toggleAstatus == LOW)
{
HTservo.writeMicroseconds(ms + six);
Serial.println("Servo Rotate:");
Serial.print(ms + six);
Serial.println("ms");
}
if (toggleBstatus == LOW)
{
HTservo.writeMicroseconds(ms - six);
Serial.println("Servo Rotate:");
Serial.print(ms - six);
Serial.println("ms");
}
}
}
I tried to put a new value(for position of servo), my code works upto
if (toggleAstatus == HIGH && toggleBstatus == HIGH)
{
HTservo.writeMicroseconds(ms);
Serial.println("Servo Rotate:");
Serial.print(ms);
Serial.println("ms");
}
but after I change the position of the toggle, it doesn't work anymore.