Arduino rocking machine

Please help me Arduino forum.

I had my fist child 5 months ago. He will only sleeps if I am rocking him in his stroller.

I am trying to make 2 40w motors, linked to a BTS7960, 43A (biggest H bridge i could find) on a rack, attached to the stroller, rock him back and forth.
I am tryning to control it with a joystick. Its suppose to go forward when I press the y axis in one direction and backwards when I press the y axis in the other direction. It will have two speed options in both directions. Pressing the joystic 1/2 in one directions->low speed forward, pressing the joystick the full lenth -> full speed forward. Same with backward motion if I am pressing the joystick in the other direction.
The x axis is initiating the continuous rocking motion. It will be stopped by pressing the joy stick button (sw).

I only managed to make the fist task. May you help me?

Thank you :slight_smile:

``
#define Len 9
#define Ren 10
#define inL 4
#define inR 5
#define sw A2

int motorSpeedA = 0;

void setup() {
pinMode (Len, OUTPUT);
pinMode (Ren, OUTPUT);
pinMode (inL, OUTPUT);
pinMode (inR, OUTPUT);

Serial.begin(9600);

{
pinMode (sw, INPUT);
digitalWrite(sw, LOW);
}
}

void loop() {

int xAxis = analogRead (A0); // Read Joystick X-axis
int yAxis = analogRead(A1); // Read Joysticks Y-axis
int sw = analogRead (A2); // SW button joystick

Serial.println(sw);

// Y-axis used for forward and backward control

if (sw == 0) {
// stop function motor
digitalWrite(inL, LOW);
digitalWrite(inR, LOW);
motorSpeedA = (0);
}
else if (yAxis < 470 and yAxis >235) {
// Set Motor A backward
digitalWrite(inL, HIGH);
digitalWrite(inR, 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 = (150);

}

  else if (yAxis <235)
 {
// Set Motor A forward
digitalWrite(inL, HIGH);
digitalWrite(inR, 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 = (255);

}

else if (yAxis > 550 and yAxis <786) {
// Set Motor A backward
digitalWrite(inL, LOW);
digitalWrite(inR, 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 = (150);

}

  else if (yAxis >786)
 {
// Set Motor A forward
digitalWrite(inL, LOW);
digitalWrite(inR, 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 = (255);

}
// If joystick stays in middle the motors are not moving
else {
motorSpeedA = 0;

}
// 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;
}

while(xAxis >550 and sw >=1 and yAxis >=540 and <=480){
// rocking function

// backward + softstart
//1
digitalWrite(inL, LOW);
digitalWrite(inR, HIGH);
motorSpeedA = (150);
delay (200);
analogRead(sw);

 //2
 digitalWrite(inL, LOW);
digitalWrite(inR, HIGH);
 motorSpeedA = (200);
 delay (200);
 analogRead(sw);

 //3
 digitalWrite(inL, LOW);
digitalWrite(inR, LOW);
 motorSpeedA = (0);
 delay (200);
 analogRead(sw);

 //forward + softstart

 //1
 digitalWrite(inL, HIGH);
digitalWrite(inR, LOW);
 motorSpeedA = (150);
 delay (200);
 analogRead(sw);

 //2

digitalWrite(inL, HIGH);
digitalWrite(inR, LOW);
motorSpeedA = (200);
delay (200);
analogRead(sw);

 //3

  digitalWrite(inL, LOW);
digitalWrite(inR, LOW);
 motorSpeedA = (0);
 delay (200);
 analogRead(sw);
 }

}
analogWrite(Len, motorSpeedA); // Send PWM signal to motor A
analogWrite(Ren, motorSpeedA); // Send PWM signal to motor A
}

}

``

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

https://smile.amazon.com/Rockit-ROCKIT01-Portable-Baby-Rocker/dp/B076KP7HKW

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.