so I'm trying to have tell the servo to goto a certain position when the arduino receive a letter over the serial port, It kind of works but not really, the servo moves to its position but goes back and doesn't really respond afterward, I have the led go high and low to verify its receiving the letter and attempting to write the servo position, but that's not working either.
#include <Servo.h>
Servo myservo;
const int ledPin = 13;
int incomingByte;
void setup(){
myservo.attach(9);
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop()
{
if (Serial.available() > 0) {
incomingByte = Serial.read();
if (incomingByte == 'H') {
myservo.write(180);
digitalWrite(ledPin, HIGH);
}
if (incomingByte == 'L') {
myservo.write(0);
digitalWrite(ledPin, LOW);
}
}
}
Is your baud rate from the IDE same as that in the program? If you force incoming byte to be H in the code, does the servo move? How are you powering the servo?
Try leaving out the If(Serial.Available() > 0) and then maybe add a 5 millisecond delay. Not sure if you need the delay though, try it with the delay and without
The servo has enough power, I put on one of the sample sketches that sweeps the servo from 0-180 and it works perfectly fine, so power isn't an issue. I'm also using the arduino processing serial monitor, so communication shouldn't be an issue. But how does one go about changing the incoming byte variable to character? thanks for the help!
Sorry, but the return type of "Seria.read" is "int".
True, but, if you KNOW that there is data to read, because you have called Serial.available(), you can store the output in a byte sized variable, with no data loss. Often, this is a desirable thing to do.
in the code i have a servo move to 180 degrees if it detects that the button is in a HIGH state and it reads an H from the serial port. The individual pieces of the circuit work when being tested by them selves so its not hardware related as far as i can tell, I'm thinking it has something to do with the && operators? Any ideas?
So you have to type a command AND use a button to move the servo? Are you sure you don't need to use || instead, so you either type a command, or use the button ?
the servo will be used to lock a deadbolt, the switch will detect when the door is closed so the deadbolt only moves when the door is closed, also i ahave already tried it with || with no luck