Hello everyone,
To begin, here is my code:
#include <RHReliableDatagram.h>
#include <RH_NRF24.h>
#include <SPI.h>
// Define addresses for radio channels (each nrf24L01)
#define CLIENT_ADDRESS 1
#define SERVER_ADDRESS 2
// Define Potentiometer/Button output pin
int potVal = A0;
int buttonData = A1;
int potServo = A3;
// Singleton instance of the radio driver (objectify)
RH_NRF24 RadioDriver;
// Class to manage message delivery and receipt, using the driver declared above
RHReliableDatagram RadioManager(RadioDriver, CLIENT_ADDRESS);
// Declare unsigned 8-bit Potentiometer array with 1 variable and 1 variable with button
uint8_t potential[3];
// Define the Message Buffer
uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];
void setup()
{
Serial.begin(9600);
// Initialize manager with defaults - 2.402 GHz (channel 2), 2Mbps, 0dBm
if (!RadioManager.init())
Serial.println("init failed");
// Defaults after init are 2.402 GHz (channel 2), 2Mbps, 0dBm
}
void loop()
{
Serial.println("Reading Potentiometer values...");
// Map (transform) Pot values from (0 - 1023) to (0 - 255)
potential[0] = map(analogRead(potVal), 0, 1023, 0, 255);
potential[1] = digitalRead(buttonData);
potential[2] = map(analogRead(potServo), 0, 1023, 0, 255);
// Display Pot values in serial monitor
Serial.print("POT value = ");
Serial.print(potential[0]);
// Display button value
Serial.print("----------Button value = ");
Serial.print(potential[1]);
// Display Servo pot values
Serial.print("----------Servo value = ");
Serial.println(potential[2]);
Serial.println("Sending Potentiometer data to nrf24 Server");
// Send a message containing Potentiometer data to manager for the server
if (RadioManager.sendtoWait(potential,sizeof(potential), SERVER_ADDRESS))
{
// Wait for a reply from the server
uint8_t len = sizeof(buf);
uint8_t from;
if (RadioManager.recvfromAckTimeout(buf, &len, 2000, &from))
{
Serial.print("Received reply from Client");
Serial.print(from, HEX);
Serial.print(": ");
Serial.print((char*)buf);
}
else
{
Serial.println("No reply from Server");
}
}
else
Serial.println("FAILED");
delay(100); // Wait a tiny bit before next transmission
}
Above is for the Arduino Pro Mini with Potentiometers attached and the NRF24L01 Transceiver
Below is for the Arduino UNO connected to an L298N Motor Driver and a DC Motor (ignore the Servo motor lines)
// Include RadioHead ReliableDatagram & NRF24 Libraries
#include <RHReliableDatagram.h>
#include <RH_NRF24.h>
#include <RH_ASK.h>
#include <ServoTimer2.h>
// Include dependant SPI Library
#include <SPI.h>
// Define addresses for radio channels
#define CLIENT_ADDRESS 1
#define SERVER_ADDRESS 2
// Create an instance of the radio driver
RH_NRF24 RadioDriver;
// Sets the radio driver to NRF24 and the server address to 2
RHReliableDatagram RadioManager(RadioDriver, SERVER_ADDRESS);
// Define a message to return if values received
uint8_t ReturnMessage[] = "Motor Control Data Received";
// Define the Message Buffer
uint8_t buf[RH_NRF24_MAX_MESSAGE_LEN];
// Define Pins
int speedPin = 5;
int in1 = 4;
int in2 = 3;
int servoPin = 6;
int pos;
ServoTimer2 leftServo;
int counter = 0;
int previousButtonState = 0;
void setup()
{
// Setup Serial Monitor
Serial.begin(9600);
// Initialize RadioManager with defaults - 2.402 GHz (channel 2), 2Mbps, 0dBm
if (!RadioManager.init())
Serial.println("init failed");
// Set up pin modes
pinMode(speedPin,OUTPUT);
pinMode(in1,OUTPUT);
pinMode(in2,OUTPUT);
pinMode(servoPin,OUTPUT);
leftServo.attach(servoPin);
}
void loop()
{
if (RadioManager.available())
{
// Wait for a message addressed to us from the client
uint8_t len = sizeof(buf);
uint8_t from;
if (RadioManager.recvfromAck(buf, &len, &from))
//Serial Print the values of Motor Control Inputs
{
Serial.print("Data received from Server ");
Serial.print(from, HEX);
Serial.print(" |||||| POT Value = ");
Serial.print(buf[0]);
Serial.print(" ---- BUTTON = ");
Serial.print(buf[1]);
Serial.print("------- POT2 Value = ");
Serial.println(buf[2]);
if (buf[1] != previousButtonState) {
if (buf[1] == HIGH) {
counter ++;
Serial.print("# of button pushes ");
Serial.println(counter);
}
else {
}
delay(50); // debounce
}
// Save the current state as previous state
previousButtonState = buf[1];
Serial.println(counter);
if (counter % 2 == 0) {
digitalWrite(in1,LOW);
digitalWrite(in2,HIGH);
analogWrite(speedPin,buf[0]);
}
else {
digitalWrite(in1,HIGH);
digitalWrite(in2,LOW);
analogWrite(speedPin,buf[0]);
}
// pos = (2250./255.)*buf[2] + 750;
// angle =
// leftServo.write(pos);
// delay(5);
// Serial.println(pos);
leftServo.write(750);
// Send a reply back to the originator client, check for error
if (!RadioManager.sendtoWait(ReturnMessage, sizeof(ReturnMessage), from))
Serial.println("sendtoWait failed");
}
}
delay(5);
}
My question is, I previously connected the pots to an Arduino UNO to begin with instead of the Arduino Pro Mini and everything worked fine. Once I began using the Arduino Pro Mini, I would change the voltage with the potentiometer and the DC motor connected with the Arduino UNO set up had very slow responses. For example it would take the motor to change speed about half a second or so after I turned the potentiometer. My guess is that the Arduino Pro Mini is the problem since the Arduino UNO I used before worked fine but I am not sure why the Arduino Pro Mini would act differently if it a 5 volt input (same as Arduino UNO). Is there something in my code that is creating this or is it the Arduino Pro Mini? Any help would be very much appreciated. I am somewhat new to all of this as well so I apologize if I have made some rookie mistakes with this post of with any nomenclature.