SOLVED: troubleshooting - Switch is not being read

Hi All,

Racking my brain as to why this is no longer working. I can’t seem to get the switch to read from my joystick. X and Y work without issue and Switched used to work.

Using these:

5V Dual-axis XY Thumb Game Joystick Module PS2 Rocker for Arduino KY-023

I have tried:

Different switches
Different boards (uno and nano)
Stripping code back to basic purely to test the input.
Tried different wiring (physical)
Different digital pin for switch read

The serial monitor simply shows Switch: 0

I have no resistors etc going from the joystick to the UNO, ie direct connections.

At the moment only the joystick is connected to the Arduino as below:

Digital pin 2 - Joystick switch
GND and 5v
X - A0
Y - A1

Any ideas?

Thank ytou

#include <FastLED.h>

// Arduino INPUT PINs
const int SW_pin_in = 2; // digital pin Input from Joystick Switch output (D2)
const int Jaw_pin_out = 5; // INPUTY DOWN (1023) LED Signal Out Digital Pin for JAW HORNS - end connects to positive LED


// FASTLED PIN OUTPUT
  #define LED_PIN     6 // DIGITAL PIN this goes from Digital pin 6 to the input DIN on the LED Strip / controller (ie note ground or power, the other one
  #define COLOR_ORDER GRB
  #define CHIPSET     WS2811
  #define NUM_LEDS    3
  #define BRIGHTNESS  255
  #define FRAMES_PER_SECOND 30


bool gReverseDirection = false;

CRGB leds[NUM_LEDS];

CRGBPalette16 gPal;

void setup() {

// FASTLED 

  FastLED.addLeds<CHIPSET, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection( TypicalLEDStrip );
  FastLED.setBrightness( BRIGHTNESS );

  
 // Set initial pin values
 
  pinMode(SW_pin_in, INPUT);
  digitalWrite(Jaw_pin_out, OUTPUT);
  digitalWrite(Jaw_pin_out, LOW);
  Serial.begin(115200);

}


void loop() {
 
//  Serial monitoring for troubleshooting
  Serial.print("Switch:  ");
  Serial.print(digitalRead(SW_pin_in));
  Serial.print("\n");
  delay(1000);

  


}

EDIT: SOLVED
Changed it from INPUT to INPUT_PULLUP and now its working.

Where are your analog pin declarations?

DKWatson:
Where are your analog pin declarations?

 // Set initial pin values
 
  pinMode(SW_pin_in, INPUT);
  digitalWrite(Jaw_pin_out, OUTPUT);
  digitalWrite(Jaw_pin_out, LOW);
  Serial.begin(115200);

Am i doing this bit wrong?

Got an example I look at?

Thankyou.

EDIT, thanks for pointing me in the right direction, changed it from INPUT to INPUT_PULLUP and now its working.

This is from eBay,

It shows output going to analog pins on the Arduino. Presumably you need to inform the library which pins you are using and then at some point get the values from those pins, either with analogRead or some library method. I don’t see any of that in your code. As there is no apparent library dealing with the joysticks, the onus is on you to read the analog values and interpret them.

From what I know, the joystick at rest (center position) would result in +/- 2.5V appearing at A0/A1 which analogRead(A0/A1) would present as about 512. Fully in one direction would be 0V (analogRead = 0) and the other direction 5V (analogRead = 1023). It's up to you to take those values and do something, presumably adjust the brightness of you LED strip. This is where your FastLED library would kick in. For that to happen though, there must be some interaction in loop() with the library.

And look, we're here to try and help you with your code, not write it for you. We'll identify where your problems exist and suggest ways in which they can be resolved. You need to take the initiative and digest the advice towards a solution. This way you learn. Otherwise you'll be back next week with the same problem.

Take it one step at a time. Make changes. Test. Make more changes. Test again. Just before the suicide urge kicks in, re-post your new code and let's have another look at it.

Thanks for the assistance, you put me in the right direction and its working :slight_smile:

Sorry the edit button was well hidden under 'more' updated post.

Good work. Have a beer for me.