Boolean operators in a while loop

while experimenting with multiple conditions within do-while loops, i came across a strange phenomena that left me with no results via internet search. i have come for enlightenment. in the code posted below, the conditions to remain in the while loop are listed as “logical and” rather than “logical or.”

my code is intended to run the motor until: the button is pressed, OR the timer runs out. with the “logical and” operator, this holds true. when using the “logical or” operator, the conditions are treated as if i used the “logical and” and thus requires the timer to reach zero or less for the button to have an effect.

the logical operators are acting in complete contrary to my understanding of “or” and “and”.

what do i not understand about this scenario that would cause the operators to be reversed in usage?

void setup() {
  pinMode(13, OUTPUT);    //setup pins and establish serial usb connection
  pinMode(12, OUTPUT);
  pinMode(A0, INPUT);
  Serial.begin(9600);
  delay(3000);

}

void loop() {
  digitalWrite(12, HIGH);   //activate motor
  int TimerMax = 100;       //designate timer maximum
  do {
    TimerMax =  (TimerMax - 1);
    Serial.println(TimerMax);
    Serial.println(analogRead(A0));
    delay(100);
  }
  while ((TimerMax > 0)  && (analogRead(A0) < 1000));         // enter do loop until: timer reaches zero, or button is pressed

  digitalWrite(12, LOW);      //stop motor, delay for reset to next cycle.
  delay(5000);
}

First, type ctrl-t in the Arduino IDE to properly format your code.

Next, post the Serial output supporting your assertion.

output written in code format. first run is with the button pressed mid way through the timer; second run goes until the timer hits zero. you can tell when the button is pressed because the timer resets back to 100. the second number in the column is the reading of the buttonstate, which is normally closed to ground.

20:35:38.070 -> 99
20:35:38.070 -> 0
20:35:38.171 -> 98
20:35:38.171 -> 0
20:35:38.272 -> 97
20:35:38.272 -> 0
20:35:38.374 -> 96
20:35:38.374 -> 0
20:35:38.473 -> 95
20:35:38.473 -> 0
20:35:38.574 -> 94
20:35:38.574 -> 0
20:35:38.674 -> 93
20:35:38.674 -> 0
20:35:38.776 -> 92
20:35:38.776 -> 0
20:35:38.876 -> 91
20:35:38.876 -> 0
20:35:38.978 -> 90
20:35:38.978 -> 0
20:35:39.079 -> 89
20:35:39.079 -> 0
20:35:39.180 -> 88
20:35:39.180 -> 0
20:35:39.282 -> 87
20:35:39.282 -> 0
20:35:39.384 -> 86
20:35:39.384 -> 0
20:35:39.487 -> 85
20:35:39.487 -> 0
20:35:39.589 -> 84
20:35:39.589 -> 0
20:35:39.690 -> 83
20:35:39.690 -> 0
20:35:39.792 -> 82
20:35:39.792 -> 0
20:35:39.893 -> 81
20:35:39.893 -> 0
20:35:39.995 -> 80
20:35:39.995 -> 0
20:35:40.063 -> 79
20:35:40.097 -> 0
20:35:40.198 -> 78
20:35:40.198 -> 0
20:35:40.266 -> 77
20:35:40.299 -> 0
20:35:40.366 -> 76
20:35:40.400 -> 0
20:35:40.468 -> 75
20:35:40.501 -> 0
20:35:40.568 -> 74
20:35:40.602 -> 0
20:35:40.670 -> 73
20:35:40.670 -> 0
20:35:40.772 -> 72
20:35:40.772 -> 0
20:35:40.873 -> 71
20:35:40.873 -> 0
20:35:45.980 -> 99
20:35:45.980 -> 0
20:35:46.083 -> 98
20:35:46.083 -> 0
20:35:46.183 -> 97
20:35:46.183 -> 0
20:35:46.284 -> 96
20:35:46.284 -> 0
20:35:46.385 -> 95
20:35:46.385 -> 0
20:35:46.487 -> 94
20:35:46.487 -> 0
20:35:46.590 -> 93
20:35:46.590 -> 0
20:35:46.691 -> 92
20:35:46.691 -> 0
20:35:46.793 -> 91
20:35:46.793 -> 0
20:35:46.895 -> 90
20:35:46.895 -> 0
20:35:46.996 -> 89
20:35:46.996 -> 0
20:35:47.097 -> 88
20:35:47.097 -> 0
20:35:47.199 -> 87
20:35:47.199 -> 0
20:35:47.303 -> 86
20:35:47.303 -> 0
20:35:47.405 -> 85
20:35:47.405 -> 0
20:35:47.474 -> 84
20:35:47.508 -> 0
20:35:47.577 -> 83
20:35:47.610 -> 0
20:35:47.678 -> 82
20:35:47.711 -> 0
20:35:47.780 -> 81
20:35:47.780 -> 0
20:35:47.881 -> 80
20:35:47.915 -> 0
20:35:47.982 -> 79
20:35:47.982 -> 0
20:35:48.083 -> 78
20:35:48.083 -> 0
20:35:48.183 -> 77
20:35:48.183 -> 0
20:35:48.286 -> 76
20:35:48.286 -> 0
20:35:48.386 -> 75
20:35:48.386 -> 0
20:35:48.488 -> 74
20:35:48.488 -> 0
20:35:48.591 -> 73
20:35:48.591 -> 0
20:35:48.692 -> 72
20:35:48.692 -> 0
20:35:48.794 -> 71
20:35:48.794 -> 0
20:35:48.895 -> 70
20:35:48.895 -> 0
20:35:48.996 -> 69
20:35:48.996 -> 0
20:35:49.098 -> 68
20:35:49.098 -> 0
20:35:49.199 -> 67
20:35:49.199 -> 0
20:35:49.300 -> 66
20:35:49.300 -> 0
20:35:49.400 -> 65
20:35:49.400 -> 0
20:35:49.500 -> 64
20:35:49.500 -> 0
20:35:49.602 -> 63
20:35:49.602 -> 0
20:35:49.703 -> 62
20:35:49.703 -> 0
20:35:49.805 -> 61
20:35:49.805 -> 0
20:35:49.905 -> 60
20:35:49.905 -> 0
20:35:50.007 -> 59
20:35:50.007 -> 0
20:35:50.108 -> 58
20:35:50.108 -> 0
20:35:50.209 -> 57
20:35:50.209 -> 0
20:35:50.311 -> 56
20:35:50.311 -> 0
20:35:50.412 -> 55
20:35:50.412 -> 0
20:35:50.513 -> 54
20:35:50.513 -> 0
20:35:50.614 -> 53
20:35:50.614 -> 0
20:35:50.716 -> 52
20:35:50.716 -> 0
20:35:50.818 -> 51
20:35:50.818 -> 0
20:35:50.920 -> 50
20:35:50.920 -> 0
20:35:51.021 -> 49
20:35:51.021 -> 0
20:35:51.090 -> 48
20:35:51.125 -> 0
20:35:51.192 -> 47
20:35:51.226 -> 0
20:35:51.295 -> 46
20:35:51.295 -> 0
20:35:51.397 -> 45
20:35:51.397 -> 0
20:35:51.498 -> 44
20:35:51.498 -> 0
20:35:51.600 -> 43
20:35:51.600 -> 0
20:35:51.701 -> 42
20:35:51.701 -> 0
20:35:51.802 -> 41
20:35:51.802 -> 0
20:35:51.902 -> 40
20:35:51.902 -> 0
20:35:52.003 -> 39
20:35:52.003 -> 0
20:35:52.104 -> 38
20:35:52.104 -> 0
20:35:52.206 -> 37
20:35:52.206 -> 0
20:35:52.308 -> 36
20:35:52.308 -> 0
20:35:52.408 -> 35
20:35:52.408 -> 0
20:35:52.509 -> 34
20:35:52.509 -> 0
20:35:52.611 -> 33
20:35:52.611 -> 0
20:35:52.713 -> 32
20:35:52.713 -> 0
20:35:52.814 -> 31
20:35:52.814 -> 2
20:35:52.916 -> 30
20:35:52.916 -> 0
20:35:53.018 -> 29
20:35:53.018 -> 0
20:35:53.119 -> 28
20:35:53.119 -> 0
20:35:53.220 -> 27
20:35:53.220 -> 0
20:35:53.320 -> 26
20:35:53.320 -> 0
20:35:53.422 -> 25
20:35:53.422 -> 0
20:35:53.524 -> 24
20:35:53.524 -> 0
20:35:53.625 -> 23
20:35:53.625 -> 2
20:35:53.726 -> 22
20:35:53.726 -> 0
20:35:53.827 -> 21
20:35:53.827 -> 0
20:35:53.930 -> 20
20:35:53.930 -> 0
20:35:54.032 -> 19
20:35:54.032 -> 0
20:35:54.133 -> 18
20:35:54.133 -> 0
20:35:54.234 -> 17
20:35:54.234 -> 0
20:35:54.335 -> 16
20:35:54.335 -> 0
20:35:54.403 -> 15
20:35:54.436 -> 0
20:35:54.537 -> 14
20:35:54.537 -> 0
20:35:54.604 -> 13
20:35:54.637 -> 0
20:35:54.705 -> 12
20:35:54.739 -> 1
20:35:54.806 -> 11
20:35:54.839 -> 0
20:35:54.907 -> 10
20:35:54.940 -> 0
20:35:55.007 -> 9
20:35:55.042 -> 0
20:35:55.109 -> 8
20:35:55.109 -> 0
20:35:55.209 -> 7
20:35:55.209 -> 0
20:35:55.313 -> 6
20:35:55.313 -> 0
20:35:55.414 -> 5
20:35:55.414 -> 0
20:35:55.515 -> 4
20:35:55.515 -> 0
20:35:55.617 -> 3
20:35:55.617 -> 0
20:35:55.716 -> 2
20:35:55.716 -> 0
20:35:55.818 -> 1
20:35:55.818 -> 0
20:35:55.919 -> 0
20:35:55.919 -> 0
[Code]

I only see one “run” in that data stream with the timer counting down from 99 to 0 and the second number never changes.

Questions:

  • Why do you use analogRead() instead of digitailRead() to determine the button state.

  • Why do you read the button state TWICE on each loop (in println() and in while clause)? Why not just read it once each loop and assign to a variable to be used again.

  • Why don’t you .print() the timer and corresponding button reading on the same line? That would make things easier to read.

You're just thinking bcakwards. It should exit on either or, so it should stay in while BOTH are true.

If you code it with an OR in there then it will stay in the while loop while either one OR the other is true. Which is exactly the behavior you describe.

You're confusing the condition to exit with the condition to stay in. A while loop needs a condition telling it when to keep running.

Your button is always reading as zero. My guess is that it is wired wrong.

johnwasser yes, the button is wired to ground in the "normally closed" configuration until the button is pressed.the arduino would then read an analog value of 1023 when the 5V source would then be connected. the program would not print the large value because the reading wouldnt be posted until the next cycle. the system is wired as intended.

Delta_G:
You're just thinking bcakwards. It should exit on either or, so it should stay in while BOTH are true.

If you code it with an OR in there then it will stay in the while loop while either one OR the other is true. Which is exactly the behavior you describe.

You're confusing the condition to exit with the condition to stay in. A while loop needs a condition telling it when to keep running.

Ah, thank you. now that you put it that way it seems so simple and obvious. i appreciate you clarifying that for me, and now i may move forward and integrate it with my larger project.

now its my turn to give advice: gfvalvo, i get you're on the forums to provide help, but your comments come across as nitpicky and mildly disdaining. my software is intentionally written incredibly simple for the sake of understanding this specific coding error and is only focusing on ONE line of code. the data and problem explanation are clear if you would just read it.

my question is answered and no further comments are necessary on this thread. cheers!

A little advice for you then if you want to keep getting advice here. Someone tried to help you for free. They felt that they needed some information to help and they asked for it. If they weren’t right about the problem that’s no reason to go on the attack. They tried. The correct response from you is “Thank you for your time and effort but we solved it with something else”. If you’re one of those (and we get a lot of them here) that’s going to start bitching every time you’re asked for an additional piece of info or if the answer is wrong then you’ll find that support for you and that attitude dries up pretty fast around here and soon you’ll wonder why nobody ever answers your posts.

Nobody is here trying to nitpick you or anything like that. Why would we waste our time on that? We are trying to help and maybe don’t come across as the most personable bunch. That’s what you mostly get with programmers. They’re not known for their people skills.

So if you want to cry about your just respects or your feelings being hurt then this isn’t the right forum for that. I hear Dr Phil has a great forum where you can talk about your feelings and how you perceived someone was being mean to you. They’ll care there and they will help you through the trauma of reading a response that didn’t immediately solve your problem. But they absolutely suck at coding so they’re not going to be able to help much with your program. You’ll just have to get some counseling there and then come back here and face the big old meanie asking for details.

well my post was taken way out of context (as are most questions with simple answers), but if you want to take personal swings at me then go ahead if that makes your day better. i suppose thats whats wrong with text- only communication, the real message doesnt come across. have a good day Delta_G.

stoic101:
now its my turn to give advice: gfvalvo, i get you're on the forums to provide help, but your comments come across as nitpicky and mildly disdaining**...blahh, blahh, blahh**

Actually, all my questions were aimed at getting a better understanding of what you’re trying to do or to better interpret your results. All so I might be able to provide informed assistance. But, no worries. I’ve added your User ID to my “list”. You won’t have to worry about me ever attempting to help you again.

Me either. Best of luck to you.