Hello,
I'm trying to set up wireless communication between two Arduino Nanos to spin a stepper motor. I want to spin the motor in a different direction depending on which switch is pressed on the transmitter end. I used the below code mostly from online tutorials to get the basics working. The Arduinos communicate and the motor spins when I flip the switch. However, I can't get anything working to differentiate between sent data. Can anyone help give me some tips on how to compare incoming data? I'm guessing I'm not comparing correct variable types or something?
For example : if (buf == "Right") doesn't seem to return true if I send "Right" over RF. Below I included the code for each Arduino I used to get everything set up.
// nrf24_client
#include <SPI.h>
#include <RH_NRF24.h>
const int RightButton = 4;
const int LeftButton = 5;
int RightButtonState = 0;
int LeftButtonState = 0;
// Singleton instance of the radio driver
RH_NRF24 nrf24;
// RH_NRF24 nrf24(8, 7); // use this to be electrically compatible with Mirf
// RH_NRF24 nrf24(8, 10);// For Leonardo, need explicit SS pin
// RH_NRF24 nrf24(8, 7); // For RFM73 on Anarduino Mini
void setup()
{
Serial.begin(9600);
while (!Serial)
; // wait for serial port to connect. Needed for Leonardo only
if (!nrf24.init())
Serial.println("init failed");
// Defaults after init are 2.402 GHz (channel 2), 2Mbps, 0dBm
if (!nrf24.setChannel(1))
Serial.println("setChannel failed");
if (!nrf24.setRF(RH_NRF24::DataRate2Mbps, RH_NRF24::TransmitPower0dBm))
Serial.println("setRF failed");
}
void loop()
{
RightButtonState = digitalRead(RightButton);
if (RightButtonState == HIGH) {
Serial.println("Sending to nrf24_server");
// Send a message to nrf24_server
uint8_t data[8] = "Right";
nrf24.send(data, sizeof(data));
delay(5);
}
}
// nrf24_server
#include <SPI.h>
#include <RH_NRF24.h>
const int stepPin = 6;
const int dirPin = 7;
const int enablePin = 5;
// Singleton instance of the radio driver
RH_NRF24 nrf24;
void setup()
{
Serial.begin(9600);
while (!Serial)
; // wait for serial port to connect. Needed for Leonardo only
if (!nrf24.init())
Serial.println("init failed");
// Defaults after init are 2.402 GHz (channel 2), 2Mbps, 0dBm
if (!nrf24.setChannel(1))
Serial.println("setChannel failed");
if (!nrf24.setRF(RH_NRF24::DataRate2Mbps, RH_NRF24::TransmitPower0dBm))
Serial.println("setRF failed");
pinMode(enablePin, OUTPUT);
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
digitalWrite(enablePin, HIGH); //disable the enable pin
digitalWrite(stepPin, LOW); //disable the step pin
}
void loop()
{
if (nrf24.available())
{
// Should be a message for us now
uint8_t buf[8];
uint8_t len = sizeof(buf);
if (nrf24.recv(buf, &len))
{
// NRF24::printBuffer("request: ", buf, len);
Serial.print("got request: ");
Serial.println((char*)buf);
for (int i = 0; i <= 750; i++) {
digitalWrite(enablePin, LOW);
delayMicroseconds(2);
digitalWrite(dirPin, LOW);
digitalWrite(stepPin, HIGH);
delayMicroseconds(1100);
digitalWrite(stepPin, LOW);
delayMicroseconds(2);
}
digitalWrite(enablePin, HIGH);
}
else
{
Serial.println("recv failed");
}
}
}
Thank you,
Cid