Go Down

Topic: using array with ShiftOut (Read 710 times) previous topic - next topic

george_georgia

Hi,
I'm writing program to control shift register.
I have this array:

int arr[8] = {0, 0, 0, 0, 0, 0, 0, 0};

It's like byte arr = 0b00000000, but i want to use array, because I need indexing and etc.
I want to use it with ShiftOut. How can i do this?


J-M-L

#1
Aug 08, 2016, 03:07 pm Last Edit: Aug 08, 2016, 03:09 pm by J-M-L
Well build a byte with your 8 individual bits and then shift it out?

There are bit related operations already defined if you don't want to do the bit masking dance yourself

See https://www.arduino.cc/en/Reference/BitSet

Or go look at the code of shiftOut and do something similar with your array instead of bits

My view though is that you should not deal with an array, just use bytes and use the bit manipulation function above for your indexing purpose. Saves memory...
Hello - Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums.
Bonjour Pas de messages privés SVP, postez dans le forum directement pour que ça profite à tous

UKHeliBob

Quote
i want to use array, because I need indexing and etc.
Why not leave it in a byte and access the bits with an index and the bitRead() function ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

george_georgia

thanks for replies,
I looked for bitSet, BitClear and BitRead, but arrays are still better for my application
here is fraction of my code:
Code: [Select]
int buttonState = digitalRead(buttonPin);

  int arr[8] = {0, 0, 0, 0, 0, 0, 0, 0};

  if (buttonState == HIGH) {
    for (int x = 6; x >= 0; x--) {
      arr[x] = arr[x+1];
    }
    arr[0] = 1;
  }

  else if (buttonState == LOW) {
    for (int x = 6; x >= 0; x--) {
      arr[x] = arr[x+1];
    }
    arr[0] = 0;
  }





michael_x

#4
Aug 08, 2016, 04:12 pm Last Edit: Aug 08, 2016, 04:13 pm by michael_x
arr[7] .. arr[1] will always remain 0, no matter how long you set the buttonPin to HIGH.

Don't know what this local int array would be good for.

george_georgia

why?
I think it should work.
if you press button 2 times. arr[0] will shift to arr[1] and arr[0] will be set 1. arr[1] and arr[0] both will be 1.

george_georgia

#6
Aug 08, 2016, 04:44 pm Last Edit: Aug 08, 2016, 04:50 pm by george_georgia
I will try to explain it with another example. if you press button first arr[0] will be set to 1. after this if you don't click button in 0.25 sec arr[0] will shift to arr[1] and arr[0] will be set to 0. after 0.25 sec arr[1] will be shifted to arr[2] and arr[1] will be set 0. by shifting I mean arr
  • = arr[x+1] (for example arr[1] = arr[2]). this will repeat again and again, until button is clicked. 

Go Up