At this time my code is like this but still only outputting the number 1, i press the Switch and it still outputs the 1
// include Arduino stepper motor library
#include <Stepper.h>
#include <LiquidCrystal.h>
// define Button
const int buttonPin = 2;
int buttonState = 0;
// LCD Config
const int rs = 12, en = 13, d4 = 4, d5 = 5, d6 = 6, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
// define number of steps per revolution
#define STEPS 32
// define stepper motor control pins
#define IN1 11
#define IN2 10
#define IN3 9
#define IN4 8
// initialize stepper library
Stepper stepper(STEPS, IN4, IN2, IN3, IN1);
// joystick pot output is connected to Arduino A0
#define joystick A0
void setup()
{
Serial.begin(9600);
pinMode(buttonPin, INPUT_PULLUP); // the switch pin
lcd.begin(16, 2);
lcd.print("Test");
delay(3000);
}
void loop()
{
buttonState = digitalRead(buttonPin); //Reads the state of the pin
Serial.print(" Button State is: ");
Serial.println(buttonState);
if (buttonState = 1) {
lcd.print(1);
delay(100);
buttonState = digitalRead(buttonPin);
}
else {
lcd.print(2);
delay(100);
}
// read analog value from the potentiometer
int val = analogRead(joystick);
// if the joystic is in the middle ===> stop the motor
if( (val > 500) && (val < 523) )
{
digitalWrite(IN1, LOW);
digitalWrite(IN2, LOW);
digitalWrite(IN3, LOW);
digitalWrite(IN4, LOW);
}
else
{
// move the motor in the first direction
while (val >= 523)
{
// map the speed between 5 and 500 rpm
int speed_ = map(val, 523, 1023, 5, 500);
// set motor speed
stepper.setSpeed(speed_);
// move the motor (1 step)
stepper.step(1);
val = analogRead(joystick);
}
// move the motor in the other direction
while (val <= 500)
{
// map the speed between 5 and 500 rpm
int speed_ = map(val, 500, 0, 5, 500);
// set motor speed
stepper.setSpeed(speed_);
// move the motor (1 step)
stepper.step(-1);
val = analogRead(joystick);
}
}
}