How to store and read a byte from an Array (Arduino Uno)

Dear community,

Please help,

I'm trying to write a smal program to make the arduino drive a shift register.
I would like the program to read the the values stored in myArray and store them in the variable "code"
I can't manage to do so, the values are translated automatically to decemal values.

I've introduced an extra step to translate them back to a byte, but this makes the the property a string which also isn't working.

[code]
// Pin mapping:

int DATA = 13; // Pin 13 is DATA output
int SRCK = 12; // Pin 12 is Shift Register Clock (on the rising edge of this clock the data is shifted into the register)
int SRCLR_ = 11; // Pin 11 is Shift Register Clear (make this pin low to clear the shift register)
int RCK = 10; // Pin 10 = Register Clock (on the rising edge of this clock the data is moved to the output register)
int G_ = 9; // Pin 9 is Output Enable (make this pin high to enable the output)


void setup() {
  // put your setup code here, to run once:
  pinMode(DATA, OUTPUT);
  pinMode(SRCK, OUTPUT);
  pinMode(SRCLR_, OUTPUT);
  pinMode(RCK, OUTPUT);
  pinMode(G_, OUTPUT);

  Serial.begin(9600); // Open a serial port
  // Clear all data and switch of all output
  digitalWrite(G_, LOW);   // Switch off the register output
  digitalWrite(SRCLR_, LOW);   // Clear the shift register
}

byte code = B00000000;
byte precode = B00000000;

int speed = 100;
byte mask = 1; //our bitmask
byte bitDelay = 100;
int loopcount = 8;
int arraycount = 0;


byte myArray[] = {B00000000, B10000000, B11100000, B01100000, B01000000, B01110000, B00110000, B00100000, B00111000, B00011000, B00010000, B00011100, B00001100, B00001000, B00001110, B00000110, B00000100, B00000111, B00000011, B00000010, B1000011, B10000001, B00000001, B11000001, B11000000};



void loop() {


  // Switch on the shift register
  precode = myArray[arraycount];
  if (arraycount > 24) {
    arraycount = 0;
  }

  Serial.print("       Variable value = ");
  Serial.print(myArray[4]);

  {
    int zeros = 8 - String(precode, BIN).length();
    String code;
    for (int i = 0; i < zeros; i++) {
      code = code + "0";
    }



  }




  digitalWrite(G_, HIGH);         // Switch on the register output
  digitalWrite(SRCLR_, HIGH);    // Disable the clear funtion


  while (loopcount > 0) {
    for (mask = 00000001; mask > 0; mask <<= 1) { //iterate through bit mask
      if (code & mask) { // if bitwise AND resolves to true
        digitalWrite(DATA, HIGH);   // send 1
        digitalWrite(SRCK, HIGH);   // Clock = 1 (shift data in)
        digitalWrite(SRCK, LOW);    // Clock = 0
        digitalWrite(DATA, LOW);    // Set data pin to 0
      }

      else { //if bitwise AND resolves to false
        digitalWrite(DATA, LOW);   // send 0
        digitalWrite(SRCK, HIGH);  // Clock = 1 (shift data in)
        digitalWrite(SRCK, LOW);   // Clock = 0
      }
      delay(bitDelay);
      loopcount--;
    }
    switch_on(); // Move register content to output
    delay(1000);
  }
  arraycount++;
}



// Functions:

void switch_on() {
  digitalWrite(RCK, HIGH);   // Move shift register to output
  digitalWrite(RCK, LOW);    // Reset shift register output
}

[/code]

byte code = B00000000;
    String code;

Which of the 2 different variables named code are you having trouble with ?

shadowbiker88:
Dear community,

Please help,

I'm trying to write a smal program to make the arduino drive a shift register.
I would like the program to read the the values stored in myArray and store them in the variable "code"
I can't manage to do so, the values are translated automatically to decemal values.

is there a reason you just can't use shiftout()?

Sorry if i wasn't clear, (i'm a newbie),

I'm having trouble with the String code, i think.
If i changed int code by hand to 01010101 it works fine.

Thank you all for your reply i will have to study on it and get back with the result.

Having 2 different variables with the same name is madness.
Having 2 different variables with the same name but different types is lunacy.

Dear BulldogLowell,

I agree using shiftout() wouldm do the job also, but this is not the major problem.
The problem is that when i read from myArray into "precode" the values in "precode"are not binairy but decimal.

They need to be binairy before storing them into "code" (if i write "byte code = B01010101;" it works perfect.

So the question is how to write the decimal content of "myArray" into the variable "code"?

Kind regards,

Shadowbiker

shadowbiker88:
Dear BulldogLowell,

I agree using shiftout() wouldm do the job also, but this is not the major problem.
The problem is that when i read from myArray into "precode" the values in "precode"are not binairy but decimal.

They need to be binairy before storing them into "code" (if i write "byte code = B01010101;" it works perfect.

So the question is how to write the decimal content of "myArray" into the variable "code"?

Kind regards,

Shadowbiker

As you know, everything your Arduino does is in BINARY. Every number it store is in binary. Your "precode" is all converted to binary. When you see a hexadecimal or a decimal representation of a byte, it has been converted for you. When you choose to represent a number in decimal or hexadecimal in your program, compiler will convert it all to binary for you.

Run and look that the output of this sketch and comment out each of the three declarations of myByte and you will see the output is identical:

void setup() 
{
  Serial.begin(9600);
  byte myByte = B11000011;
  //byte myByte = 0xC3;
  //byte myByte = 195;
  Serial.println(myByte, DEC);
  Serial.println(myByte, BIN);
  Serial.println(myByte, HEX);
  shiftOut(1,1,LSBFIRST, myByte); // <<<< will shift out the same no matter which of the three assignments you choose above!!!!
}

void loop() 
{

}

I hope that explains it to you so that you understand...

So what is it you are trying to do in you sketch?

Thank you for your quick reply BulldogLowell,

I will try to explain what i'm trying to do.

I have a shift register with leds connected to my Aduino.
If I give the variable "code" the value: byte code = B01010101; // at the top, declaration of the variable "code"

The leds connected to the shift register are off-on-off-on-off-on-off-on

I'm trying to store the values stored in "myArray" one after each other "myArray[0]", "myArray[1]", etc. Into the variabe "code" so the leds will light up according the byte patterns stored in "myArray".

Thank you very much for your help.

Kind regards

Shadowbiker

shadowbiker88:
Thank you for your quick reply BulldogLowell,

I will try to explain what i'm trying to do.

I have a shift register with leds connected to my Aduino.
If I give the variable "code" the value: byte code = B01010101; // at the top, declaration of the variable "code"

The leds connected to the shift register are off-on-off-on-off-on-off-on

I'm trying to store the values stored in "myArray" one after each other "myArray[0]", "myArray[1]", etc. Into the variabe "code" so the leds will light up according the byte patterns stored in "myArray".

Thank you very much for your help.

Kind regards

Shadowbiker

something like this, untested:

//Pin connected to ST_CP of 74HC595
int latchPin = 8;
//Pin connected to SH_CP of 74HC595
int clockPin = 12;
////Pin connected to DS of 74HC595
int dataPin = 11;

byte myArray[] = {B00000000, B10000000, B11100000, B01100000, B01000000, B01110000, B00110000, B00100000, B00111000, B00011000, B00010000, B00011100, B00001100, B00001000, B00001110, B00000110, B00000100, B00000111, B00000011, B00000010, B1000011, B10000001, B00000001, B11000001, B11000000};

void setup() 
{
  Serial.begin(9600);
  //set pins to output because they are addressed in the main loop
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
}

void loop() 
{
  for (int i = 0; i < sizeof(myArray)/sizeof(myArray[0]); i++)
  {
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, LSBFIRST, myArray[i]);
    digitalWrite(latchPin, HIGH);
    delay(100);
  }
}

shadowbiker88:
I'm trying to store the values stored in "myArray" one after each other "myArray[0]", "myArray[1]", etc. Into the variabe "code" so the leds will light up according the byte patterns stored in "myArray".

...and what happens?

What happens is that the leds stay dark.
It looks like the program isn't overwriting the "code" variable.
I'm very curious were a'm going wrong.

BulldogLowell, I will try to learn from your code, thank you.

shadowbiker88:
BulldogLowell, I will try to learn from your code, thank you.

what you are doing is very common... so much so that the shiftOut() function is part of the IDE!!!

I'm beginning to understand that it's very common. :-[
But the trouble I'm having is not in communicating with the shift register I think.

I think my problem lies in getting the data out of the myArray variable and storing it in the code variable.
What I don't understand is why if I give the variable code a value by hand it works. But when I written it from myArray to code it doesn't.

Thanks for your patience :confused:

It looks like the program isn't overwriting the "code" variable.

Have you still got 2 variables named code in your program ?

Sorry, I don't get the point, before getting into the loop I declare that the variable byte code =B00000000

Inside the loop I'm overwriting the variable with the content of the myAray variable

See reply #1.

If I give the variable "code" the value: byte code = B01010101; // at the top, declaration of the variable "code"

The leds connected to the shift register are off-on-off-on-off-on-off-on

That is not the variable named code that is used in the loop() function. It is this one

   String code;

They are different variables of different types.

shadowbiker88:
Sorry, I don't get the point, before getting into the loop I declare that the variable byte code =B00000000

Inside the loop I'm overwriting the variable with the content of the myAray variable

Why? Why?

You don't need a "batter's box." Just access the array, like I showed you.

Thank you all very much,

It took me a while to understand, but it's working now.

Thank you very much for your patience BulldogLowell.

Please find the working code below:

// Pin mapping:

int DATA = 13; // Pin 13 is DATA output
int SRCK = 12; // Pin 12 is Shift Register Clock (on the rising edge of this clock the data is shifted into the register)
int SRCLR_ = 11; // Pin 11 is Shift Register Clear (make this pin low to clear the shift register)
int RCK = 10; // Pin 10 = Register Clock (on the rising edge of this clock the data is moved to the output register)
int G_ = 9; // Pin 9 is Output Enable (make this pin high to enable the output)

void setup() {
  // put your setup code here, to run once:
  pinMode(DATA, OUTPUT);
  pinMode(SRCK, OUTPUT);
  pinMode(SRCLR_, OUTPUT);
  pinMode(RCK, OUTPUT);
  pinMode(G_, OUTPUT);

  const byte numPins = 8;

  Serial.begin(9600); // Open a serial port
  // Clear all data and switch of all output
  digitalWrite(G_, LOW);   // Switch off the register output
  digitalWrite(SRCLR_, LOW);   // Clear the shift register
}

byte code = B00000000;
int arraycount = 0;

int myArray[] = {B00000000, B10000000, B11100000, B01100000, B01000000, B01110000, B00110000, B00100000, B00111000, B00011000, B00010000, B00011100, B00001100, B00001000, B00001110, B00000110, B00000100, B00000111, B00000011, B00000010, B1000011, B10000001, B00000001, B11000001, B11000000};

void loop() {

  code = myArray[arraycount];

  if (arraycount > 24) {
    arraycount = 0;
  }

  arraycount++;

  // Switch on the shift register
  digitalWrite(G_, HIGH);         // Switch on the register output
  digitalWrite(SRCLR_, HIGH);    // Disable the clear funtion

  shiftOut(DATA, SRCK, LSBFIRST, code);

  // switch_on(); // Move register content to output
  switch_on();
  delay(300);
}

// Functions:

void switch_on() {
  digitalWrite(RCK, HIGH);   // Move shift register to output
  digitalWrite(RCK, LOW);    // Reset shift register output
}