Basic Arduino Serial Communication -
i'm trying basics of serial communication started; i'm trying use example found, understand should working. want type serial monitor output back, can see how works. tried removing while serial.available in case serial monitor doesn't trigger condition. here code:
// buffer store incoming commands serial port string indata; void setup() { serial.begin(9600); serial.println("initialized\n"); } void loop() { while (serial.available() > 0) { char recieved = serial.read(); indata += recieved; // process message when new line character recieved if (recieved == '\n') { serial.println("arduino received: "); serial.println(indata); indata = ""; // clear recieved buffer } } }
it uploads fine, , prints "initialized" doesn't work if try "send" data.
serial.read()
returns , int
. need cast (char)
in order store char.
char recieved = (char)serial.read();
btw: variable name should received :)
edit:
maybe never receiving data reason. let's try super simple, use serialevent() suggested @sohnryang , print text serial.available()
triggers:
while (serial.available() > 0) { serial.println("something has been received"); }
you should see message every time send arduino.
Comments
Post a Comment