Hi there everybody!
I was wondering if anybody could help me out with the following Arduino sketch. I have a circuit with a shift register to which 8 buttons are connected. I have also connected a adafruit led driver with for now 8 LEDs. I will scale this up to 80 buttons wth 80 LEDs. This has all worked. Once a button is pressed, the linked LED switches on and when the ame button is pressed again, the signal is ignored and the light remains on.
The problem: once I upload the sketch again, the lights remain on until I push any button.
After seeing the code, is there anyone who can spot what the problem is?
Thanks so much in advance!
// -------------- LED DRIVERS --------------
#include "Adafruit_TLC5947.h"
// How many boards do you have chained?
#define NUM_TLC5974 1
#define data 4
#define clock 5
#define latch 6
#define oe -1 // set to -1 to not use the enable pin (its optional)
Adafruit_TLC5947 tlc = Adafruit_TLC5947(NUM_TLC5974, clock, data, latch);
int lightOn[80];
// -------------- SHIFT REGISTERS --------------
/* How many shift register chips are daisy-chained.
*/
#define NUMBER_OF_SHIFT_CHIPS 1
/* Width of data (how many ext lines).
*/
#define DATA_WIDTH NUMBER_OF_SHIFT_CHIPS * 8
/* Width of pulse to trigger the shift register to read and latch.
*/
#define PULSE_WIDTH_USEC 5
/* Optional delay between shift register reads.
*/
#define POLL_DELAY_MSEC 1
/* You will need to change the "int" to "long" If the
NUMBER_OF_SHIFT_CHIPS is higher than 2.
*/
#define BYTES_VAL_T unsigned int
int ploadPin = 8; // Connects to Parallel load pin the 165
int clockEnablePin = 9; // Connects to Clock Enable pin the 165
int dataPin = 11; // Connects to the Q7 pin the 165
int clockPin = 12; // Connects to the Clock pin the 165
BYTES_VAL_T pinValues;
BYTES_VAL_T oldPinValues;
/* This function is essentially a "shift-in" routine reading the
serial Data from the shift register chips and representing
the state of those pins in an unsigned integer (or long).
*/
BYTES_VAL_T read_shift_regs()
{
long bitVal;
BYTES_VAL_T bytesVal = 0;
/* Trigger a parallel Load to latch the state of the data lines,
*/
digitalWrite(clockEnablePin, HIGH);
digitalWrite(ploadPin, LOW);
delayMicroseconds(PULSE_WIDTH_USEC);
digitalWrite(ploadPin, HIGH);
digitalWrite(clockEnablePin, LOW);
/* Loop to read each bit value from the serial out line
of the SN74HC165N.
*/
for (int i = 0; i < DATA_WIDTH; i++)
{
bitVal = digitalRead(dataPin);
/* Set the corresponding bit in bytesVal.
*/
bytesVal |= (bitVal << ((DATA_WIDTH - 1) - i));
/* Pulse the Clock (rising edge shifts the next bit).
*/
digitalWrite(clockPin, HIGH);
delayMicroseconds(PULSE_WIDTH_USEC);
digitalWrite(clockPin, LOW);
}
return (bytesVal);
}
void setup()
{
Serial.begin(9600);
// Start leddrivers
tlc.begin();
// Zet alle lampen uit
for (int i; i < 80; i++) {
lightsOff(i);
}
/* Initialize our digital pins...
*/
pinMode(ploadPin, OUTPUT);
pinMode(clockEnablePin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, INPUT);
digitalWrite(clockPin, LOW);
digitalWrite(ploadPin, HIGH);
/* Read in and display the pin states at startup.
*/
pinValues = read_shift_regs();
display_pin_values();
oldPinValues = pinValues;
}
void loop()
{
/* Read the state of all zones.
*/
pinValues = read_shift_regs();
/* If there was a chage in state, display which ones changed.
*/
if (pinValues != oldPinValues)
{
display_pin_values();
oldPinValues = pinValues;
}
delay(POLL_DELAY_MSEC);
}
// -------------- FUNCTIONS VOOR LED DRIVERS --------------
/* LAMPEN UIT + VARIABELE AANMAKEN
*/
void lightsOff(uint8_t lednum) {
tlc.setPWM(lednum, 0);
tlc.write();
lightOn[lednum] = 0;
}
/* LAMPJE AAN LATEN GAAN
*/
void setLEDPWM(uint8_t lednum, uint16_t pwm) {
if (lightOn[lednum] == 0) {
for (int j = 0; j < 20; j++) {
tlc.setPWM(lednum, pwm / (20 - j));
tlc.write();
delay(40);
}
lightOn[lednum] = 1;
}
}
// -------------- FUNCTIONS VOOR SHIFT REGISTERS --------------
/* PRINT WELKE KNOP INGEDRUKT WORDT
*/
void display_pin_values() {
for (int i = 0; i < DATA_WIDTH; i++) {
if ((pinValues >> i) & 1) {
Serial.println(i);
setLEDPWM(i, 4095);
}
}
}