I am using 2 UNO's to make a serial connection between them. I want to send a "1" to the reciever arduino and check if its acutally "1" or not. I can easily send data and the recevier read it. But I cannot check the value. How can I check the value or is it even possible? Here are the codes:
The sender code is here:
char mystr[] = {'1', '2', '3', '4', '5', '6'};
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.write(mystr[0]); //Write the serial data
}
Code is verified, so I do not think the problem is in here.
Here is the reciever code:
char mystr[10]; //Initialized variable to store recieved data
int alinan;
String okunan;
void setup() {
// Begin the Serial at 9600 Baud
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
if (Serial.available()) {
okunan = Serial.readString();
Serial.println(okunan);
}
if (okunan.equals("1")) {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(500); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(500); // wait for a second
}
And I can not check the read value by any means. I have tried checking it with if - else too. I have been struggling with this problem for a while now. Any solutions are greatly appreciated!
1. If you have connected your 2 UNOs in the following style (Fig-1), the communication might fail as the UNO has only one hardware UART Port which is permanently engaged with the IDE and Serial Monitor. Please, try to connect them using Sofware UART Port (SUART) as depicted in Fig-2 and then modify your program codes for both Transmitter and Receiver accordingly.
2. A Software UART Port (SUART) is created by including the following lines in the sketch:
3. All commands of UART Port are also applicable for the SUART Port except the void serialEvent(){} procedure which is reserved for hardware UART Port.
4.
Figure-1: UNO/UNO connection using hardware UART Ports.
5.
Figure-2: UNO/UNO connection using software UART Ports
6. Recommended Codes for the Receiver UNO-2 of Fig-2
#include<SoftwareSerial.h>
SoftwareSerial mySUART(2, 3);
//char mystr[10]; //Initialized variable to store recieved data
//int alinan;
//String okunan;
byte x;
void setup()
{
// Begin the Serial at 9600 Baud
Serial.begin(9600);
mySUART.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
}
void loop()
{
if (mySUART.available())
{
x = mySUART.read();
Serial.println((char)x);
}
if (x == '1')
{
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(500); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(500); // wait for a second
}
}