I have two arduino nano that I want to use a serial communication between them using TX & RX.
I just want a simple example to get the idea.
For such as the first arduino nano to turns on/off led from the second arduino and the second arduino could turn on/off led from the first arduino too.
Actually I made two PCB circuit (based on arduino nano) for my project, and I assigned TX & RX (pin 0 & 1) to make communication between them. So I can't replace the pins.
All what I want is to send a character from one arduino to the another one to perform some actions (both ways, from the first arduino to the second one and from the second arduino to the first one)
Have a look at the examples in Serial Input Basics - simple reliable non-blocking ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.
The technique in the 3rd example will be the most reliable. It is what I use for Arduino to Arduino and Arduino to PC communication.
You can send data in a compatible format with code like this (or the equivalent in any other programming language)
I'm requesting a example code because what I tried were just one way communication and not for nano-nano (it was uno-mega) because of that I have no idea about my case.
void loop()
{
if(Serial.available())
{
char ch = Serial.read();
switch(ch)
{
case '0':
// switch led on receiver off
...
...
break;
case '1':
// switch led on receiver on
...
...
break;
}
}
hsalame:
I assigned TX & RX (pin 0 & 1) to make communication between them. So I can't replace the pins.
I don't quite understand this, but it sounds like a seriously bad bit of planning. However, so long as you can disconnect the 0>1 pins. It is quite OK to use hardware serial. You just need to have them disconnected when you upload the programmes. This presupposes that you are not using hardware serial for something we haven't heard about - yet.
void loop()
{
if(Serial.available())
{
char ch = Serial.read();
switch(ch)
{
case '0':
// switch led on receiver off
...
...
break;
case '1':
// switch led on receiver on
...
...
break;
}
}
I want the communication to be two way. The transmitter could receive a char and the receiver could transmit a char.
Start by getting the appropriate example from the link in Reply #5 working for one-way communication. Then put the same functions (but in the opposite order) into your two Arduinos and you will have two-way communication.
Actually in the first arduino I have a connected servo and a button and in the second arduino I have RFID reader and servo.
So when the button in the first arduino click the servo in the second arduino moves, and when the RFID reader in the second arduino reads an authorized tag, the servo in the first arduino moves.
Appreciate all your support guys.
Here is the code:
First Arduino nano:
#include <Servo.h>
Servo myservo;
int button = A6;
int vala;
int a = 0;
int b = 0;
void setup()
{
Serial.begin(9600);
myservo.attach(2);
pinMode(button, INPUT);
while (!Serial) {
;
}
}
void loop()
{
if (Serial.available())
{
char c = Serial.read();
if (c == '0' && a == 0)
{
myservo.write(90);
a = 1;
}
if (c == '1' && a == 1)
{
myservo.write(146);
a = 0;
}
}
vala = analogRead(button);
if (vala > 100 && b == 0)
{
Serial.print(1);
b=1;
}
else if (vala < 100 && b == 1){
Serial.print(0);
b=0;
}
}
Second Arduino nano:
#include <Servo.h>
Servo myservo;
char c = ' ';
int a = 0;
#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 10
#define RST_PIN 9
MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class
MFRC522::MIFARE_Key key;
byte nuidPICC[4];
String Card_1 = "89687148179";
void setup()
{
myservo.attach(A3);
Serial.begin(9600);
SPI.begin();
rfid.PCD_Init();
}
void loop()
{
if (Serial.available())
{
char c = Serial.read();
if (c == '0' && a == 0)
{
myservo.write(146);
a = 1;
}
if (c == '1' && a == 1)
{
myservo.write(90);
a = 0;
}
}
Check_For_RFID();
}
void Check_For_RFID()
{
if ( ! rfid.PICC_IsNewCardPresent())
return;
if ( ! rfid.PICC_ReadCardSerial())
return;
for (byte i = 0; i < 4; i++) {
nuidPICC[i] = rfid.uid.uidByte[i];
}
printHex(rfid.uid.uidByte, rfid.uid.size);
// Serial.println();
rfid.PICC_HaltA();
rfid.PCD_StopCrypto1();
}
void printHex(byte *buffer, byte bufferSize) {
String UID = "";
for (byte i = 0; i < bufferSize; i++) {
UID += buffer[i], HEX;
// Serial.print(buffer[i], HEX);
}
if (UID == Card_1)
{
Serial.print(1);
}
else
{
Serial.print(0);
}
// Serial.print(UID);
}
Always so nice to hear about the full project afterwards
And one advise, do not use single character global variable names. In a year's time, you will no longer know what they were used for. And searching where they were used in the code is a disaster.