Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <StringSplitter.h>
- #include <Wire.h>
- #include<string.h>
- #include "rgb_lcd.h"
- #define LED 2
- #include<math.h>
- rgb_lcd lcd;
- const int B=4275;
- const int R0 = 100000;
- const int czujnikTemperatury = A0;
- const int czujnikDotyku=5;
- struct
- {
- int stan;
- }czujnik_dotyku;
- struct
- {
- int okres_wlaczenia;
- int okres_wylaczenia;
- int czas_ostatniej_zmiany;
- int stan;
- int czestotliwosc;
- int kaput;
- }dioda;
- struct
- {
- int ton;
- int stan;
- int okres_wlaczenia;
- int okres_wylaczenia;
- int czas_ostatniej_zmiany;
- int razy;
- int m;
- } buzzer;
- struct
- {
- int kelwiny;
- int celsjusze;
- int m;
- }czujniktemp;
- struct
- {
- String tekst;
- int colorR;
- int colorG;
- int colorB;
- } ekran;
- void setup() {
- pinMode(6, OUTPUT);
- pinMode(LED, OUTPUT);
- pinMode(czujnikDotyku, INPUT);
- dioda.okres_wlaczenia = 500 ;
- dioda.okres_wylaczenia =500 ;
- buzzer.okres_wlaczenia= 1000;
- buzzer.okres_wylaczenia= 1000;
- dioda.stan = LOW;
- dioda.czas_ostatniej_zmiany =millis();
- buzzer.czas_ostatniej_zmiany =millis();
- buzzer.stan = LOW;
- Serial.begin(9600);
- dioda.kaput=LOW;
- int kelwiny=LOW;
- int celsjusze=LOW;
- czujniktemp.m=0;
- lcd.begin(16,2);
- ekran.colorR=255;
- ekran.colorG=0;
- ekran.colorB=0;
- buzzer.m=0;
- }
- void ekranik()
- {
- lcd.setRGB(ekran.colorR, ekran.colorG, ekran.colorB);
- }
- void obsluga_diody()
- {
- int uplynelo = millis() - dioda.czas_ostatniej_zmiany;
- if (dioda.kaput==LOW)
- {
- if((dioda.stan==HIGH)&&(uplynelo>dioda.okres_wlaczenia))
- {
- digitalWrite(LED, LOW);
- dioda.stan=LOW;
- dioda.czas_ostatniej_zmiany =millis();
- }
- else
- if((dioda.stan==LOW)&&(uplynelo>dioda.okres_wlaczenia))
- {
- digitalWrite(LED, HIGH);
- dioda.stan=HIGH;
- dioda.czas_ostatniej_zmiany =millis();
- }
- }
- }
- void obsluga_buzzera()
- {int uplynelo = millis() - buzzer.czas_ostatniej_zmiany;
- if (buzzer.stan==LOW)
- {
- digitalWrite(6,LOW);
- noTone(6);
- }
- else
- if((buzzer.razy!=0)&& (buzzer.stan==HIGH))
- {
- if((buzzer.stan==HIGH)&&(uplynelo>buzzer.okres_wlaczenia)&&(buzzer.m<buzzer.razy))
- {
- tone(6, buzzer.ton);
- buzzer.czas_ostatniej_zmiany =millis();
- buzzer.m++;
- Serial.println(buzzer.m);
- buzzer.stan==LOW;}
- if(((buzzer.stan==LOW)&&(uplynelo>buzzer.okres_wylaczenia))||(buzzer.m>=buzzer.razy))
- {
- noTone(6);
- buzzer.czas_ostatniej_zmiany =millis();
- buzzer.stan==HIGH;
- }
- }
- else
- if(buzzer.stan==HIGH)
- {
- digitalWrite(6,HIGH);
- tone(6, buzzer.ton);
- }
- }
- void temperatura()
- {
- int a = analogRead(czujnikTemperatury);
- if ((czujniktemp.celsjusze==HIGH)&&(czujniktemp.m==0))
- {
- float R = 1023.0/a-1.0;
- R = R0*R;
- float temperature = 1.0/(log(R/R0)/B+1/298.15)-273.15;
- Serial.print("temperatura wynosi ");
- Serial.println(temperature);
- czujniktemp.m=1;
- }
- else
- if((czujniktemp.kelwiny==HIGH)&&(czujniktemp.m==0))
- {
- Serial.print("temperatura wynosi ");
- Serial.println(a);
- czujniktemp.m=1;
- }
- }
- void dotyk()
- {
- czujnik_dotyku.stan = digitalRead(czujnikDotyku);
- if(czujnik_dotyku.stan == 1)
- {
- temperatura();
- }
- }
- void loop()
- {
- obsluga_diody();
- obsluga_buzzera();
- dotyk();
- ekranik();
- if(Serial.available())
- {
- String intxt = Serial.readString();
- if((intxt.startsWith("buzzer "))&&(buzzer.stan== LOW))
- {buzzer.ton = intxt.substring(7).toInt();
- buzzer.stan=HIGH;
- Serial.print("buzzer włączony\n");
- int a;
- if((a=intxt.indexOf("razy"))!=-1)
- {
- buzzer.razy=intxt.substring(a+4).toInt();
- Serial.println(buzzer.razy);
- Serial.println(a);
- }
- }
- else
- if ((intxt == "buzzer stop\n")&&(buzzer.stan==HIGH))
- {buzzer.stan= LOW;
- Serial.print("buzzer wyłączony\n");
- }
- else
- if(intxt == "info\n")
- {Serial.print("(C) 2020 Majkel Dzordan");}
- else
- if(intxt==("dioda stop\n"))
- {dioda.kaput=HIGH;
- digitalWrite(LED, LOW);
- dioda.stan=LOW;
- }
- else
- if(intxt.startsWith("dioda"))
- {dioda.czestotliwosc = intxt.substring(6).toInt();
- dioda.kaput=LOW;
- dioda.okres_wlaczenia=(1000/(2*dioda.czestotliwosc));
- dioda.okres_wylaczenia=(1000/(2*dioda.czestotliwosc));
- Serial.print("dioda miga ");
- Serial.print(dioda.czestotliwosc,DEC);
- Serial.print(" razy na sekundę\n");
- }
- else
- if(intxt==("temp C\n"))
- {czujniktemp.celsjusze=HIGH;
- czujniktemp.kelwiny=LOW;
- czujniktemp.m=0;
- }
- else
- if(intxt==("temp K\n"))
- {czujniktemp.kelwiny=HIGH;
- czujniktemp.celsjusze=LOW;
- czujniktemp.m=0;
- }
- else
- if (intxt.startsWith("lcd ")&& intxt.indexOf("kolor")!=-1)
- {int lastStringLength = intxt.length();
- ekran.tekst = intxt.substring(4,lastStringLength-19);
- lcd.clear();
- lcd.print(ekran.tekst);
- ekran.colorR= intxt.substring(lastStringLength-11,lastStringLength-8).toInt();
- ekran.colorG=intxt.substring(lastStringLength-7,lastStringLength-5).toInt();
- ekran.colorB=intxt.substring(lastStringLength-4,lastStringLength-1).toInt();
- Serial.print("lcd ok\n");
- }
- else
- if ((intxt.startsWith("lcd"))&& intxt.indexOf("kolor")==-1)
- {int lastStringLength = intxt.length();
- ekran.tekst=intxt.substring(4,lastStringLength-1);
- lcd.clear();
- lcd.print(ekran.tekst);
- Serial.print("lcd ok\n");
- }
- }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement