Please help a newbie

I have to write a program that will flash a yellow led a few times to show that everything is ok.Then a green led will blink at .25Hz until a press button is pushed and held down. Once the push button is pressed and held, the green led will be off and a red led will blink at .5Hz. I'm having problems having the yellow led to only blink a few times and then stop being part of the loop. Only I would like to use a check command to make sure the board check more often to see if the press button is activated. I know this probably don't make sense. I tried hard to make it work. Attached is my code. Thank you for the help

const int button = 2; // pin number of the pushbutton pin const int greenLed = 8; // pin number of the green led const int redLed = 7 ; // pin number of the red led const int yellowLed= 13 ; // pin number of the yellow led int buttonState = 0; // variable for reading the pushbutton status void setup() { pinMode (greenLed, OUTPUT) ;// establish the LED pin as an output: pinMode (redLed, OUTPUT) ;// establish the LED pin as an output: pinMode (yellowLed, OUTPUT);// establish the LED as an output: pinMode(button, INPUT); // establish thepushbutton as an input: } void loop() {{ digitalWrite(yellowLed, HIGH); delay (1000); digitalWrite(yellowLed, LOW); delay (1000); } digitalRead(button);// read the state of the pushbutton value: if (buttonState == HIGH) { // if button is press turn on led. digitalWrite(redLed, HIGH); // led on if the button press delay(2000); digitalWrite (redLed, LOW); // led will flash at a frequency of .5Hz } else { // if button is not press, the red led will be off and the green on

digitalWrite(greenLed, HIGH); //led on, if button not press delay (4000); digitalWrite(greenLed, LOW); //led will flash at a frequency of .25 Hz delay (4000); } }

With delays of 1, 2, 4 seconds how will you ever manage to flash LEDs at .25 or .5 Hz? My mistake, look at a state machine for flashing.

I will change it, thank you.

Make sure you put your code in the code tags, or people get a little angry on here. Hold your mouse over the different symbols above the post, like bold italics. Mine aren't showing up at the moment for some reason, so I forget which symbol it is. # maybe?

You said you wanted to only do this if a button is pushed and held. Since a "press" is considered different than a "hold", how long should the button be pressed before it becomes a "hold". Standard in my industry is 2-3s. And forgive me, but just to make sure since button presses etc are not measured in Hz, you need seconds or milliseconds to be precise. It's an inverse relationship, right? So 1/2 Hz is 2s?

Nope there it is. Far right, looks like a piece of paper with <> on it… next to chat bubble.

I want the yellow to flash three times and stop. Then I want a loop that will flash the green light until the button is hold down. At that moment the red light will blink until the button is release. I would like to create a function that will check every 100 ms if the button is hold on.

Thank you so much for your tips and help.

Okay, so you want a yellow light to flash 3 times then stop to say things are okay. After it flashes three times, a green light flashes forever until a button is pushed. FYI, momentary contacts (button presses) sure usually 500ms, not 100ms, so you don't need to do a 100ms check. If you want to do a hold, then it should be for 2-3s. If you just want someone to push a button to go to the next step, then just have them push the button. Which do you prefer?

Just push the button to go to the next step please.

const int button = 2;     // pin number of the pushbutton pin
const int greenLed =  8;    // pin number of the green led 
const int redLed = 7 ;       // pin number of the red led 
const int yellowLed= 13 ;    // pin number of the yellow led 
boolean buttonState = 0;         //USE BOOLEAN FOR STATES
boolean systemOK = 1;        // TO CHECK WHETHER YELLOW SHOULD FLASH
void setup() {
  pinMode (greenLed, OUTPUT) ;// establish the LED pin as an output:
  pinMode (redLed, OUTPUT) ;//  establish the LED pin as an output:
  pinMode (yellowLed, OUTPUT);// establish  the LED as an output:
  pinMode(button, INPUT);  // establish  thepushbutton as an input:  
}
void loop()
{
  digitalRead(button);// read the state of the pushbutton value:
  if (systemOK)
  {
      for (int x=1; x<=3; x++)    // You need a loop to blink more than once and 
      {                     // it makes it easier to blink more or less if needed
        digitalWrite(yellowLed, HIGH);
        delay (1000);
        digitalWrite(yellowLed, LOW);
        delay (1000);
        !systemOK;
      }
  }

  
  while (!systemOK)          //enter loop after yellow blinks are over
  {
      digitalWrite(greenLed,HIGH);
      delay(4000);
      digitalWrite(greenLed,LOW);
      delay(4000);
  
      while (buttonState == HIGH)  //enters this loop once button pressed
      {     // if button is press turn on led.
           digitalWrite(redLed, HIGH); // led on if the button press
             delay(2000);
           digitalWrite (redLed, LOW);  // led will flash at a frequency of .5Hz]
           delay(2000);
           systemOK;
      }                            //stays in loop until button let go
        
  }                        // stays in this loop if button hasn't been pressed
}

There is still one problem that will make your code not work like you want. That is your liberal use of the delay() command. That stops all arduino processing. I need to work on this as well for a project of mine, but you have to use the mills() command so that you can do button blinks without wasting cycles. For example, as it is with the yellow blinks, there will be 6 seconds where it doesn’t matter whether they push the button or not.

I can't thank you enough for your help.

This gets rid of the delays, may have to adjust some elements for your case:
If you have questions we can walk you through the code.

const int button = 2;        // pin number of the pushbutton pin
const int greenLed =  8;     // pin number of the green led 
const int redLed = 7 ;       // pin number of the red led 
const int yellowLed= 13 ;    // pin number of the yellow led 
int buttonState = 0;         // variable for reading the pushbutton status

unsigned long yellowTime;    // used to time things see blink without delay
unsigned long greenTime;  
unsigned long redTime;    
unsigned long checkSWtime;

byte yellowCounter = 0 ;     // the number of flashes on the yellow LED
byte currentState = 0;       // the start sate of the machine

void setup() {
  pinMode (yellowLed, OUTPUT);// establish  the LED as an output:
  pinMode (greenLed, OUTPUT) ;// establish the LED pin as an output:
  pinMode (redLed, OUTPUT) ;//  establish the LED pin as an output:
  pinMode (button, INPUT);  // establish  thepushbutton as an input: 

  digitalWrite(yellowLed,HIGH);  //Turn off LEDs (if negative logic make this LOW)
  digitalWrite(greenLed,HIGH);
  digitalWrite(redLed,HIGH);

  currentState = 0;      //start out at machine state 0
  yellowTime = millis();    
  checkSWtime = millis();

}
void loop()
{

  //***************************************
  //state machine section
  switch (currentState)
  {
    //****
  case 0:
    //is it time to toggle the LED?
    if ((millis() - yellowTime >= 1000UL) && (yellowCounter < 6))
    {
      //toggle LED
      digitalWrite(yellowLed, !digitalRead(yellowLed));
      yellowTime = millis();
      //
      yellowCounter++;
    }

    //have we flashed enough?
    if (yellowCounter >= 6)
    {
      //change to next machine state, i.e.green
      currentState = 1;  
      greenTime = millis();
      digitalWrite(yellowLed, HIGH); //turn off the led if it is on (might have to be LOW)
    }
    break; 

    //****
  case 1:
    //time to toggle LED?
    if (millis() - greenTime >= 2000UL )
    {
      //do it
      digitalWrite(greenLed, !digitalRead(greenLed));
      greenTime = millis();
    }

    //Time to check for a switch push?
    if (millis() - checkSWtime >= 100)
    {
      checkSWtime = millis();

      if (digitalRead(button) == HIGH) //is the button pushed (may have to be LOW)
      {
        currentState = 2;              //change to red
        digitalWrite(greenLed, HIGH);  //turn off the LED if it is on (might need to be LOW)
      }
    }
    break;

    //****
  case 2:
    //time to toggle LED?
    if ( millis() - redTime >= 4000UL && (digitalRead(button) == HIGH ))
    {
      //do it
      digitalWrite(redLed, !digitalRead(redLed));
      redTime = millis();
    }
    break;

    //****
  default:
    break;

  } //end of switch/case

  //***************************************
  //other loop code stuff here 


}  //end of loop

Thank you, I will educate myself on the code and probably ask questions later. I really appreciate the help received.

I tried the program and I had to change a few minor things. It's starting like I want and it switches led when I press the button, the only thing is that I can't switch it back from one led to another. Looks like I can only use the button once.

Thanks again

Yes, it is written for a single use at reset only, but this can be changed by rewriting some sections. Do you understand what the idea of a state machine is all about? Do you understand what && is doing? Do you understand what the toggle section of code is doing? Do you see any delays( )

Hi Larry,

I’m still reading about all the above. There is no delays. Can you please point me in the right direction to make it work?

Thanks

Would you please describe what you want to change?

I want the led to switch between red and green every time I press the button.

Thank you

What is the purpose of the variable “pushed”?
Why is it needed?

const int button = 2;        // pin number of the pushbutton pin
const int greenLed =  8;     // pin number of the green led 
const int redLed = 7 ;       // pin number of the red led 
const int yellowLed= 13 ;    // pin number of the yellow led 
int buttonState = 0;         // variable for reading the pushbutton status

unsigned long yellowTime;    // used to time things see blink without delay
unsigned long greenTime;  
unsigned long redTime;    
unsigned long checkSWtime;

byte yellowCounter = 0 ;     // the number of flashes on the yellow LED
byte currentState = 0;       // the start sate of the machine

boolean pushed = false;      // flag showing if the button has been pushed

void setup() {
  pinMode (yellowLed, OUTPUT);// establish  the LED as an output:
  pinMode (greenLed, OUTPUT) ;// establish the LED pin as an output:
  pinMode (redLed, OUTPUT) ;//  establish the LED pin as an output:
  pinMode (button, INPUT);  // establish  thepushbutton as an input: 

  digitalWrite(yellowLed,HIGH);  //Turn off LEDs (if negative logic make this LOW)
  digitalWrite(greenLed,HIGH);
  digitalWrite(redLed,HIGH);

  currentState = 0;      //start out at machine state 0
  yellowTime = millis();    
  checkSWtime = millis();

}
void loop()
{

  //***************************************
  //state machine section
  switch (currentState)
  {
    //****
  case 0:
    //is it time to toggle the LED?
    if ((millis() - yellowTime >= 1000UL) && (yellowCounter < 6))
    {
      //toggle LED
      digitalWrite(yellowLed, !digitalRead(yellowLed));
      yellowTime = millis();
      //
      yellowCounter++;
    }

    //have we flashed enough?
    if (yellowCounter >= 6)
    {
      //change to next machine state, i.e.green
      currentState = 1;  
      greenTime = millis();
      digitalWrite(yellowLed, HIGH); //turn off the led if it is on (might have to be LOW)
    }
    break; 

    //****
  case 1:
    //time to toggle LED?
    if (millis() - greenTime >= 2000UL )
    {
      //do it
      digitalWrite(greenLed, !digitalRead(greenLed));
      greenTime = millis();
    }

    if (digitalRead(button) == HIGH) //is the button pushed (may have to be LOW)
    {
      currentState = 2;              //change to red
      digitalWrite(greenLed, HIGH);  //turn off the LED if it is on (might need to be LOW)
      pushed = true;                 
    }
    break;

    //****
  case 2:
    //time to toggle LED?
    if ( millis() - redTime >= 4000UL)
    {
      //do it
      digitalWrite(redLed, !digitalRead(redLed));
      redTime = millis();
    }

    //has the button been released?
    if (millis() - checkSWtime >= 100 && pushed == true )
    {
      checkSWtime = millis();

      if (digitalRead(button) == LOW) //is the button released (may have to be HIGH)
      {
        pushed = false;               // 
        currentState = 1;             //change to green
        digitalWrite(redLed, HIGH);   //turn off the LED if it is on (might need to be LOW)
      }
    }
    break;

    //****
  default:
    break;

  } //end of switch/case

  //***************************************
  //other loop code stuff here 


}  //end of loop

What is the purpose of this section of code? if (millis() - checkSWtime >= 100 && pushed == true ) { checkSWtime = millis(); . . . }

Why do I say these things in the comments? if (digitalRead(button) == LOW) //is the button released (may have to be HIGH) and digitalWrite(yellowLed,HIGH); //Turn off LEDs (if negative logic make this LOW)

What does this do? if ( millis() - redTime >= 4000UL) { //do it digitalWrite(redLed, !digitalRead(redLed)); redTime = millis(); }

Have you gone through the sample sketches that come with the Arduino IDE?

Do you have any questions?