Go Down

Topic: need help to toggle between 2 pins and 2 leds (Read 896 times) previous topic - next topic

JamesAus

Nov 18, 2012, 12:39 pm Last Edit: Nov 18, 2012, 08:45 pm by Nick Gammon Reason: 1
Hi guys using the simple BUTTON sketch found in our example library i would like to know how i can press the momentary switch and toggle between 2 pins and 2 leds
example
off state
pressing the momentary switch on pin 2 would turn on led pin 13 and power up pin 12
pressing the switch again would turn on led pin 11 and power up pin 10.
press once more and goes back to off state
Regards James
Code: [Select]
/*
 Button

Turns on and off a light emitting diode(LED) connected to digital  
pin 13, when pressing a pushbutton attached to pin 2.


The circuit:
* LED attached from pin 13 to ground
* pushbutton attached to pin 2 from +5V
* 10K resistor attached to pin 2 from ground

*/

// constants won't change. They're used here to
// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
 // initialize the LED pin as an output:
 pinMode(ledPin, OUTPUT);      
 // initialize the pushbutton pin as an input:
 pinMode(buttonPin, INPUT);    
}

void loop(){
 // read the state of the pushbutton value:
 buttonState = digitalRead(buttonPin);

 // check if the pushbutton is pressed.
 // if it is, the buttonState is HIGH:
 if (buttonState == HIGH) {    
   // turn LED on:    
   digitalWrite(ledPin, HIGH);  
 }
 else {
   // turn LED off:
   digitalWrite(ledPin, LOW);
 }
}


Moderator edit: [code] ... [/code] tags added. (Nick Gammon)

scottyjr

Quote
power up pin 12

What does 'power up' mean?

Quote
LED attached from pin 13 to ground

Through a resistor, right?

Nothing in your code has anything to do with pins 10,11, or 12 so you can't expect anything to happen on those pins. Is that the part you are needing help with?

- Scotty

JamesAus


Quote
power up pin 12

What does 'power up' mean?
i just mean it switches it on so i can hook up a relay to it after and use the led on pin 13 to  know the state of the relay

Quote
LED attached from pin 13 to ground

Through a resistor, right?
yes through a resistor

Nothing in your code has anything to do with pins 10,11, or 12 so you can't expect anything to happen on those pins. Is that the part you are needing help with?
correct i would like to add that part of the code  into the sketch.
James

- Scotty

PaulS

Code: [Select]
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     

Your comments are correct. Pay careful attention to what is happening, and, more importantly, what is NOT happening.

That code will do something when the switch is pressed. It doesn't matter if the switch was also pressed last time. If you want to toggle pins each time the switch is pressed, you do need to care if the switch was pressed last time, and only toggle the pin state when the switch state changes (to the pressed state).

You aren't using a button are you? Put it back on your shirt, and use a switch. Switches work a lot better.

scottyjr

One way to program for that would be to use a variable to keep track of how many times the momentary switch was depressed and to use switch / case (http://arduino.cc/en/Reference/SwitchCase) to control (turn on/power up) your output pins. After the count of three and your outputs have been turned off, reset the variable to zero.

You will need to use debounce circuitry or code to eliminate any false counts due to contact bounce(http://www.youtube.com/watch?v=_LCCGFSMOr4).

Only very small relays can be driven directly from an Arduino pin; otherwise you sill need circuitry to drive the relay. If the relay is small enough to be directly driven with an Arduino pin you will have to connect a diode across the relay coil to protect the Arduino.

- Scotty

Go Up