Go Down

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

e10eqs

I am trying to make one button turn on a green led when it has been pressed once and a red led when it has been pressed twice. I have attached a screenshot of the code that I have been using, as I have seen other people have more success when doing this. Thanks for the help!

larryd

#1
Jul 09, 2017, 06:05 am Last Edit: Jul 09, 2017, 06:08 am by larryd
== is a comparison operator, you want =
Watch out for placement of  ;  at the end of your 'if' statements.

x == 2;  wrong
x = 2; right

Don't show us an image of your code.

Always show us your current compete sketch.
Use CTRL T to format the sketch.
Please use code tags. Use the </> icon in the posting menu.   
[code] Paste sketch here. [/code]


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

e10eqs

Thanks for the response! I changed the comparison operators and the format, so here is my code now.

Code: [Select]

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

int buttonState = 0;
int buttoncount = 0;


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

void loop()
{
  buttonState = digitalRead(button);
  if (buttonState == LOW)
  {
    buttoncount == buttoncount++;
  }
  if (buttoncount = 1)
  {
    digitalWrite(ledgreen, HIGH);
    delay(3000);
    digitalWrite(ledgreen, LOW);
  }
  if (buttoncount = 2)
  {
    digitalWrite(ledred, HIGH);
    delay(3000);
    buttoncount == 0;
  }
}

larryd

#3
Jul 09, 2017, 07:16 pm Last Edit: Jul 09, 2017, 07:31 pm by larryd
No!
Read: https://www.arduino.cc/en/Reference/Assignment

https://www.arduino.cc/en/Reference/If

if (buttoncount = 2)
You need
if (buttoncount == 2)

buttoncount == 0;
You need
buttoncount = 0;

Also
buttoncount == buttoncount++;
You need:
buttoncount++;



Are you aware   delay(3000);   stops code execution for 3 seconds?
Have you looked at the BWD (Blink Without Delay) example in the IDE?


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

larryd

#4
Jul 09, 2017, 07:22 pm Last Edit: Jul 09, 2017, 07:25 pm by larryd
See if you can follow this.
Follow these instructions:
Swap the comment marks // on the next two lines to see a comparison
  //usingDelay();
  usingMillis();
i.e.
  usingDelay();
  //usingMillis();


Code: [Select]

/*
  GetRidOfDelay.ino
  LarryD
  Version YY/MM/DD
  1.00    17/02/01  Running code

*/

//LED wiring:
// +5V---220 ohm resistor---LED anode---LED cathode---Arduino pin

const byte heartBeatLED            = 13;    //used to see if the sketch is blocking
const unsigned long heartBeatDelay = 100UL;
const byte myLED                   = 12;    //wired so a LOW turns the LED on

//SRAM variables
unsigned long currentMillis;
unsigned long heartBeatMillis;
unsigned long myLEDmillis;
unsigned long waitTime = 0;
unsigned long currentMicros;

byte LEDstate = 0;

//                          s e t u p ( )
//**********************************************************************
void setup()
{
  pinMode(heartBeatLED, OUTPUT);

  pinMode(myLED, OUTPUT);
  digitalWrite(myLED, HIGH);        //HIGH is LED off

} //                 E n d  o f  s e t u p ( )

//                          l o o p ( )
//**********************************************************************
void loop()
{
  currentMillis = millis(); //for milli second timing
  currentMicros = micros(); //for mirco second timing

  //***************************
  //HeartBeat LED, should toggle every heartBeatDelay milliseconds if code is nonblocking
  if (currentMillis - heartBeatMillis >= heartBeatDelay)
  {
    heartBeatMillis = heartBeatMillis + heartBeatDelay; //reset timing

    //Toggle heartBeatLED
    digitalWrite(heartBeatLED, !digitalRead(heartBeatLED));
  }

  //***************************
  //swap the comment marks // on the next two lines to see a comparison

  //usingDelay();
  usingMillis();


} //                    E n d  o f  l o o p ( )

//======================================================================
//                        F U N C T I O N S
//======================================================================

//                     u s i n g D e l a y ( )
//**********************************************************************
void usingDelay()
{
  digitalWrite(myLED, LOW);  //LOW is LED on
  delay(2000);
  digitalWrite(myLED, HIGH); //HIGH is LED off
  delay(1000);
  digitalWrite(myLED, LOW);  //LOW is LED on
  delay(500);
  digitalWrite(myLED, HIGH); //HIGH is LED off
  delay(250);

}//                E n d  o f  u s i n g D e l a y ( )

//                     u s i n g M i l l i s ( )
//**********************************************************************
void usingMillis()
{
  //is it time to process the next state code?
  if (currentMillis - myLEDmillis < waitTime)
  {
    //No, it is not time
    return;
  }

  //Yes, it is now time
  myLEDmillis = currentMillis;      //reset timing

  //state code
  switch (LEDstate)
  {
    case 0:
      digitalWrite(myLED, LOW);     //turn LED on
      LEDstate = 1;                 //next state
      waitTime = 2000UL;            //setup the wait amount
      break;

    case 1:
      digitalWrite(myLED, HIGH);   //turn LED off
      LEDstate = 2;                //next state
      waitTime = 1000UL;           //setup the wait amount
      break;

    case 2:
      digitalWrite(myLED, LOW);    //turn LED on
      LEDstate = 3;                //next state
      waitTime = 500UL;            //setup the wait amount
      break;

    case 3:
      digitalWrite(myLED, HIGH);   //turn LED off
      LEDstate = 0;                //next state
      waitTime = 250UL;            //setup the wait amount
      break;

  } //End of switch/case

}//                E n d  o f  u s i n g M i l l i s ( )

//======================================================================
//                        E N D  O F  C O D E
//======================================================================


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

e10eqs

I checked out your code, and the difference is very clear, but I'm not sure how to implement it into my code. Also I have changed my comparison operators again, so hopefully they are correct this time.

larryd

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

e10eqs

Code: [Select]
const int button = 2;
const int ledgreen = 13;
const int ledred = 12;

int buttonState = 0;
int buttoncount = 0;


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

void loop()
{
  buttonState = digitalRead(button);
  if (buttonState == LOW)
  {
    buttoncount++;
  }
  if (buttoncount == 1)
  {
    digitalWrite(ledgreen, HIGH);
    delay(3000);
    digitalWrite(ledgreen, LOW);
  }
  if (buttoncount == 2)
  {
    digitalWrite(ledred, HIGH);
    delay(3000);
    digitalWrite(ledred, LOW);
    buttoncount = 0;
  }
  else
  {
    digitalWrite(ledred, LOW);
    digitalWrite(ledgreen, LOW);
  }
}

larryd

Quote
turn on a green led when it has been pressed once and a red led when it has been pressed twice.
On the 3rd press what will happen?


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

e10eqs

Hopefully it will go back to turning on the green LED for 3 seconds.

larryd

If you press the button once you want the green LED to come on for 3 seconds then go off.
Then when you press the button a second time you want the red LED to come on for 3 seconds then go off.
Then on the third press you want to repeat things.

Or

If you press the button once 'within a certain period' then the green LED flashes.
If you press the button twice 'within a certain period' then the green LED flash.


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

e10eqs

I want the first scenario described, I hope to eventually use this type of program to make a motor run forward and backward using and L298N motor controller.

larryd

What happens if the user presses the button when the green LED is on?


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

e10eqs

I want nothing to happen until the 3 second wait period is up.

larryd

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

Go Up