-1

How do I use 4 input lines as a binary decoder? I would like to read the 4 inputs (16 variations) and based on that choose one of 16 output behaviors?

I need to be able to set my “Ten Moon Phase Indicator” (MPI ECP) circuit project to the phase of the Moon as seen in the sky on this day, or to a specific time as indicated in one of the many apps available on the Internet. Currently the MPI ECP resets to the start of the Full Moon loop, as it is the only loop available. (this code works) NOTE: Due to the circuit & Arduino processor power consumption for this application, a eternal power will be necessary.

To resolve this problem my intent is to use four of the available 14 I\O on the Arduino Uno as INPUT's employing four debounced toggle switches (with pull up resistors): (0) Off, (1) as On. The other ten available I/O's to be used for the ten Moon display OUTPUT LED's or to control ten relays.

What I would like the sketch program to accomplish:

  1. digitalRead pins 0, 1, 12, 13
  2. Depending on 1 of 16 resulting four on/off combinations that are possible, run a specific loop.

This is what I came up with: (note, the code does not compile, I'm new)

 void setup()  {
 pinMode(2, OUTPUT);   // New     Waxing:  
 pinMode(3, OUTPUT);   // Young  Waxing:
 pinMode(4,OUTPUT);   // Crescent Waxing:
 pinMode(5, OUTPUT);   // Quarter Waxing: 
 pinMode(6, OUTPUT);   // Gibbous Waxing: 
 pinMode(7,OUTPUT);   // Full Moon: 
 pinMode(8, OUTPUT);   // Gibbous Wanning:
 pinMode(9, OUTPUT);   // Quarter Wanning:
 pinMode(10, OUTPUT);  // Crescent Wanning: 
 pinMode(11, OUTPUT);  // Old Wanning: 
 pinMode(13, INPUT);  // Switch 4
 pinMode(12,INPUT);     // Switch 3
 pinMode(1, INPUT);     // Switch 2
 pinMode(0,INPUT);      // Switch 1 

 } void loop() {
 int Reading = digitalRead;
Reading = digitalRead (12); Reading = digitalRead (13);
Reading = digitalRead (1);   Reading = digitalRead (0); 

 if  (digitalRead(0) == HIGH; digitalRead (1)==
   HIGH; digitalRead(12) == HIGH; (digitalRead(13) == HIGH);  { } else 
    {   // loop Full Moon Start   digitalWrite(7, HIGH);  // FULL MOON ON
  delay(5000); 
  digitalWrite(7, LOW);    // FULL MOON OFF
        delay(10);
   digitalWrite(8,HIGH);   // WAN GIBBOUS MOON ON
        delay(5000);  
 digitalWrite(8, LOW);    // WAN GIBBOUS MOON OFF
          delay(10); 
  digitalWrite(9, HIGH);   // 3RD QUARTER MOON ON
          delay(5000);
   digitalWrite(9, LOW);     // 3RD QUARTER MOON OFF
          delay(10); 
  digitalWrite(10, HIGH);    // WAN CRESENT MOON ON
         delay(5000); 
  digitalWrite(10, LOW);     // WAN CRESENT MOON OFF
          delay(10); 
  digitalWrite(11, HIGH);     // OLD MOON ON
         delay(5000);  
 digitalWrite(11, LOW);      // OLD MOON OFF
          delay(5000); 
   digitalWrite(2, HIGH);  // NEW MOON ON
          delay(5000); 
  digitalWrite(2, LOW);   // NEW MOON OFF
          delay(10);  
 digitalWrite(3, HIGH);  // YOUNG MOON ON
          delay(5000);
   digitalWrite(3, LOW);  // YOUNG MOON OFF
          delay(100); 
 digitalWrite(4, HIGH);   // WAX CRESCENT MOON ON
         delay(5000);
   digitalWrite(4, LOW);    // WAX CRESCENT MOON OFF
          delay(10); 
  digitalWrite(5, HIGH);   // FIRST QUARTER ON
          delay(5000);
   digitalWrite(5, LOW);     // FIRST CRESCENT OFF  
          delay(10);  
 digitalWrite(6, HIGH);   // WAX GIBBOUS ON
         delay(5000); 
  digitalWrite(6, LOW);    // WAX GIBBOUS OFF
          delay(10);

// loop for New moon Reset   if  (digitalRead(0) == LOW;digitalRead (1)==LOW; digitalRead(12) == LOW;(digitalRead(13) ==
    LOW);  
  { } else   // loop New Moon Start
 digitalWrite(2, HIGH);  // NEW MOON ON
          delay(5000);
   digitalWrite(2, LOW);   // NEW MOON OFF
          delay(10);
   digitalWrite(3, HIGH);  // YOUNG MOON ON
          delay(5000);
   digitalWrite(3, LOW);    // YOUNG MOON OFF
          delay(100);
   digitalWrite(4, HIGH);   // WAX CRESCENT MOON ON
         delay(5000);
   digitalWrite(4, LOW);    // WAX CRESCENT MOON OFF
          delay(10);
   digitalWrite(5, HIGH);   // FIRST QUARTER ON
          delay(5000); 
  digitalWrite(5, LOW);     // FIRST CRESCENT OFF  
          delay(10); 
  digitalWrite(6, HIGH);   // WAX GIBBOUS ON
         delay(5000);
   digitalWrite(6, LOW);    // WAX GIBBOUS OFF
          delay(10);
  digitalWrite(7, HIGH);  // FULL MOON ON
        delay(5000);
   digitalWrite(7, LOW);    // FULL MOON OFF
        delay(10); 
   digitalWrite(8,HIGH);   // WAN GIBBOUS MOON ON
        delay(5000);
   digitalWrite(8, LOW);    // WAN GIBBOUS MOON OFF
          delay(10);
   digitalWrite(9, HIGH);   // 3RD QUARTER MOON ON
          delay(5000);
   digitalWrite(9, LOW);     // 3RD QUARTER MOON OFF
          delay(10); 
  digitalWrite(10, HIGH);    // WAN CRESENT MOON ON
         delay(5000);
   digitalWrite(10, LOW);     // WAN CRESENT MOON OFF
          delay(10); 
  digitalWrite(11, HIGH);     // OLD MOON ON
         delay(5000);
   digitalWrite(11, LOW);      // OLD MOON OFF
          delay(5000); 
 { } else  { // Young moon Reset Start 
 if  (digitalRead(0) == LOW; digitalRead (1)==LOW; digitalRead(12) ==
    LOW;(digitalRead(13) == HIGH;
 )
   { } else  // loop New Moon Start
         digitalWrite(3, HIGH);
  // YOUNG MOON ON
          delay(5000);
   digitalWrite(3, LOW);    // YOUNG MOON OFF
          delay(100); 
  digitalWrite(4, HIGH);   // WAX CRESCENT MOON ON
         delay(5000); 
  digitalWrite(4, LOW);    // WAX CRESCENT MOON OFF
          delay(10);
   digitalWrite(5, HIGH);   // FIRST QUARTER ON
          delay(5000); 
  digitalWrite(5, LOW);     // FIRST CRESCENT OFF  
          delay(10); 
  digitalWrite(6, HIGH);   // WAX GIBBOUS ON
         delay(5000); 
  digitalWrite(6, LOW);    // WAX GIBBOUS OFF
          delay(10); 
 digitalWrite(7, HIGH);    // FULL MOON ON
        delay(5000);
   digitalWrite(7, LOW);    // FULL MOON OFF
        delay(10); 
   digitalWrite(8,HIGH);   // WAN GIBBOUS MOON ON
        delay(5000); 
  digitalWrite(8, LOW);    // WAN GIBBOUS MOON OFF
          delay(10); 
  digitalWrite(9, HIGH);   // 3RD QUARTER MOON ON
          delay(5000);  
 digitalWrite(9, LOW);     // 3RD QUARTER MOON OFF
          delay(10); 
  digitalWrite(10, HIGH);    // WAN CRESENT MOON ON
         delay(5000);
   digitalWrite(10, LOW);     // WAN CRESENT MOON OFF
          delay(10); 
  digitalWrite(11, HIGH);     // OLD MOON ON
         delay(5000); 
  digitalWrite(11, LOW);      // OLD MOON OFF
          delay(5000); 
         digitalWrite(2, HIGH);   // NEW MOON ON
          delay(5000);
   digitalWrite(2, LOW);       // NEW MOON OFF
          delay(10);
  } 
 // Waxing Crescent Reset Start if  (digitalRead(0) == LOW; digitalRead (1)== LOW; digitalRead(12) ==
    HIGH; digitalRead(13) == HIGH;)    { } else  // Waxing Crescent Moon
    reset Start
  digitalWrite(4, HIGH);   // WAX CRESCENT MOON ON
         delay(5000);
   digitalWrite(4, LOW);    // WAX CRESCENT MOON OFF
          delay(10); 
 digitalWrite(5, HIGH);   // FIRST QUARTER ON
          delay(5000);
   digitalWrite(5, LOW);     // FIRST CRESCENT OFF  
          delay(10);
   digitalWrite(6, HIGH);   // WAX GIBBOUS ON
         delay(5000); 
  digitalWrite(6, LOW);    // WAX GIBBOUS OFF
          delay(10);
  digitalWrite(7, HIGH);    // FULL MOON ON
        delay(5000);
   digitalWrite(7, LOW);    // FULL MOON OFF
        delay(10);  
  digitalWrite(8,HIGH);   // WAN GIBBOUS MOON ON
        delay(5000);
   digitalWrite(8, LOW);    // WAN GIBBOUS MOON OFF
          delay(10); 
  digitalWrite(9, HIGH);   // 3RD QUARTER MOON ON
          delay(5000);
   digitalWrite(9, LOW);     // 3RD QUARTER MOON OFF
          delay(10);  
 digitalWrite(10, HIGH);    // WAN CRESENT MOON ON
         delay(5000);
   digitalWrite(10, LOW);     // WAN CRESENT MOON OFF
          delay(10); 
  digitalWrite(11, HIGH);     // OLD MOON ON
         delay(5000);   digitalWrite(11, LOW);      // OLD MOON OFF
         delay(5000);
   digitalWrite(2, HIGH);   // NEW MOON ON
          delay(5000);
   digitalWrite(2, LOW);      // NEW MOON OFF
          delay(10);
   digitalWrite(3, HIGH);  // YOUNG MOON ON
          delay(5000);
   digitalWrite(3, LOW);       // YOUNG MOON OFF
          delay(100);
  } 
  // First Quarter Moon Reset if  (digitalRead(0) == LOW; digitalRead(1) == HIGH; digitalRead(12) ==
    HIGH; digitalRead(13) == HIGH;)   { else  {
  //FIRST QUARTER  Moon
    reset Start
digitalWrite(5, HIGH);   // FIRST QUARTER ON
          delay(5000);
   digitalWrite(5, LOW);     // FIRST CRESCENT OFF  
          delay(10);
   digitalWrite(6, HIGH);   // WAX GIBBOUS ON
         delay(5000); 
  digitalWrite(6, LOW);    // WAX GIBBOUS OFF
          delay(10);
  digitalWrite(7, HIGH);  // FULL MOON ON
        delay(5000);
   digitalWrite(7, LOW);    // FULL MOON OFF
        delay(10); 
   digitalWrite(8,HIGH);   // WAN GIBBOUS MOON ON
        delay(5000); 
  digitalWrite(8, LOW);    // WAN GIBBOUS MOON OFF
          delay(10); 
  digitalWrite(9, HIGH);   // 3RD QUARTER MOON ON
          delay(5000);
   digitalWrite(9, LOW);     // 3RD QUARTER MOON OFF
          delay(10); 
  digitalWrite(10, HIGH);    // WAN CRESENT MOON ON
         delay(5000);
   digitalWrite(10, LOW);     // WAN CRESENT MOON OFF
          delay(10); 
  digitalWrite(11, HIGH);     // OLD MOON ON
         delay(5000); 
  digitalWrite(11, LOW);      // OLD MOON OFF
          delay(5000); 
  digitalWrite(2, HIGH);   // NEW MOON ON
          delay(5000); 
  digitalWrite(2, LOW);    // NEW MOON OFF
          delay(10);
   digitalWrite(3, HIGH);  // YOUNG MOON ON
          delay(5000);
   digitalWrite(3, LOW);    // YOUNG MOON OFF
          delay(100);
   digitalWrite(4, HIGH);   // WAX CRESCENT MOON ON
         delay(5000); 
  digitalWrite(4, LOW);        // WAX CRESCENT MOON OFF
          delay(10); 
 } 
// code would continue with other loops. Thanks for looking at  my 16 RESET problem, email me. Any suggestions would be appreciated
5
  • 3
    edit your code to be readable!
    – esoterik
    Jul 23, 2018 at 19:07
  • 2
    If the code doesn't compile, the compiler shows some error messages. Include them in your question.
    – chrisl
    Jul 23, 2018 at 20:34
  • If the code does compile, does it do what you want it to? If not, what does it do?
    – Majenko
    Jul 23, 2018 at 22:01
  • 1
    you have a lot of unnecessarily repeating code ..... put these three lines in a function digitalWrite(X, HIGH); delay(5000); digitalWrite(X, LOW); .... then just call the function with a pin number as an argument
    – jsotola
    Jul 24, 2018 at 7:00
  • 1
    Edit your question, with a simple title (not a long winded explanation and commentary), put your question in the main body of text, and explain what exactly is wrong with your code. Take a look at other highly voted questions on the site to see how to write a good clear question. And indent your code correctly Jul 24, 2018 at 21:39

3 Answers 3

1

a skeleton solution:

int in[] = {0,1,12,13};
int out[] = {2,3,4,5,6,7,8,9,10,11};
...
void setup()
{
    for(int i=0; i < 4; i++)
        pinMode(in[i], INPUT)

    for(int i=0; i < 10; i++)
        pinMode(out[i], OUTPUT)
}
...
void do0 {...};
void do1 {...};
void do2 {...};
void do3 {...};
void do4 {...};
void do5 {...};
void do6 {...};
void do7 {...};
void do8 {...};
void do9 {...};
void do10 {...};
void do11 {...};
void do12 {...};
void do13 {...};
void do14 {...};
void do15 {...};
...
void loop()
{
    int addr = 0;
    for(int i=0; i<4; i++)
       addr |= digitalRead(in[i]) << i;

    switch(addr)
    {
    case 0:
       do0();
       break;
    case 1:
       do1();
       break;
    case 2:
       do2();
       break;
    case 3:
       do3();
       break;
    case 4:
       do4();
       break;
    case 5:
       do5();
       break;
    case 6:
       do6();
       break;
    case 7:
       do7();
       break;
    case 8:
       do8();
       break;
    case 9:
       do9();
       break;
    case 10:
       do10();
       break;
    case 11:
       do11();
       break;
    case 12:
       do12();
       break;
    case 13:
       do13();
       break;
    case 14:
       do14();
       break;
    case 15:
       do15();
       break;
    }
}
1

This may be a little too advanced for the OP but... reading esoterik’s answer, I couldn't help the urge to transform that big switch/case into an array lookup. Admittedly, the syntax for declaring the array of function pointers is a little bit esoteric. ;-)

void loop()
{
    // List of implemented behaviors.
    static void (* const behaviors[])() = {
        do0, do1, do2,  do3,  do4,  do5,  do6,  do7,
        do8, do9, do10, do11, do12, do13, do14, do15
    };

    // Build the index of the requested behavior.
    int addr = 0;
    for (int i = 0; i < 4; i++)
       addr |= digitalRead(in[i]) << i;

    // Now do it.
    behaviors[addr]();
}

Edit: To address the OP's issue. I may have missed something, but it looks to me like all the cases do the exact same thing: loop through the outputs, turn each one on, wait 5 seconds, then turn it off. The only difference between the cases is the index of the output that is turned on first. In this case, all this repetitive code can be factored into a single loop as follows:

// Turn on then off all the outputs in turn,
// starting from the given index.
void loop_outputs(int start_index)
{
    int index = start_index;
    for (int count = 0; count < 10; count++) {

        // Light up that LED for 5 s.
        digitalWrite(out[index], HIGH);
        delay(5000);
        digitalWrite(out[index], LOW);
        delay(10);

        // Go to the next one, wrapping index 10 -> 0.
        if (++index == 10) index = 0;
    }
}
1
  • I thought about function pointers, but figured they were to complicated for this.
    – esoterik
    Jul 25, 2018 at 22:31
0

Note that A0 to A5 are available as for digitalRead() also. I normally initialize them as

byte pin14 = 14; // make the name mean something to you

pinMode (pin14, INPUT_PULLUP);

Then you can read it as

if (digitalRead(pin14) == LOW){

// pin is connected to Gnd

}

Or if you use D14-17 for your 4 inputs, you can read all 4 with a PORT read, and act on that (you still use pinMode() to initialize them):

byte addressByte = PORTC & 0x0F; // result is 0 to 15 based on state of the 4 pins

(I think PORTC is A0 to A5, might want to check that)

then read them and act accordingly. Way cleaner than a while bunch of digitalRead()s and if() statements.

switch(addressByte){

case 0:

// whatever happens

:

:

case 15:

// whatever happens
}
1
  • 1
    I think PORTC is A0 to A5”. The OP is using an Uno, and this is indeed correct on that board. Jul 25, 2018 at 18:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.