Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Kommentit:
- Koodia voisi vähentää ja funktioita, mutta toistaiseksi pysyy tällaisena, voi siinä muutamat funtiot sitten yhdistää myöhemmin.
- */
- #include <LiquidCrystal.h>
- volatile boolean kytkin;
- long previousTime = 0;
- long previousTime2 = 0;
- long previousTime3 = 0;
- long previousTime4 = 0;
- long previousTime5 = 0;
- long previousTime6 = 0;
- long interval = 1000;
- long interval2 = 350;
- int sekunnit = 0;
- int tunnit = 12;
- int minuutit = 35;
- int halytystunnit = 0;
- int halytysminuutit = 0;
- const int ledPin = 6;
- const int buttonPin2 = 4;
- const int buttonPin1 = 9;
- const int buttonPin3 = 10;
- const int buttonPin4 = 2;
- int buttonState1 = 0;
- int buttonState3 = 0;
- int buttonState2 = 0;
- int buttonState4 = 0;
- LiquidCrystal lcd(13, 11, 5, 8, 3, 7);
- void setup() {
- // put your setup code here, to run once:
- lcd.begin(16, 2);
- pinMode(ledPin, OUTPUT);
- pinMode(buttonPin1, INPUT);
- pinMode(buttonPin2, INPUT);
- pinMode(buttonPin3, INPUT);
- pinMode(buttonPin4, INPUT);
- attachInterrupt(0, modi, FALLING);
- sei();
- Serial.begin(9600);
- }
- void loop() {
- // put your main code here, to run repeatedly:
- buttonState2 = digitalRead(buttonPin2);
- buttonState1 = digitalRead(buttonPin1);
- buttonState3 = digitalRead(buttonPin3);
- buttonState4 = digitalRead(buttonPin4);
- paivita_aika();
- aseta_halytys();
- halytys();
- lopeta_halytys();
- tarkista_aika();
- nayta_halytys();
- aseta_aika();
- nayta_aika();
- }
- void paivita_aika() {
- unsigned long currentTime = millis();
- if (currentTime - previousTime > interval) {
- previousTime = currentTime;
- sekunnit++;
- }
- }
- void nayta_aika() {
- unsigned long currentTime = millis();
- if (currentTime - previousTime5 > interval2) {
- previousTime5 = currentTime;
- lcd.setCursor(0, 0);
- lcd.print("Time: ");
- if (tunnit < 10) {
- lcd.print(0);
- }
- lcd.print(tunnit);
- lcd.print(":");
- if (minuutit < 10) {
- lcd.print(0);
- }
- lcd.print(minuutit);
- }
- }
- void tarkista_aika() {
- if (tunnit == 24) {
- tunnit = 0;
- }
- if (minuutit == 60) {
- minuutit = 0;
- tunnit++;
- }
- if (sekunnit == 60) {
- sekunnit = 0;
- minuutit++;
- }
- }
- void aseta_halytys() {
- unsigned long currentTime = millis();
- if (currentTime - previousTime2 > interval2) {
- previousTime2 = currentTime;
- if (buttonState2 == LOW && buttonState3 == LOW && kytkin == true) {
- halytysminuutit++;
- }
- if (buttonState2 == LOW && buttonState1 == LOW && kytkin == true) {
- halytysminuutit--;
- }
- if (buttonState2 == HIGH && buttonState3 == LOW && kytkin == true) {
- halytystunnit++;
- }
- if (buttonState2 == HIGH && buttonState1 == LOW && kytkin == true) {
- halytystunnit--;
- }
- if (halytysminuutit == 60) {
- halytysminuutit = 0;
- }
- if (halytysminuutit == -1) {
- halytysminuutit = 59;
- }
- if (halytystunnit == -1) {
- halytystunnit = 23;
- }
- if (halytystunnit == 24) {
- halytystunnit = 0;
- }
- }
- }
- void nayta_halytys() {
- unsigned long currentTime = millis();
- if (currentTime - previousTime6 > interval2) {
- previousTime6 = currentTime;
- lcd.setCursor(0, 1);
- lcd.print("Alarm: ");
- if (halytystunnit < 10) {
- lcd.print(0);
- }
- lcd.print(halytystunnit);
- lcd.print(":");
- if (halytysminuutit < 10) {
- lcd.print(0);
- }
- lcd.print(halytysminuutit);
- if (kytkin == true) {
- lcd.print("Amod");
- } else {
- lcd.print("Tmod");
- }
- }
- }
- void halytys() {
- if (halytystunnit == tunnit && halytysminuutit == minuutit && sekunnit - 2 <= 0) {
- digitalWrite(ledPin, HIGH); // joku koodi joka aktivoi hälytyksen
- }
- }
- void lopeta_halytys() {
- if ( buttonState2 == LOW ) {
- digitalWrite(ledPin, LOW);
- }
- }
- void aseta_aika() {
- unsigned long currentTime = millis();
- if (currentTime - previousTime3 > interval2) {
- previousTime3 = currentTime;
- if (buttonState3 == LOW && buttonState2 == LOW && kytkin == false) {
- minuutit++;
- }
- if (buttonState1 == LOW && buttonState2 == LOW && kytkin == false) {
- minuutit--;
- }
- if (buttonState3 == LOW && buttonState2 == HIGH && kytkin == false) {
- tunnit++;
- }
- if (buttonState1 == LOW && buttonState2 == HIGH && kytkin == false) {
- tunnit--;
- }
- if (minuutit == 60) {
- minuutit = 0;
- }
- if (minuutit == -1) {
- minuutit = 59;
- }
- if (tunnit == -1) {
- tunnit = 23;
- }
- if (tunnit == 24) {
- tunnit = 0;
- }
- }
- }
- void modi() {
- cli();
- unsigned long currentTime = millis();
- if (currentTime - previousTime4 > interval2) {
- previousTime4 = currentTime;
- kytkin = !kytkin;
- Serial.println(kytkin);
- }
- sei();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement