I have a simple back-and-forth LED chaser, only one LED is lit at a time. My approach is multiply x2 until I get to 128 then I multiply by 0.5 until I reach 1.
It works fine with one shift register. Now I've added 5 more shift registers and I get nothing like I would expect, many LEDs are lit at once, the register closest to the Arduino behaves the worst, random, non-repeating, etc. Sometimes I will get repeating patterns that will freeze. Looking at my Serial.print's, the loops appear to be doing what they're supposed to.
Now for the strange part. If I handle the breadboard, it will unfreeze and start misbehaving again. Makes me amost wonder if there is something wrong with my breadboard. I do have a small capacitor between the latch pin and ground on each of the 595s. All the LED's cathodes are tied together going through a single current limiting resistor to ground (I thought that would be OK as only one LED should be lit at a time). Here's my code for the scaled up version (the non-working one) if that helps. Any hot tips appreciated.
Carl
// Notes : LED chaser, sweeps back and forth
// : shiftOutCode, blink without delay, analogRead
//**************************************************************//
int latchPin = 8; // Pin connected to ST_CP of 74HC595 (pin 12 on the 595 green wire)
int clockPin = 12; // Pin connected to SH_CP of 74HC595 (pin 11 on the 595 yellow wire)
int dataPin = 11; // Pin connected to DS of 74HC595 (pin 14 on the 595 blue wire)
int potPin = 2; // A2 select the analog pin that connects to the middle lead on the potentiometer
int speakerPin = 3; // has to be a PWM digital pin
int buttonPin = 2; // D2 the number of the pushbutton pin
int potVal = 0; // variable to store the value coming from the sensor (potentiometer)...
long previousMillis = 0; // will store last time display was updated
long interVal = 1000; // interval at which to update display (milliseconds)
int numOfRegisters = 6;
int regToDisplay = 1;
byte numberToDisplay = 1;
byte zeroByte = 0;
int numOfLEDs = numOfRegisters * 8;
//numberToDisplay counting up -or- down?
double x = 2;
void setup() {
//set pins to output so you can control the shift register
pinMode(latchPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(potPin, INPUT);
pinMode(buttonPin, INPUT);
Serial.begin(9600);
//initSwitches();
}
void loop() {
potVal = analogRead(potPin); // read the value from the sensor
potVal = map(potVal,0,1024,50,241);
unsigned long currentMillis = millis();
interVal = 60000/potVal/numOfLEDs; //converts potVal beats per minute to mS
if(currentMillis - previousMillis > interVal) {
// save the last time display was updated
previousMillis = currentMillis;
if (numberToDisplay == 1 && x == .5){ //currently counting down and at the botom
if(regToDisplay == 1) {
x = 2; // start counting up
numberToDisplay = numberToDisplay * x;
tone(speakerPin,2640,10);
} else {
numberToDisplay = 128;
regToDisplay = regToDisplay - 1;
}
} else if (numberToDisplay == 128 && x == 2){ //currently counting up and at the top
if(regToDisplay == numOfRegisters) {
x = .5; // start counting down
numberToDisplay = numberToDisplay * x;
tone(speakerPin,2640,10);
} else {
numberToDisplay = 1;
regToDisplay = regToDisplay + 1;
}
} else {
numberToDisplay = numberToDisplay * x;
}
digitalWrite (latchPin, LOW);
for (int thisReg = numOfRegisters; thisReg > 0; thisReg --) {
//Serial.println(thisReg);
if (thisReg == regToDisplay){
shiftOut(dataPin, clockPin, LSBFIRST, numberToDisplay);
} else {
shiftOut(dataPin, clockPin, LSBFIRST, zeroByte);
}
}
digitalWrite (latchPin, HIGH);
/*
Serial.print("reg ");
Serial.print(regToDisplay);
Serial.print(" num ");
Serial.println(numberToDisplay);
*/
}
}