// chess clock controller Lewis // 26/05/2013 // updates 19/4/2019 int ANALOG_HIGH = 1023; int ANALOG_LOW = 0; int iResetInput = 0; boolean inc = false; boolean firstIN1 = true; boolean firstIN2 = true; int x = 5; int a = 5; int b = 0; int c = 0; int d = 0; int e = 5; int f = 0; int g = 0; int h = 0; //switched int numToShow[] = {e,f,g,h,a,b,c,d}; // Setup void setup() { // Serial.begin(9600); // Serial.println("--- hello!! ---"); // Set up LED columns as output // for(int pin = 0; pin < 6 ; pin++ ){ pinMode(2,INPUT); // rocker 1 pinMode(3,OUTPUT); // display segment pinMode(4,OUTPUT);// display segment pinMode(5,OUTPUT);// display segment pinMode(6,OUTPUT);// display segment pinMode(7,OUTPUT);// display segment pinMode(8,OUTPUT);// display segment pinMode(9,OUTPUT);// display segment pinMode(10,OUTPUT); // common annode display 1 pinMode(11,OUTPUT); // rocker2 pinMode(12,OUTPUT); // display segment (dot) pinMode(13,OUTPUT); // common annode display 1 analogWrite(A0,ANALOG_LOW); // A0 common annode display 1 analogWrite(A1,ANALOG_LOW); // A1 common annode display 1 analogWrite(A2,ANALOG_LOW); // A2 common annode display 2 analogWrite(A3,ANALOG_LOW); // A3 common annode display 2 analogWrite(A4,ANALOG_LOW); // A4 common annode display 2 analogWrite(A5,ANALOG_LOW); // A5 common annode display 2 // rocker1 digitalWrite(13,LOW); digitalWrite(10,LOW); digitalWrite(0,LOW); } // The Loop void loop() { boolean changed = 0; int lastStateP1 = 0; int lastStateP2 = 0; int sensorValueP1 = 0; int sensorValueP2 = 0; int buttonState2 = 0; int buttonState11 = 0; for (int iInd=0; iInd <= 5000; iInd++) { int resetState[] = {1,1,1,1,1,1,1,1}; int numToShow1[] = {2,2,2,2,2,2,2,2}; int zeros[] = {0,0,0,0,0,0,0,0}; int nnn=0; while (1==1) { iResetInput = analogRead(7); // read the input pin if (iResetInput>500) { // reest request, enter reset mode resetMode(); iResetInput = 0; } buttonState2 = digitalRead(2); while (buttonState2== HIGH ) { if (firstIN1 && inc) { firstIN1= false; firstIN2 = true; // Serial.println("add 5 secs to L"); // efgh abcd if (g<5) { g=g+5; } else { f++; g=g-5; if (f>5) { f=0; e++; } } } numToShow[7] = h; numToShow[6] = g; numToShow[5] = f; numToShow[4] = e; numToShow[3] = d; numToShow[2] = c; numToShow[1] = b; numToShow[0] = a; shownumbers (numToShow,5,false); buttonState2 = digitalRead(2); d--; if (d<0) {d=9;c--;} if (c<0) {c=9;b--;} if (b<0) {b=5;a--;} if (a<0) { a=0;b=0;c=0;d=0; numToShow[7] = h; numToShow[6] = g; numToShow[5] = f; numToShow[4] = e; numToShow[3] = d; numToShow[2] = c; numToShow[1] = b; numToShow[0] = a; // Send the buzzer signal shownumbers (numToShow,5,true); // wait for the reset while (analogRead(7)<500) { a=0;b=0;c=0;d=0; numToShow[7] = h; numToShow[6] = g; numToShow[5] = f; numToShow[4] = e; numToShow[3] = d; numToShow[2] = c; numToShow[1] = b; numToShow[0] = a; // reest request, enter reset mode shownumbers (numToShow,5,false); } resetMode(); } } buttonState11 = analogRead(6); while (buttonState11>500 ) { if (firstIN2 && inc) { firstIN2= false; firstIN1 = true; // Serial.println("add 5 secs to R"); // efgh abcd if (c<5) { c=c+5; } else { b++; c=c-5; if (b>5) { b=0; a++; } } } numToShow[7] = h; numToShow[6] = g; numToShow[5] = f; numToShow[4] = e; numToShow[3] = d; numToShow[2] = c; numToShow[1] = b; numToShow[0] = a; shownumbers (numToShow,5,false); buttonState11 = analogRead(6); h--; if (h==-1) {h=9;g--;} if (g==-1) {g=9;f--;} if (f==-1) {f=5;e--;} if (e==-1) { f=0;g=0;f=0;e=0; numToShow[7] = h; numToShow[6] = g; numToShow[5] = f; numToShow[4] = e; numToShow[3] = d; numToShow[2] = c; numToShow[1] = b; numToShow[0] = a; // Send the buzzer signal shownumbers (numToShow,5,true); // wait for the reset while (analogRead(7)<500) { h=0;g=0;f=0;e=0; numToShow[7] = h; numToShow[6] = g; numToShow[5] = f; numToShow[4] = e; numToShow[3] = d; numToShow[2] = c; numToShow[1] = b; numToShow[0] = a; // reest request, enter reset mode shownumbers (numToShow,5,false); } resetMode(); } } shownumbers (numToShow ,6,false); } } } // Reset and set a new time if needed void resetMode() { a = 5; b = 0; c = 0; d = 0; e = 5; f = 0; g = 0; h = 0; x = 5; while ((analogRead(6)<500 ) && (digitalRead(2)== LOW )) { numToShow[7] = d; numToShow[6] = c; numToShow[5] = b; numToShow[4] = a; numToShow[3] = d; numToShow[2] = c; numToShow[1] = b; numToShow[0] = a; shownumbers (numToShow,5,false); iResetInput = analogRead(7); // read the input pin if (iResetInput>500) { if (x == 11) // show 10 p(lus) 5 (secs) { a=10; b=50; c = 5; d = 0; inc = true; x++; } else if (x == 5) { a=5; b=50; c = 5; d = 0; inc = true; x++; } else if (x == 12) { a=1; b=0; c = 0; d = 0; inc = false; x=1; } else { a++; b=0; c = 0; d = 0; x++; inc = false; } iResetInput = 0; delay(20); } } // set numbers properly here =========================== if (x>10) { x=11; // a=10; // Serial.println("add increment"); } if (x>5) { x--; } a=x; b=0; c=0; d=0; e=x; f=0; g=0; h=0; numToShow[7] = 0; numToShow[6] = 0; numToShow[5] = 0; numToShow[4] = x; numToShow[3] = 0; numToShow[2] = 0; numToShow[1] = 0; numToShow[0] = x; } int shownumbers(int theNumbers[], int framelength, boolean sendBeeps) { int cathodes[] = {A4,A3,A5,A2,A1,A0,10,13}; // to switch displays (L / R) ================================ int iTemp[] = {0,theNumbers[4],theNumbers[5],theNumbers[6],0,theNumbers[0],theNumbers[1],theNumbers[2]}; if (theNumbers[4] == 10 ) { iTemp [0]= 1; iTemp [1] = 0; } if (theNumbers[0] == 10 ) { iTemp [4]= 1; iTemp [5] = 0; } theNumbers = iTemp; // ============================================================ if (sendBeeps) // different sound for other side { digitalWrite(10,HIGH); digitalWrite(13,HIGH); analogWrite(A0,ANALOG_HIGH); analogWrite(A1,ANALOG_HIGH); analogWrite(A2,ANALOG_HIGH); analogWrite(A3,ANALOG_HIGH); analogWrite(A4,ANALOG_HIGH); analogWrite(A5,ANALOG_HIGH); digitalWrite(3,HIGH); digitalWrite(4,HIGH); digitalWrite(5,HIGH); digitalWrite(6,LOW); digitalWrite(7,HIGH); digitalWrite(8,HIGH); digitalWrite(9,HIGH); int x = 0; int tune[] = {150,100,100,200,200,800}; for (int iN = 0; iN<6; iN++) { digitalWrite(11,HIGH); delay(tune[iN]); digitalWrite(11,LOW); delay(50); } return 0; } // send beeps end for (int cnt = 0; cnt <=framelength; cnt++) { for (int dgt=0; dgt <= 7; dgt++) { delay(2); // Set all the common annodes back low again digitalWrite(13,LOW); digitalWrite(10,LOW); analogWrite(A3,ANALOG_LOW); analogWrite(A4,ANALOG_LOW); analogWrite(A5,ANALOG_LOW); analogWrite(A2,ANALOG_LOW); analogWrite(A0,ANALOG_LOW); analogWrite(A1,ANALOG_LOW); if ((dgt<=1)) { digitalWrite(cathodes[dgt],HIGH); } else { analogWrite(cathodes[dgt],ANALOG_HIGH); } if ((dgt==5)||dgt==1)// just light the decimal point after the 2nd digit on each display { digitalWrite(12,LOW); } else { digitalWrite(12,HIGH); } switch (theNumbers [dgt]) { case 0: digitalWrite(3,LOW); digitalWrite(4,LOW); digitalWrite(5,LOW); digitalWrite(6,HIGH); digitalWrite(7,LOW); digitalWrite(8,LOW); digitalWrite(9,LOW); break; case 1: digitalWrite(3,HIGH); digitalWrite(4,HIGH); digitalWrite(5,LOW); digitalWrite(6,HIGH); digitalWrite(7,HIGH); digitalWrite(8,HIGH); digitalWrite(9,LOW); break; case 2: digitalWrite(3,LOW); digitalWrite(4,LOW); digitalWrite(5,LOW); digitalWrite(6,LOW); digitalWrite(7,LOW); digitalWrite(8,HIGH); digitalWrite(9,HIGH); break; case 3: digitalWrite(3,LOW); digitalWrite(4,HIGH); digitalWrite(5,LOW); digitalWrite(6,LOW); digitalWrite(7,LOW); digitalWrite(8,HIGH); digitalWrite(9,LOW); break; case 4: digitalWrite(3,HIGH); digitalWrite(4,HIGH); digitalWrite(5,LOW); digitalWrite(6,LOW); digitalWrite(7,HIGH); digitalWrite(8,LOW); digitalWrite(9,LOW); break; case 5: digitalWrite(3,LOW); digitalWrite(4,HIGH); digitalWrite(5,HIGH); digitalWrite(6,LOW); digitalWrite(7,LOW); digitalWrite(8,LOW); digitalWrite(9,LOW); break; case 6: digitalWrite(3,LOW); digitalWrite(4,LOW); digitalWrite(5,HIGH); digitalWrite(6,LOW); digitalWrite(7,LOW); digitalWrite(8,LOW); digitalWrite(9,LOW); break; case 7: digitalWrite(3,HIGH); digitalWrite(4,HIGH); digitalWrite(5,LOW); digitalWrite(6,HIGH); digitalWrite(7,LOW); digitalWrite(8,HIGH); digitalWrite(9,LOW); break; case 8: digitalWrite(3,LOW); digitalWrite(4,LOW); digitalWrite(5,LOW); digitalWrite(6,LOW); digitalWrite(7,LOW); digitalWrite(8,LOW); digitalWrite(9,LOW); break; case 9: digitalWrite(3,LOW); digitalWrite(4,HIGH); digitalWrite(5,LOW); digitalWrite(6,LOW); digitalWrite(7,LOW); digitalWrite(8,LOW); digitalWrite(9,LOW); break; case 10://special 10 character digitalWrite(3,LOW); digitalWrite(4,HIGH); digitalWrite(5,LOW); digitalWrite(6,LOW); digitalWrite(7,LOW); digitalWrite(8,LOW); digitalWrite(9,HIGH); break; case 50://'p'character meaning plus for increment digitalWrite(3,HIGH); digitalWrite(4,LOW); digitalWrite(5,LOW); digitalWrite(6,LOW); digitalWrite(7,LOW); digitalWrite(8,LOW); digitalWrite(9,HIGH); break; case 51:// all OFF digitalWrite(3,HIGH); digitalWrite(4,HIGH); digitalWrite(5,HIGH); digitalWrite(6,HIGH); digitalWrite(7,HIGH); digitalWrite(8,HIGH); digitalWrite(9,HIGH); break; } } } }