Hey all.
Im trying to build a small game with some leds and button presses.
It seems i dont understand IF and WHILE.
The buttons dont realy change the values i think.
Is there a way to see what the arduino registers on button presses and so forth?
// Initialize variables
const int gledPin = 12;
const int yledPin = 11;
const int rledPin = 10;
const int armerPin = 9;
const int desarmerPin = 8;
int togglePassiv = true;
int toggleArmeret = false;
int toggleDesarmeret = false;
//Configure devices
void setup() {
pinMode(gledPin, OUTPUT);
pinMode(yledPin, OUTPUT);
pinMode(rledPin, OUTPUT);
pinMode(armerPin, INPUT_PULLUP);
pinMode(desarmerPin, INPUT_PULLUP);
}
void loop() {
//Knaptryk
if (digitalRead(armerPin) == false) {
toggleArmeret = true;
togglePassiv = false;
}
if (digitalRead(desarmerPin) == false) {
toggleDesarmeret = true;
toggleArmeret = false;
togglePassiv = false;
}
while (togglePassiv == true) {
digitalWrite(yledPin, HIGH);
delay(1000);
digitalWrite(yledPin, LOW);
delay(1000);
}
while (toggleArmeret == true) {
digitalWrite(rledPin, HIGH);
delay(700);
digitalWrite(rledPin, LOW);
delay(700);
}
while (toggleDesarmeret == true) {
digitalWrite(gledPin, HIGH);
delay(500);
digitalWrite(gledPin, LOW);
delay(500);
}
}
Working code looks like this:
// Initialize variables
const int gledPin = 12;
const int yledPin = 11;
const int rledPin = 10;
const int armerPin = 9;
const int desarmerPin = 8;
int togglePassiv = true;
int toggleArmeret = false;
int toggleDesarmeret = false;
//Configure devices
void setup() {
pinMode(gledPin, OUTPUT);
pinMode(yledPin, OUTPUT);
pinMode(rledPin, OUTPUT);
pinMode(armerPin, INPUT_PULLUP);
pinMode(desarmerPin, INPUT_PULLUP);
}
void loop() {
//Opstart af program. Bombe starter som passiv med gult lys.
while (togglePassiv == true) {
digitalWrite(yledPin, HIGH);
delay(1000);
digitalWrite(yledPin, LOW);
delay(1000);
//Knaptryk Armerer bomben
if (digitalRead(armerPin) == false) {
toggleArmeret = true;
togglePassiv = false;
}
}
//Når bomben er armeret lys rød led.
while (toggleArmeret == true) {
digitalWrite(rledPin, HIGH);
delay(700);
digitalWrite(rledPin, LOW);
delay(700);
//Hvis desarmer knappen trykkes desarmer bomben.
if (digitalRead(desarmerPin) == false) {
toggleDesarmeret = true;
toggleArmeret = false;
togglePassiv = false;
}
}
while (toggleDesarmeret == true) {
digitalWrite(gledPin, HIGH);
delay(500);
digitalWrite(gledPin, LOW);
delay(500);
}
}
Thank you for trying to use code tags but you got it wrong so I fixed it for you
In my experience the easiest way to tidy up the code and add the code tags is as follows
Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.
This loop will continue while togglePassiv is true but there is nothing in the loop that ever changes its value so the loop will never end
The code is executed line by line so the code outside of the while loop will not be executed as long as the while loop continues if that was what you were hoping for
wherever, all over, anywhere you want to see the flow of your code going by, and to confirm the values of variables that inform the flow or sensor readings or pushbutton states or calcualtions.
I don't think I have ever gotten something to work and know for sure it was working to my satisfaction, without feedback beyond apparent correct behaviour.
You spend a lot of time in delay(1000); which is blocking code, within blocking code, the while() loop. Great potential to miss button state changes exactly when you want them to occur. In your sketch, it's not really a big deal because you only have a couple of things going on. But if and when you decide to expand, I'm sure you understand how little issues can quickly become bigger, complicated ones to troubleshoot.
I always recommend to regular, amateur folks (like me) building games with Arduino to learn about and build their game as a state machine.
In the IDE, the examples are File>Examples>Control>Switch Case (and Switch Case 2) but they don't do a great job of explaining the power of the state machine in an Arduino game such as you are trying to build.
Can you please describe the game to us? What is supposed to happen, what makes it a "game", what are your goals for it?
I ask because with more info, I likely already have a sketch in my sketchbook that does something similar to what you're after; thus, I'll be able to better explain the state machine in your particular instance, and may even have some nifty functions for you you supe up your original goal
The idea of the game is.
When the Arduino powers up it starts the game with the bomb passive, blinking a yellow led. Once the armer button is pressed the yellow led should be stopped and the red led should start blinking. And the game state should change to armed. If the game state is armed and the disarm button is pressed the green leds should light up.
Ill add time and other functions to the game later but i think that as soon as i understand the basics i can actually start to realy learn.
Citatblok Your use of if statements looks OK but not your use of the while statements
Citatblok This loop will continue while togglePassiv is true but there is nothing in the loop that ever changes its value so the loop will never end
Citatblok The code is executed line by line so the code outside of the while loop will not be executed as long as the while loop continues if that was what you were hoping for
Thank you this was exactly what i needed. I just put the IF states inside the WHILE and since i apparently fixed the IF sentence the program works as intended :).