1 2 3 Previous Next 38 Replies Latest reply on Dec 29, 2012 6:27 PM by Columbo Go to original post
      • 15. Re: Error in sketch code


          A table is just a fancy name for array, although in

        some languages it might be a fancy type of array such

        as an associative array.

          If you have 10 digits, each involving 4 led settings,

        you might have a struct that contains 4 boolean values for

        those leds, and then an array of 10 of these structs.

        To find the led settings for digit n, you would use n to index

        the array to lookup the struct with the 4 led settings for n.

        This would take the place of the switch statement.


        Here are two tutorials:




        The reason a table lookup is preferable to a switch statement

        is that it makes your code simpler, so there are fewer things

        that can go wrong.  For example, you can't forget a break statement.

        It also makes it easier to check that all the values in the table are correct

        since they are in one place rather than scattered in the switch statement.


        • 16. Re: Error in sketch code

          You are welcome.  You seem to be adapting and advancing quite quickly.  Feel free to read my blog  "BP 0x0004".

          • 17. Re: Error in sketch code

            Thanks Coder27,


            When you mentioned a 'lookup table' the first thing that came to my mind was a 'lookup table' such as in dBase which is held in a database but of course that wouldn't be possible in Arduino so it got me wondering what it was.


            I will take a look at those tutorials that you rcommended and see what I can come up with.  Keeping the code as short as possible is always a good thing. 


            Thanks again, and have a very Merry Christmas and a Great New Year.

            • 18. Re: Error in sketch code

              Thanks billabott,


              I find that choosing a project and actually trying to get it to work is a lot easier for me to learn rather than just reading a book.  Books and tutorials are very helpful of course, but hands-on gets your mind working and understanding it better.  I'm going to make a number of errors along the way but that's part of the learning process,... as long as you learn from your mistakes.    Certainly having some experienced people here on Element14 who are so willing to take the time and patience to help others is priceless.


              Merry Christmas to you and yours from my house.

              • 19. Re: Error in sketch code

                Hi Coder27,


                I am reading up a bit on those arrays and I have a question for you if you don't mind?


                Is it possible to use an array element in digitalWrite() like in the following example?


                digitalWrite(myArray[2], HIGH);  or   digitalWrite(myArray[2], myArray[3]);


                Or,... if a multi-dimensional array was used such as myArray[8][8] with, say, myArray[2] holding the pin number and the second dimension of the array [3] holding the HIGH or LOW, how would the syntax for that work?  I would think that the syntax would be incorrect to write it as

                digitalWrite(myArray[2], [3]);


                Don't know if my question is clear or not but,.... you might get the picture of what I am trying to do here.  I am looking at having separate arrays for seconds, minutes and hours.






                • 20. Re: Error in sketch code


                    Yes, you can use an array element in digitalWrite, either as the first

                  or second parameter (or both), essentially anywhere you might otherwise

                  use a simple number or variable or algebraic expression.

                    As you seem to understand, you can either have more than one

                  single-dimensional array, sometimes called parallel arrays, or you

                  can have an array of structs, or an array of arrays.


                  > digitalWrite(myArray[2], [3]);


                  The "[3]" isn't right.  Each parameter to digitalWrite is treated separately,

                  so the "[3]" needs to have the name of the array variable in front of it,

                  like you have for myArray[2], except it would probably have a different array name,

                  because the second parameter of digitalWrite is the LOW/HIGH setting,

                  rather than the pin number.  Read up on arrays so you are clear how it works.

                  • 21. Re: Error in sketch code

                    I think I am getting the idea of how to set up an array, or even a multi-dimensional array but I am struggling a bit with how to use it in the BCD clock.  If it was a matter of simply turning on or off one individual LED at a time I don't think I would have a problem but for displaying the seconds portion of the time, I am using 7 LEDs which will sometimes have  only one LED turned on while at other times it will have two and sometimes three LEDs turned on at the same time.


                    It works fine using the switch statement but I can't figure out how to do the same thing using an array.


                    The following is a portion of the code that I used in the switch statement showing the first 9 of the 60 seconds needed.


                    void loop(){




                       case 1:

                          digitalWrite(led1, HIGH);  //led1 ON



                       case 2:

                          digitalWrite(led2, HIGH);  //led2 ON

                          digitalWrite(led1, LOW);  



                       case 3:

                         digitalWrite(led1, HIGH);  //led1 and led2 ON



                       case 4:

                         digitalWrite(led3, HIGH);  //led3 ON

                         digitalWrite(led2, LOW);

                         digitalWrite(led1, LOW);



                       case 5:

                         digitalWrite(led1, HIGH);  //led3 and led1 ON



                       case 6:

                         digitalWrite(led2, HIGH);  //led3 and led2 ON

                         digitalWrite(led1, LOW);



                       case 7:

                         digitalWrite(led1, HIGH);  //led3, led2 and led1 ON



                       case 8:

                         digitalWrite(led4, HIGH);  //led4 ON

                         digitalWrite(led3, LOW);

                         digitalWrite(led2, LOW);

                         digitalWrite(led1, LOW);



                       case 9:

                         digitalWrite(led1, HIGH);  //led4 and led1 ON





                    As you can see, with each increment of the seconds different pins are set HIGH and others are set LOW.  I can't seem to wrap my head around how to come up with a matrix that will do the same thing.  Any thoughts?





                    • 22. Re: Error in sketch code


                      If you set up a single array (0-9) with the decimal number that matches the binary value of the leds you need to turn on. (ie 0 = none, while 127= all 7 leds)


                      At each second you could read the array element matching the second, and then turn on the leds.


                      You can use the port manipulation ( http://www.arduino.cc/en/Reference/PortManipulation ) to set multiple pins at once.

                      Decimal numbers also work, so direct reference to your array should work.



                      Does this make sense.??




                      • 23. Re: Error in sketch code

                        Hi Mark,


                        As you can see, coding in 'C' and the Arduino are both new to me and I am in a learning process.  Coder27 suggested using arrays rather than the switch statement and that makes sense since the code for a 24 hour clock could get quite large and more difficult to read with the switch statement.  I looked at a couple of short tutorials on arrays and I think I get the general idea.


                        Your suggestion certaiinly makes sense and is something that I would never have thought of. I will follow your link and read up on the port manipulation to see if I can get an understanding of how that works as well.


                        Thank you very much for your help.  It is very much appreciated.


                        Have a very Merry Christmas and all the best in the 2013.



                        • 24. Re: Error in sketch code

                          Hi Mark,


                          I have been looking at some info on the manipulation of the ports on the Arduino.  I don't have it all down pat yet but I am getting a better understanding of how it works. One thing that I am unsure of is whether I can use a variable in the port designation.  Since I will likely be using pins 1 to 13.


                          Pins 1 to 7 is on PORTD and pins 8 to 13 is on PORTB,  so could I use a variable such as PORTx or PORTy?


                          For example:


                          char x='B'

                          char y='D'


                          for( int i=1; i< 13; i++ ) {

                              if( i < 8) {

                                 PORTx = pinArray[3];   // PORTD     



                          else {

                               PORTy = pinArray[9];    // PORTB




                          I am practicing with the UNO but once I get to the point where I can get things working properly I will start using the Mega because it has more pins and of course more memory.





                          • 25. Re: Error in sketch code

                            Ok, I tried to use a variable for the PORTD and PORTB and that won't work but I figured out another way to achieve what I needed.


                            I re-wrote the code, eliminating the switch statement, and used  PORTD and PORTB which is controlled by the secsArray[], the minsArray[], and the hrsArray[].  I ran it using the Arduino Simulator and it is working fine so far.  I only have it coded to where it counts the seconds and minutes so I have to work on the hours now. If I can get that working properly in the simulator, I will upload it to the UNO and see how it runs there.  Using the arrays and the PORTS sure shortens up the code a lot.


                            It has been quite a learning experience but I'm getting a handle on this 'Arduino C' coding now.


                            Thanks to Coder27, billabott, and Mark Beckett.  You guys are great!



                            • 26. Re: Error in sketch code


                              I was in the same boat as you just 12 months ago. No experience with c/c++, and its been a steep but enjoyable experience.


                              Can I suggest you try picking up The Arduino Cookbook (by Michael Margolis) 2nd edition. It is well layed out and full of useful advise and solutions.


                              You can manipulate some parts of the port using suggestions here ( http://www.arduino.cc/en/Reference/PortManipulation )

                              It has a link to The Bitmath Tutorial and note that you can read the port as well.



                              Re memory, the 328 has 30k of code space but only 2048 bytes of ram. Libraries can consume a lot, and you can help yourself by looking inside the library to see what variables they use, and pass the data using the same variable.

                              My last project I had to remove all the library parts I didn't need, as I ran out of ram.....


                              You should also reduce the variable to the maximum size you need ie if you only have a number 0-255, then a byte variable saves ram.

                              Also if you have a lot of serial.print("this is text blah  ")  statements you can save ram by off loading them using this :- (Can't recall where I found it ...sorry to the original author)


                              #include <avr/pgmspace.h>  // allows storing strings in Flash memory


                              void showString (PGM_P s)


                                 char c;

                                  while ((c = pgm_read_byte(s++)) != 0)




                              then in your code...

                              showString(PSTR("print this text\n"));    // the \n is a CR/LF


                              You might not need the Mega after all.




                              • 27. Re: Error in sketch code

                                Thanks Mark,


                                Interesting concept.  I think I will probably use the Mega anyway.  For my BCD clock project I need to control 20 LEDs and the Mega has 54 pins that can be used. I looked at maybe multi-plexing the UNO but to be honest, I haven't been able to figure out how to do it with 20 LEDs where 2 or more may be required to be turned on or off at once. I'm not really all that knowledgable about multi-plexing with the Arduino.


                                I have to locate some info on the Mega's ports though.  On the UNO,  PORTD controls pins 1 - 7 and PORTB controls pins 8 - 13 however, I have not been able to find any info on which ports are on the Mega and which pins that they control. 


                                Given that I only started learning the Arduino and 'Arduino C' two weeks ago, I feel that I have made reasonable progress but I still have a long way to go. The help that is available from users here in this forum certainly makes the learning process a lot easier.


                                I just happened to stumbled upon that Arduino Simulator and I found it to be quite helpful for testing my code. 



                                • 28. Re: Error in sketch code

                                  I'll bet if you try to translate this PIC C code into 'Duino C you will learn a lot; you can even do it in the Simulator.  (Oh, oh, the source code I wrote for the hpLED chaser display in the video was over written. Sorry)  This one works in a similar fashion:


                                  This even points out an excellant method of handling the array implementation for super efficiency.

                                  This is the second version which is rewritten to take advantage of

                                    Arrays to reduce the amount of space required by the application.


                                  myke predko





                                  __CONFIG(INTIO & WDTDIS & PWRTEN & MCLRDIS & UNPROTECT \

                                    & UNPROTECT & BORDIS & IESODIS & FCMDIS);



                                  int i, j, n;

                                  int Value = 0;

                                  int Dlay = 65;                  //  LED Time on Delay Variable

                                  const char PORTAValue[8] = {0b010000, 0b100000, 0b010000, 0b000100,

                                                              0b100000, 0b000100, 0b000100, 0b000010};

                                  const char TRISAValue[8] = {0b001111, 0b001111, 0b101011, 0b101011,

                                                              0b011011, 0b011011, 0b111001, 0b111001};

                                              // only two of the six bits will be 0 for output at a time

                                  const char NOTPORTA[8] = {0, 0, 0, 0, 0, 0, 0, 0};





                                      PORTA = 0;


                                      CMCON0 = 7;                //  Turn off Comparators

                                      ANSEL = 0;                  //  Turn off ADC

                                                                         // details that the 'Duino IDE auto-magically handles for us


                                      j = 0;                      //  Reset the Display Counter


                                      while(1 == 1)               //  Loop Forever


                                          for (i = 0; i < 8; i++ )

                                          {                       //  Loop through Each of the 8 LEDS

                                              for (n = 0; n < Dlay; n++);

                                              if ((Value & (1 << i)) == 0)

                                                  PORTA = NOTPORTA[i];


                                                  PORTA = PORTAValue[i];

                                              TRISA = TRISAValue[i];

                                          }  //  rof


                                          j = j + 1;              //  Increment the Counter every 1/2s

                                          if (j >= 50)


                                              Value = Value + 1;  //  Increment Display Counter

                                              j = 0;              //  Reset the Counter

                                          }  //  fi

                                      }  //  elihw

                                  }  //  End cLEDDisp

                                  Source: Myke Predko's "123 PIC Microcontroller Experiments for the Evil Genius"



                                  Example: If you were to try this using only 3 digital i/o pins, each pin has a current limiting Resistor whose value is R/2.  A good number for R is 10K, therefore R/2 = 5K.  Attached to the R/2s there are hetero-paired LEDs (1 anode and 1 cathode on each node) for a total of 6 LEDs.  The first hpLED is connected to R1-R2, the second hpLED is connected to R1-R3, and the third hpLED is connected to R2-R3.  Initialize by setting all 3 pins to INPUT (high impedance /.: not enough current flows to light up any LED).  We can select any two pins and make them OUTPUTs hence when one pin  is sourcing current (0 or LOW) and the other pin is sinking current (1 or HIGH) the direct conversion of electrical energy to a photonic emission will occur (... that means the LED will light UP).   Question: If P1 is High and P2 is Low; which P(out) should we toggle to make the LED blink and at what speed? Answer: Either one (it doesn't matter) and slower that 48 Hz.



                                  In this video you can see a PICKIT1 Flash Starter Kit running the code I wrote for hpLED_chaser.   If the wait loop time(s) were very much shorter, all the LEDs would appear to be ON simultaneously; and that, my friends, is Persistance Of Vision at work.  Check out some of these POV videos on YouTube.


                                  It is great that you are trying to get a better understanding of how to apply these concepts.  For more details please see:  http://en.wikipedia.org/wiki/Charlieplexing

                                  Warning: Hetero pairing leads to photonic emissions.

                                  • 29. Re: Error in sketch code

                                    Thanks billabott,


                                    I will look at the code and the videos as well.