How to use joystick movement as a toggle switch?

For my project, I want any movement detected by a joystick to cause the built in LED to illuminate, and for it to de-illuminate only after another movement has been detected. (Toggling the LED by moving a joystick). The code currently lights up the LED when the joystick is moved and prints a value corresponding to the LED state, but I have no clue how to toggle it on and off. Any help would be very appreciated!

const byte ledPin = LED_BUILTIN;

#define xPin A0
#define yPin A1

int xValue;
int yValue;
int joyStickState;



void setup() {

  Serial.begin(9600);

  pinMode(xPin, INPUT);
  pinMode(yPin, INPUT);


}



void loop() {

  xValue = analogRead(xPin);
  yValue = analogRead(yPin);

  if(xValue != 512 || yValue != 512) {
    joyStickState = 1;
    digitalWrite(ledPin, HIGH);
  }
  else {
    joyStickState = 0;
    digitalWrite(ledPin, LOW);
  }


  Serial.println(joyStickState);
}

const byte ledPin = LED_BUILTIN;
const byte xPin = A0;

void setup() {
  pinMode(ledPin, OUTPUT);
}

void loop() {
  static int xValue=0;
  xValue = abs(analogRead(xPin) - 512);
  digitalWrite(ledPin, xValue > 20);
}

This did not work for me

on a higher level, What you need to do, is set a threshold value which identifies ‘movement’ in whichever axis you’re working.

When that delta value from the previous joystick value is exceeded, turn the LED on, start a millis() timer and re-set the previous threshold ‘old’ value, then after the desired period (2-secs), turn the LED off… of course if the joystick value keeps moving/triggering, you’d restart the LED timer, so it only goes off 2 seconds after the last movement.

Try this


const byte ledPin = LED_BUILTIN;
const byte xPin = A0;
byte ledstate = 0;

void setup() 
{
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, ledstate);
}

void loop() 
{
  int xValue;
  xValue = analogRead(xPin);
  if ((xValue > 576) || (xValue < 448)) ledstate = !ledstate;
  digitalWrite(ledPin,ledstate);
  delay(50);
}

Have a nice day!

@MrEbuilder

Did it work OK?

It works for my purposes, though, I noticed that when the joystick is moved, the LED only toggles when ledState = !ledState. This means that since the value of ledState is switching constantly (010101010), you essentially have to release from the joystick exactly when the code lands on ledState = !ledState for the LED to toggle. (Sometimes it does not and ledState lands on it's current value) I've increased the delay value so that it works for my project, yet I'm stull curious if there is a way to toggle the LED using a joystick that is completely independent of time.

Yes, your are right, that code was wrong.
This one works the way you want.

#define ON 1
#define OFF 0
const byte ledPin = LED_BUILTIN;
byte LEDstate=0;
bool switch_state=OFF, last_switch_state=OFF;

void setup() 
{
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LEDstate);
}

void loop() 
{
  int Value = analogRead(A0);

  //if (Value > 600)
  if ((Value > 576) || (Value < 448)) 
  {
    switch_state = ON;
  }
  else
  {
    switch_state = OFF;
  }
  
  if (switch_state != last_switch_state) // changed
  {
    if ((last_switch_state == OFF) && (switch_state == ON))
    {
      LEDstate = !LEDstate;
      digitalWrite(ledPin,LEDstate);
    }
    last_switch_state = switch_state;
  }
  delay(50);
}

That's exactly what I was looking for. Thanks!

Now you can have a nice day!

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