简体   繁体   中英

Communicating over Serial to an Arduino not working?

Im trying to power a 360 servo via serial commands when I use servo1.writeMicroseconds(1000); the servo turns clockwise at full power, replacing 1000 with 1500 stops it, and 2000 makes it go anticlockwise.

The Serial.write(val); to debug also gives a weird answer, when I use the value of 1, the board receives it as:

--> 49 --> 10

Board is an Arduino Mega 2560

#include <Servo.h>
Servo servo1;
int val = 1500;

void setup() {
servo1.attach(7);
servo1.writeMicroseconds(val);
Serial.begin(9600); 
}

void loop(){

if(Serial.available()){ 

int val = Serial.read();
Serial.write(val);
servo1.writeMicroseconds(val);

}
} 

Don't use Serial.write() . It's for writing bytes. Instead use Serial.println(...) which will print a line and handle multiple input types.

Another recommendation is don't use Serial.read() for just integers. Use Serial.parseInt() as described here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM