Hi,
I am trying to connect raspberry Pi to arduino using usb serial which it works.Now i am trying to make the arduino take a number from serial,decode it and then using if statments send a IR signal to my tv(the IR part works).the problem is i cant get it to decode i have even setup a small debug info for serial monitor but it wont take the command.Please i am desperate dont say me use google,this is a noob question etc. I will tell you any aditional info you need here is the code:
char rx_byte = 0;
String code = "";
const int led = 10;
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
pinMode(led, OUTPUT);
}
void loop() {
//Reads the command code
if (Serial.available() > 0) {
rx_byte = Serial.read();
if (rx_byte != '\n') {//moves the characters to a string
code += rx_byte;
Serial.print("Byte ");
Serial.println(rx_byte);
Serial.print("String ");
Serial.println(code);
//Looks up which command the control code responds to
if (code == 1) {
digitalWrite(led, HIGH);
}
else if(code == 2) {
digitalWrite(led, LOW);
delay(100);
}
}//end char search
}//end main if
}//end loop
You need to look at the ASCII character table and there you will see the rx_byte you got has a value of 30 if a zero or a value of 31, if a one. You need to convert the ASCII character to a numeric value. If you will only, ever get a number, subtract 30 from rx_byte then add to code.
I think the pi serial is at 3.3v , the Arduino at 5v . You need level shifting between the two , if you haven’t done this .
The serial bit is the same whatever the device , the Arduino does love numbers over 9 in the same way too.
Hi i want to use the pi for the processing because i plan to run multiple of arduinos and i dont trust connecting them over wifi or bluethout soo this is the easistes hardwiring i can do i i also love using Node red for my automation.I am also 15 yr old and my parents get annoyed when i ask them to get me something of ebay and it gets a lot more expensive because there is no electronics store near me to buy a level shiffter(which i know about).What I meant is that the standard Serial.read dosent like a 2 decimal number like 10 or higher atleast that is my understanding of it.
Look at the second example in the serial input basics tutorial to see how to receive a string (not String) with more than one character. With that example you can receive all of the characters of the string then act on the complete string.