Joystick Button

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

}

}

Oops

Please remember to use code tags when posting code, and please mind your language

You only need to do this once

Totally right i'am sorry, just that already stressed out with this :smiley:

Did know what happened there, i olny have it once in the code, strange

Any help???

Did you fix the problem in reply #2?

How is the button switch wired? Is there a pull-up or pulldown resistor?

If you make changes to the code, post the new version so we can keep up.

Post code in code tags.

We did not have that in double only once.

My button Switch is connected to digital 2 in the arduino.

already tried a buch of codes but it seemes it always reads the first if and even if we press the button in the Joystick it never reads anything else.

Are you going to answer the questions in reply #7?

I see that one side of the switch is wired to pin 2. Where does the other side of the switch go?

The Joystick SW is conected to the digital 2 in the arduino, the VRX is connected to the analog0

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

= for assignment, == for comparison. In an if structure, you need to do a comparison.

No answers to my questions. I'm out. Good luck.

Than you can help me out what you need answers for i don't understand what information do you need??

OK, I will try one more time.

Still not right. = for assignment, == for comparison. If statement is a comparison. Read the reference.

How is the switch wired? A switch has 2 terminals. One goes to pin 2, where does the other go?

An open switch is "floating" so the state of the switch is random. You use a pullup or pulldown resistor (depending on how the switch is wired) to give the switch a known state when the switch is open. See this @Grumpy_Mike tutorial.

Now i understood.

My Joystick has 5 pins, gnd, VCC, VRx, VRy and SW, i have all 4 connected, the SW to 2, the VRx to analog A0, GND and VCC directly to the Breadboard.

Already changed - if (buttonState == 1) {

Will check the tutorial you sent me

Thanks!!!

Finally!!!

Sorted out!!!

Thanks everyone!!!

Please be so kind as to post the solution so that others may benefit.

here you have it

void loop()
{
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);
  
  buttonState = digitalRead(buttonPin);
    if (buttonState == HIGH) {
    Serial.println("not pressed");
    lcd.print(1);
    delay(10);
  }
  else {
    Serial.println("PRESSED!");
    lcd.print(2);
    delay(10);
  }