Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <LedControl.h> // Library for LED control with MAX72XX
- #include <Bounce2.h> // Library for Bounce of switches
- /*
- Pins of Arduino Nano for LedControl:
- Pin #7 is connected to DataIn (DIN)
- Pin #8 is connected to CLK (CLK)
- Pin #9 is connected to LOAD (CS)
- There is only one display with MAX72XX
- */
- Bounce SW2 = Bounce(); // Define Bounce to read StartStop switch
- Bounce SW1 = Bounce(); // Define Bounce to read StartStop switch
- LedControl lc = LedControl(7, 8, 9, 1); // LedControl(dataPin, clkPin, csPin, numDevices)
- int pinStartStop = 4; // Start-Stop Pin
- int pin2 = 5;
- bool setting = true;
- int curPos = 0;
- int switch_counter = 0;
- unsigned long current_seconds = 0;
- int hours_dec = 0;
- int hours_int = 0;
- int minutes_dec = 0;
- int minutes_int = 0;
- int seconds_dec = 0;
- int seconds_int = 0;
- void setup() {
- Serial.begin(9600);
- pinMode (pinStartStop, INPUT_PULLUP);
- pinMode (pin2, INPUT_PULLUP);
- lc.shutdown(0, false); // The MAX72XX is in power-saving mode on startup, we have to do a wakeup call
- lc.setIntensity(0, 2); // Set the brightness of display between 0 and 15
- lc.clearDisplay(0); // Clear the display
- //print_milis(0);
- Serial.begin(9600);
- pinMode (pinStartStop, INPUT_PULLUP);
- SW1.attach(pinStartStop); // Sets the pin (Internal Pull-Up) & matches the internal state
- SW1.interval(3); // Sets the debounce time in milliseconds
- pinMode(3, OUTPUT); // sets the digital pin 3 as output
- SW2.attach(pin2); // Sets the pin (Internal Pull-Up) & matches the internal state
- SW2.interval(2); // Sets the debounce time in milliseconds
- pinMode(2, OUTPUT); // sets the digital pin 2 as output
- }
- void setTime(int hours, int minutes, int seconds){
- lc.setDigit(0, 7, 0, false);
- lc.setDigit(0, 6, 0, false);
- lc.setDigit(0, 5, hours / 10, false);
- lc.setDigit(0, 4, hours % 10, true);
- lc.setDigit(0,3, minutes / 10, false);
- lc.setDigit(0,2, minutes % 10, true);
- lc.setDigit(0,1, seconds / 10, false);
- lc.setDigit(0,0,seconds % 10, false);
- }
- void loop() {
- while(true){
- // SETTING UP TIME
- while(setting){
- SW1.update();
- SW2.update();
- if(SW1.fell()){
- switch_counter++;
- if(curPos < 5)
- curPos++;
- else
- {
- curPos = 0;
- }
- }
- if(SW2.fell()){
- switch_counter = 0;
- switch(curPos){
- case 0:
- {
- if (seconds_int < 9){
- seconds_int++;}
- else
- seconds_int = 0;
- break;
- }
- case 1:
- {
- if (seconds_dec < 5){
- seconds_dec++;}
- else
- seconds_dec = 0;
- break;
- }
- case 2:
- {
- if (minutes_int < 9){
- minutes_int++;}
- else
- minutes_int = 0;
- break;
- }
- case 3:
- {
- if (minutes_dec < 5){
- minutes_dec++;}
- else
- minutes_dec = 0;
- break;
- }
- case 4:
- {
- if (hours_int < 9 && hours_dec < 2){
- hours_int++;}
- else if (hours_int < 3 && hours_dec == 2){
- hours_int++;}
- else
- hours_int = 0;
- break;
- }
- case 5:
- {
- if (hours_dec < 2){
- hours_dec++;}
- else
- hours_dec = 0;
- break;
- }
- }
- }
- setTime(hours_dec * 10 + hours_int, minutes_dec * 10 + minutes_int, seconds_dec * 10 + seconds_int);
- if(switch_counter > 6)
- {
- current_seconds = seconds_int + seconds_dec * 10 + minutes_int * 60 + minutes_dec * 600 + hours_int * 3600 + hours_dec * 36000;
- setting = false;
- switch_counter=0;
- }
- }
- while(true){
- current_seconds++;
- if (current_seconds > 86399)
- current_seconds = 0;
- unsigned long hours = current_seconds/3600;
- unsigned long minutes = (current_seconds - hours * 3600) / 60;
- unsigned long seconds = current_seconds - hours * 3600 - minutes * 60;
- Serial.println(current_seconds);
- Serial.println(hours);
- Serial.println(minutes);
- Serial.println(seconds);
- Serial.println("---------------");
- SW1.update();
- if(SW1.fell())
- {
- setting=1;
- break;
- }
- setTime(hours, minutes, seconds);
- delay(998);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement