// setting integer variables to keep track of the number of seconds, tens of seconds, minutes, and tens of minutes that have passed // setting a float variable counts to keep track of the number of oscillations measured from the circuit int secondsOnes = 0; int secondsTens = 0; int minutesOnes = 0; int minutesTens = 0; float counts = 0; // defining the setup function to run once at the start void setup() { // setting the pins 13, 12, 11, 10, 9, 8, and 7 as outputs for the seven-segment display pinMode(13, OUTPUT); pinMode(12, OUTPUT); pinMode(11, OUTPUT); pinMode(10, OUTPUT); pinMode(9, OUTPUT); pinMode(8, OUTPUT); pinMode(7, OUTPUT); // setting pin 2 as an input to track oscillations from the circuit pinMode(2, INPUT); // calling the setSegment() function for segments 0, 1, 2, and 3 with value 0 to zero the display at the start setSegment(0, 0); setSegment(1, 0); setSegment(2, 0); setSegment(3, 0); } // setting up the main loop of the program to run repeatedly void loop() { // setting an if statement to check when a high-voltage signal reaches Pin 2 then increasing counts by 1 if (digitalRead(2) == HIGH){ counts ++; } // setting a second if statement to check when the number of counts has reached the threshold for 1 second. // This value may differ based on latency in your circuit // The ones digit of the seconds is then increased by 1 and the setTime() function is called to set the time // counts is then set back to zero to count the next second if (counts == 45000){ secondsOnes ++; setTime(); counts = 0; } } // defining the setTime() function that sets the time using the current values in each of the time variables from above void setTime() { // making an if statement that increases the tens digit in the seconds if the ones digit is equal to 10 then resets the ones digit to 0 if (secondsOnes >= 10) { secondsTens ++; secondsOnes = 0; } // making an if statement that increases the ones digit in the minutes if the tens digit of seconds is equal to 6 then resets the tens digit to 0 if (secondsTens >= 6) { minutesOnes ++; secondsTens = 0; } // making an if statement that increases the tens digit in the minutes if the ones digit is equal to 10 then resets the ones digit to 0 if (minutesOnes >= 10) { minutesTens ++; minutesOnes = 0; } // making an if statement that resets the tens digit of the minutes if the tens digit of seconds is equal to 6 if (minutesTens >= 6) { minutesTens = 0; } // running the setSegment() function for each of the display segments (0, 1, 2, 3) with values for the tens of minutes, minutes, tens of seconds, and seconds // values respectively setSegment(0, minutesTens); setSegment(1, minutesOnes); setSegment(2, secondsTens); setSegment(3, secondsOnes); } // defining the setSegment() function that takes in the segment number, s, and the time value, v, as its arguments and sets the seven segment outputs\ // to display the current time void setSegment(int s, int v){ // setting pin 13 to high to enable a change in the values on the seven segment digitalWrite(13, HIGH); // having multiple if/else if statements to check which display we want to change // if s = 0 we change the left-most display (tens of minutes) // if s = 1 we change the second from the left display (minutes) // if s = 2 we change the second from the right display (tens of seconds) // if s = 3 we change the right-most display (seconds) if (s == 0){ digitalWrite(12, HIGH); digitalWrite(11, HIGH); } else if (s == 1){ digitalWrite(12, LOW); digitalWrite(11, HIGH); } else if (s == 2){ digitalWrite(12, HIGH); digitalWrite(11, LOW); } else if (s == 3){ digitalWrite(12, LOW); digitalWrite(11, LOW); } // multiple if/else if statements that check what value we assign to the chosen display then setting the pins 10, 9, 8, and 7 to some combination of highs // and lows to display the correct value on the display if (v == 0){ digitalWrite(10, LOW); digitalWrite(9, LOW); digitalWrite(8, LOW); digitalWrite(7, LOW); } else if (v == 1){ digitalWrite(10, HIGH); digitalWrite(9, LOW); digitalWrite(8, LOW); digitalWrite(7, LOW); } else if (v == 2){ digitalWrite(10, LOW); digitalWrite(9, HIGH); digitalWrite(8, LOW); digitalWrite(7, LOW); } else if (v == 3){ digitalWrite(10, HIGH); digitalWrite(9, HIGH); digitalWrite(8, LOW); digitalWrite(7, LOW); } else if (v == 4){ digitalWrite(10, LOW); digitalWrite(9, LOW); digitalWrite(8, HIGH); digitalWrite(7, LOW); } else if (v == 5){ digitalWrite(10, HIGH); digitalWrite(9, LOW); digitalWrite(8, HIGH); digitalWrite(7, LOW); } else if (v == 6){ digitalWrite(10, LOW); digitalWrite(9, HIGH); digitalWrite(8, HIGH); digitalWrite(7, LOW); } else if (v == 7){ digitalWrite(10, HIGH); digitalWrite(9, HIGH); digitalWrite(8, HIGH); digitalWrite(7, LOW); } else if (v == 8){ digitalWrite(10, LOW); digitalWrite(9, LOW); digitalWrite(8, LOW); digitalWrite(7, HIGH); } else if (v == 9){ digitalWrite(10, HIGH); digitalWrite(9, LOW); digitalWrite(8, LOW); digitalWrite(7, HIGH); } // setting pin 13 back to low to stop changes from being made digitalWrite(13, LOW); }