#include <Servo.h>
int vals = 0;
int down = 0;
int up = 0;
Servo servox;
Servo servoy;
void setup() {
servox.attach(2);
servoy.attach(3);
Serial.begin(9600);
Serial.println("Servo Serial Control");
}
void loop()
{
if (Serial.available())
{
vals = Serial.read();
if(vals == 115&& down <=140)
{
down = down + 10;
servox.write(down);
}
if(vals == 119&& up <=140)
{
up = up + 10;
servox.write(up);
}
}
}
I just want to move the servo up or down when w (up) or s (down)
is pressed,
so, servo start at 90 degrees
when w is pressed +10 and when s is pressed -10
and repeat that.
I do not understand why you are using 2 variables, up and down.
If I understand what you are trying to do (and correct me if I am wrong) is to use the two keys to move up and down.
Use a single variable, and increment it or decrement it based on which keys are pressed.
But there is one thing,
when I upload the code and open my COM Terminal it works great for about a minute. Then it freezes and resets.
Sometimes it does so randomly.
So what is the solution? Does this concern the Baud rate?