Hey everyone! I've been at this for about 4 weeks now and after running blink for the first time i jumped right into my meat and potatoes. I am building a pan/tilt using stepper motors instead of servos. The reason for my choice in hardware is irrelevant.
Setup is as follows:
-Transmitter: Arduino Nano (Chinese Clone(ch340)) with NRF24L01 module using 3.3v on-board and all plugged into a sunfounder remote robot controller which has power in(battery) and a PS2 style joystick.
The pan and tilt axes are both controlled by the two axes of the joystick with the two values transmitted to the slave/receiver which reads the values as "joystick [0]" and "joystick[1]".
-Receiver: Arduino UNO (Chinese Clone(ch340)) with NRF24L01 module using 3.3v through a Proteneer CNCSHIELD_V3. Only the X and Y axis motor driver sockets are being utilized. In the two sockets are a pair of Pololu A4988 drivers.
The annoying part is: the code works great and then again doesn't.
I can't get proper operation of the motors. They both spin when the joystick's x-axis is articulated but not the y-xis. On serial monitor, the two transceivers are sending and receiving perfectly.
What I need to accomplish is to move the steppers in somewhat 'real-time' using the remote control.
My guess is that I need to be working off of a stored value that is updated with each new packet, but I could never find a stepper library that would to speed-control through an a4988 which would simplify things greatly so I didn't have to have such huge blocks of code to drive the motors.
Transmitter's Code
/*----- Import all required Libraries -----*/
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
/*----- Declare all constant pin to be used ----*/
#define JOYSTICK_X A0
#define JOYSTICK_Y A1
const uint64_t pipe = 0xE8E8F0F0E1LL; // This is the transmit pipe to communicate the two module
/*-----Object Declaration ----*/
RF24 radio(9, 10); // Activate the Radio
/*-----Declaration of Variables -----*/
int joystick[2]; // Two element array holding the Joystick readings
void setup()
{
Serial.begin(115200); /* Opening the Serial Communication */
radio.begin();
radio.setPALevel(RF24_PA_MAX);
radio.openWritingPipe(pipe);
}//--(end setup )---
void loop() /* Runs Continuously */
{
joystick[0] = analogRead(JOYSTICK_X); // Reading Analog X
delay(.75);
joystick[1] = analogRead(JOYSTICK_Y); // Reading Analog Y
radio.write( joystick, sizeof(joystick) );
Serial.print("X = ");
Serial.print(joystick[0]);
Serial.print(" Y = ");
Serial.println(joystick[1]);
}//--(end main loop )---
Receiver's Code
/*----- Import all required Libraries -----*/
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
/*----- Declare all constant pin to be used ----*/
// This is the transmit pipe to communicate the two module
const uint64_t pipe = 0xE8E8F0F0E1LL;
//output pins for motion control(based on CNC_SHIELD_V3)
//y-axis
const int ydir = 6;
const int ystep = 3;
//enable pin for drivers
const int enable = 8;
//x-axis
const int xdir = 7;
const int xstep = 4;
/*-----Object Declaration ----*/
// Activate the Radio
RF24 radio(9, 10);
/*-----Declaration of Variables -----*/
// Two element array holding the Joystick readings
int joystick[2];
void setup()
{
Serial.begin(115200); /* Opening the Serial Communication */
Serial.println("Nrf24L01 Receiver Starting");
delay(1);
radio.begin();
/* Set the PA Level low to prevent power supply related issues
and for close proximity ie: benchtop */
radio.setPALevel(RF24_PA_MAX);
/* Open a reading pipe to receive data from remote*/
radio.openReadingPipe(1,pipe);
/* Start the radio listening for data*/
radio.startListening();
/*assign pin activity roles*/
pinMode (xdir, OUTPUT);
pinMode (xstep, OUTPUT);
pinMode (ydir, OUTPUT);
pinMode (ystep, OUTPUT);
pinMode (enable, OUTPUT);
}
void loop()
{
if ( radio.available() )
{
// Reading the data payload until the RX received everything
unsigned long message;
bool done = false;
while (!done)
{
// Fetching the data payload
done = radio.read( joystick, sizeof(joystick) );
int x = joystick[0];
int y = joystick[1];
int T;
//x-axis control
/*if (x > 400 and x < 600){
digitalWrite(enable, HIGH);}
if (x > 600){
//int T;
digitalWrite(enable, LOW);
delay(.5);
T=map(x, 600, 1023, 5, 0);
digitalWrite(xdir, HIGH);
delay(.2*T); // waits for a millisecond increment * the joystick position
digitalWrite(xstep, HIGH); // steps motor
delay(.5); // waits for a millisecond increment
digitalWrite(xstep, LOW);}
else
if (x < 517){
//int T;
digitalWrite(enable, LOW);
delay(.5);
T=map(x, 0, 400, 0, 5);
digitalWrite(xdir, LOW);
delay(.2*T); // waits for a millisecond increment * the joystick position
digitalWrite(xstep, HIGH); // steps motor
delay(.5); // waits for a millisecond increment
digitalWrite(xstep, LOW);}
//y-axis control
if (y > 500 and y < 540){
digitalWrite(enable, HIGH);}
if (y > 539){
//int T;
digitalWrite(enable, LOW);
delay(.5);
T=map(y, 540, 1023, 5, 0);
digitalWrite(ydir, HIGH);
delay(.2*T); // waits for a millisecond increment * the joystick position
digitalWrite(ystep, HIGH); // steps motor
delay(.5); // waits for a millisecond increment
digitalWrite(ystep, LOW);}
if (y < 501){
//int T;
digitalWrite(enable, LOW);
delay(.5);
T=map(y, 0, 500, 0, 5);
digitalWrite(ydir, LOW);
delay(.2*T); // waits for a millisecond increment * the joystick position
digitalWrite(ystep, HIGH); // steps motor
delay(.5); // waits for a millisecond increment
digitalWrite(ystep, LOW);}*/
Serial.print("X = ");
Serial.print(joystick[0]);
Serial.print(" Y = ");
Serial.println(joystick[1]);
delay(25);
}
if(done);
{
Serial.println("received");
}
}
else
{
if (!radio.available()){
delay(25);
Serial.println("No radio available");}
}
}