1
\$\begingroup\$

I am trying to do this(http://www.arduino.cc/en/Tutorial/Switch) without using any external parts because I was able to use button example from same site with this code: http://en.textsave.org/VjL) with my msp430g2553 using Energia but its not working, any help?

const int buttonPin = PUSH2;     // the number of the pushbutton pin
const int ledPin =  GREEN_LED;   // the number of the LED pin

int state = HIGH;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin

// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers

void setup(){
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);}

void loop(){
reading = digitalRead(ledPin);

// if the input just went from LOW and HIGH and we've waited long enough
// to ignore any noise on the circuit, toggle the output pin and remember
// the time
if (reading == HIGH && previous == LOW && millis() - time > debounce) {
if (state == HIGH){
  state = LOW;
}
else
  state = HIGH;
  time = millis();    }

digitalWrite(ledPin, state);
previous = reading;}
\$\endgroup\$
  • \$\begingroup\$ What isn't working about it, do you have a circuit diagram? \$\endgroup\$ – lilSebastian Oct 6 '13 at 21:54
  • \$\begingroup\$ Did you manage to get Debounce working yet? \$\endgroup\$ – Ignacio Vazquez-Abrams Oct 6 '13 at 22:09
  • \$\begingroup\$ Is the buttonPin double as an analog input? If so, it's it set as a digital port? The datasheet suggests if it is configured as analog, the digital input will not function. \$\endgroup\$ – DrRobotNinja Oct 7 '13 at 1:46
  • \$\begingroup\$ green led is just on and nothing happens when i press button, there are some schematics after page 18 : ti.com/lit/ug/slau318d/slau318d.pdf \$\endgroup\$ – Cagurtay Oct 7 '13 at 6:04
  • \$\begingroup\$ i tried this : energia.nu/Tutorial_Debounce.html but nothing happens, if i change pinMode(buttonPin, INPUT); as pinMode(buttonPin, INPUT_PULLUP); it works like that button example , i didnt even noticed that this is the same thing that i was trying to do. \$\endgroup\$ – Cagurtay Oct 7 '13 at 6:17
3
\$\begingroup\$

Eh, most likely you are using a rev1.5 Launchpad. The external pullup pin for the p1.3 button is not populated. Since it is not populated, there is no steady state change. You must enable the internal pullup.

Instead of pinMode(buttonPin); use pinMode(buttonPin, INPUT_PULLUP);

Also, you are trying to read the wrong pin.

Instead of reading = digitalRead(ledPin); use reading = digitalRead(buttonPin);

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ nothing happens when i change it to pinMode(buttonPin, INPUT_PULLUP); and reading = digitalRead(ledPin); is same in my code. \$\endgroup\$ – Cagurtay Oct 7 '13 at 6:21
  • \$\begingroup\$ and yes i am using rev 1.5 \$\endgroup\$ – Cagurtay Oct 7 '13 at 6:30
  • \$\begingroup\$ @Cagurtay the reading part is wrong. Think about it... \$\endgroup\$ – Passerby Oct 7 '13 at 7:10
  • \$\begingroup\$ Bah... I am sorry, i switched variables to my kit but accidently put output instead of input. reading = digitalRead(buttonPin); , thank you. \$\endgroup\$ – Cagurtay Oct 7 '13 at 8:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.