Multiple Blink States with a button no delay

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

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.

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?

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.

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.

Consistently using { and } after if statements makes it clear what the extent of the block is. One is not left to guess whether the block consisting of one statement is what the programmer meant.

Using Tools + Auto Format to properly indent the code helps, too. Unlike python, where indenting a statement, or not, affects whether it is part of a block, the indenting means nothing to the CC++ compiler. But it does to people. Using the formatting tool to properly indent the code often reveals cases where the program looks right, but the indenting was misleading, and the code was not actually correct.

We need to see what you actually intend for the program to do, by adding curly braces in all the optional places, and by proper indentation.

Then, we can help you sort out the issues of why it does not behave as you intend.

Then, we can help you add the additional states.

guys i tried making his program, i have a couple of prblem, maybe my program won be the most effective but im trying, hehe the problem that i have now is when im my switch case in state 3
it does not behave like my array should be.... please help and tell me why in case 2 if count ==6 will it work like its suppose to wont it suppose to be count ==4
heres my code

uint8_t Switch = 2;
uint8_t Led[4]={5,7,9,13};
uint8_t Led1[4]={6,8,10,12};

boolean LedState =LOW;
int SwitchState =0;
int SwitchDebounce;
int LastSwitchState=HIGH;
int LastSwitchDebounce=LOW;
int State=0;
boolean LastLedState=LOW;
int Count=0;
int LastState=0;

long previousMillis = 0;
long interval = 500; 
unsigned long LastDebounceTime = 0;
unsigned long DebounceDelay = 50;

int State2[4][2]=
    {
      (0,1),
      (1,1),
      (0,0),
      (1,0),
    };

int State3[8][3]=
    {
      (0,0,0),
      (0,0,1),
      (0,1,0),
      (0,1,1),
      (1,0,0),
      (1,0,1),
      (1,1,0),
      (1,1,1),
    };

void setup()
{
  pinMode(Switch,INPUT);
  digitalWrite(Switch,HIGH);
  for(int i=0;i<4;i++)
    {
      pinMode(Led[i],OUTPUT);
      pinMode(Led1[i],OUTPUT);
      digitalWrite(Led1[i],LOW);
    }
    Serial.begin(9600);
}

void loop() 
{
  int CurrentSwitch = digitalRead(Switch);
  if (CurrentSwitch != LastSwitchDebounce)
  {
    LastDebounceTime = millis();
  } 
  if ((millis() - LastDebounceTime) > DebounceDelay) 
  {
    SwitchState = digitalRead(Switch);
    if (SwitchState != LastSwitchState) 
    {
      if (SwitchState == LOW)
      {
        State++;
        if (State==4)State=0;
        
      } 
      LastState =State;
    }
    LastSwitchState=SwitchState;
  }
  LastSwitchDebounce = CurrentSwitch;
  unsigned long currentMillis = millis();
  if(currentMillis - previousMillis > interval) 
  {
    previousMillis = currentMillis;
    LedState = !LedState;
    
  }
  
  if( LastLedState!=LedState)
  {
    
    (Count==15)? Count=0:  Count++; 
  }
  if (State != LastState) Count=-1;
  switch (State)
  {
    case (0):
    for(int i=0;i<4;i++)
    {
      digitalWrite(Led[i],LOW);

    }
    break;
    case(1):
    digitalWrite(Led[0],HIGH);
          
    break;
    case(2):
    
    digitalWrite(Led[0],State2[Count][0]);
    digitalWrite(Led[1],State2[Count][1]);
    if (Count==6)Count=0;
    break;
    case(3):
    
    digitalWrite(Led[0],State3[Count][0]);
    digitalWrite(Led[1],State3[Count][1]);
    digitalWrite(Led[2],State3[Count][2]);
    if (Count==9)Count=0;
    break;
    default:
    State=0;
    break;
  }
  LastLedState=LedState;

  
}

State and Count are lousy names. State of what? Count of what? What is the code doing? What is it supposed to do?

PaulS:
i use state is to follow the op state 0-4 kinda thing
and for the count i dont know what would make it a better name since its only funtion is to let the led follow the light pattern as it advance one by one.

ash901226:
PaulS:
i use state is to follow the op state 0-4 kinda thing
and for the count i dont know what would make it a better name since its only funtion is to let the led follow the light pattern as it advance one by one.

You still didn't explain what "it does not behave like an array" is supposed to mean. If you want help, you need to give us a better description than that. What is it doing, and what is is supposed to be doing?

Look at the reference at http://arduino.cc/en/Reference/BooleanVariables

A boolean shouldn't be set to anything other the 'true' or 'false' and not 'LOW'

if ( State == 4 )
{
    State = 0;
}

can be less awkwardly expressed as

State = ++State % 4;

same with this awkward statement

(Count == 15) ? Count = 0 : Count++;

lloyddean:
Look at the reference at http://arduino.cc/en/Reference/BooleanVariables

A boolean shouldn't be set to anything other the 'true' or 'false' and not 'LOW'

if ( State == 4 )

{
    State = 0;
}




can be less awkwardly expressed as



State = ++State % 4;




same with this awkward statement



(Count == 15) ? Count = 0 : Count++;

thanks lloyddean i dont know that you could do that
but that aside how about this?

case(2):
    
    digitalWrite(Led[0],State2[Count][0]);
    digitalWrite(Led[1],State2[Count][1]);
    if (Count==6)Count=0;
    break;
    case(3):
    
    digitalWrite(Led[0],State3[Count][0]);
    digitalWrite(Led[1],State3[Count][1]);
    digitalWrite(Led[2],State3[Count][2]);
    if (Count==9)Count=0;
    break;

but that aside how about this?

Aside from
the horrid
indenting
that snippet
looks OK.

What does that actually do? How does that differ from what you expect?

PaulS, My real code is in reply #6,
what it does, well it work flawlessly for switch statement from 0-2 but then it go to case 3, something weird happen, it does not follow the pattern that was set thru array pattern 3, it just goes blinking randomly. other then that theres s few thing that bothering me, i know that array pattern2 have only 4 state but in the case 2 i have to make it as though if count==6 to become 0 only then will the program for case 2 work flawlessly

Not sure if related, but I think you need to make sure the leds you don't want to lit stay low. However, if you are going through the states in sequence and only in one direction, it might actually look ok. In any other case, some led would be lit all the time.

Why do you set Count to a negative when the state changes? However, I'm not even sure does it go there, because you set LastState =State; early in the routine in some cases, and not as the last thing as I thought it should be. The Count may not equal 0, when you think it does, and that's why you have that count==6 too. With any luck, if Count ever gets higher than 15, it'll just keep on adding until it has blinked the state of the whole random memory through.

there have been many time that count become a runaway, haha but i had done a few if statement so that it will never go higher then 15. i have use the serial.println to see all the value of Count and it does go to 0. btw its not random that what happen to case 3, it happen in a perticular sequence that is not like i state in patern3

Some Serial.print() statements would go a long way towards understanding your issue (which I still don't). On any given pass through loop(), State has a value. What is it? Under what circumstances does it change? Do those circumstances happen correctly? Does State change appropriately?

On any given pass through loop(), Count starts with some value. What is it? Count changes. Does it change at the appropriate time? To the appropriate value?

Pauls,
I have verify that State value will be updated everytime i press the Switch,
i also have verify that count will start at 0 and every 500millisecond it will increase by 1
i also verify that if State value change, count will reset. what i dont get is this one

int State2[4][2]=
    {
      (0,1),
      (1,1),
      (0,0),
      (1,0),
    };

int State3[8][3]=
    {
      (0,0,0),
      (0,0,1),
      (0,1,0),
      (0,1,1),
      (1,0,0),
      (1,0,1),
      (1,1,0),
      (1,1,1),
    };

and this one

case(2):
    
    digitalWrite(Led[0],State2[Count][0]);
    digitalWrite(Led[1],State2[Count][1]);
    if (Count==6)Count=0;
    break;
    case(3):
    
    digitalWrite(Led[0],State3[Count][0]);
    digitalWrite(Led[1],State3[Count][1]);
    digitalWrite(Led[2],State3[Count][2]);
    if (Count==9)Count=0;
    break;

as you can see that in case2 i have use the pattern in State2, however what i dont get why do i need to reset Count==6 as oppose to 4/5?
and in case 3
i use the pattern that i set in State3, however what i dont get is although it doesnt follow the pattern that i have set in array State3, it does however able to repeat the same sequence every time though its not the one that i have set, i can also verify that all my led work and pin work

as you can see that in case2 i have use the pattern in State2, however what i dont get why do i need to reset Count==6 as oppose to 4/5?

I don't either. Why do you think you need to? It looks to me like when the value gets to 3, it should be reset to 0, since 3 is the highest valid index into State2.

In case 3, Count should be reset when it gets to 7, since that is the highest valid index into State3.

Of course, using int as the type for those arrays is a waste of space.

Serial.print() the array (one element at a time) to see if something is stepping on the array.