Hello everyone! I'm new to this place, but during my search online with arduino infomations i've come to find that this is a friendly and helpfull community.
I'm helping a friend build a scaled tower crane for his university degree. The mechanical part is almost completed and now the trycly part comes to place. Building the electrical system.
I'm using an Arduino UNO R3, with 3x A4988 Stepper Drivers, with one NRF24L01 for the crane part.
I'm using 3 stepper motors, 2xNema17 for the rotation and the troley, and a NEMA 23 for the load.
I'm dissasembling an old usb joystick which i want to use for the remote, along with an Arduino Nano and an NRF24L01 transmitter.
The main prolem i'm encountering is that i cannot find any code for running potentiometer values (joystick) wirelessly to another arduino equipped with A4988's. Only thing i was able to find are sketches for servo controls.
The idea is that when the joysticks are in neutral positions, the crane doesn't move, but when i do move the joysticks, the stepper motors should increase the speed proportional to the joystick movements.
I want to use the right joystick forward and back movements to control the movement of one NEMA 17 that actuates the troley and makes it move forward and backward, and at the same time have 2 analog inputs on the Arduino UNO that limits my movement in one (or another) direction if the troley reaches the margin of the crane arm.
The right joystick (left and right) movements are used to control the second NEMA 17 stepper, to rotate the crane arm left and right.
The NEMA 23 stepper will be controlled by the left joystick forward and backward movements (and maybe if needed will ad limit switches for the low and high point of the crane hook, but this is not really needed at this poin).
For the Arduino Nano part i've came up with this code, which reads the values of the 3 joystick movements and sends them to the Arduino UNO.
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define JOYSTICK_X A0
#define JOYSTICK_Y A1
#define JOYSTICK_Z A2
int joystick[3];
RF24 radio(7, 8); // CE, CSN
const byte address[6] = "00001";
void setup() {
Serial.begin(9600);
radio.begin();
radio.openWritingPipe(address); // 00001
radio.setPALevel(RF24_PA_MIN);
radio.stopListening();
}
void loop() {
joystick[0] = analogRead(JOYSTICK_X);
joystick[1] = analogRead(JOYSTICK_Y);
joystick[2] = analogRead(JOYSTICK_Z);
radio.write(joystick, sizeof(joystick));
}
Now, the trycky part for me is to read the values that i've sent from the NANO to the UNO, and make them rotate my stepper.
For the Reciver code, i found and tried to use some code online, but without any chance. It gives me multiple errors, and whatever i tried i cannot make it work propperly.
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(9,10);
const byte adress[6] = "00001";
int joystick[2]
int joystick[3]
int joystick[1]
// Define stepper driver pins
#define step_pin1 3
#define step_pin2 5
#define step_pin3 6
#define dir_pin1 2
#define dir_pin2 4
#define dir_pin3 7
#define Limit01 A0
#define Limit02 A1
void setup()
{
while(!Serial);
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(0, adress);
radio.setPALevel(RF24_PA_MIN);
radio.startListening();
pinMode(dir_pin1, OUTPUT);
pinMode(dir_pin2, OUTPUT);
pinMode(dir_pin3, OUTPUT);
pinMode(step_pin1, OUTPUT);
pinMode(step_pin2, OUTPUT);
pinMode(step_pin3, OUTPUT);
pinMode(Limit01, INPUT);
pinMode(Limit02, INPUT);
digitalWrite(dir_pin1, HIGH);
digitalWrite(dir_pin2, HIGH);
digitalWrite(dir_pin3, HIGH);
}
void loop(){
if (radio.available())
radio.read(&joystick, sizeof(joystick));
customDelay1Mapped = speedUp1(); // Gets custom delay values from the custom speedUp function
// Makes pules with custom delay, depending on the Potentiometer, from which the speed of the motor depends
if (digitalRead(joystick[0]) > 600) {
if (!digitalRead(Limit01)) {} //Check if limit switch is activated
else { // If limit switch is not activated, move motor clockwise
digitalWrite(dir_pin1, LOW);
digitalWrite(step_pin1, HIGH);
delayMicroseconds(customDelay1Mapped);
digitalWrite(step_pin1, LOW);
delayMicroseconds(customDelay1Mapped);
}
}
if (digitalRead(joystick[0]) < 500){
if(!digitalRead(Limit02)) {} //Check if limit switch is activated
else { // If limit switch is not activated, move motor clockwise
digitalWrite(dir_pin1, LOW);
digitalWrite(step_pin1, HIGH);
delayMicroseconds(customDelay1Mapped);
digitalWrite(step_pin1, LOW);
delayMicroseconds(customDelay1Mapped);
}
}
int speedUp1(){
int customDelay1 = digitalRead(joystick[0]);
int newCustom1 = map(customDelay1, 0, 1023, 500, 2000);
return newCustom1;
}
customDelay2Mapped = speedUp2();
if(digitalRead(joystick[1] > 600))
{
digitalWrite(dir_pin2, HIGH);
digitalWrite(step_pin2, HIGH);
delayMicroseconds(customDelay2Mapped);
digitalWrite(step_pin2, LOW);
delayMicroseconds(customDelay2Mapped);
}
if(digitalRead(joystick[1] < 500)) {
digitalWrite(dir_pin2, LOW);
digitalWrite(step_pin2, HIGH);
delayMicroseconds(customDelay2Mapped);
digitalWrite(step_pin2, LOW);
delayMicroseconds(customDelay2Mapped);
}
int speedUp2 ()
{
int customDelay2 = digitalRead(joystick[1]);
int newCustom2 = map(customDelay2, 0, 1023, 300, 2000);
return newCustom2;
}
customDelay3Mapped = speedUp3
if(digitalRead(joystick[2] > 600))
{
digitalWrite(dir_pin3, HIGH);
digitalWrite(step_pin3, HIGH);
delayMicroseconds(customDelay3Mapped);
digitalWrite(step_pin, LOW);
delayMicroseconds(customDelay3Mapped);
if(digitalRead(joystick[2] < 500)){
digitalWrite(dir_pin3, LOW);
digitalWrite(step_pin3, HIGH);
delayMicroseconds(customDelay3Mapped);
digitalWrite(step_pin, LOW);
delayMicroseconds(customDelay3Mapped);
}
int speedUp3()
{
int customDelay3 = digitalRead(joystick[2]);
int newCustom3 = map(customDelay3, 0, 1023, 500, 2000);
return newCustom3;
}
}
}
If somebody have the patience and time to help me help my friend would be awsome
It's my first time using arduino, and got myself into a relatively complicated project, and at this point he's invested too much to back out, and he's gonna have to present his project in about a month.
Thanks in advance guys, wish you the best :D!