Hello ... i am working on project involving a reaction time game, where i have 52 illuminated led push buttons, the play should hit the button as soon as it lids.
I am using a series of 74hc165 and 74hc595 chained together and connected to an arduino.
This my code :
#define NUMBER_OF_SHIFT_CHIPS 7
#define DATA_WIDTH NUMBER_OF_SHIFT_CHIPS * 8
const int DataPin= 11; // Connect Pin 11 to SER_OUT (Serial data out) - Pin 9 (Q7) of 74HC165
const int LoadPin = 8; // Connect Pin 8 to SH/!LD (shift or active low load) - Pin 1 (!PL) of 74HC165
const int ClockPin= 12; // Connect Pin 12 to CLK (the clock that times the shifting) - Pin 2 (CP) of 74HC165
const int EnablePin = 9; // Connect Pin 9 to !CE (clock enable, active low) - Pin 15 (!CE) of 74HC165
unsigned char Total_Switch[57] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57};
#define T_Poll 250 // register polling period (ms)
unsigned long oldtime;
unsigned int i;
unsigned char Switch_number = 0;
unsigned char old_Switch_number = 0;
void setup()
{
Serial.begin(9600);
pinMode(LoadPin, OUTPUT);
pinMode(ClockPin, OUTPUT);
pinMode(EnablePin, OUTPUT);
pinMode(DataPin, INPUT);
digitalWrite(ClockPin, LOW);
digitalWrite(LoadPin, HIGH);
oldtime = millis();
}
void loop()
{
buttonWait();
}
void buttonWait(){
if (millis() - oldtime > T_Poll) {
while(1){
Switch_number = read_shift_regs();
if ((Switch_number != old_Switch_number)) {
Serial.println(Switch_number);
old_Switch_number = Switch_number;
return;
}
}
oldtime = millis();
}
}
unsigned char read_shift_regs()
{
Switch_number = 0;
// Enable the register!
digitalWrite(EnablePin,HIGH);
digitalWrite(LoadPin, LOW);
delayMicroseconds(5);
digitalWrite(LoadPin,HIGH);
digitalWrite(EnablePin,LOW);
// Read the bits on the register and store in var
for (i = 0; i < 57; i++)
{
if (digitalRead(DataPin) == HIGH){
Switch_number += Total_Switch[i] ;
}
digitalWrite(ClockPin,HIGH);
delayMicroseconds(5);
digitalWrite(ClockPin,LOW);
}
return Switch_number;
}
i am still working only on the "switch part" of the code.
i am trying to resolving debouncing problem, but i am not sure it is working, because i am getting, lets say i click button 128 and then 3 and then 10 :
128
0
3
0
10
0
is it enough to use
Switch_number = read_shift_regs();
if ((Switch_number != old_Switch_number) && Switch_number != 0) {
Serial.println(Switch_number);
old_Switch_number = Switch_number;
return;
}
to keep it from sending dobble value ? .. thank you