General Questions about code/programming

I’ve been experimenting so much with the arduino (I love it) However, I’m learning Electronics and the language at the same time, so quite a brick wall.

Anyway after a few days with some 595 Shift registers I finally got my current project working as intended.

Components
18x 220 Ohm resistors
2x 300 Ohm resistors
1x 1k Pot
3x 595 registers
20 Led’s (a mix cause I was running out)

My goal was to do the Knightrider (tutorial) and shift out with an extra 595 that has a Variable delay which is adjusted by the potentiometer.

Like I said it works! I mish mashed a lot of different codes and wrote a lot by hand, but here it is. My question is would there have been a simpiler way in regards to the code:

//Pin connected to latch pin (ST_CP) of 74HC595
const int latchPin = 8;
//Pin connected to clock pin (SH_CP) of 74HC595
const int clockPin = 12;
////Pin connected to Data in (DS) of 74HC595
const int dataPin = 11;
int registerOne = 0;
int registerTwo = 0;
int registerThree = 0;
int oneArray[] = {1, 2, 4, 8, 16, 32, 64, 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int twoArray[] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 4, 8, 16, 32, 64, 128, 0, 0, 0, 0, 0, 0, 0, 0};
int threeArray[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 4, 8, 16, 32, 64, 128};
int sensorValue = 0;
int sensorPin = A0;

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

void loop() {

  

  for (int i = 0; i <= 23; i++) {
  sensorValue = analogRead(sensorPin); 
  Serial.println(sensorValue);  
  digitalWrite(latchPin, HIGH);
  shiftOut(dataPin, clockPin, MSBFIRST, threeArray[i]);
  shiftOut(dataPin, clockPin, MSBFIRST, twoArray[i]);
  shiftOut(dataPin, clockPin, MSBFIRST, oneArray[i]);
  
  // turn on the output so the LEDs can light up:
  digitalWrite(latchPin, LOW);
  delay(sensorValue);
  }
  for (int i = 23; i >= 0; i--) {
      sensorValue = analogRead(sensorPin);
      Serial.println(sensorValue);    
      digitalWrite(latchPin, HIGH);
      shiftOut(dataPin, clockPin, MSBFIRST, threeArray[i]);
      shiftOut(dataPin, clockPin, MSBFIRST, twoArray[i]);
      shiftOut(dataPin, clockPin, MSBFIRST, oneArray[i]);
  
      // turn on the output so the LEDs can light up:
      digitalWrite(latchPin, LOW);
      delay(sensorValue);
  }   
}

hmm, you can use the bit shift operator << to shift bits, doing this on 1, such as

1 << 1

will result in 2, with this technique, you can generate powers of 2, like 1 2 4 8 16 etc by incrementing the right-hand-side of the << operator

if you did this, you can probably get rid of the three arrays, which might be useful if you wanted to save some memory


You should already realize your three arrays contain identical data, why not just use one?


Also you shouldn’t be using shiftOut for this job… just put in 1 bit, toggle the clock manually until that bit reaches the correct position, and then latch the shift register

Take a gooooood read of the 595’s datasheet, also understand how latches work

First off I don’t really understand bitshift, I tried to read more on it, but I didn’t really grasp it (yet)

In regards to the arrays, I suppose I could have technically gone (for the other registers) i + 8, or i - 8? If that’s not what you meant I’m sorry, Using the shift out needs the data to be sent to make sure that the others aren’t lit. (If this is way off base forgive me, like I said I’m very new).

in regards to the 1 bit, toggle manually, where can I read up on that some more?

I appreciate your help, and thank you for the response.

please check - http://arduino.cc/en/Reference/Bitshift - http://arduino.cc/en/Reference/BitwiseAnd