For Im trying to make the servo go 45 degrees when I press a and then 90 degrees when I press b on my keyboard, but it only turns once to another degree and doesnt respond to anything else.
Here is my code
#include <Servo.h>
int inByte = 0; // initialize the variable inByte
Servo servo1;
void setup(){
servo1.attach(2);
Serial.begin(9600); // set serial monitor to same speed
}
void loop(){
if (Serial.available()>0) { // check if any data received
inByte = Serial.read(); // yes, so read it from incoming buffer
Serial.print(inByte, DEC);
if(inByte == 97){ // so when I press a
servo1.write(45);}
else if (inByte == 98){ // so when I press b
servo1.write(45);}
jeanpaulbadjo:
, but it only turns once to another degree and doesnt respond to anything else.
Have you noticed that both of your servo1.write() statements tell it to move to 45 degrees?
If you use the AutoFormat tool to lay out your code consistently it will be much easier to read. Every } should be on a line on its own so you can clearly see where a block of code ends.
It looks like you don't exactly understand servos. Servo.write(xxx) sends the servo to a specific position xxx. It doesn't tell it to move by xxx from where it is now. So if you always write the same value e.g. 45 then it won't move because it's already there.