OK. I have been working on something off and on for months, have used tutorials, examples, and this forum to try to educate myself. I still consider myself very amateur but feel like I have done quite a bit of (unsuccessful) troubleshooting so far and am finally throwing it out there to see if anyone can point me in a good direction.
I am wanting a stepper motor to move based on button pressed on a "remote" end. The code works when everything is wired to the same microcontroller. Adding in the nrf24l01 component is throwing it off.
I have nano clone boards (says v3.0) that have atpmega328p hooked up to transmitting and receiving ends. Stepper is 28byj-48. I have buck converters going to each nrf24l01 that are currently stable at 3.5-3.8v output and my multimeter does not show a change or dip in voltage during button presses (sent only to nrf board, no other components, motor and nano are powered off another source, the stepper has its own source, not connected "nano's power"). I have soldered a 10uf capacitor to the 8 nrf boards I have tried so far (some electrolytic, some ceramic). I have grounds going from nano to each negative strip on the breadboard as I realized pretty early that the communication between nrf and nano was spotty otherwise. I have added some debugging code and added "stop/start listening" again at the end of the loop as I saw somewhere that may help. I've used low and high PA levels. Setting the channel used specifically did not make a difference either. I have also read over and referenced the "simple nrf24l01 tutorial" as it is mentioned in pretty much all the nrf24 posts on this forum that I've come across.
The serial monitor of the transmitting end says "radio initialized successfuly", "radio setup complete", recognizes the button press, and says it sent them successfully (the first 1-3 times after turning the setup on). After that, when buttons are pressed it says, "failed to send button states...retrying" and just stays there. A sketch that tests the communication between the nrf and nano board comes out ok. A sample repeating hello world sketch transmits well. A single button press to turn on an led works over the nrf ok.
The code was originally constantly sending the button state over so I altered it to only send button data when a change has occurred. That has not helped. I thought maybe it had to do with too much data or too frequently sending was overloading the system. I am clueless at this point. I have a couple nano every boards, an uno, and a leonardo that I will try running the code over soon to try to rule out board issues. I have used 3 different nano clone boards so far.
Any of you more experienced folks have any thoughts or ideas or a direction to point me perhaps? Code is below. I will try to add a crude wiring diagram later on for reference. Thank you.
Transmitter code, pre-debug version:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
// Define the pins for the buttons
const int buttonPin2 = 2;
const int buttonPin3 = 3;
const int buttonPin4 = 4;
const int buttonPin5 = 5;
// Variables to store button states
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int buttonState5 = 0;
// Setup NRF24L01 communication
RF24 radio(9, 10); // CE, CSN
const byte address[6] = "00001";
void setup() {
Serial.begin(9600);
// Initialize the buttons
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(buttonPin4, INPUT);
pinMode(buttonPin5, INPUT);
// Initialize NRF24L01
radio.begin();
radio.openWritingPipe(address);
radio.setPALevel(RF24_PA_HIGH);
radio.stopListening();
}
void loop() {
// Read button states
buttonState2 = digitalRead(buttonPin2);
buttonState3 = digitalRead(buttonPin3);
buttonState4 = digitalRead(buttonPin4);
buttonState5 = digitalRead(buttonPin5);
// Send the button states
int message[] = { buttonState2, buttonState3, buttonState4, buttonState5 };
radio.write(&message, sizeof(message));
delay(250); // Small delay to prevent spamming
}
Transmitter with debugging:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <ezButton.h>
// Define the pins for the buttons
const int buttonPin2 = 2;
const int buttonPin3 = 3;
const int buttonPin4 = 4;
const int buttonPin5 = 5;
// Create ezButton objects for each button
ezButton button2(buttonPin2);
ezButton button3(buttonPin3);
ezButton button4(buttonPin4);
ezButton button5(buttonPin5);
// Variables to store the last button states
int lastButtonState2 = 0;
int lastButtonState3 = 0;
int lastButtonState4 = 0;
int lastButtonState5 = 0;
// Setup NRF24L01 communication
RF24 radio(9, 10); // CE, CSN
const byte address[6] = "00001";
void setup() {
Serial.begin(9600);
// Initialize buttons with debounce time
button2.setDebounceTime(50); // 50 milliseconds debounce time
button3.setDebounceTime(50);
button4.setDebounceTime(50);
button5.setDebounceTime(50);
// Initialize NRF24L01
if (!radio.begin()) {
Serial.println("Radio hardware not responding!");
while (1) {} // Halt execution if radio initialization fails
} else {
Serial.println("Radio initialized successfully.");
}
radio.openWritingPipe(address);
radio.setPALevel(RF24_PA_HIGH);
radio.setChannel(108); // Set a specific channel to avoid interference
radio.setDataRate(RF24_250KBPS); // Lower data rate for better reliability
radio.setRetries(15, 15); // Set retries and delay between retries
radio.stopListening();
Serial.println("Radio setup complete.");
}
void loop() {
// Update the button states (required for debouncing)
button2.loop();
button3.loop();
button4.loop();
button5.loop();
// Read current debounced button states
int buttonState2 = button2.isPressed();
int buttonState3 = button3.isPressed();
int buttonState4 = button4.isPressed();
int buttonState5 = button5.isPressed();
// Check if any button state has changed
if (buttonState2 != lastButtonState2 || buttonState3 != lastButtonState3 || buttonState4 != lastButtonState4 || buttonState5 != lastButtonState5) {
// Send the button states only if there's a change
int message[] = { buttonState2, buttonState3, buttonState4, buttonState5 };
if (radio.write(&message, sizeof(message))) {
Serial.println("Button states sent successfully:");
} else {
Serial.println("Failed to send button states. Retrying...");
radio.begin(); // Attempt to reset the radio
}
// Print the states for debugging
Serial.print("Button 2: ");
Serial.println(buttonState2);
Serial.print("Button 3: ");
Serial.println(buttonState3);
Serial.print("Button 4: ");
Serial.println(buttonState4);
Serial.print("Button 5: ");
Serial.println(buttonState5);
}
// Update the last button states
lastButtonState2 = buttonState2;
lastButtonState3 = buttonState3;
lastButtonState4 = buttonState4;
lastButtonState5 = buttonState5;
// Optional: Add a small delay to reduce CPU usage
delay(10);
}
finally receiver code:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Stepper.h>
const int stepsPerRevolution = 150; // Change this number to change how far stepper turns
const int stepsPerRevolution2 = 450;
Stepper myStepper = Stepper(stepsPerRevolution, 2, 4, 3, 5);
// Setup NRF24L01 communication
RF24 radio(9, 10); // CE, CSN
const byte address[6] = "00001";
void setup() {
myStepper.setSpeed(55);
Serial.begin(9600);
// Initialize NRF24L01
radio.begin();
radio.openReadingPipe(0, address);
radio.setPALevel(RF24_PA_HIGH);
radio.setChannel(108); // Set a specific channel to avoid interference
radio.setDataRate(RF24_250KBPS); // Lower data rate for better reliability
radio.startListening();
}
void loop() {
if (radio.available()) {
int message[4];
radio.read(&message, sizeof(message));
// Check button states and move stepper accordingly
if (message[0] == HIGH) {
Serial.println("clockwise");
myStepper.step(stepsPerRevolution);
}
if (message[1] == HIGH) {
Serial.println("counterclockwise");
myStepper.step(-stepsPerRevolution);
}
if (message[2] == HIGH) {
Serial.println("clockwisetriple");
myStepper.step(stepsPerRevolution2);
}
if (message[3] == HIGH) {
Serial.println("counterclockwisetriple");
myStepper.step(-stepsPerRevolution2);
}
// Reset motor pins to LOW after movement
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
delay(250); // Delay to ensure smooth operation
radio.startListening();
}
}