Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 9 of 9

Thread: if statements (newbie questions)

  1. #1
    Junior Member
    Join Date
    Jun 2020
    Location
    The Netherlands
    Posts
    5

    if statements (newbie questions)

    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 defragster; 06-05-2020 at 05:02 PM. Reason: added # / CODE marking to show indentation

  2. #2
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,376
    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).

  3. #3
    Junior Member
    Join Date
    Jun 2020
    Location
    The Netherlands
    Posts
    5
    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

  4. #4
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,376
    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.

  5. #5
    Junior Member
    Join Date
    Jun 2020
    Location
    The Netherlands
    Posts
    5
    ,,,,, Ha.,.,., oke yes.
    Thanks for this, how i totaly overlooked this.

    It makes sense now.

    Merci

  6. #6
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    990
    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

  7. #7
    Junior Member
    Join Date
    Jun 2020
    Location
    The Netherlands
    Posts
    5
    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

  8. #8
    Senior Member
    Join Date
    Apr 2020
    Location
    DFW area in Texas
    Posts
    115
    Quote Originally Posted by Danny123 View Post
    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

  9. #9
    Junior Member
    Join Date
    Jun 2020
    Location
    The Netherlands
    Posts
    5
    Briljant, found straight what i was looking for :-)

    Thank you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •