Hirdetés

Keresés

Új hozzászólás Aktív témák

  • lajbi30

    csendes tag

    válasz lajbi30 #1308 üzenetére

    No itt egy működő óra!

    // simple sketch to display a digital Alarm clock on an LCD keypad shield without RTC
    // see the LiquidCrystal documentation for more info on this
    //

    /*
    The circuit:
    * LCD RS (Data or Signal Display Selection) pin to digital pin 8
    * LCD Enable pin to digital pin 9
    * LCD D4 pin to digital pin 4
    * LCD D5 pin to digital pin 5
    * LCD D6 pin to digital pin 6
    * LCD D7 pin to digital pin 7
    * LCD R/W pin to ground
    * 10K resistor:
    * ends to +5V and ground
    * wiper to LCD VO pin (pin 3)
    * Backlit Control to Digital 10
    * Button (select, up, right, down and left) to Analog 0
    */

    #include <LiquidCrystal.h>
    // include header file for time function
    #include <Time.h>
    /*
    You can download the library from here:

    https://github.com/JChristensen/Timer
    */

    // Button defination for LCD Keypad shield
    #define btnRIGHT 0 // Okay
    #define btnUP 1 // inc
    #define btnDOWN 2 // dec
    #define btnLEFT 3 // Select
    #define btnSELECT 4 // Menu
    #define btnNONE 5
    // Observed values:
    // NONE: 1023
    // SELECT: 723
    // LEFT: 481
    // DOWN: 307
    // UP: 133
    // RIGHT: 0

    #define beeper A1 // Alarm buzzer
    #define shortBeep 100
    #define longBeep 500

    #define TIME_MSG_LEN 11 // time sync to PC is HEADER followed by unix time_t as ten ascii digits
    #define TIME_HEADER 'T' // Header tag for serial time sync message
    #define TIME_REQUEST 7 // ASCII bell character requests a time sync message

    // define variables
    int lcd_key = 0;
    int adc_key_in = 0;
    int lastDay = 0;
    int lastMonth = 0;
    int lastYear = 0;
    int lastHour = 0;
    int lastMinute = 0;
    int movementTimer = 0;
    int menuOptions = 4;
    int menuOption = 0;
    int alarmHours = 0;
    int alarmMinutes = 0;
    bool alarmSet = 0;
    bool backLightOn = 1;
    int fadeValue = 255;

    // define constants
    const int backLight = 10; // pin 10 will control the LCD backlight

    LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

    void setup()
    {
    pinMode(backLight, OUTPUT);
    pinMode(beeper, OUTPUT);
    digitalWrite(backLight, HIGH); // turn backlight on. Replace 'HIGH' with 'LOW' to turn it off.
    Serial.begin(9600);
    setSyncProvider( requestSync); //set function to call when sync required
    }

    void loop(){
    if(Serial.available() )
    {
    processSyncMessage();
    }
    else
    if(timeStatus()== timeNotSet)
    {
    // setTime(1356210000);
    setTime(8, 0, 0, 20, 1, 2013); // hour, min, sec, day, month, year
    // Replace this with the most current time
    }

    if(timeStatus()!= timeNotSet)
    {
    digitalWrite(13,timeStatus() == timeSet); // on if synced, off if needs refresh
    digitalClockDisplay();
    }

    for (int i = 0; i < 9000; i++)
    {
    button_loop(); //check for button pushed
    }
    }

    void printDigits(byte digits){
    // utility function for digital clock display: prints preceding colon and leading 0
    lcd.print(":");
    if(digits < 10)
    lcd.print('0');
    lcd.print(digits,DEC);
    }

    void digitalClockDisplay(){
    //lcd.clear();
    lcd.begin(16,2);
    lcd.setCursor(3,0);
    lcd.print(" ");
    lcd.setCursor(3,1);
    lcd.print(" ");
    lcd.setCursor(3,0);
    if(day() <10)
    lcd.print('0');
    lcd.print(day(),DEC);
    lcd.print("/");

    if(month() <10)
    lcd.print('0');
    lcd.print(month(),DEC);
    lcd.print("/");
    lcd.print((year()));

    //lcd.print(" ");
    if(hour() <10)
    lcd.setCursor(5,1);
    lcd.setCursor(4,1);

    // digital clock display of current time
    lcd.print(hour(),DEC);
    printDigits(minute());
    printDigits(second());
    // for time set
    lastDay = day();
    lastMonth = month();
    lastYear = year();
    lastHour = hour();
    lastMinute = minute();

    //check for alarm
    if (alarmSet)
    {
    // alarm set
    if (alarmHours == lastHour && alarmMinutes == lastMinute)
    {
    //sound alarm
    setOffAlarm();
    }
    }
    }

    void processSyncMessage() {
    // if time sync available from serial port, update time and return true
    while(Serial.available() >= TIME_MSG_LEN ){ // time message consists of a header and ten ascii digits
    char c = Serial.read() ;
    Serial.print(c);
    if( c == TIME_HEADER ) {
    time_t pctime = 0;
    for(int i=0; i < TIME_MSG_LEN -1; i++){
    c = Serial.read();
    if( c >= '0' && c <= '9'){
    pctime = (10 * pctime) + (c - '0') ; // convert digits to a number
    }
    }
    setTime(pctime); // Sync Arduino clock to the time received on the serial port
    }
    }
    }

    time_t requestSync()
    {
    Serial.write(TIME_REQUEST);
    return 0; // the time will be sent later in response to serial mesg
    }

    void button_loop()
    {
    int button = read_LCD_buttons();
    if (button == btnSELECT)
    {
    timedBeep(shortBeep,1);
    selectMenu();
    }
    if (button == btnDOWN)
    {
    fadeValue = fadeValue -5;
    if (fadeValue < 5) { fadeValue = 0; }
    analogWrite (backLight, fadeValue);
    delay (100);
    }
    if (button == btnUP)
    {
    fadeValue = fadeValue +5;
    if (fadeValue > 254) { fadeValue = 255; }
    analogWrite (backLight, fadeValue);
    delay (100);
    }
    }

    void selectMenu()
    {
    int button = 0;
    menuOption = 1;
    lcdClear();
    lcd.print("Minute Timer");

    while (menuOption <= menuOptions)
    {
    button = read_LCD_buttons();
    if (button == btnSELECT)
    {
    timedBeep(shortBeep,1);
    menuOption++;

    if (menuOption == 2)
    {
    lcdClear();
    // clearAlarm feature
    lcd.print("Set/Clear Alarm");
    }
    if (menuOption == 3)
    {
    lcdClear();
    lcd.print("Set Date/Time");
    }
    if (menuOption == 4)
    {
    lcdClear();
    lcd.print("Stop Watch Timer");
    }
    }

    if (button == btnLEFT)
    {
    if (menuOption == 1)
    {
    timedBeep(shortBeep,1);
    minuteTimer();
    return;
    }
    if (menuOption == 2)
    {
    timedBeep(shortBeep,1);
    // clearAlarm feature
    //check for existing alarm
    if (alarmSet)
    {
    clearAlarm();
    }
    else
    {
    setAlarm();
    }
    return;
    }
    if (menuOption == 3)
    {
    timedBeep(shortBeep,1);
    // setDateTime feature
    setDateTime();
    return;
    }
    if (menuOption == 4)
    {
    timedBeep(shortBeep,1);
    stopwatch_time_counter();
    return;
    }
    }
    }
    }

    // clearAlarm feature
    void clearAlarm()
    {
    int button = 0;
    bool clearIt = true;

    lcdClear();
    lcd.print("Alarm Set For");
    lcd.setCursor(0,1);
    lcd.print(alarmHours);
    lcd.print(":");
    lcd.print(alarmMinutes);
    delay(2000);
    lcdClear();
    lcd.print("Clear Alarm?");
    lcd.setCursor(0,1);
    lcd.print("Yes");

    while (button != btnSELECT)
    {
    button = read_LCD_buttons();
    if (button == btnUP)
    {
    timedBeep(shortBeep,1);
    clearIt = !clearIt;
    }
    if (button == btnDOWN)
    {
    timedBeep(shortBeep,1);
    clearIt = !clearIt;
    }
    if (button == btnRIGHT)
    {
    timedBeep(shortBeep,1);
    alarmSet = !clearIt;
    if (clearIt)
    {
    lcdClear();
    timedBeep(shortBeep,2);
    lcd.print("Alarm Cleared!");
    delay(2000);
    }
    return;
    }
    lcd.setCursor(0,1);
    if (clearIt)
    {
    lcd.print("Yes");
    }
    else{
    lcd.print("No ");
    }
    }
    }

    void minuteTimer()
    {
    // Pass maxCount to getTimerMinutes
    int timerMinutes = getTimerMinutes("Set Minutes", 0, 60);
    if (timerMinutes > 0)
    {
    timedCountDown(timerMinutes*60, "Minute Timer");
    }
    else
    {
    timerCancelled("Timer");
    }
    return;
    }

    void stopwatch_time_counter()
    {
    static unsigned long elapsed_time = 0;
    static unsigned long last_read;
    static unsigned char is_ticking = 0;
    static unsigned char top_is_pressed = 0;
    static unsigned char bottom_is_pressed = 0;
    static unsigned char last_seconds = 0;
    int button = 0;

    while (button != btnSELECT)
    {

    unsigned long current_time = millis();
    if(is_ticking)
    {
    elapsed_time += current_time - last_read;
    }
    last_read = current_time;

    button = read_LCD_buttons();
    if(button == btnUP)
    {
    if(!top_is_pressed)
    {
    // reset
    top_is_pressed = 1;
    is_ticking = 0;
    elapsed_time = 0;
    timedBeep(shortBeep,1);

    //wait_for_button_release(TOP_BUTTON);clear();// tmphax to make this work on org06a01
    lcdClear();
    lcd.print("Stop Watch Timer");
    }
    }
    else
    {
    top_is_pressed = 0;
    }

    if(button == btnDOWN)
    {
    if(!bottom_is_pressed)
    {
    // start/stop
    bottom_is_pressed = 1;
    is_ticking = !is_ticking;
    timedBeep(shortBeep,1);
    }
    }
    else
    {
    bottom_is_pressed = 0;
    }

    lcd.setCursor(0,1);
    lcd.print(elapsed_time/1000/60/60/10%10); // tens of hours
    lcd.print(elapsed_time/1000/60/60%10); // hours
    lcd.print(":");
    lcd.print(elapsed_time/1000/60/10%10%6); // tens of minutes
    lcd.print(elapsed_time/1000/60%10); // minutes
    lcd.print(":");
    lcd.print(elapsed_time/1000%60/10); // tens of seconds
    unsigned char seconds = elapsed_time/1000%60%10;
    lcd.print(seconds);
    lcd.print(".");
    lcd.print(elapsed_time/100%10); // tenths of seconds
    lcd.print(elapsed_time/10%10); // hundredths of seconds

    // beep every second
    if(seconds != last_seconds && elapsed_time != 0)
    {
    //timedBeep(shortBeep,1);
    }
    last_seconds = seconds;
    }

    timerCancelled("StopWatch");

    }

    void setAlarm()
    {
    int button = 0;
    // Pass maxCount to getTimerMinutes
    alarmHours = getTimerMinutes("Set Alarm Hour", alarmHours, 23);
    // Validate alarm hours > 0 and < 24
    if (alarmHours >= 0 && alarmHours < 24)
    {
    // Pass maxCount to getTimerMinutes
    alarmMinutes = getTimerMinutes("Set Minutes", alarmMinutes, 59);
    // allow alarm minutes to be 0
    if (alarmMinutes < 60)
    {
    lcdClear();
    lcd.setCursor(0,1);
    //display alarm time
    lcd.print(alarmHours);
    lcd.print(":");
    if (alarmMinutes < 10)
    lcd.print("0");
    lcd.print(alarmMinutes);
    if (button == btnRIGHT)
    {
    timedBeep(shortBeep,1);
    alarmSet = true;
    lcd.setCursor(0,0);
    lcd.print("Alarm Set for");
    delay(1000);
    return;
    }
    else
    {
    timerCancelled("Alarm");
    return;
    }
    }
    else
    {
    timerCancelled("Alarm");
    }
    }
    else
    {
    timerCancelled("Alarm");
    }
    }

    // setDateTime feature
    void setDateTime()
    {
    int button = 0;

    //get month
    int setMonth = getTimerMinutes("Set Month", lastMonth, 12);
    if (setMonth > 0 && setMonth < 13)
    {
    //get day
    // default day and hour settings on set date/time
    int setDay = getTimerMinutes("Set Day", lastDay, 31);
    if (setDay > 0 && setDay < 32)
    {
    //get year
    int setYear = getTimerMinutes("Set Year", lastYear, 2999);
    if (setYear > 2000 && setYear < 3000)
    {
    //get hour
    int thisHour = lastHour;
    // default day and hour settings on set date/time
    int setHour = getTimerMinutes("Set Hour", thisHour, 23);
    if (setHour >= 0 && setHour < 24)
    {
    //get minutes
    int setMinute = getTimerMinutes("Set Minute", lastMinute, 59);
    if (setMinute < 60)
    {
    // RTC.adjust(DateTime(setYear,setMonth,setDay,setHour,setMinute)); // for DS1307
    setTime(setHour, setMinute, 0, setDay, setMonth, setYear); // sec, min, hour, date, month, year

    lcd.setCursor(0,0);
    lcd.print("Saving... ");
    delay(1000);
    return;
    }
    else
    {
    timerCancelled("");
    return;
    }
    }
    else
    {
    timerCancelled("");
    }
    }
    else
    {
    timerCancelled("");
    }
    }
    else
    {
    timerCancelled("");
    }
    }
    else
    {
    timerCancelled("");
    }

    }

    // read the buttons from LCD keypad shield
    int read_LCD_buttons()
    {
    adc_key_in = analogRead(0); // read the value from the sensor
    // my buttons when read are centered at these valies: 0, 131, 307, 481, 722
    // we add approx 50 to those values and check to see if we are close
    // No button pressed should be 1023
    if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
    if (adc_key_in < 50) return btnRIGHT;
    if (adc_key_in < 195) return btnUP;
    if (adc_key_in < 380) return btnDOWN;
    if (adc_key_in < 555) return btnLEFT;
    if (adc_key_in < 790) return btnSELECT;
    return btnNONE; // when all others fail, return this...

    }

    void timedCountDown(int secondCount, char countLabel[])
    {
    long seconds = 0;
    long minutes = 0;

    lcdClear();
    lcd.print(countLabel);
    for (int i = secondCount; i >= 0; i--)
    {
    seconds = i;
    minutes = i / 60;
    if (minutes > 0)
    {
    seconds = seconds - (minutes * 60);
    }

    if (minutes > 0)
    {
    lcd.setCursor(0,1);
    lcd.print(minutes);
    lcd.print(" min ");
    }
    else
    {
    lcd.setCursor(0,1);
    }
    if (seconds < 10) lcd.print("0");
    lcd.print(seconds);
    lcd.print(" sec remaining");
    if (seconds > 0) delay(1000);
    if (read_LCD_buttons() == btnSELECT) //cancel
    {
    timerCancelled("Timer");
    i = 0;
    return;
    }
    }
    lcd.setCursor(6,1);
    timedBeep(longBeep,3);
    }

    // Pass maxCount to getTimerMinutes
    int getTimerMinutes(char timerText[], int startNum, int maxCount)
    {
    int minutes = startNum;
    int button = 0;
    lcdClear();
    lcd.print(timerText);
    lcd.setCursor(0,1);
    lcd.print(minutes);

    while (button != btnSELECT)
    {
    button = read_LCD_buttons();
    Serial.println(button);

    if (button == btnLEFT)
    {
    if ((minutes + 10) <= maxCount)
    {
    timedBeep(shortBeep,1);
    minutes = minutes + 10;
    }
    else
    {
    timedBeep(shortBeep,2);
    }
    }

    if (button == btnUP)
    {
    if (minutes < maxCount)
    {
    timedBeep(shortBeep,1);
    minutes++;
    }
    else
    {
    timedBeep(shortBeep,2);
    minutes = 0;
    }
    }
    if (button == btnDOWN)
    {
    if (minutes > 0)
    {
    timedBeep(shortBeep,1);
    minutes--;
    }
    else
    {
    timedBeep(shortBeep,2);
    minutes = maxCount;
    }
    }
    if (button == btnRIGHT)
    {
    timedBeep(shortBeep,1);
    return minutes;
    }
    lcd.setCursor(0,1);
    lcd.print(minutes);
    lcd.print(" ");
    }
    return 0;
    }

    void timedBeep(int beepTime, int beepCount)
    {
    for (int i = 0; i < beepCount; i ++)
    {
    digitalWrite(beeper, HIGH);
    delay(beepTime);
    digitalWrite(beeper, LOW);
    delay(beepTime);
    }
    }

    void lcdClear(){
    lcd.clear();
    lcd.begin(16,2);
    lcd.setCursor(0,0);
    }

    void timerCancelled(char message[])
    {
    lcdClear();
    lcd.print(message);
    lcd.print(" Cancelled");
    timedBeep(shortBeep,3);
    }

    void setOffAlarm()
    {
    int button = 0;
    int i = 0;
    Serial.println(i);
    digitalWrite(backLight, HIGH); // turn backlight on
    while (button != btnSELECT)
    {
    button = read_LCD_buttons();
    lcdClear();
    i++;
    if (i > 50)
    {
    lcdClear();
    lcd.print("Alert Alert");
    lcd.setCursor(0,1);
    lcd.print(" Alert Alert");
    i = 0;
    timedBeep(shortBeep,3);
    }

    }
    timerCancelled("Alarm");
    alarmSet = false;
    }

Új hozzászólás Aktív témák