Issue with boolean toggle, and statement

Hey guys, I'm working on a project for an intro to engineering course, and we haven't had any experience with C++ prior to this. The professor isn't helping at all, so I figured I'd ask you guys.

Basically this is a miniature game, where if you press the button while the Red LED (ledPin1) is on, the Red LED will turn off, a green Win LED will turn on (winPin), and a motor will spin.

For some reason I can't get this and statement working... It might be the boolean, the if statement, or the and statement. When I verify Arduino says it's fine.

Thanks!


const int ledPin1 = 0;
const int buttonPin1 = 1;
const int winPin = 2;
int buttonState1 = 0;
boolean ledState1 = false;

void setup() {
pinMode(ledPin1, OUTPUT);
pinMode(buttonPin1, INPUT);
}

void loop() {
buttonState1 = digitalRead(ButtonPin1);
digitalWrite(ledPin1, HIGH);
ledState1 = !ledState1;
delay(10000);
digitalWrite(ledPin1, LOW);
ledState1 = !ledState1;
delay(10000);

// First LED, First button

if (buttonState1 == HIGH and ledState1 == true) {
digitalWrite(ledPin1, LOW);
// add motor start here
digitalWrite(winPin, HIGH);
delay(7000); //thinking about changing this delay to a longer number, has to be longer than any delays in the LED on/off cycle, which will later be expanded to seem semi-random
// add motor end here
digitalWrite(winPin, LOW);
}

//cycle repeats, will check

}

Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is that the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. If your browser doesn't show the posting toolbar then you can just manually add the code tags:
[code]``[color=blue]// your code is here[/color]``[/code]
Using code tags and other important information is explained in the How to use this forum post. Please read it.

Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read. If you're using the Arduino Web Editor you will not have access to this useful tool but it's still unacceptable to post poorly formatted code. I recommend you to use the standard IDE instead.

Please remove unnecessary blank lines from your code before posting to the forum. One or two to separate code into logical sections is fine but large spaces for no reason or random blank lines just make for more scrolling when we're trying to read your code.

OK now let's look at the code.
Line 5:

boolean ledState1 = false;

ledState1 is initialized to false
Line 20:

ledState1 = !ledState1;

value of ledState1 is inverted to true.
Line 23:

ledState1 = !ledState1;

value of ledState1 is inverted to false
Line 28:

if (buttonState1 == HIGH and ledState1 == true) {

If statement checks if the value of ledState1 is true.

So there is no way for your if statement to ever evaluate true.

See what you can do to fix that and if you are still having trouble, come back here with your updated code and a clear description of exactly what you're trying to accomplish and what the program is doing instead of that.

1 Like

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom... :slight_smile: