Μετάβαση στο κύριο περιεχόμενο

Ενημερωμένος κώδικας για σχολικό κουδούνι

 Ενημερωμένος κώδικας για σχολικό κουδούνι


/*

Modified on Nov 24, 2020
Modified by MehranMaleki from Arduino Examples
Modified by Panagiotis Toumpaniaris on March 3,2024
*/
#include <EEPROM.h>
#include <Keypad.h>
#include <Wire.h>
#include <DS1307RTC.h>
#include <LiquidCrystal_I2C.h>
#include <TimeLib.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);  // I2C address 0x27, 16 column and 2 rows

const char *monthName[12] = {
  "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
tmElements_t tm;

const int ROW_NUM    = 4; // four rows
const int COLUMN_NUM = 4; // four columns

char keys[ROW_NUM][COLUMN_NUM] = {
  {'1','2','3', 'A'},
  {'4','5','6', 'B'},
  {'7','8','9', 'C'},
  {'*','0','#', 'D'}
};

byte pin_rows[ROW_NUM] = {6, 5, 4, 3};  //connect to the row pinouts of the keypad
byte pin_column[COLUMN_NUM] = {10, 9, 8, 7};; //connect to the column pinouts of the keypad

Keypad keypad = Keypad(makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM );
int cursorColumn = 0;
int ResetPin = 12;
 
int hours = 0;
int minutes = 0;
int seconds = 0;
int h1;
int m1;
String hh;
String mm;
String hh0;





void setup() {
  Serial.begin(9600);
  pinMode(2,OUTPUT);
  pinMode(12,OUTPUT);
  digitalWrite(2,HIGH);
  digitalWrite(12,HIGH);

  lcd.init();         // initialize the lcd
  lcd.backlight();    // open the backlight
   lcd.clear();
 // lcd.setCursor(cursorColumn, 0); // move cursor to   (cursorColumn, 0)
 // lcd.print("ok");  
 // while (!Serial) ;
 // wait for serial
  delay(200);
 
}


void loop() {
 //  char key = keypad.getKey();
getTimeFromKeypad();

}

void loadprog(int hset,int minset) {
while (millis>=0) {
settimes(8,18);

// set time2
 settimes(9,3);
  settimes(9,8);

  // set time3
  settimes(9,530);
   settimes(10,3);
  // set time4
  settimes(10,48);
 settimes(10,58);
 // set time 5
  settimes(11,43);
   settimes(11,53);
   // set time 6
    settimes(12,38);
     settimes(12,43);
     // set time 7
      settimes(13,28);
       settimes(13,33);
     //set time 8
     
     settimes(00,17);
     
     // set this time for demostration
      settimes(hset,minset);
     
    // settimes(15,39);
  if (RTC.read(tm)) {
    Serial.print("Ok, Time = ");
    print2digits(tm.Hour);
    Serial.write(':');
    print2digits(tm.Minute);
    Serial.write(':');
    print2digits(tm.Second);
    Serial.print(", Date (D/M/Y) = ");
    Serial.print(tm.Day);
    Serial.write('/');
    Serial.print(tm.Month);
    Serial.write('/');
    Serial.print(tmYearToCalendar(tm.Year));
    Serial.println();
     lcd.clear();
  lcd.setCursor(0, 0);       // start to print at the first row
   print2digits1(tm.Hour);
    lcd.print(':');
    print2digits1(tm.Minute);
    lcd.print(':');
    print2digits1(tm.Second);
 
  lcd.setCursor(0, 1);       // start to print at the second row
  lcd.print(tm.Day);
    lcd.write('/');
    lcd.print(tm.Month);
    lcd.write('/');
    lcd.print(tmYearToCalendar(tm.Year));

  // delay(500);
 
  } else {
    if (RTC.chipPresent()) {
      Serial.println("The DS1307 is stopped.  Please run the SetTime");
      Serial.println("example to initialize the time and begin running.");
      Serial.println();
    } else {
      Serial.println("DS1307 read error!  Please check the circuitry.");
      Serial.println();
    }
    delay(9000);
  }
  delay(1000);

}

}

void print2digits(int number) {
  if (number >= 0 && number < 10) {
    Serial.write('0');
  }
  Serial.print(number);
}

void print2digits1(int number) {
  if (number >= 0 && number < 10) {
    lcd.print('0');
  }
  lcd.print(number);
}


bool getTime(const char *str)
{
  int Hour, Min, Sec;

  if (sscanf(str, "%d:%d:%d", &Hour, &Min, &Sec) != 3) return false;
  tm.Hour = Hour;
  tm.Minute = Min;
  tm.Second = Sec;
  return true;
}


bool getDate(const char *str)
{
  char Month[12];
  int Day, Year;
  uint8_t monthIndex;

  if (sscanf(str, "%s %d %d", Month, &Day, &Year) != 3) return false;
  for (monthIndex = 0; monthIndex < 12; monthIndex++) {
    if (strcmp(Month, monthName[monthIndex]) == 0) break;
  }
  if (monthIndex >= 12) return false;
  tm.Day = Day;
  tm.Month = monthIndex + 1;
  tm.Year = CalendarYrToTm(Year);
  return true;
}

void settimes(int h1,int m1) {
   if(tm.Hour==h1 and tm.Minute==m1) {
      if (tm.Second > 0 & tm.Second<5) {
         digitalWrite(2,LOW);
         digitalWrite(12,LOW);
          // delay(1000);
                                       }
    else if(tm.Second < 2 or tm.Second >4){
      digitalWrite(2,HIGH);
      digitalWrite(12,HIGH);
      delay(100);
    }
}
}

void getTimeFromKeypad() {
   String timeStr = "";
    char key = '\0';

    while (key != '#') { // Wait until '#' is pressed to indicate end of input
        key = keypad.getKey();
        if (key) {
            lcd.setCursor(timeStr.length(), 1);
            lcd.print(key);
            timeStr += key;
            delay(200); // Debouncing delay
        }
    }
    lcd.clear();

    // Parse the time string (format: HHMM)
    hours = (timeStr.substring(0, 2)).toInt();
    minutes = (timeStr.substring(2, 4)).toInt();
    loadprog(hours,minutes);

}

Σχόλια

Δημοφιλείς αναρτήσεις από αυτό το ιστολόγιο

Tmp36GZ θερμόμετρο με LCD οθόνη

 Tmp36GZ με LCD οθόνη // Define to which pin of the Arduino the output of the TMP36 is connected: #include <LiquidCrystal_I2C.h> // Look for LiquidCrystal I2C library by Marco Schwartz LiquidCrystal_I2C lcd ( 0x 27 , 16 , 2 ) ;  // set the LCD address to 0x3F for a 16 chars and 2 line display #define sensorPin A0 void setup () {   // Begin serial communication at a baud rate of 9600:   Serial . begin ( 9600 ) ;   lcd . init () ;   lcd . clear () ;           lcd . backlight () ;      // Make sure backlight is on     // Print a message on both lines of the LCD.   lcd . setCursor ( 2 , 0 ) ;   //Set cursor to character 2 on line 0   lcd . print ( "Hello world!" ) ;     lcd . setCursor ( 2 , 1 ) ;   //Move cursor to character 2 on line 1   lcd . print ( "LCD Tutorial" ) ; } void loop () {   // Get a reading from the temperature sensor:   int reading ...

Σερβοκινητήρας

  Βασικός έλεγχος σερβομηχανισμού Θα μάθουμε πώς να ελέγχετε έναν τυπικό σερβοκινητήρα, να πηγαίνει εμπρός και πίσω κατά 180 μοίρες, χρησιμοποιώντας ένα «βρόχο for()». Αυτό γίνεται με τη βοήθεια της βιβλιοθήκης Servo, η οποία είναι προεγκατεστημένη βιβλιοθήκη στο Arduino IDE (τόσο εκτός σύνδεσης όσο και σε ηλεκτρονική έκδοση). . Αυτό γίνεται με τη βοήθεια της βιβλιοθήκης  Servo     , η οποία είναι προεγκατεστημένη βιβλιοθήκη στο Arduino IDE (τόσο εκτός σύνδεσης όσο και σε ηλεκτρονική έκδοση).      Χρειαζόμαστε τα παρακάτω:     Arduino IDE Arduino UNO  Σερβοκινητήρας 4,8V - 6V  Καλώδια βραχυκυκλωτήρα. Τυπικοί σερβοκινητήρες Οι τυπικοί σερβοκινητήρες είναι ενεργοποιητές που επιτρέπουν τον ακριβή έλεγχο της θέσης (γωνία). Χαρακτηριστικό  είναι ότι η γωνία του κινητήρα είναι 0 - 180 μοίρες. Με άλλα λόγια, μπορεί να κάνει το μισό μιας περιστροφής. Ένας τυπικός σερβοκινητήρας, όπως και άλλοι κινητήρες, είναι ουσιαστικά απλώς ένας...

Αντιστάσεις pull up, pull down

  Αντιστάσεις pull up (Pull up resistors) Οι αντιστάσεις pull up  είναι πολύ συνηθισμένες όταν χρησιμοποιούνται μικροελεγκτές (MCU) ή οποιαδήποτε συσκευή ψηφιακής λογικής. Θα εξηγήσουμε πότε και πού να χρησιμοποιήσετε αντιστάσεις pull up και, στη συνέχεια, θα κάνουμε έναν απλό υπολογισμό για να δείξουμε γιατί είναι σημαντικά τα pull-ups. Τι είναι μια αντίσταση  Pull up Ας υποθέσουμε ότι έχετε ένα MCU με ένα pin διαμορφωμένο ως είσοδο. Εάν δεν υπάρχει τίποτα συνδεδεμένο με τον ακροδέκτη και το πρόγραμμά σας διαβάζει την κατάσταση του ακροδέκτη, θα είναι ψηλά (τραβηγμένο στο VCC) ή χαμηλό (τραβηγμένο στη γείωση);  Αυτό το φαινόμενο αναφέρεται ως  αιωρούμενο  (floating). Για να αποφευχθεί αυτή η άγνωστη κατάσταση, μια αντίσταση pull-up ή pull-down θα διασφαλίσει ότι η ακίδα βρίσκεται είτε σε υψηλή είτε σε χαμηλή κατάσταση, ενώ χρησιμοποιεί επίσης χαμηλή ποσότητα ρεύματος. Για απλότητα, θα εστιάσουμε στα pull-ups καθώς είναι πιο συνηθισμένα από τα pull-down. Λε...