Pages: 1 [2]   Go Down
Author Topic: Toggle LED with Switch (off same Adruino PIN)  (Read 1006 times)
0 Members and 1 Guest are viewing this topic.
Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 514
Posts: 31534
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes that would fix it.
Logged

Hull
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Hull
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 514
Posts: 31534
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: 1 [2]   Go Up
Jump to: