Estoy comunicando dos Arduino, es decir, Arduio UNO como remitente & Arduino Mega como receptor a través del módulo transiver RF-433 MHz. La comunicación está funcionando, pero hay un problema. Si quiero enviar un mensaje, digamos, un número o un carácter, tengo que enviar ese número o carácter varias veces utilizando el monitor de serie Arduino. En otras palabras, tengo que presionar esa tecla y enviar varias veces antes de que el receptor pueda recibir el mensaje y actuar en consecuencia.
Pregunta: ¿Qué debo hacer para que mi receptor reciba el mensaje con solo presionar una tecla?
Mis códigos:
Código del transmisor:
/* Bomb Displacing and Disposing Robot
Transmitter Code (Arduino UNO) */
#include <VirtualWire.h>
int wrongSignal = 8;
void setup()
{
Serial.begin(9600);
vw_setup(2400);
vw_set_tx_pin(7);
pinMode(wrongSignal,OUTPUT);
}
void loop()
{
if(Serial.available())
{
char c= Serial.read();
if(c == '8') //To move foreward
{
vw_send((uint8_t *)c, 1); //send one bit i.e. 8
}
else if(c=='4') //To move left
{
vw_send((uint8_t *)c, 1); //send one bit i.e. 4
}
else if (c=='6') //To move right
{
vw_send((uint8_t *)c, 1); //send only one bit i.e. 6
}
else if(c=='2') // To move reverse
{
vw_send((uint8_t *)c, 1); // send only one bit i.e. 2
}
else if(c=='5') // To stop
{
vw_send((uint8_t *)c, 1); // send only one bit i.e. 5
}
else if(c=='0') // To stop
{
vw_send((uint8_t *)c, 1); // send only one bit i.e. 5
}
Serial.println(c);
}
else
digitalWrite(wrongSignal, HIGH);
}
Código del receptor:
//Receiver Code (Arduino Mega ADK 2560)
#include <VirtualWire.h>
// extra pins --------------------------------
const int wrongSignal = 8; // for indication that user sending invalid command
const int enablePin = 12; // enable of the ICs used in circuit
//---------------------------------------------
// base controling pins -------------------
// Front Left motor M1
const int frontLeft1 = 30;
const int frontLeft2 = 31;
// Front Right motor M2
const int frontRight1 = 32;
const int frontRight2 = 33;
//Back Left motor M3
const int backLeft1 = 34;
const int backLeft2 = 35;
//Back Right motor M4
const int backRight1 = 36;
const int backRight2 = 37;
//------------------------------------------
void setup() {
//Extra pins
pinMode(enablePin, OUTPUT); // initialize the enable pin as an output
pinMode(wrongSignal, OUTPUT); //initialize wrongSignal pin as output pin
// for Motor drive IC pins
pinMode(frontLeft1, OUTPUT);
pinMode(frontLeft2, OUTPUT);
pinMode(frontRight1, OUTPUT);
pinMode(frontRight2, OUTPUT);
pinMode(backLeft1, OUTPUT);
pinMode(backLeft2, OUTPUT);
pinMode(backRight1, OUTPUT);
pinMode(backRight2, OUTPUT);
digitalWrite(enablePin, HIGH);
Serial.begin(9600); // initialize serial communications:
vw_setup(2400); // baud rate is 2400
vw_set_rx_pin(7); // Receiver pin is pin 7
vw_rx_start(); // start receiving
}
void loop()
{
uint8_t buflen = VW_MAX_MESSAGE_LEN;
uint8_t buf[buflen];
if(vw_get_message(buf, &buflen))
{
for (int i = 0; i < buflen; i++)
{
if (buf[i] == '8') // move forward, all motors uni-direction
{
digitalWrite(frontLeft1, HIGH);
digitalWrite(frontLeft2, LOW);
digitalWrite(frontRight1, HIGH);
digitalWrite(frontRight2, LOW);
digitalWrite(backLeft1, HIGH);
digitalWrite(backLeft2, LOW);
digitalWrite(backRight1, HIGH);
digitalWrite(backRight2, LOW);
}
else if (buf[i] == '4') // move left, M1 >, M4 <
{
digitalWrite(frontLeft1, HIGH); //M1
digitalWrite(frontLeft2, LOW);
digitalWrite(frontRight1, LOW); //M2
digitalWrite(frontRight2, LOW);
digitalWrite(backLeft1, LOW); //M3
digitalWrite(backLeft2, LOW);
digitalWrite(backRight1, LOW); //M4
digitalWrite(backRight2, HIGH);
}
// If the input is '0', turn off the ledPin i.e. send a low signal
else if (buf[i] == '6') // move right, M2>, M3<
{
digitalWrite(frontLeft1, LOW); //M1
digitalWrite(frontLeft2, LOW);
digitalWrite(frontRight1, HIGH); //M2
digitalWrite(frontRight2, LOW);
digitalWrite(backLeft1, LOW); //M3
digitalWrite(backLeft2, HIGH);
digitalWrite(backRight1, LOW); //M4
digitalWrite(backRight2, LOW);
}
else if (buf[i] == '2') // reverse
{
digitalWrite(frontLeft1, LOW);
digitalWrite(frontLeft2, HIGH);
digitalWrite(frontRight1, LOW);
digitalWrite(frontRight2, HIGH);
digitalWrite(backLeft1, LOW);
digitalWrite(backLeft2, HIGH);
digitalWrite(backRight1, LOW);
digitalWrite(backRight2, HIGH);
}
else if (buf[i] == '5') // all motors stop
{
digitalWrite(frontLeft1, LOW);
digitalWrite(frontLeft2, LOW);
digitalWrite(frontRight1, LOW);
digitalWrite(frontRight2, LOW);
digitalWrite(backLeft1, LOW);
digitalWrite(backLeft2, LOW);
digitalWrite(backRight1, LOW);
digitalWrite(backRight2, LOW);
}
else if (buf[i] == '0') // all motors stop
{
digitalWrite(frontLeft1, LOW);
digitalWrite(frontLeft2, LOW);
digitalWrite(frontRight1, LOW);
digitalWrite(frontRight2, LOW);
digitalWrite(backLeft1, LOW);
digitalWrite(backLeft2, LOW);
digitalWrite(backRight1, LOW);
digitalWrite(backRight2, LOW);
}
else
digitalWrite(wrongSignal, HIGH);
}
}
}