I'm, still a novice, working on a LED clock, which will have 12 output LED's, for the hour hand, and 12 output LED's for the minutes hand (set at 5 minute intervals). I have the hardware working, and most of the sketch finished including the main "time keeping" routine. I have added two buttons, and have successfully been able to drive the shift register, be it chaotically, and have not been able to incorporate the button shift changes into the main time keeping routine. The routine continues where it left off after the button press and expired delay time loops it. Please help me with the time setting in the "void stateChange()" portion of this code .
//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;
int pbIn = 0; // Interrupt 0 is on DIGITAL PIN 2!
volatile int state = LOW; // The input state toggle
//holders for infromation you're going to pass to shifting function
byte dataRED;
byte dataGREEN;
byte dataRGB;
byte dataArrayRED[144]; //the number in the [ ] is the amount of array lines
byte dataArrayGREEN[144]; //the number in the [ ] is the amount of array lines
byte dataArrayRGB[144];
void setup() {
//set pins to output because they are addressed in the main loop
pinMode(latchPin, OUTPUT);
Serial.begin(9600);
//Attach the interrupt to the input pin and monitor for ANY Change
attachInterrupt(pbIn, stateChange, CHANGE);
//Arduino doesn't seem to have a way to write binary straight into the code
//so these values are in HEX. Decimal would have been fine, too.
//chip#1 The LED's are RED or GREEN, just named them dataArrayRGB because its short
dataArrayRGB[0] = 0x11; // begin hour 1, minute 5
dataArrayRGB[1] = 0x21; // 10m
dataArrayRGB[2] = 0x41; // 15m
dataArrayRGB[3] = 0x81; // 20m
dataArrayRGB[4] = 0x01;
dataArrayRGB[5] = 0x01;
dataArrayRGB[6] = 0x01;
dataArrayRGB[7] = 0x01;
dataArrayRGB[8] = 0x01;
dataArrayRGB[9] = 0x01;
dataArrayRGB[10] = 0x01;
dataArrayRGB[11] = 0x01; // 1h finish
// it continues up to dataArrayRGB[144] but had to cut this due to restrictions on post size on this site
// chip#3
dataArrayRED[0] = 0x0; // begin 1h
dataArrayRED[1] = 0x0;
dataArrayRED[2] = 0x0;
dataArrayRED[3] = 0x0;
dataArrayRED[4] = 0x10; // 25
dataArrayRED[5] = 0x20; // 30
dataArrayRED[6] = 0x40; // 35
dataArrayRED[7] = 0x80; // 40
dataArrayRED[8] = 0x0;
dataArrayRED[9] = 0x0;
dataArrayRED[10] = 0x0;
dataArrayRED[11] = 0x0;
// it continues up to dataArrayRED[144] but had to cut this due to restrictions on post size on this site
//Arduino doesn't seem to have a way to write binary straight into the code
//so these values are in HEX. Decimal would have been fine, too.
dataArrayGREEN[0] = 0x0; // begin 1h
dataArrayGREEN[1] = 0x0;
dataArrayGREEN[2] = 0x0;
dataArrayGREEN[3] = 0x0;
dataArrayGREEN[4] = 0x0;
dataArrayGREEN[5] = 0x0;
dataArrayGREEN[6] = 0x0;
dataArrayGREEN[7] = 0x0;
dataArrayGREEN[8] = 0x10; // 45
dataArrayGREEN[9] = 0x20; // 50
dataArrayGREEN[10] = 0x40; // 55
dataArrayGREEN[11] = 0x80; // 60
// it continues up to dataArrayGREEN[144] but had to cut this due to restrictions on post size on this site
//function that blinks all the LEDs
//gets passed the number of blinks and the pause time
blinkAll_2Bytes(2,500);
}
void loop() {
for (int j = 0; j < 144; j++) { // in for (int j = 0; j < X; j++) {
//the X is the amount of one run before the array loops again.
//load the light sequence you want from array
dataRED = dataArrayRED[j];
dataGREEN = dataArrayGREEN[j];
dataRGB = dataArrayRGB[j];
//ground latchPin and hold low for as long as you are transmitting
digitalWrite(latchPin, 0);
//move 'em out
shiftOut(dataPin, clockPin, dataGREEN);
shiftOut(dataPin, clockPin, dataRED);
shiftOut(dataPin, clockPin, dataRGB);
//return the latch pin high to signal chip that it
//no longer needs to listen for information
digitalWrite(latchPin, 1);
delay(1300);
}
}
// Below lies the problem code:
void stateChange()
{
state = !state;
int j;
dataGREEN = dataArrayGREEN[j++];
dataRED = dataArrayRED[j++];
dataRGB = dataArrayRGB[j++];
digitalWrite(latchPin, LOW);
//move 'em out
shiftOut(dataPin, clockPin, dataGREEN);
shiftOut(dataPin, clockPin, dataRED);
shiftOut(dataPin, clockPin, dataRGB);
//return the latch pin high to signal chip that it
//no longer needs to listen for information
digitalWrite(latchPin, HIGH);
}
// the heart of the program
void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {
// This shifts 8 bits out MSB first,
//on the rising edge of the clock,
//clock idles low
//internal function setup
int i=0;
int pinState;
pinMode(myClockPin, OUTPUT);
pinMode(myDataPin, OUTPUT);
//clear everything out just in case to
//prepare shift register for bit shifting
digitalWrite(myDataPin, 0);
digitalWrite(myClockPin, 0);
//for each bit in the byte myDataOut?
//NOTICE THAT WE ARE COUNTING DOWN in our for loop
//This means that %00000001 or "1" will go through such
//that it will be pin Q0 that lights.
for (i=7; i>=0; i--) {
digitalWrite(myClockPin, 0);
//if the value passed to myDataOut and a bitmask result
// true then... so if we are at i=6 and our value is
// %11010100 it would the code compares it to %01000000
// and proceeds to set pinState to 1.
if ( myDataOut & (1<<i) ) {
pinState= 1;
}
else {
pinState= 0;
}
//Sets the pin to HIGH or LOW depending on pinState
digitalWrite(myDataPin, pinState);
//register shifts bits on upstroke of clock pin
digitalWrite(myClockPin, 1);
//zero the data pin after shift to prevent bleed through
digitalWrite(myDataPin, 0);
}
//stop shifting
digitalWrite(myClockPin, 0);
}
//blinks the whole register based on the number of times you want to
//blink "n" and the pause between them "d"
//starts with a moment of darkness to make sure the first blink
//has its full visual effect.
void blinkAll_2Bytes(int n, int d) {
digitalWrite(latchPin, 0);
shiftOut(dataPin, clockPin, 0);
shiftOut(dataPin, clockPin, 0);
digitalWrite(latchPin, 1);
delay(200);
for (int x = 0; x < n; x++) {
digitalWrite(latchPin, 0);
shiftOut(dataPin, clockPin, 255);
shiftOut(dataPin, clockPin, 255);
digitalWrite(latchPin, 1);
delay(d);
digitalWrite(latchPin, 0);
shiftOut(dataPin, clockPin, 0);
shiftOut(dataPin, clockPin, 0);
digitalWrite(latchPin, 1);
delay(d);
}
}