Byte array

Hi there.
I am currently working on making a 8x8 led table.
I found this code and i have some questions.

// Project 16
#include <TimerOne.h>
//Pin connected to Pin 12 of 74HC595 (Latch)
int latchPin = 8;
//Pin connected to Pin 11 of 74HC595 (Clock)
int clockPin = 12;
//Pin connected to Pin 14 of 74HC595 (Data)
int dataPin = 11;
uint8_t led[8];
long counter1 = 0;
long counter2 = 0;
void setup() {
//set pins to output
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
led[0] = B11111111;
led[1] = B10000001;
led[2] = B10111101;
led[3] = B10100101;
led[4] = B10100101;
led[5] = B10111101;
led[6] = B10000001;
led[7] = B11111111;
Timer1.initialize(10000);
Timer1.attachInterrupt(screenUpdate);
}
void loop() {
counter1++;
if (counter1 >=100000) {counter2++;}
if (counter2 >= 10000) {
counter1 = 0;
counter2 = 0;
for (int i=0; i<8; i++) {
led[i]= ~led[i];
}
}
}
void screenUpdate() {
uint8_t row = B00000001;
for (byte k = 0; k < 9; k++) {
// Open up the latch ready to receive data
! digitalWrite(latchPin, LOW);
shiftIt(~row );
shiftIt(led[k] ); // LED array
// Close the latch, sending the data in the registers out to the
matrix
digitalWrite(latchPin, HIGH); row = row << 1;
}
}
void shiftIt(byte dataOut) {
// Shift out 8 bits LSB first,
// on rising edge of clock
boolean pinState;
//clear shift register read for sending data
digitalWrite(dataPin, LOW);
// for each bit in dataOut send out a bit
for (int i=0; i<8; i++) {
//set clockPin to LOW prior to sending bit
digitalWrite(clockPin, LOW);
// if the value of DataOut and (logical AND) a bitmask
// are true, set pinState to 1 (HIGH)
if ( dataOut & (1<<i) ) {
pinState = HIGH;
}
else {!
pinState = LOW;
}
//sets dataPin to HIGH or LOW depending on pinState
digitalWrite(dataPin, pinState);
//send bit out on rising edge of clock
digitalWrite(clockPin, HIGH);
digitalWrite(dataPin, LOW);
}
//stop shifting
digitalWrite(clockPin, LOW);
}

Can i replace

led[0] = B11111111;
led[1] = B10000001;
led[2] = B10111101;
led[3] = B10100101;
led[4] = B10100101;
led[5] = B10111101;
led[6] = B10000001;
led[7] = B11111111;

with

led = {
B11111111;
B10000001;
B10111101;
B10100101;
B10100101;
B10111101;
B10000001;
B11111111};

?
And could i make something like lets say
pattern1 = {etc}
pattern2 = {etc}
pattern3 = {etc}
and in the void loop just
led = pattern1 then led = pattern2 to just change what i’m displaying?

Put that code through the auto format tool first, the complete lack of indenting is an eye sore.

and in the void loop just

There is no such thing as a "void loop". There is a loop function that has a void return type.

Can i replace

led[0] = B11111111;
led[1] = B10000001;
led[2] = B10111101;
led[3] = B10100101;
led[4] = B10100101;
led[5] = B10111101;
led[6] = B10000001;
led[7] = B11111111;
with

led = {
B11111111;
B10000001;
B10111101;
B10100101;
B10100101;
B10111101;
B10000001;
B11111111};

No
In the first case the the led array has been declared as a global variableuint8_t led[8];and the 8 individual statements that define the value of each level of the array are in setup(). In the second case you are trying to declare the variable and define the values at the same time. This is possible but needs to be done like thisuint8_t led[] = {B11111111, B10000001, B10111101, B10100101, B10100101, B10111101, B10000001, B11111111};outside of the setup() function.

As to copying arrays, no you can't do it like that. Try a search of this forum.

icamaster:
And could i make something like lets say
pattern1 = {etc}
pattern2 = {etc}
pattern3 = {etc}
and in the void loop just
led = pattern1 then led = pattern2 to just change what i'm displaying?

Yes you can do that by using a pointer to the array, exemple:

uint8_t pattern1[] = { 1, 2, 3 };
uint8_t pattern2[] = { 4, 5, 6 };

uint8_t* led = pattern1;
// now led[0] = 1, led[1] = 2 ... 

led = pattern2;
// now led[0] = 4, led[1] = 5 ...

Thank you all for the support. I am sorry if i make you angry with my very poor programming skills, i’m mostly good at the hardware stuff.

So, first of all, here is the code with indents (i didn’t even know arduino had auto-format, thank you, the code looked hard to read to me too).

// Project 16
#include <TimerOne.h>
//Pin connected to Pin 12 of 74HC595 (Latch)
int latchPin = 8;
//Pin connected to Pin 11 of 74HC595 (Clock)
int clockPin = 12;
//Pin connected to Pin 14 of 74HC595 (Data)
int dataPin = 11;
uint8_t led[8];
long counter1 = 0;
long counter2 = 0;
void setup() {
  //set pins to output
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  led[0] = B11111111;
  led[1] = B10000001;
  led[2] = B10111101;
  led[3] = B10100101;
  led[4] = B10100101;
  led[5] = B10111101;
  led[6] = B10000001;
  led[7] = B11111111;
  Timer1.initialize(10000);
  Timer1.attachInterrupt(screenUpdate);
}
void loop() {
  counter1++;
  if (counter1 >=100000) {
    counter2++;
  }
  if (counter2 >= 10000) {
    counter1 = 0;
    counter2 = 0;
    for (int i=0; i<8; i++) {
      led[i]= ~led[i];
    }
  }
}
void screenUpdate() {
  uint8_t row = B00000001;
  for (byte k = 0; k < 9; k++) {
    // Open up the latch ready to receive data
    digitalWrite(latchPin, LOW);
    shiftIt(~row );
    shiftIt(led[k] ); // LED array
    // Close the latch, sending the data in the registers out to the matrix
      digitalWrite(latchPin, HIGH); 
    row = row << 1;
  }
}
void shiftIt(byte dataOut) {
  // Shift out 8 bits LSB first,
  // on rising edge of clock
  boolean pinState;
  //clear shift register read for sending data
  digitalWrite(dataPin, LOW);
  // for each bit in dataOut send out a bit
  for (int i=0; i<8; i++) {
    //set clockPin to LOW prior to sending bit
    digitalWrite(clockPin, LOW);
    // if the value of DataOut and (logical AND) a bitmask
    // are true, set pinState to 1 (HIGH)
    if ( dataOut & (1<<i) ) {
      pinState = HIGH;
    }
    else {
        pinState = LOW;
    }
    //sets dataPin to HIGH or LOW depending on pinState
    digitalWrite(dataPin, pinState);
    //send bit out on rising edge of clock
    digitalWrite(clockPin, HIGH);
    digitalWrite(dataPin, LOW);
  }
  //stop shifting
  digitalWrite(clockPin, LOW);
}

I have found another sketch with a similar table and I ask if this would work.

pattern.h

#define FRAMECNT 3
Frame Frames[FRAMECNT] = {
{B10000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00000000, B00001000},

{B01000000, B11000000, B00000000, B00000000, B00000000, B00000000, B00011000, B00010000},

{B11100000, B10100000, B00000000, B00000000, B00000000, B11111000, B00101000, B00111000}
 };

first part of main code

typedef byte Frame[8];
uint8_t *led;
int curFrameIdx;
#include pattern.h

setframe function

void setFrame(int idx) {
  led = Frames[idx];
}

and the loop

void loop() {
 
  if (++curFrameIdx == FRAMECNT) {
    curFrameIdx = 0;
  }

  // select frame for display
  setFrame(curFrameIdx);

  delay(400); // i won t use delay in the final program
}

Here is the full code of the other project :

/*
 * Show animations on a DIMxDIM led matrix
 *
 * Uses Timer1 library to
 * constantly run an interrupt routine
 * at a specified frequency. This
 * refreshes the display without the
 * main loop having to do anything.
 *
 */
#include <TimerOne.h>

//#define TESTMODE // continuously sequence thru the LED's 

#define DIM 5 // x/y dimension - 5x5 matrix
#define DIM1 (DIM-1)

typedef byte Frame[DIM];

#include "frames.h"

byte row = 0;
byte *curFrame;
int curFrameIdx;

// col[xx] of leds - anodes
// these are the arduino pins assigned to each column
int cols[DIM] = {12,11,10,9,8};

// row[xx] of leds - cathodes
// these are the arduino pins assigned to each row
int rows[DIM] = {7,6,5,4,3};

Frame blankFrame =
{B00000,
 B00000,
 B00000,
 B00000,
 B00000};

// blank the screen
void clearLeds() {
  // blank screen
  curFrame = blankFrame;
}

// select a frame to display
// idx = 0 -> FRAMDECNT-1
void setFrame(int idx) {
  curFrame = Frames[idx];
}

// Interrupt routine
// each time display() is called, it turns off the previous row
// and turns on the next row
byte bitMask = B00000011;
void display() {
  digitalWrite(rows[row], HIGH);  // Turn whole previous row off

  if (bitMask == B00010000) {
    bitMask = B00000011;  // light the right 2 columns (pins 9,8)
    // increment row and wrap if necessary
    if (++row == DIM) {
      row = 0;
    }
  }
  else if (bitMask == B00000011) {
    bitMask = B00001100;  // light the middle 2 columns (pins 11,10)
  }
  else { // bitMaskIdx == B00001100
    bitMask = B00010000;  // light the leftmost column (pin 12)
  }
  
  // direct port manipulation.
  // PORTB is a pseudo variable for digital pins 8 to 13 The two high bits (6 & 7) map to the crystal pins and are not usable
  // the bottom 5 bits are our columns.  We don't want to change the other bits,
  // so we first mask the bits we ignore, and then set the bits we want to light
  PORTB &= B11100000;
  PORTB |= curFrame[row] & bitMask;
  
  digitalWrite(rows[row], LOW); // Turn whole row on at once (for equal lighting times)
}


void setup() {
  int i;
  
  // sets the pins as output
  for (i = 0; i < DIM; i++) {
    pinMode(cols[i], OUTPUT);
    pinMode(rows[i], OUTPUT);
  }

  // set up cols and rows (set display to dark)
  for (i = 0; i < DIM; i++) {
    digitalWrite(cols[i], LOW);
  }

  for (i = 0; i < DIM; i++) {
    digitalWrite(rows[i], HIGH);
  }


  clearLeds();
  
#ifdef TESTMODE  
  while (1) {
  for (i=0;i < DIM;i++) {
    digitalWrite(rows[i],LOW);
    for (int j=0;j < DIM;j++) {
      digitalWrite(cols[j],HIGH);
      delay(250);
      digitalWrite(cols[j],LOW);
    }
    digitalWrite(rows[i],HIGH);
  }
  }
#else // !TESTMODE
  // interrupt interval in uSec
  // this determines how long to keep each row turned on
  // so if we have 5 rows, we redraw the whole screen
  // once every 5 rows * 3 cycles per row * 1000 usec = .015 sec -> 66.67Hz
  // if you perceive flickering you can decrease to 500 for a 133.33Hz rate
  Timer1.initialize(1000);
  
  Timer1.attachInterrupt(display); // ISR

  curFrameIdx = -1;
#endif // TESTMODE
                                                 
}

void loop() {
  // increment the frame index and wrap if necessary
  if (++curFrameIdx == FRAMECNT) {
    curFrameIdx = 0;
  }

  // select frame for display
  setFrame(curFrameIdx);

  delay(400); // wait time between frames in ms
}

The other question, besides “will it work” is why in the second program (the last one) the cuPrameIdx doesn’t have a default value, and how come it is set to 0 in the loop, and not 1, because in the pattern.h FRAMECNT can’t be 0.

PS Just to make sure, i want to combine the first sketch with the 3 parts i posted after, the last code is only there so show you where i copied those parts.

icamaster:
I am sorry if i make you angry

Who's angry?