Hi I am new to this forum so please excuse protocol errors.
I have created a multiplexed chess board with leds on each square. Next to the leds I have fitted a magnetic reed switch and taken the outputs (all 64) to 8 shift registers. The code scans the entire register and tells the multiplexer to flash the last move. All works!!
The hard part was creating a countdown timer so that each of the players could stop the clock. The following code uses interupts and I have tested the output using the serial monitor although the actual times will be displaced on a 4 line LCD
Could I have done this easier??????? My god, it took quite a lot of code
#define ledPin 13
int timer1_counter;
int ctr1_1 = 1;
int ctr2_2 = 1;
int minute_multiple =1 ;
const int buttonPin1 = 2; //player one button
const int buttonPin2 = 3; //player two button
int playerone;
int playertwo;
int player_flag; // which player is playing
int chessclock = 0; // set to 1 if running
int playerone_minutes;
int playerone_seconds;
int playertwo_minutes;
int playertwo_seconds;
int Player_start = 0; // 1 for player1 2 for player2
void setup()
{
pinMode(buttonPin1, INPUT_PULLUP); //pin3
pinMode(buttonPin2, INPUT_PULLUP); //pin4
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
// initialize timer1
noInterrupts(); // disable all interrupts
TCCR1A = 0;
TCCR1B = 0;
timer1_counter = 34286; // preload timer 65536-16MHz/256/2Hz
TCNT1 = timer1_counter; // preload timer
TCCR1B |= (1 << CS12); // 256 prescaler
TIMSK1 |= (1 << TOIE1); // enable timer overflow interrupt
interrupts(); // enable all interrupts
}
ISR(TIMER1_OVF_vect) // interrupt service routine
{
TCNT1 = timer1_counter; // preload timer
playerone = digitalRead(buttonPin1); //read switches will be at 1 until pressed then 0
playertwo = digitalRead(buttonPin2);
if (playerone == 0 && playertwo == 0) { // both buttons pressed = reset clock
chessclock = 0; //stop timers
Serial.print ("Clock set at ");
Serial.println (minute_multiple);
// set countdown vale
ctr1_1 = minute_multiple * 60; //initial state of the timers
ctr2_2 = minute_multiple * 60;
// Serial.println(ctr1_1);
if (minute_multiple >= 20) {
minute_multiple = 0;
}
ctr1_1 = minute_multiple * 60; //initial state of the timers
ctr2_2 = minute_multiple * 60;
minute_multiple = minute_multiple + 1;
}
if (playerone == 0 && playertwo == 1) {
player_flag = 1;
chessclock = 1;
Serial.println("player1 running"); // debug line
}
if (playerone == 1 && playertwo == 0) {
player_flag = 2;
chessclock = 1;
Serial.println("player2 running"); // debug line
}
playerone_minutes = ctr1_1 / 60;
playerone_seconds = ctr1_1 % 60;
playertwo_minutes = ctr2_2 / 60;
playertwo_seconds = ctr2_2 % 60;
if (player_flag == 1 && chessclock == 1) {
Serial.print("Player1 ");
Serial.print("M:");
Serial.print(playerone_minutes);
Serial.print(" S: ");
Serial.println(playerone_seconds);
if (playerone_seconds < 0) {
chessclock = 0; // reset clock but dont run
Serial.println("stopped"); // debug line replace with beeper
}
ctr1_1 = ctr1_1 - 1; // count down seconds
}
if (player_flag == 2 && chessclock == 1) {
Serial.print("Player2 ");
Serial.print("M:");
Serial.print(playertwo_minutes);
Serial.print(" S: ");
Serial.println(playertwo_seconds);
if (playertwo_seconds < 0) {
Serial.println("out of time"); // debug line
chessclock = 0; // count down seconds
}
ctr2_2 = ctr2_2 - 1;
}
}
void loop()
{
// delay(5000);
// Serial.println("im still in the loop"); // debug line
}