1

This is my first manually typed code. I'm sure there may be some glaring "schoolboy errors" in it, so wondered if people could give it the 'once over' In particular, I have never used C++ arrays, storing binary data within, and then trying to read them bit by bit. In perl, much easier; you would simply use a multi-dimensional something like this:

@code1=('10101010', '01010101');
$bit=$code1[0][0] (..through to..) code1[0][7]
$bit=$code1[1][0] ... code1[1][7]

So this is my Arduino code. Welcome to suggestions.

int pinArray[]={2,3,4,5,6,7,8,9};
int code1[]={B10101010, B01010101,B10101010, B01010101,B10101010, B01010101};
int count=0;
int split=0;    // Counter for the bit loop
int led;    // Store bit data
byte x;     // Used to store the binary data in code1

void setup(){
    for (count=0; count<8; count++){
    pinMode(pinArray[count],OUTPUT); // Sets pinMode as output on pinArray pins
    ]
}

void loop(){
    for (count=0; count<6; count++){
    x=code1[count];
    ledDisplay(x);
    }
}

void ledDisplay(){
    for (split=7; split>=0; split--){
    led=bitRead(x,split);
        if (led == 1){
        digitalWrite(split+2,HIGH);
        }
        else{
        digitalWrite(split+2,LOW);
        }
    }
}

Have just modified split as realised bit7 is the leftmost digit. In split, I am adding 2 to the value. Does this need to be enclosed in brackets,ie (split+2) ?

Cristofayre
  • 11
  • 1
  • 3

1 Answers1

2

There are only two errors I can spot in your program:

  1. A block of code should be delimited by curly braces:

    for (count=0; count<8; count++){
        pinMode(pinArray[count],OUTPUT);
    }  // ← ‘}’ instead of ‘]’
    
  2. If a function expects to be passed arguments, these should be declared as parameters in the function prototype:

    void ledDisplay(byte x){  // ← mind the “byte x”
        // ...
    }
    

Other than that, here a few suggestions to make the code look nicer, and hopefully more maintainable:

  • indent consistently
  • use const to qualify constants
  • use named constants instead of magic numbers
  • keep variables local if possible
  • note that an int that is either 0 or 1 is equivalent to a boolean
  • use pinArray[] for accessing the pins

Applying those suggestions, I get the following:

const int PIN_COUNT = 8;
const int pinArray[PIN_COUNT] = { 2, 3, 4, 5, 6, 7, 8, 9 };
const int CODE_COUNT = 6;
const int code1[CODE_COUNT] = {
    B10101010, B01010101, B10101010, B01010101, B10101010, B01010101
};

void setup() {
    for (int count = 0; count < PIN_COUNT; count++) {
        pinMode(pinArray[count], OUTPUT);
    }
}

void loop() {
    for (int count = 0; count < CODE_COUNT; count++) {
        ledDisplay(code1[count]);
    }
}

void ledDisplay(byte x) {
    for (int split = 7; split >= 0; split--) {
        if (bitRead(x, split)) {
            digitalWrite(pinArray[split], HIGH);
        } else {
            digitalWrite(pinArray[split], LOW);
        }
    }
}
Edgar Bonet
  • 45,094
  • 4
  • 42
  • 81