On-Off-On switch connected to 2 LED's on arduino pro micro

So I'm trying to wire one Toggle swich like the below to 2 LED's, one green another yellow.

I want like position1 turn on LED1 and position2 turn on LED2. the midlle position is of course both off

I'm using an arduino pro micro and the switch will work with joystick library by MHeironimus.

the code i have is this and work without LED's.

#include <Joystick.h>

//Buttons
#define button1 8
#define button2 9

Joystick_ Joystick(JOYSTICK_DEFAULT_REPORT_ID,JOYSTICK_TYPE_GAMEPAD,
  2, 0,                  // Button Count, Hat Switch Count
  false, false, false,     // X and Y, but no Z Axis
  false, false, false,   // No Rx, Ry, or Rz
  false, false,          // No rudder or throttle
  false, false, false);  // No accelerator, brake, or steering

void setup() {
  // Initialize Button Pins
  pinMode(button1, INPUT_PULLUP);
  pinMode(button2, INPUT_PULLUP); 

  Joystick.begin(false);
}


void loop() {
  
  if (digitalRead(button1) == 0)  Joystick.pressButton(0); 
  if (digitalRead(button2) == 0)  Joystick.pressButton(1); 

  Joystick.sendState();//send state 1st before we clear the button states
  ClearButtons();
}

void ClearButtons()
{
  Joystick.releaseButton(0);
  Joystick.releaseButton(1);
}

I have them wired like these

image

Forgget, one friend helped me.

*Note that when you turn the switch to position 1 it will turn LED2, the green one. I don't know if its error mine or its exactly like that. So when u turn on one position it will turn on the led connected to the other position.

You only need one resistor, common for each LED.

For the green I'm using 330ohm and to the yellow I'm using 100ohm

If you need different value resistors then there is no choice :slight_smile:

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