Offline
Newbie
Karma: 0
Posts: 13
|
 |
« on: October 12, 2012, 10:13:53 pm » |
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); }
} }
|
|
|
|
|
Logged
|
|
|
|
|
Sydney
Offline
God Member
Karma: 14
Posts: 717
Big things come in large packages
|
 |
« Reply #1 on: October 12, 2012, 10:18:46 pm » |
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?
|
|
|
|
« Last Edit: October 12, 2012, 10:20:45 pm by marco_c »
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 6
|
 |
« Reply #2 on: October 13, 2012, 02:31:22 am » |
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
|
|
|
|
|
Logged
|
|
|
|
|
Manchester (England England)
Offline
Brattain Member
Karma: 272
Posts: 25464
Solder is electric glue
|
 |
« Reply #3 on: October 13, 2012, 03:05:48 am » |
Try leaving out the If(Serial.Available() > 0) No don't. Change the incoming byte variable to a char.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 137
Posts: 19015
I don't think you connected the grounds, Dave.
|
 |
« Reply #4 on: October 13, 2012, 03:26:09 am » |
Have you connected the grounds?
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #5 on: October 13, 2012, 04:09:23 am » |
Try giving it more power.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 13
|
 |
« Reply #6 on: October 13, 2012, 09:03:56 am » |
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!
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 137
Posts: 19015
I don't think you connected the grounds, Dave.
|
 |
« Reply #7 on: October 13, 2012, 09:14:13 am » |
Are you actually hitting "send"?
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #8 on: October 13, 2012, 09:37:57 am » |
But how does one go about changing the incoming byte variable to character? thanks for the help! Using the text editor, of course. Highlight the incorrect type, and type the correct type.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 137
Posts: 19015
I don't think you connected the grounds, Dave.
|
 |
« Reply #9 on: October 13, 2012, 09:46:03 am » |
Sorry, but the return type of "Seria.read" is "int".
OP, what observations have you made? What else have you tried?
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #10 on: October 13, 2012, 09:59:37 am » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 13
|
 |
« Reply #11 on: October 13, 2012, 06:43:06 pm » |
SO anybody have any idea what is up? the sketch works perfectly fine if i remove the moving of the servo, the LED part I mean.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 13
|
 |
« Reply #12 on: October 13, 2012, 06:46:49 pm » |
nevermind everybody fixed it, used previous suggestion of adding a delay, added a 1500ms delay after writing the servo position!
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #13 on: October 13, 2012, 06:48:05 pm » |
the sketch works perfectly fine What sketch is that? The one full of errors that we have been pointing out, or something derived from that that fixes the errors? What does removing the servo moving code have to do with incorrectly reading serial data?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 13
|
 |
« Reply #14 on: October 15, 2012, 08:14:24 pm » |
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? #include <Servo.h>
Servo myservo;
const int buttonPin = 2; const int ledPin = 13; int incomingByte;
int buttonState = 0;
void setup(){ myservo.attach(9); Serial.begin(9600); pinMode(ledPin, OUTPUT); pinMode(buttonPin, INPUT);
}
void loop() { buttonState = digitalRead(buttonPin);
if (Serial.available() > 0) {
incomingByte = Serial.read();
}
if (incomingByte == 'H' && buttonState == HIGH) { myservo.write(180); delay(1500); digitalWrite(ledPin, HIGH); }
if (incomingByte == 'L' && buttonState == LOW) { myservo.write(90); delay(1500); digitalWrite(ledPin, LOW); }
}
|
|
|
|
|
Logged
|
|
|
|
|
|