/* This Arduino code drives 4 separate 7 segment common cathode LED displays Via a pushbutton switch, it starts a timer display from 00:00 (mm:ss) to 59:59. The button also pauses the timer. Holding the button in for greater than 2 seconds resets There is a built-in debounce to limit bouncing of pulses via the pushbutton. Create by Chris in Feb 2015 Also now trying to include an LDR to control the intensity of the digits 7 Segment display pin layout is as follows: A ------- | | F| |B | G | -------- | | E| |C | D | -------- DP */ //-------------------------------------------------------------------------------------------- int A = LOW; // Set to 1 for common anode digits. Set to 0 if common cathode int B = HIGH; // Set to 0 for common anode digits. Set to 1 if common cathode int ldrIn = 14; // Analog input pin A0. Attach to first leg of LDR and 10K to GND int ledPin = 16; // Analog output A2. One leg to the "test" LED int value = 0; // A variable used in controlling the light intensity value int buttonChange = 15; // Analog in pin (A1) used to pause, restart and reset timer // Other end of led to GND via a 10K resistor int state = LOW; // The current state of the output pin int reading; // The current reading of the input pin int previous = HIGH; // The previous reading from the input pin int check = 0; // Used to pause the clock when the button is preassed - whilst reset int delayMp = 2; // Used to delay the muliplexing int sec_ones = 0; // Used to indicate what number the digit is set to int sec_tens = 0; // Used to increment the tens unit int min_ones = 0; // Used to increment the min_ones units int min_tens = 0; // Used to increment the min_tens units long time1 = 0; // Used in delaying for debouncing of pushbutton long time2 = 0; // Used in delaying the increment clock time long secondMicro = 996000; // One second increment clock time long debounce = 500; // Debounce time - longer than usual so it works for me unsigned long currentMillis = 0; // unsigned long previousMillis = 0; // int stepMultiplex = 1; // Variable used to step through each segment int reset = 2000; // Duration required for button press to reset time int firstTime = 1; // Used in recording button press duration unsigned long startTime = 0; // Used in recording button press duration unsigned long pressTime = 0; // Used in recording button press duration // the following array is used to define each led common byte commons [4] = {6,9,10,11}; // pins to each digit common (these are PWM outputs) // the following array is used to define each led segment for number 0 to 9 byte sevenSegmentPin[7] = {2,3,4,5,7,8,12}; byte sevenSegment[10][7] = { {B,B,B,B,B,B,A}, // this is 0 {A,B,B,A,A,A,A}, // this is 1 {B,B,A,B,B,A,B}, // this is 2 {B,B,B,B,A,A,B}, // this is 3 {A,B,B,A,A,B,B}, // this is 4 {B,A,B,B,A,B,B}, // this is 5 {B,A,B,B,B,B,B}, // this is 6 {B,B,B,A,A,A,A}, // this is 7 {B,B,B,B,B,B,B}, // this is 8 {B,B,B,A,A,B,B}, // this is 9 }; void setup() //-------------------------------------------------- { Serial.begin(9600); pinMode(ledPin,OUTPUT); for (byte i=0; i<7; i++) pinMode(sevenSegmentPin[i],OUTPUT); // this sets pins 2,3,4,5,6,8 and 12 as outputs for(byte i=0; i<4; i++) pinMode(commons[i], OUTPUT); // this sets pins 6,9, 10 and 11 as outputs for (byte i=0; i<7; i++) { digitalWrite(sevenSegmentPin[i], sevenSegment[0][i]); // this sets pins 2,3,4,5,6 and 8 as 0 } } // This is code for displaying the incremental digit for the "sec_ones" void sevenSegWrite_1(byte sec_ones) { for (byte i=0; i<7; i++) { digitalWrite(sevenSegmentPin[i], sevenSegment[sec_ones][i]); } } // This is code for displaying the incremental digit for the "sec_tens" void sevenSegWrite_2(byte sec_tens) { for (byte i=0; i<7; i++) { digitalWrite(sevenSegmentPin[i], sevenSegment[sec_tens][i]); } } // This is code for displaying the incremental digit for the "min_ones" void sevenSegWrite_3(byte min_ones) { for (byte i=0; i<7; i++) { digitalWrite(sevenSegmentPin[i], sevenSegment[min_ones][i]); } } // This is code for displaying the incremental digit for the "min_tens" void sevenSegWrite_4(byte min_tens) { for (byte i=0; i<7; i++) { digitalWrite(sevenSegmentPin[i], sevenSegment[min_tens][i]); } } void loop() //-------------------------------------------------- { value = analogRead(ldrIn); // Read the LDR analogue value on analog pin and // store the value between 0 and 1023 value = constrain (value, 900, 1010); // Constrain the value between 900 and 1010 value = map (value, 900, 1010, 0, 255); // The digital pin outputs values from 0 to 255 // so convert the value received in the above line analogWrite (ledPin, value); // Output the value on the digital pin reading = digitalRead(buttonChange); // Check the state of the button currentMillis = millis(); // Set variable for multiplexing timing // Used to toggle starting and pausing the clock... if (reading == HIGH && previous == LOW && millis() - time1 > debounce) { if (state == HIGH) { state = LOW; time1 = millis (); } else { state = HIGH; time1 = millis(); } } previous = reading; // Used to check if the button is pressed long enough to reset the time if (reading == HIGH) { if (firstTime == 1) { startTime = millis(); firstTime = 0; } pressTime = millis () - startTime; } else if (firstTime == 0) { firstTime = 1; } if (pressTime > 2000) { state = LOW; sec_ones=0; sec_tens=0; min_ones=0; min_tens=0; time1 = 0; } //------------------------------------------------------------------- if(stepMultiplex == 1 && (currentMillis - previousMillis) >= delayMp) { analogWrite(commons [0],1023); previousMillis=currentMillis; sevenSegWrite_1(sec_ones); analogWrite(commons [3],value); stepMultiplex=2; } if(stepMultiplex == 2 && (currentMillis - previousMillis) >= delayMp) { analogWrite(commons [3],1023); previousMillis=currentMillis; sevenSegWrite_2(sec_tens); analogWrite(commons [2],value); stepMultiplex=3; } if(stepMultiplex == 3 && (currentMillis - previousMillis) >= delayMp) { analogWrite(commons [2],1023); previousMillis=currentMillis; sevenSegWrite_3(min_ones); analogWrite(commons [1],value); stepMultiplex=4; } if(stepMultiplex == 4 && (currentMillis - previousMillis) >= delayMp) { analogWrite(commons [1],1023); previousMillis=currentMillis; sevenSegWrite_4(min_tens); analogWrite(commons [0],value); stepMultiplex=1; } // The next few lines are used to advance the clock if (micros() - time2 > secondMicro && state == HIGH) { check=1; //this is used to pause the clock while the button is pressed if (check == 1 & reading == LOW) { sec_ones++; check=0; } if(sec_ones >9) { sec_ones=0; sec_tens++; } if(sec_tens>5) { sec_tens=0; min_ones++; } if(min_ones>9) { min_ones=0; min_tens++; } if(min_tens>5) { sec_ones=0; sec_tens=0; min_ones=0; min_tens=0; } time2 = micros(); } }