I everyone!
I'm building a shop crane that I want to be able to control both wirelessly and localy. I have a 3 axis joystick and a hoist crane wireless controller.
I have a code for the remote and a code for the joystick but can't successfully combine both.
I want to add x, y and z axis from the joystick to the following code:
const int moteurGAv (5);
const int moteurGArr (6);
const int moteurDAv (9);
const int moteurDArr (10);
const int mAv (22); //foward Remote
const int mArr (24); //backward Remote
const int mGAv (26); //left turn Remote
const int mDAv (28); //right turn Remote
//const int winchUp (30);
//const int winchDown (32);
//const int selRL (34);
//const int onRemote (36);
const unsigned long ACCEL_SPEEDus = 1000; // 3.9 ms between speed increments
const unsigned long ACCEL_SPEEDus2 = 1000;
const unsigned long ACCEL_SPEEDus3 = 12000;
const unsigned long ACCEL_SPEEDus4 = 12000;
const int decelAjs = 50;
unsigned long most_recent_bump_us;
unsigned long most_recent_bump_us2;
unsigned long most_recent_bump_us3;
unsigned long most_recent_bump_us4;
int speed1;
int speed2;
int speed3;
int speed4;
int speed5;
int speed6;
int speed7;
int speed8;
void setup() {
Serial.begin(9600);
pinMode(moteurGAv, OUTPUT);
pinMode(moteurGArr, OUTPUT);
pinMode(moteurDAv, OUTPUT);
pinMode(moteurDArr, OUTPUT);
pinMode(mAv, INPUT);
pinMode(mArr, INPUT);
pinMode(mGAv, INPUT);
pinMode(mDAv, INPUT);
//pinMode(winchUp, INPUT);
//pinMode(winchDown, INPUT);
//pinMode(selRL, INPUT); //selector Remote / Local
//pinMode(onRemote, INPUT); // ON button on the Remote
analogWrite(moteurGAv, 0);
analogWrite(moteurGArr, 0);
analogWrite(moteurDAv, 0);
analogWrite(moteurDArr, 0);
speed1 = 0;
speed2 = 0;
speed3 = 0;
speed4 = 0;
speed5 = 0;
speed6 = 0;
speed7 = 0;
speed8 = 0;
}
void loop() {
if(digitalRead(mAv) == LOW)
slowDown();
if((digitalRead(mAv) == HIGH)&&(speed3 ==0 )&&(speed4 ==0)&&(speed5 <100)&&(speed7 < 100))
speedUp();
if(digitalRead(mArr) == LOW)
slowDown2();
if((digitalRead(mArr) == HIGH)&&(speed1 == 0)&&(speed2 == 0)&&(speed5==0)&&(speed7==0))
speedUp2();
if(digitalRead(mGAv) == LOW)
slowDown3();
if((digitalRead(mGAv) == HIGH)&&(speed3 == 0)&&(speed4 == 0)&&(speed1==0)&&(speed2==0))
speedUp3();
if((digitalRead(mDAv) == LOW) && (digitalRead(mAv)==LOW))
slowDown4();
if((digitalRead(mDAv) == HIGH)&&(speed3 == 0)&&(speed4 == 0)&&(speed1==0)&&(speed2==0))
speedUp4();
delay(5);
}
void slowDown() {
if((speed1 == 0)||(speed2 == 0))
return; // stopped
//if(speed2 == 0) return;
unsigned long t = micros();
if((speed1 == 255)||(speed2==255)) {
if (digitalRead(mGAv) == HIGH)
//start of deceleration
most_recent_bump_us = t;
}
else if(t - most_recent_bump_us < ACCEL_SPEEDus) {
// not time to bump the speed yet
return;
}
else {
// this method keeps the rate consistent
most_recent_bump_us += ACCEL_SPEEDus;
}
analogWrite(moteurGAv, --speed1);
analogWrite(moteurDAv, --speed2);
}
void speedUp() {
if((speed1 == 255) || (speed2 == 255))return; // max speed
unsigned long t = micros();
if((speed1 == 0)||(speed2 == 0)) {
// start of acceleration
most_recent_bump_us = t;
}
else if(t - most_recent_bump_us < ACCEL_SPEEDus) {
// not time to bump the speed yet
return;
}
else {
// this method keeps the rate consistent
most_recent_bump_us += ACCEL_SPEEDus;
}
analogWrite(moteurGAv, ++speed1);
analogWrite(moteurDAv, ++speed2);
}
void slowDown2() {
if((speed3 == 0)||(speed4 == 0))
return; // stopped
//if(speed2 == 0) return;
unsigned long t = micros();
if((speed3 == 255)||(speed4==255)) {
//start of deceleration
most_recent_bump_us2 = t;
}
else if(t - most_recent_bump_us2 < ACCEL_SPEEDus2) {
// not time to bump the speed yet
return;
}
else {
// this method keeps the rate consistent
most_recent_bump_us2 += ACCEL_SPEEDus2;
}
analogWrite(moteurGArr, --speed3);
analogWrite(moteurDArr, --speed4);
}
void speedUp2() {
if((speed3 == 255) || (speed4 == 255))return; // max speed
unsigned long t = micros();
if((speed3 == 0)||(speed4 == 0)) {
// start of acceleration
most_recent_bump_us2 = t;
}
else if(t - most_recent_bump_us2 < ACCEL_SPEEDus2) {
// not time to bump the speed yet
return;
}
else {
// this method keeps the rate consistent
most_recent_bump_us2 += ACCEL_SPEEDus2;
}
analogWrite(moteurGArr, ++speed3);
analogWrite(moteurDArr, ++speed4);
}
void slowDown3() { //left turn
if(speed5 == 0)
return; // stopped
//if(speed2 == 0) return;
unsigned long t = micros();
if(speed5 == 255) {
//start of deceleration
most_recent_bump_us3 = (t/decelAjs);
}
else if(t - most_recent_bump_us3 < ACCEL_SPEEDus3) {
// not time to bump the speed yet
return;
}
else {
// this method keeps the rate consistent
most_recent_bump_us3 += ACCEL_SPEEDus3;
}
analogWrite(moteurGArr, --speed6);
analogWrite(moteurDAv, --speed5);
}
void speedUp3() {
if(speed5 == 255)return; // max speed
unsigned long t = micros();
if(speed5 == 0){
// start of acceleration
most_recent_bump_us3 = t;
}
else if(t - most_recent_bump_us3 < ACCEL_SPEEDus3) {
// not time to bump the speed yet
return;
}
else {
// this method keeps the rate consistent
most_recent_bump_us3 += ACCEL_SPEEDus3;
}
analogWrite(moteurDAv, ++speed5);
analogWrite(moteurGArr, ++speed6);
}
void slowDown4() { //right turn
if(speed7 == 0)
return; // stopped
//if(speed2 == 0) return;
unsigned long t = micros();
if(speed7 == 255) {
//start of deceleration
most_recent_bump_us4 = (t/decelAjs);
}
else if(t - most_recent_bump_us4 < ACCEL_SPEEDus4) {
// not time to bump the speed yet
return;
}
else {
// this method keeps the rate consistent
most_recent_bump_us4 += ACCEL_SPEEDus4;
}
analogWrite(moteurGAv, --speed7);
analogWrite(moteurDArr, --speed8);
}
void speedUp4() {
if(speed7 == 255)return; // max speed
unsigned long t = micros();
if(speed7 == 0) {
// start of acceleration
most_recent_bump_us4 = t;
}
else if(t - most_recent_bump_us4 < ACCEL_SPEEDus4) {
// not time to bump the speed yet
return;
}
else {
// this method keeps the rate consistent
most_recent_bump_us4 += ACCEL_SPEEDus4;
}
analogWrite(moteurDArr, ++speed8);
analogWrite(moteurGAv, ++speed7);
}
I took this code from internet that works pretty well but have to add smooth stop / soft start to it and the selector mode function and combine it to the remote code.
#define enA 9
#define in1 4
#define in2 5
#define enB 10
#define in3 6
#define in4 7
int const deadband = 400;
int motorSpeedA = 0;
int motorSpeedB = 0;
void setup() {
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
//motorSpeedA.attach(4); //smoothing program
//motorSpeedB.attach(5);
}
void loop() {
int xAxis = analogRead(A0); // Read Joysticks X-axis
int yAxis = analogRead(A1); // Read Joysticks Y-axis
int zAxis = analogRead(A2);
// z-axis used for fast left and right turn control
if (zAxis < 470-deadband) {
// Set Motor A backward
digitalWrite(in1, HIGH);
digitalWrite(in2, HIGH);
// Set Motor B Forward
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
// Convert the declining Y-axis readings for going backward from 470 to 0 into 0 to 255 value for the PWM signal for increasing the motor speed
motorSpeedA = map(zAxis, 470, 0, 0, 255);
motorSpeedB = map(zAxis, 470, 0, 0, 255);
}
else if (zAxis > 550 + deadband) {
// Set Motor A forward
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
// Set Motor B BACKWARD
digitalWrite(in3, HIGH);
digitalWrite(in4, HIGH);
// Convert the increasing Y-axis readings for going forward from 550 to 1023 into 0 to 255 value for the PWM signal for increasing the motor speed
motorSpeedA = map(zAxis, 550, 1023, 0, 255);
motorSpeedB = map(zAxis, 550, 1023, 0, 255);
delay(100);
}
else if (470 < zAxis < 550)
if (yAxis < 470) {
// Set Motor A backward
digitalWrite(in1, HIGH);
digitalWrite(in2, HIGH);
// Set Motor B backward
digitalWrite(in3, HIGH);
digitalWrite(in4, HIGH);
// Convert the declining Y-axis readings for going backward from 470 to 0 into 0 to 255 value for the PWM signal for increasing the motor speed
motorSpeedA = map(yAxis, 470, 0, 0, 255);
motorSpeedB = map(yAxis, 470, 0, 0, 255);
delay(100);
}
else if (yAxis > 550) {
// Set Motor A forward
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
// Set Motor B forward
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
// Convert the increasing Y-axis readings for going forward from 550 to 1023 into 0 to 255 value for the PWM signal for increasing the motor speed
motorSpeedA = map(yAxis, 550, 1023, 0, 255);
motorSpeedB = map(yAxis, 550, 1023, 0, 255);
delay(100);
}
// If joystick stays in middle the motors are not moving
else {
motorSpeedA = 0;
motorSpeedB = 0;
delay(100);
}
// X-axis used for left and right control
if (xAxis < 470) {
// Convert the declining X-axis readings from 470 to 0 into increasing 0 to 255 value
int xMapped = map(xAxis, 470, 0, 0, 255);
// Move to left - decrease left motor speed, increase right motor speed
motorSpeedA = motorSpeedA - xMapped;
motorSpeedB = motorSpeedB + xMapped;
// Confine the range from 0 to 255
if (motorSpeedA < 0) {
motorSpeedA = 0;
}
if (motorSpeedB > 255) {
motorSpeedB = 255;
delay(100);
}
}
if (xAxis > 550) {
// Convert the increasing X-axis readings from 550 to 1023 into 0 to 255 value
int xMapped = map(xAxis, 550, 1023, 0, 255);
// Move right - decrease right motor speed, increase left motor speed
motorSpeedA = motorSpeedA + xMapped;
motorSpeedB = motorSpeedB - xMapped;
// Confine the range from 0 to 255
if (motorSpeedA > 255) {
motorSpeedA = 255;
}
if (motorSpeedB < 0) {
motorSpeedB = 0;
delay(100);
}
}
// Prevent buzzing at low speeds (Adjust according to your motors. My motors couldn't start moving if PWM value was below value of 70)
if (motorSpeedA < 70) {
motorSpeedA = 0;
}
if (motorSpeedB < 70) {
motorSpeedB = 0;
}
analogWrite(enA, motorSpeedA); // Send PWM signal to motor A
analogWrite(enB, motorSpeedB); // Send PWM signal to motor B
delay(100);
}
THANKS!!!