Hi everybody,
I have a code to control two stepper motors using joystick and control one servo motor with push button and it works very well. But now i want to make wireless control by using 433Mhz RF Receiver and Transmitter and two arduino uno. But i have no idea what the code should be!
The second arduino with transmitter should wired to the joystick and the push button.
...so any help would be much appreciated.
#include <AccelStepper.h>
#include <Bounce2.h>
#include <Servo.h> //including the library of servo motor
Servo sg90;
int initial_position = 0; //Declaring the initial position at 0
int servopin=9;
int pb = 2;
// definition of the constants of the Arduino pins
const int ledEnable = 13; // the led on board will show us the activation status of the motors
const int pinSwEnable = 7; // the button in the joystick module that enables or disables the control
const int pinEnable = 8; // the pins that control the ENABLE status of the A4988 drivers are connected in series, so only one pin is needed to manage them both
unsigned long debounceDelay = 10; // milliseconds for the button debonuce
const int jX = A4; // analog pin reading the values for the Xs
const int stepX = 3; // digital pin that sends the STEP signals to the X driver
const int dirX = 4; // digital pin that sends the DIRECTION signal to the X driver
long speedX, valX, mapX; // variables for managing motor movements X
const int jY = A5; //analog pin reading the values for Y
const int stepY = 5; //digital pin that sends the STEP signals to the Y driver
const int dirY = 6; //digital pin that sends the DIRECTION signal to the Y driver Y
long speedY, valY, mapY; //Y motor movements management variables
// variables used by the AccelStepper library
const int maxSpeed = 1000; //according to the library documentation this value can be set up to 4000 for an Arduino UNO
const int minSpeed = 0; //minimum engine speed
const float accelerazione = 50.0; //number of steps per second in acceleration
const int treshold = 30; //the reading of the potentiometers is never 100% reliable, this value helps to determine the point to consider as "Stay still" in the movements
long tresholdUp, tresholdDown; //service variables to perform the task described above
boolean abilitato, muoviX, muoviY, enable; //movements management variables
Bounce btnEnable = Bounce(); //instantiate a button from the Bounce library
//instantiate the engines
AccelStepper motoreX(AccelStepper::DRIVER, stepX, dirX);
AccelStepper motoreY(AccelStepper::DRIVER, stepY, dirY);
void setup() {
sg90.attach(servopin); // attaches the servo on pin 9
sg90.write(initial_position); //Move servo at 90 degree
pinMode(pb, INPUT_PULLUP);
//initialize values
speedX = speedY = 0;
enable = false;
//definition of the modalities of the pins
pinMode(ledEnable, OUTPUT);
pinMode(pinEnable, OUTPUT);
pinMode(pinSwEnable, INPUT_PULLUP); //the input of the switch needs to be set as INPUT_PULLUP
digitalWrite(ledEnable, enable);
digitalWrite(pinEnable, !enable); //The A4988 drivers disable the motor commands if on the ENABLE pin they receive a HIGH signal for this reason the value is opposite to that of the LED
//configure the joystick button using the Bounce library
btnEnable.attach(pinSwEnable);
btnEnable.interval(debounceDelay);
//calculate range values within which to consider the position of the joystick as "Stand still"
tresholdDown = (maxSpeed / 2) - treshold;
tresholdUp = (maxSpeed / 2) + treshold;
//configure engine parameters
motoreX.setMaxSpeed(maxSpeed);
motoreX.setSpeed(minSpeed);
motoreX.setAcceleration(accelerazione);
motoreY.setMaxSpeed(maxSpeed);
motoreY.setSpeed(minSpeed);
motoreY.setAcceleration(accelerazione);
}
void loop() {
if(digitalRead(pb)==0) {sg90.write(180);}
else {sg90.write(0);}
//execute check and read function of the button that determines the enabling status
checkEnable();
digitalWrite(ledEnable, enable); //show enabling status via LED on pin 13
digitalWrite(pinEnable, !enable); //set the opposite value on the ENABLE pins of the drivers
//perform analogue reading of the values coming from the joystick potentiometers
valX = analogRead(jX);
valY = analogRead(jY);
//map the values read according to the maximum and maximum speed
mapX = map(valX, 0, 1023, minSpeed, maxSpeed);
mapY = map(valY, 0, 1023, minSpeed, maxSpeed);
//run engine command function
pilotaMotori(mapX, mapY);
}
void pilotaMotori(long mapX, long mapY) {
if (mapX <= tresholdDown) {
//x goes back
speedX = -map(mapX, tresholdDown, minSpeed, minSpeed, maxSpeed);
muoviX = true;
} else if (mapX >= tresholdUp) {
//x go forward
speedX = map(mapX, maxSpeed, tresholdUp, maxSpeed, minSpeed);
muoviX = true;
} else {
//x stands still
speedX = 0;
muoviX = false;
}
if (mapY <= tresholdDown) {
//y goes down
speedY = -map(mapY, tresholdDown, minSpeed, minSpeed, maxSpeed);
muoviY = true;
} else if (mapY >= tresholdUp) {
//y goes up
speedY = map(mapY, maxSpeed, tresholdUp, maxSpeed, minSpeed);
muoviY = true;
} else {
//y stands still
speedY = 0;
muoviY = false;
}
if (muoviX) {
motoreX.setSpeed(speedX);
motoreX.run();
} else {
motoreX.stop();
}
if (muoviY) {
motoreY.setSpeed(speedY);
motoreY.run();
} else {
motoreY.stop();
}
}
void checkEnable() {
btnEnable.update();
if (btnEnable.fell()) {
enable = !enable;
}
}