Go Down

Topic: multiple functions with one button (Read 445 times) previous topic - next topic

e10eqs

So that the LED can complete its 3 second time.

larryd

#16
Jul 10, 2017, 06:30 am Last Edit: Jul 10, 2017, 06:31 am by larryd
No, What are you going to use this for, school, work, personal project . . .
No technical PMs.
The last thing you did is where you should start looking.

e10eqs

Oh, I see-I am using it for a personal project.

larryd

#18
Jul 10, 2017, 06:56 am Last Edit: Jul 10, 2017, 07:10 am by larryd
Do you understand what is happening in this example?

Why is this line necessary or is it? greenFlag = false;

Code: [Select]

const byte button    = 2;
const byte ledgreen  = 13;
const byte ledred    = 12;

byte buttonState     = 0;
byte lastButtonState = HIGH;
byte buttonCounter   = 0;

bool greenFlag = false;      //when true the green LED can be flahed

//********************************************
void setup()
{
  pinMode(button, INPUT_PULLUP);
  pinMode(ledgreen, OUTPUT);
  pinMode(ledred, OUTPUT);

} //END of      s e t u p ( )

//********************************************
void loop()
{
  checkButton();

  if (greenFlag == true && buttonCounter == 1)
  {
    digitalWrite(ledgreen, HIGH);
    delay(3000);
    digitalWrite(ledgreen, LOW);
    greenFlag = false;
  }
  
  if (buttonCounter == 2)
  {
    digitalWrite(ledred, HIGH);
    delay(3000);
    digitalWrite(ledred, LOW);
    buttonCounter = 0;
  }

} //END of     l  o o p ( )



//********************************************
void checkButton()
{
  buttonState = digitalRead(button);

  if (lastButtonState != buttonState)
  {
    lastButtonState = buttonState;

    //has the button been released (HIGH is not pushed)
    if (buttonState == HIGH)
    {
      buttonCounter++;

      if (buttonCounter == 1)
      {
        //allow the green LED to be flashed
        greenFlag = true;
      }
    }
  }

} //END of     c h e c k B u t t o n ( )

//********************************************

No technical PMs.
The last thing you did is where you should start looking.

e10eqs

I think I understand, the greenFlag line is necessary because it allows the LED to turn off after 3 seconds and move on to buttonCount == 2.

larryd

#20
Jul 10, 2017, 07:36 pm Last Edit: Jul 10, 2017, 07:37 pm by larryd
Put comments in front of this line to see what result it gives.

//greenFlag = false;

Explain the new results?



Currently you have to press the switch then let it go before the timing sequence starts.

What has to be done so the timing starts the moment the switch is pressed?


.


No technical PMs.
The last thing you did is where you should start looking.

e10eqs

I'm not sure how to start the timing sequence, the LED just eventually shuts off for me and wait's to the next button press to move on.

larryd

#22
Jul 13, 2017, 12:55 am Last Edit: Jul 13, 2017, 01:01 am by larryd
If you don't have greenFlag at that location, buttonCounter == 1 will still be true (at then end of loop), then timing continues again and again, the green LED goes on again. greenFlag limits the code to running once.

Using greenFlag, only when the counter reaches 1 again is greenFlag set back to true which then allows the green LED to go on again. Therfore greenFlag enables and disables code execution.
Do you understand?

I'm not sure how to start the timing sequence, the LED just eventually shuts off for me and wait's to the next button press to move on.
Not sure what you are saying here.

How would you start the timing 'when' the button is first pushed i.e. not like it is now, when you 'release' the switch?


.

No technical PMs.
The last thing you did is where you should start looking.

Go Up