hello everybody, I am stucking with controling stepper motor by arduino and joystick shield. I tried to use RF24 and 2 arduino board to connect with 2 stepper motor. By using accelstepper libary, I made first motor to work, but the second didn't. Please help me with this problem. Here is my code
TX:
#include <Arduino.h>
#include <SPI.h>
#include "printf.h"
#include "RF24.h"
#define X A0
#define Y A1
RF24 radio(9,10);
const byte address[6] = "12345";
uint8_t send[2];
uint8_t Analog_X = 0;
uint8_t Analog_Y = 0;
void setup()
{
//Set up serial
//----------------------------------------------------------------------------
Serial.begin(9600);
//----------------------------------------------------------------------------
//Set up rf24
//----------------------------------------------------------------------------
if (!radio.begin())
{
Serial.println("Module not work...!!");
while (1) {}
}
radio.openWritingPipe(address);
radio.setPALevel(RF24_PA_MIN);
radio.setChannel(80);
radio.setDataRate(RF24_250KBPS);
radio.stopListening();
if (!radio.available())
{
Serial.println("fail to connect to RX...!!");
Serial.println("Waiting.......");
}
//----------------------------------------------------------------------------
//Joystick X
//----------------------------------------------------------------------------
pinMode(X,INPUT);
pinMode(Y,INPUT);
}
void loop()
{
Analog_X = map(analogRead(X), 0,1023,0,100);
Analog_Y = map(analogRead(Y), 0,1023,0,100);
send[0] = Analog_X;
send[1] = Analog_Y;
radio.write(&send,sizeof(send));
Serial.print("X: ");Serial.print(send[0]);Serial.print(" Y: ");Serial.println(send[1]);
delay(10);
}
RX:
#include <AccelStepper.h> //accelstepper library
#include <SPI.h>
#include "printf.h"
#include "RF24.h"
AccelStepper stepper(1, 2, 3); // direction Digital 9 (CCW), pulses Digital 8 (CLK)
AccelStepper stepper2(1,33, 35); // direction Digital 11 (CCW), pulses Digital 10 (CLK)
//RF24
RF24 radio(9, 53); // CE, CSN
const byte address[6] = "12345";
byte receive[2];
//Pins
#define Analog_X_pin A0 //x-axis readings
#define Analog_Y_pin A1 //y-axis readings
//Variables
int Analog_X = 0; //x-axis value
int Analog_Y = 0; //y-axis value
int Analog_X_AVG = 50; //x-axis value average
int Analog_Y_AVG = 51; //y-axis value average
void InitialValues();
void ReadAnalog_X();
void ReadAnalog_Y();
void setup()
{
//SERIAL
Serial.begin(9600);
//----------------------------------------------------------------------------
// InitialValues(); //averaging the values of the 3 analog pins (values from potmeters)
//----------------------------------------------------------------------------
// Setup RF24
//----------------------------------------------------------------------------
if (!radio.begin())
{
Serial.println("Module not work...!!");
while (1) {}
}
radio.openReadingPipe(0, address);
radio.setPALevel(RF24_PA_MIN);
radio.setChannel(80);
radio.setDataRate(RF24_250KBPS);
radio.startListening();
if (!radio.available())
{
Serial.println("fail to connect to RX...!!");
Serial.println("waiting.......");
}
//----------------------------------------------------------------------------
//Stepper parameters
//setting up some default values for maximum speed and maximum acceleration
stepper.setMaxSpeed(5000); //SPEED = Steps / second
stepper.setAcceleration(1000); //ACCELERATION = Steps /(second)^2
stepper.setSpeed(500);
stepper.setEnablePin(4);
stepper.setPinsInverted(/*dir*/ true, false,true);//dir false: ngược chiều kim đồng hồ
stepper.enableOutputs();
delay(500);
//----------------------------------------------------------------------------
stepper2.setMaxSpeed(5000); //SPEED = Steps / second
stepper2.setAcceleration(1000); //ACCELERATION = Steps /(second)^2
stepper2.setSpeed(500);
stepper2.setEnablePin(37);
stepper2.setPinsInverted(/*dir*/ true, false,true);//dir false: ngược chiều kim đồng hồ
stepper2.enableOutputs();
delay(500);
}
void loop()
{
if (radio.available())
{
radio.read(&receive, sizeof(receive));
ReadAnalog_X();
stepper.runSpeed(); //step the motor (this will step the motor by 1 step at each loop indefinitely)
ReadAnalog_Y();
stepper2.runSpeed();
Serial.print(stepper.speed());Serial.print(" ");Serial.println(stepper2.speed());
}
}
void ReadAnalog_X()
{
//Reading the 3 potentiometers in the joystick: x, y and r.
Analog_X = receive[0];
//if the value is 25 "value away" from the average (midpoint), we allow the update of the speed
//This is a sort of a filter for the inaccuracy of the reading
if(abs(receive[0]-Analog_X_AVG)>10)
{
stepper.setSpeed(10*(receive[0]-Analog_X_AVG));
}
else
{
stepper.setSpeed(0);
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
}
void ReadAnalog_Y()
{
Analog_Y = receive[1];
if(abs(receive[1]-Analog_Y_AVG)>10)
{
stepper2.setSpeed(10*(receive[1]-Analog_Y_AVG));
}
else
{
stepper2.setSpeed(0);
}
}