1

I am trying to make LED in MSP430G2 Launchpad keep blinking / off with button like Switch example (http://www.arduino.cc/en/Tutorial/Switch) in Arduino/Energia but I seem to miss something stupid and i don't know what...

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_PULLUP);
  pinMode(ledPin, OUTPUT);
}

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

  // 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;
      digitalWrite(ledPin, HIGH);   
      delay(100);                  
      digitalWrite(ledPin, LOW);    
      delay(100); 
    }

    time = millis();    
  }

  digitalWrite(ledPin, state);

  previous = reading;
}

It blinks once when i press the button. I know that this

digitalWrite(ledPin, HIGH);

or state part messes up. Any help? Thank you.

jippie
  • 33,033
  • 16
  • 93
  • 160
Cagurtay
  • 89
  • 2
  • 12
  • Your else statement doesn't include anything past the one line below it without brackets. – Samuel Dec 03 '13 at 21:05
  • I fixed Else but it still blinks once. – Cagurtay Dec 03 '13 at 21:16
  • Where is the loop? –  Dec 03 '13 at 21:19
  • I tried to put blink in loop but I don't how to read button inside loop or what to write as loop condition. – Cagurtay Dec 03 '13 at 21:28
  • Oh, reading this again, you don't want it to just blink once, you want it to **keep blinking** until you press the button again? Then try combining http://arduino.cc/en/Tutorial/BlinkWithoutDelay with the Switch tutorial, maybe with a while or for loop. – Passerby Dec 04 '13 at 07:43
  • Because right now, your code works exactly like it is supposed to. If the button goes high after 200ms (0.2s) has passed since the last time it went high, it will go into the if statement. The first time, the led will stay off (State = HIGH at the start of your code). The second time it will turn on, wait 0.1s, turn off, wait 0.1s, then turn on. The third time, it will turn the led off. And it repeats from the second time (Press button, Blink 1, Stay On, Press Button, Turn Off, etc. etc.) – Passerby Dec 04 '13 at 07:49

2 Answers2

3

Is this what you are trying to accomplish?

  • Push-and-release => LED starts blinking
  • Push-and-release => LED turns off
  • and so forth ..

I slightly changed your code to accomplish that. I defined a flag 'blink' that tells the blink routine further down either to blink the LED or to turn it off.

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

boolean state = HIGH;      // the current state of the output pin
boolean blink = LOW;       // the current mode of the LED either blinking (HIGH) or off (LOW)
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_PULLUP);
  pinMode(ledPin, OUTPUT);
}

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

  // 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 ) ) {
    blink = !blink;  // invert the current value for the blink flag
    time = millis();    
  }


  // if blink flag is HIGH then turn on and off the LED
  // if blink flag is LOW then turn off the LED
  if ( blink == HIGH ) {
    digitalWrite(ledPin, state);
    state = !state;   // invert the current state (LOW to HIGH and HIGH to LOW)
    delay(100);
  } else {
    digitalWrite(ledPin, LOW);
  }
  previous = reading;
}

The disadvantage of this code is that the loop runs slow due to the delay while blinking. The blinker loop can be improved for that behavior, but it introduces a slightly more complex mechanism:

  if ( blink == HIGH ) {
    state = ( millis() >> 8 ) & 1;
    digitalWrite(ledPin, state);
  } else {
    digitalWrite(ledPin, LOW);
  }

Here the delay is replaced by making state depend on the number of milliseconds since start, divided by 256 and checking the least significant bit.

jippie
  • 33,033
  • 16
  • 93
  • 160
  • Tested on Energia with a Stellaris LaunchPad. – jippie Dec 03 '13 at 22:11
  • Just as a note for OP, this code is so simple, there is no difference in using it on a MSP430G2xxx Launchpad or the Stellaris LaunchPad. – Passerby Dec 04 '13 at 07:59
  • Thank you! That was what i was trying to do but improved loop doesn't work for me. – Cagurtay Dec 04 '13 at 10:31
  • @Cagurtay You are right, I had an old version of the improved loop copied by accident. The correct improved line should read `state = (millis() >> 8 ) & 1;` – jippie Dec 04 '13 at 18:12
1

Else needs an opening curly bracket.

else {

(Error since corrected by OP and Question, but also due to a different interpretation of what OP's vague intentions are (code not working as written vs code not working as intended))

Passerby
  • 72,580
  • 7
  • 90
  • 202