Hello, I bought a RX/TX and I am having problems in the communication.
Could anybody help myself with the code?
#include <SoftwareSerial.h>
#define rxPin 2
#define txPin 3
#include <Stdio.h>
int val = 0;
int PreVal = 0;
int flag = 0;
int dado = 0;
SoftwareSerial rfSerial = SoftwareSerial(rxPin, txPin);
void setup() {
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
rfSerial.begin(9600);
Serial.begin(9600);
}
void loop()
{
if (flag == 0)
{
Serial.println("Arduino Pronto!");
flag = 1;
}
if (Serial.available() > 0)
{
dado = Serial.read();
Serial.print("Dado a ser transmitido: ");
Serial.println(dado);
}
if (dado == 49) // 49 == 1
{
val = dado;
if(val != PreVal)
{
Serial.print("Transmitir: ");
Serial.println(val);
rfSerial.print(val);
PreVal = val;
delay(100);
}
receptor();
}
if (dado == 50) // 50 == 2
{
val = dado;
if(val != PreVal)
{
Serial.print("Transmitir: ");
Serial.println(val);
rfSerial.print(val);
PreVal = val;
delay(100);
}
receptor();
}
}
void receptor()
{
val = rfSerial.read();
Serial.print("Receptor: ");
Serial.println(val);
delay(100);
}
Thank you.