Results 1 to 6 of 6
  1. #1
    75+ Posting Member
    Join Date
    Nov 2011
    Location
    UK
    Posts
    77
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Multiple digitalWrites

    Hi guys,

    I'm trying to figure out how to include multiple outputs in a digitalWrite statement. As you can see below Ive got one led to turn off when the battery master is off but how do I include this across multiple led's? I have had a play but cant figure it out.
    Many thanks
    Marc

    Code:
    void QUESTION(){    // The first identifier was "?"CodeIn = getChar(); // Get the second identifier
      switch(CodeIn) {// Now lets find what to do with it
        case 'K'://Found the second identifier
       Bvm = "";
            Bvm += getChar();
            Bvmi = Bvm.toInt();
            if (Bvmi == 0)
            {digitalWrite(5, LOW);}else{digitalWrite(5, HIGH);}

  2. #2
    150+ Forum Groupie
    Join Date
    Apr 2011
    Location
    United States of America
    Posts
    232
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: Multiple digitalWrites

    From the arduino.cc post, PaulS is correct - if multiple outputs are used, break up the one line "if" statement.
    Code:
    case 'K'://Found the second identifier
       Bvm = "";
            Bvm += getChar();
            Bvmi = Bvm.toInt();
            if (Bvmi == 0){
                digitalWrite(5, LOW);
                digitalWrite(LED2, LOW);
                digitalWrite(LED3, LOW);        
             }else{           
                digitalWrite(5, HIGH);           
                digitalWrite(LED2, HIGH);
                digitalWrite(LED3, HIGH);
            }

  3. #3
    150+ Forum Groupie
    Join Date
    Nov 2013
    Location
    Evansville, Indiana
    Posts
    243
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: Multiple digitalWrites

    Code:
    if (Bvmi == 0)
    {
    	digitalWrite(5, LOW);
    	digitalWrite(6, LOW);
    	digitalWrite(7, LOW);
    }
    else
    {
    	digitalWrite(5, HIGH);
    	digitalWrite(6, HIGH);
    	digitalWrite(7, HIGH);
    }
    ~Fess
    I will only ever be, half the Geek that I wished I was.
    TheGeekForge.Com

  4. #4
    150+ Forum Groupie
    Join Date
    Nov 2013
    Location
    Evansville, Indiana
    Posts
    243
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: Multiple digitalWrites

    JMeller beat me by 1 minute in reply.
    I will only ever be, half the Geek that I wished I was.
    TheGeekForge.Com

  5. #5
    75+ Posting Member
    Join Date
    Nov 2011
    Location
    UK
    Posts
    77
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: Multiple digitalWrites

    Hi guys, thanks for the reply.
    Something I have noticed a few times on the arduino forum is for people like me whose only exposure to coding arduino is link2fs and the examples given, some comments can be quite unhelpful and discouraging.

    What I was trying to accomplish was for my annunciator indications. I was using Jim's array script, I really struggle to see what is going on so for my own learning I decided to write a simpler that I could understand and develop. It's long and I am sure there are far more effective ways to do it. Here it is.

    https://gist.github.com/marc21121980...3d4c21334f3f90

    As you can see I have gone for the individual void SLASH case approach. With another section under void LESSTHAN to switch off the indications when the battery master is off. The only other thing I would like to include is a push to test button, however what I tried in the above lit all the indications up but then they all switched off rather than just the ones not being annunciated....if that makes sense.

    Thanks again
    Marc

  6. #6
    150+ Forum Groupie
    Join Date
    Apr 2011
    Location
    United States of America
    Posts
    232
    Contribute If you enjoy reading the
    content here, click the below
    image to support MyCockpit site.
    Click Here To Contribute To Our Site

    Re: Multiple digitalWrites

    lol- Fess.

    Yeah Marc, there are some unsavory characters in the arduino forum which are nothing but intelligent bullies behind a keyboard.

    Now, to your issue:
    If I understand your problem correctly, your annunciators are working properly until you press the test button. This would be caused by a lack of recall of the current state.
    So, to correct this I would implement an array to store the current state; this will also make code easy to create/read if your pins are in sequential order.

    Example:
    Code:
    void PUSHTOTEST(){
    
      int ARRAY_myrecall[3]; \\create 3 slots 
    
      for(int i=5; i<7; i++){
          ARRAY_myrecall[i-5] = digitalRead(i);  //since i started at 5, subtract 5 (5-5=0) and begin storing in slot 0 of the array.
      }
      
    
      buttonState = digitalRead(pttButton);   
      if(buttonState==LOW){
         for(int i=5; i<7; i++){
          digitalWrite(i, HIGH); //light pins 5 thru 7
         }
      return; // prevent blinking of annunc by not allowing the code below to run.
      }
    
    
    
      for(int i=5; i<7; i++){
          digitalWrite(i, ARRAY_myrecall[i-5]);  // recall led state of pins 5 thru 7 prior to button push
      }
    
    }