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!
== 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]
.
Thanks for the response! I changed the comparison operators and the format, so here is my code now.
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;
}
}
No!
Read: = - Arduino Reference
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?
.
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();
/*
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
//======================================================================
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.
Show us your current code.
.
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);
}
}
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?
.
Hopefully it will go back to turning on the green LED for 3 seconds.
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.
.
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.
What happens if the user presses the button when the green LED is on?
.
I want nothing to happen until the 3 second wait period is up.
Why do you want this.
.
So that the LED can complete its 3 second time.
No, What are you going to use this for, school, work, personal project . . .
Oh, I see-I am using it for a personal project.
Do you understand what is happening in this example?
Why is this line necessary or is it? greenFlag = false;
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 ( )
//********************************************
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.
