Go Down

Topic: Multiple Blink States with a button no delay (Read 1 time) previous topic - next topic

wdw0003

My TA does not know what is going on.  I am trying to modify this code so that the LED is off initially, I press the button once the led comes on, I press it once more, and the LED blinks with one second intervals, then the process repeats.


const int switchPin = 2;
const int ledPin = 13;
int buttonstate;
int lightState;
int blinkstate = LOW;
long previousmillis = 0;
long interval = 1000;
unsigned long currentmillis = 0;

// the setup routine runs once when you press reset:
void setup() {
  pinMode(switchPin, INPUT);
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
  buttonstate = digitalRead(switchPin);
}

void loop() {
  int val = digitalRead(switchPin);
  delay(10);
  int val2 = digitalRead(switchPin);
  if (val == val2){
  if (val  != buttonstate) {
    if (val == LOW){
      if(lightState == 0){
        lightState = 1;
        Serial.println("Button just pressed, Blinking");
        currentmillis = 0;
        previousmillis = 0;
        blinkstate = LOW;
      }
       
       
      else{
        lightState = 0;
        digitalWrite(ledPin,LOW);
        Serial.println("Button just pressed, light off");
      }
  }
  }
  }
  // Blinking Section
  if (lightState == 1){
    currentmillis = millis();
    if (currentmillis - previousmillis > interval)
    previousmillis = currentmillis;
    if (blinkstate == LOW)
    blinkstate = HIGH;
    else
    blinkstate = LOW;
    digitalWrite(ledPin,blinkstate);

   
  buttonstate = val;
 
}

--Thanks

wdw0003

Also, I am trying to add three more LEDs (a total of four) to do the following:
•   Program the circuit to include 5 light states, detailed below. There should be no response delay in the circuit and you should use a debounce check.
o   State 0: All LEDs off
o   State 1: After the button is pressed the first time, one LED (referred to from here on as Light 1 (pin 10) turns on and remains on until the              button is pressed again.
o   State 2: Immediately after the button is pressed the second time Light 2 (pin11) should turn on as Light 1 goes off. In this state the lights should turn on and off according to the pattern in Table 1.

?
Table 1 - State 2 Light Pattern
Time (sec)   Light 1   Light 2
0             Off             On
0.5             On             On
1.0             Off             Off
1.5             On            Off
2.0            Repeat Pattern

o   State 3: After the button is pressed the third time, Light 3 (pin 12) immediately turns on and Lights 1 and 2 are off. In this state the lights should turn on and off according to the pattern in Table 2.

Table 2 - State 3 Light Pattern
Time (sec)   Light 1   Light 2   Light 3
0             Off             Off             On
0.5             On             Off            Off
1.0             On             On            Off
1.5             On             On            On
2.0             Off             Off            Off
2.5                     Repeat Pattern

o   State 4: Create a custom light state that does the following after the fourth button press and continues until the fifth button press:
?   Utilizes all 4 LEDs
?   Has a repeating blink sequence of at least 4 time steps
o   State 0: After the fifth button press, the code should revert back to State 0.

PaulS

The first question that we need to ask is what does that code do? What do you want it to do? How do they differ?

Well, OK, that was three questions but I have one more. How is the switch wired?

marco_c

Look at Multiblink at the site in my signature block. It allows you to define the patterns but you will need to add the changes of state to do what you want.
Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

wdw0003

PaulS:

The code presented has an initial state of the LED off, then you press the button and the LED starts blinking, the button is pressed once more and the LED returns to the initial state.  The setup is correct; that information is given by the lab manual.

Go Up