Hey Ash
I hope you are good!
Listen, I need your help one more time... When I use the potentiometer to determine the speed variable, the joystick and everything else works perfectly. However, when the joystick is not moved, in other words, when a micro switch is not pressed in and the joystick is in the center, the motor stops, right.... Now, when it stops, it doesn't have a break. So now when the joystick is not moved, the motor's shaft can still be turned with my fingers which is not entirely what it should do. It shouldn't move and must still have some sort of break on it...
In the code below there is a function to check to whether one or both buttons are not pressed and then in that function I set the break on HIGH. It doesn't seem to be working though. Maybe I'm missing something? It also seems as if the power to the motor is off when it is not rotating while using the joystick.
#include <Bounce.h>
#include <Stepper.h>
const int num_step = 40;
const int pwm_cha = 3;
const int pwm_chb = 11;
const int dir_cha = 12;
const int dir_chb = 13;
const int brake_cha = 9;
const int brake_chb = 8;
const int Pin1=2;
const int Pin2=4;
int potPin = 2; // Declaring int for Potentiometer on analog pin 0;
int spr=10;
Stepper stepper(num_step, dir_cha, dir_chb);
Bounce Bounce1 = Bounce(Pin1,10);
Bounce Bounce2 = Bounce(Pin2,10);
void setup()
{
Serial.begin(9600);
pinMode(pwm_cha, OUTPUT);
pinMode(pwm_chb, OUTPUT);
pinMode(brake_cha, OUTPUT);
pinMode(brake_chb, OUTPUT);
digitalWrite(pwm_cha, HIGH);
digitalWrite(pwm_chb, HIGH);
digitalWrite(brake_cha, LOW);
digitalWrite(brake_chb, LOW);
pinMode(potPin, INPUT); // Declaring Potentiometer as INPUT
pinMode(Pin1,INPUT);
pinMode(Pin2,INPUT);
digitalWrite(Pin1,HIGH);
digitalWrite(Pin2,HIGH);
}
void loop()
{
int potValue = analogRead(potPin); // Declare potentiometer value
int motorSpeed = map(potValue, 0, 1023, 80, 350); // Declare a motor speed value based on pot mapping
Bounce1.update ( );
Bounce2.update ( );
int valB1=Bounce1.read();
int valB2=Bounce2.read();
if (valB1 && !valB2){
digitalWrite(pwm_cha, HIGH);
digitalWrite(pwm_chb, HIGH);
digitalWrite(brake_cha, LOW);
digitalWrite(brake_chb, LOW);
stepper.step(spr);
stepper.setSpeed(motorSpeed); // Set speed to mapped motorSpeed
}
if(!valB1 && valB2){
digitalWrite(pwm_cha, HIGH);
digitalWrite(pwm_chb, HIGH);
digitalWrite(brake_cha, LOW);
digitalWrite(brake_chb, LOW);
stepper.setSpeed(motorSpeed); // Set speed to mapped motorSpeed
stepper.step(-spr);
}
if(!valB1 && !valB2){
digitalWrite(pwm_cha, LOW);
digitalWrite(pwm_chb, LOW);
digitalWrite(brake_cha, HIGH);
digitalWrite(brake_chb, HIGH);
}
}
Any ideas?
Cheers