Go Down

Topic: Toggle LED with Switch (off same Adruino PIN) (Read 1 time) previous topic - next topic


Below is the code which I used to create a multiplexer for Arduino - Console Shield

const int PIN9 = 9; //LED 1 and Push Button 1
const int PIN8 = 8; //LED 0 and Push Button 0

int SW1Count = 0;
int SW1State = 0;
int LSW1State = 0;

int SW0Count = 0;
int SW0State = 0;
int LSW0State = 0;

void setup () {
pinMode(PIN9, INPUT);
pinMode(PIN8, INPUT); 
}

void loop() {
 
  //Switch 1 and LED 1 (MSB Multiplexer)
SW1State = digitalRead(PIN9);

if (SW1State != LSW1State) {
if (SW1State == HIGH) {
  SW1Count++;
  }
 
  LSW1State = SW1State;
 
}

if (SW1Count == 1) {
pinMode(PIN9,OUTPUT);
digitalWrite(PIN9, HIGH);
pinMode(PIN9, INPUT);
}

if (SW1Count == 2) {
SW1Count = 0;
}

  //Switch 0 and LED 0 (LSB Multiplexer)
SW0State = digitalRead(PIN8);

if (SW0State != LSW0State) {
if (SW0State == HIGH) {
  SW0Count++;
  }
 
  LSW0State = SW0State;
 
}

if (SW0Count == 1) {
pinMode(PIN8,OUTPUT);
digitalWrite(PIN8, HIGH);
pinMode(PIN8, INPUT);
}

if (SW0Count == 2) {
SW0Count = 0;
}

}

Sorry there aren't any comments with the code just yet. I will populate with comments later this evening.

Cheers

Mike

I checked over the code yesterday and tested it for some time to see if I could find any issues or errors.

The only issue I could find was in some instances when the LED is on when I push the button it doesn't seem to switch off the LED but then can work ok for a number of times.

If anybody could provide a little feedback it would be great thank you.

Grumpy_Mike

You are switching the pin between input and output this normally is not a good idea.
You need to do two things, read the how to use this forum sticky and post the code correctly.
Then you need to post a schematic of your hardware wiring.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy