Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <LiquidCrystal.h>
- #include <Time.h>
- #include <Wire.h>
- #include "LedControl.h"
- int buttonPin1 = 4;
- int buttonPin2 = 3;
- int buttonPin3 = 2;
- int buzzerPin = 13;
- int din = 16;
- int cs = 15;
- int clk = 14;
- unsigned long delaytime1=50;
- unsigned long delaytime2=50;
- int tSeconds = 10, tMinutes = 0, hours = 0;
- int centiseconds = 0, sSeconds = 0, sMinutes = 0;
- int button1Counter = 0;//allows the buttons to have multiple operations
- int button1State = 0;
- int lastButton1State = 0;
- int button2Counter = 0;
- int button2State = 0;
- int lastButton2State = 0;
- LiquidCrystal lcd (7,8,9,10,11,12);
- LedControl lc= LedControl(16,15,14,1);
- void columns() {
- for(int col=0;col<8;col++) {
- delay(delaytime2);
- lc.setColumn(0,col,B10100000);
- delay(delaytime2);
- lc.setColumn(0,col,(byte)0);
- for(int i=0;i<col;i++) {
- delay(delaytime2);
- lc.setColumn(0,col,B10100000);
- delay(delaytime2);
- lc.setColumn(0,col,(byte)0);
- }
- }
- }
- void setup()
- {
- lcd.begin (16,2);
- pinMode (buttonPin1, INPUT_PULLUP);
- pinMode (buttonPin2, INPUT_PULLUP);
- pinMode (buttonPin3, INPUT_PULLUP);
- pinMode (buzzerPin, OUTPUT);
- lc.shutdown(0, false);
- lc.setIntensity(0,8);
- lc.clearDisplay(0);
- Serial.begin (9600);
- }
- void loop()
- {
- button1State = digitalRead (buttonPin1);//this section gives the "mode button the ability to have three seperate operations
- Serial.println(button1State);
- if (button1State != lastButton1State)
- {
- if (button1State = HIGH)
- {
- button1Counter++;
- button2Counter = 1;
- delay (200);
- }
- }
- lastButton1State = button1State;
- if (button1Counter > 3)
- {
- button1Counter = 1;
- }
- button2State = digitalRead (buttonPin2);//"start/stop" button the ability to have two seperate functions
- if (button2State != lastButton2State)
- {
- if (button2State = HIGH)
- {
- button2Counter++;
- delay (200);
- }
- }
- lastButton2State = button2State;
- if (button2Counter > 2)
- {
- button2Counter = 1;
- }
- switch (button1Counter)
- {
- case 1:
- lcd.clear();
- lcd.setCursor (2,1);
- lcd.print(":");
- lcd.setCursor(5, 1);
- lcd.print(":");
- timerFunction();
- break;
- case 2: //if "Mode" is pressed again, the clock switches once more to stopwatchFunction()
- lcd.clear();
- lcd.setCursor(2, 1);
- lcd.print(":");
- lcd.setCursor(5, 1);
- lcd.print(".");
- stopwatchFunction();
- break;
- }
- }
- void timerFunction()
- {
- if(button2Counter == 2) //if "Start/Stop" is pressed, the timer counts down
- {
- static unsigned long lastTick = 0;
- if (tSeconds > 0)
- {
- if (millis() - lastTick >= 1000)
- {
- lastTick = millis();
- tSeconds--;
- lcdOutput();
- }
- }
- if (tMinutes > 0)
- {
- if (tSeconds <= 0)
- {
- tMinutes--;
- tSeconds = 60;
- }
- }
- if (hours > 0)
- {
- if (tMinutes <= 0)
- {
- hours--;
- tMinutes = 60;
- }
- }
- }
- else //if "Start/Stop" is unpressed or pressed a second time, display the current time, but don't count down
- {
- lcdOutput();
- }
- if(hours == 00 && tMinutes == 00 && tSeconds == 00) //when timer ends, the alarm goes off
- {
- button2Counter = 1;
- columns();
- while(digitalRead(buttonPin3) == HIGH)//the alarm will only go off until "Restart" is pressed
- {
- lcd.setCursor(0, 1);
- lcd.print("00:00:00");
- digitalWrite(buzzerPin, HIGH);
- delay(100);
- digitalWrite(buzzerPin, LOW);
- delay(100);
- }
- if(digitalRead(buttonPin3) == LOW) //when "Restart" is pressed, the timer resets
- {
- hours = 0; //this part also
- tMinutes = 0;
- tSeconds = 10;
- lcdOutput();
- }
- }
- if(digitalRead(buttonPin3) == LOW && button2Counter == 1) //resets the timer when "Restart" button is pressed, as long as the timer is NOT running
- {
- hours = 0; //this part also must be changed when the timer is altered, to match the initial time
- tMinutes = 0;
- tSeconds = 10;
- delay(10);
- }
- }
- void stopwatchFunction()
- {
- lcd.setCursor(6, 1);
- printDigits(centiseconds);
- lcd.setCursor(3, 1);
- printDigits(sSeconds);
- lcd.setCursor(0, 1);
- printDigits(sMinutes);
- if(button2Counter == 2) //if the "Start/Stop" button is pressed, the time begins running
- {
- delay(5);
- centiseconds++;
- }
- if(digitalRead(buttonPin3) == LOW && button2Counter == 1) //if the "Restart" button is pressed, as long as the timer is paused, the stopwatch resets to 00:00.00
- {
- centiseconds = 0;
- sSeconds = 0;
- sMinutes = 0;
- }
- if(centiseconds == 100)
- {
- centiseconds = 0;
- sSeconds++;
- }
- if(sSeconds == 60)
- {
- sSeconds = 0;
- sMinutes++;
- }
- }
- void lcdOutput()
- {
- lcd.setCursor(0, 1);
- printDigits(hours);
- lcd.setCursor(3, 1);
- printDigits(tMinutes);
- lcd.setCursor(6, 1);
- printDigits(tSeconds);
- delay(100);
- }
- void printDigits(int digits)
- {
- if(digits < 10)
- {
- lcd.print("0");
- lcd.print(digits);
- }
- else
- {
- lcd.print(digits);
- }
- }
Add Comment
Please, Sign In to add comment