Problems with Arduino specific C++

Discussion in 'Embedded Systems and Microcontrollers' started by TheButtonThief, Sep 22, 2015.

  1. TheButtonThief

    Thread Starter Active Member

    Feb 26, 2011
    219
    38
    Hi.

    Currently writing the code for a 7-seg LED clock using 4 digits, an Arduino Uno and a HEF4511. I'm also currently pulling my hair out as I know the following code should work but it doesn't, the code multiplexes the LED display. I get an error "D was not declared in the scope" and "dig was not declared in the scope" with regards to digOn().

    There's more code than what I've shown below but I don't think any of it is relative. If anyone would like to see the entire sketch, I'll gladly post it.

    Thanks for taking a look

    Jay


    Code (Text):
    1. int D3 = 11; // Pin 11 to D3 of IC4511
    2. int D2 = 10; // Pin 10 to D2 of IC4511
    3. int D1 = 9;  // Pin 9 to D1 of IC4511
    4. int D0 = 8;  // Pin 8 to D0 of IC4511
    5.  
    6. int dig0 = 4; // Pin 4 to digit 0 transistor (right right)
    7. int dig1 = 5; // Pin 5 to digit 1 transistor (midle right)
    8. int dig2 = 6; // Pin 6 to digit 2 transistor (midle left)
    9. int dig3 = 7; // Pin 7 to digit 3 transistor (left left)
    10.  
    11. int number = 0; // Decimal number for output digit
    12. int digOn; // Which digit to hold high
    13.  
    14. int binMatrix[][4] = { // Binary output to IC4511
    15. {0,0,0,0},
    16. {0,0,0,1},
    17. {0,0,1,0},
    18. {0,0,1,1},
    19. {0,1,0,0},
    20. {0,1,0,1},
    21. {0,1,1,0},
    22. {0,1,1,1},
    23. {1,0,0,0},
    24. {1,0,0,1}
    25. };
    26.  
    27. int digMatrix[][4] = { // digit to be held high for muxing
    28. {0,0,0,1}, // digit 0
    29. {0,0,1,0}, // digit 1
    30. {0,1,0,0}, // digit 2
    31. {1,0,0,0}, // digit 3
    32. };
    33.  
    34. void digitOn() { // write binary to multiplexer and mux digit
    35.  
    36. for (int I=0; i<=3; i++) {
    37.   digitalWrite (D[i], binMatrix[number][i]);  // Output binary number via D0, D1, D2, D3 using binMatrix
    38.   digitalWrite (dig[i], digMatrix[digOn][i]);  // Switch current digit high and the rest low using digMatrix
    39. }//end for
    40. }
    41.  
    42. void setup() {
    43.  
    44. pinMode (D0, OUTPUT);
    45. pinMode (D1, OUTPUT);
    46. pinMode (D2, OUTPUT);
    47. pinMode (D3, OUTPUT);
    48. pinMode (dig0, OUTPUT);
    49. pinMode (dig1, OUTPUT);
    50. pinMode (dig2, OUTPUT);
    51. pinMode (dig3, OUTPUT);
    52. }
    53.  
    54. voild loop() {
    55.  
    56. //lots of stuff here that works
    57.  
    58. }
     
  2. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,376
    494
    Have you tried declaring D<i> and dig<i>?
     
  3. TheButtonThief

    Thread Starter Active Member

    Feb 26, 2011
    219
    38
    so...
    int D3 = 11; // Pin 11 to D3 of IC4511
    int D2 = 10; // Pin 10 to D2 of IC4511
    int D1 = 9; // Pin 9 to D1 of IC4511
    int D0 = 8; // Pin 8 to D0 of IC4511
    int D<I>;


    ...?
     
  4. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    2,787
    825
    Check out the Arduino Regerence page for arrays. Your syntax is wrong.
     
  5. TheButtonThief

    Thread Starter Active Member

    Feb 26, 2011
    219
    38
    I think I know where I've gone wrong, it needs to look something like:

    int D[] = {8, 9, 10, 11};
    int dig[] = {4, 5, 6, 7};

    Then...

    void setup() {
    for (int i=0; i<=3; i++) {
    pinMode (D, OUTPUT);
    pinMode (dig, OUTPUT);
    }

    Then...

    void digitOn() { // write binary to multiplexer and mux digit

    for (int i=0; i<=3; i++) {
    digitalWrite (D, binMatrix[number]); // Output binary number via D0, D1, D2, D3 using binMatrix
    digitalWrite (dig, digMatrix[digOn]); // Switch current digit high and the rest low using digMatrix
    }//end for

    }

    I'll test it when I get home =)

    Cheers
     
  6. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    2,787
    825
    Your references to D and dig in your for loop need to be investigated. PinMode is expecting an int as the first parameter; you are passing a pointer (to the arrays). You have to qualify the parameters in pinMode to refer to only one element. You have a similar problem in digitOn and the digitalWrite call.

    Your initialization looks good...
     
  7. Papabravo

    Expert

    Feb 24, 2006
    10,135
    1,786
    It's not C++
     
  8. TheButtonThief

    Thread Starter Active Member

    Feb 26, 2011
    219
    38
    Ahh! I meant to do this...

    Code (Text):
    1. int D[] = {8, 9, 10, 11};
    2. int dig[] = {4, 5, 6, 7};
    Then...


    Code (Text):
    1. void setup() {
    2.  
    3. for (int i=0; i<=3; i++) {
    4.   pinMode (D[i]OUTPUT);
    5.   pinMode (dig[i], OUTPUT);
    6. }

    Then...


    Code (Text):
    1. void digitOn() { // write binary to multiplexer and mux digit
    2.   for (int i=0; i<=3; i++) {
    3.     digitalWrite (D[i], binMatrix[number]); // Output binary umber via D0, D1, D2, D3 using binMatrix
    4.     digitalWrite (dig[i], digMatrix[digOn]); // Switch current digit high and the rest low using digMatrix
    5.   }//end for
    6. }


    I've seen other programs written in a similar way, for example I've seen the following many times before...

    Code (Text):
    1. void setup() {
    2.   for (int i=0; i<=5; i++) {
    3.     pinMode (LEDpin[i], OUTPUT);  // set LED pins for Output
    4.   }
    5. }

    However, the int's had been declared like this...

    Code (Text):
    1. int LEDpin1 = 13;
    2. int LEDpin2 = 12;
    3. int LEDpin3 = 11;
    4. int LEDpin4 = 10;
    5. int LEDpin5 = 9;

    and not like...

    Code (Text):
    1. int LEDpin[6] = {13, 12, 11, 10, 9};

    I'll see if I can get it to work by using an array to declare the int's


    The whole project works if I use:

    Code (Text):
    1. void digitOn() {  // write binary to BCD multiplexer and mux digit
    2.   digitalWrite (D0, binMatrix[number][0]);
    3.   digitalWrite (D1, binMatrix[number][1]);
    4.   digitalWrite (D2, binMatrix[number][2]);
    5.   digitalWrite (D3, binMatrix[number][3]);
    6.  
    7.   digitalWrite (dig0, digMatrix[digOn][0]);
    8.   digitalWrite (dig1, digMatrix[digOn][1]);
    9.   digitalWrite (dig2, digMatrix[digOn][2]);
    10.   digitalWrite (dig3, digMatrix[digOn][3]);
    11. }
     
    Last edited: Sep 22, 2015
  9. TheButtonThief

    Thread Starter Active Member

    Feb 26, 2011
    219
    38
    right-o, got it working as above =)

    ...run into new problems though, may start a new thread about it.
     
Loading...