if statements (newbie questions)

Status
Not open for further replies.

Danny123

Member
Hi Forum-members,

First a small introduction, because i am new here,
recently accuiered a teensyboard to learn coding, and what al the possibilities are with it.
i was looking for the newbie-questions-section to give room to the more experienced users, but coulnd find it.
(i guess this is a propper newbie guestion :))

so i try to wrap my head around the if,else,and,or and try to code some basics. with three buttons and three led, and also the onboard led.

But it looks like, that what i try to do, works the opposite way than the logic in my head, and what the reactions are if i program the board. (its good fun, but not the way i wanted it to respond)

so what i try to do, is 3 buttons and three leds all activated on its own like

Button1 = led1, etc etc etc,

but when pushing all the buttons at the same time, nothing should happen : all leds off, only the onboard led (13) can light up.

ive included the sketch, (i know i am not at the finished code) would be appreciated if one could have a quick look, and guide me in the right direction.
(or a good link with some more detailed info, i am getting stuck here on this site, and more google madness)

Thanks in advance
yours
Danny


Code:
void setup() {
  pinMode(8, INPUT_PULLUP);
  pinMode(9, INPUT_PULLUP);
  pinMode(10, INPUT_PULLUP);
  pinMode(11, OUTPUT);
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
  pinMode(14, OUTPUT);

}

void loop()
{
  if (digitalRead(8) == HIGH) {
    digitalWrite(11, LOW);
  }
  else {
    digitalWrite(11, HIGH);
  }

  if (digitalRead(9) == HIGH) {
    digitalWrite(12, LOW);
  }
  else {
    digitalWrite(12, HIGH);
  }

  if (digitalRead(10) == HIGH) {
    digitalWrite(14, LOW);
  }
  else {
    digitalWrite(14, HIGH);
  }
  if (digitalRead(8) == HIGH && digitalRead(9) == HIGH && digitalRead(10)== HIGH) { 
    digitalWrite(13, HIGH); 
  }
  else { 
    digitalWrite(13, LOW);
  }

  
delay(10);
}
 
Last edited by a moderator:
Follow through what your code does when all three buttons are pushed.
Pin 8 is LOW so turn on 11
Pin 9 is LOW so turn on 12
pin 10 is LOW so turn on 14.
All three pins are LOW (all pushed) so the last if statement sets pin 13 LOW.
Change this to test for all three pins LOW and then pin 13 will be on.
BUT, you've turned on 11, 12 and 14 and nothing has turned off, so now you have all four LEDs on.
The last if statement will need to turn on pin 13 but also turn off the other three.
There's another little wrinkle to be handled here but try this first.

Pete
P.S. Your code would be more readable if you use the IDE to autoformat it for you (Tools|Autoformat).
And it would help others to read your post if you paste your code between code tags (the # icon generates them for you).
 
Pfoe, tricky one for me to wrap my brain around (i thought a good weekend with a nice break, could clear it up a bit :))

the thing i am looking at is the opposite respons of things i try to achieve,
If i can take Button1 and led1 as an example, (same as in the sketch above)

Code:
  if (digitalRead(8) == HIGH) {
    digitalWrite(11, LOW);
  }
  else {
    digitalWrite(11, HIGH);

so if the button is pressed it sees the HIGH, but i need to program digitalread to LOW, and the "else-statement" to HIGH,
in order for it to work as i wanted it to work, simply push the button and light up the led.

my brain (or logik) says this is not correct, and it should be more like this :

Code:
if (digitalRead(8) == HIGH) {
    digitalWrite(11, HIGH);
  }
  else {
    digitalWrite(11, LOW);

so a swap of the digitalwrites between the if & else statement.
(hoping when this clear up for me a bit, i could work on the bigger statements)

Thnx for the Time,
Danny
 
so if the button is pressed it sees the HIGH
No. The pinMode is INPUT_PULLUP. The PULLUP means that if the button (*) is not pushed, the pullup will keep the pin HIGH. When the button is pushed (and it is wired correctly) it connects the pin to ground and will now read LOW.
Your first snippet in your last message will turn off the led when the button is not pushed (HIGH). It will turn the led on when the button is pushed (LOW). Which is what you want.

Pete
(*) I'm assuming a normally-open button. A normally-closed button would work the other way round.
 
BTW: Unless you just want to practice "ifs" you can simply write

Code:
digitalWrite(11,!digitalRead(8));

instead. This sets pin 11 to the inverse of the input at pin 8
 
instead. This sets pin 11 to the inverse of the input at pin 8

Ah nice, and it lookes logical indeed (will give it a test)

but indeed i first stick with the basics and getting to understand the language first, and seeing the logick in it how to construct them,
Small things like a N.O button while jused to the respons of N.C buttons make me laugh a bit :) ,,,,,,,, and carry on.

What i was wondering was if it existed, was some sort of list with a overview that one could use for coding? like a sort of cheat sheet.
with for example : int, cont int, float, digitalwrite, digitalread, fallingedge, etc etc etc
there are so many options that i would like to print in out on paper, and have it beside me, as reminder, and how to spell it correctly. (some cappitals are overlooked, and than it would get the orange color, or blue)

Greets
 
What i was wondering was if it existed, was some sort of list with a overview that one could use for coding? like a sort of cheat sheet.
with for example : int, cont int, float, digitalwrite, digitalread, fallingedge, etc etc etc
there are so many options that i would like to print in out on paper, and have it beside me, as reminder, and how to spell it correctly. (some cappitals are overlooked, and than it would get the orange color, or blue)

Greets

Danny123:

If you are using the Arduino IDE, you should take a look at Help->Reference & see if that's a start to what you are looking for.

Good luck & have fun !!

Mark J Culross
KD5RXT
 
Status
Not open for further replies.
Back
Top