Hi,
Getting back on Arduino after a few years, and struggling with some things.
I'am making some testing with the Joystick button switch.
I can get it to send text to an lcd1602 on the high position, but when i press the button the low position doens't do nothing.
here's the code:
// include Arduino stepper motor library
#include <Stepper.h>
#include <LiquidCrystal.h>
// define Button
const int swPin = 2;
int switchState = 1;
// 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(swPin, INPUT);
digitalWrite(swPin, HIGH);
lcd.begin(16, 2);
lcd.print("Test");
delay(3000);
Serial.begin(9600);
pinMode(swPin, INPUT);
digitalWrite(swPin, HIGH);
}
void loop()
{
switchState = digitalRead(swPin);
if (switchState = LOW ) {
lcd.print("Switch pressed");
delay(3000);
}
else {
lcd.print("Dwitch Depressed");
delay(3000);
}
// 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);
}
}
}