Pages: [1] 2   Go Down
Author Topic: Multiple Blink States with a button no delay  (Read 996 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 45935
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 27
Posts: 1178
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 45935
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
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;

 
}

Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 45935
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

California
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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'

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

can be less awkwardly expressed as

Code:
State = ++State % 4;

same with this awkward statement

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

Logged

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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'

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

can be less awkwardly expressed as

Code:
State = ++State % 4;

same with this awkward statement

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



thanks lloyddean i dont know that you could do that
but that aside how about this?
Code:
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;
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 45935
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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?
Logged

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Finland
Offline Offline
Jr. Member
**
Karma: 1
Posts: 84
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1] 2   Go Up
Jump to: