Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <IRremote.h> //Include the IRremote library
- #include <Wire.h> //Include the Wire library
- #include <LiquidCrystal_I2C.h> //Include the LCD library
- #include <avr/pgmspace.h>
- LiquidCrystal_I2C lcd(0x3F, 16, 2); //Set the LCD kind
- unsigned int z = 600;
- unsigned int o = 1800;
- unsigned int t = 4200;
- unsigned int h = 8000;
- const unsigned int ACIntro PROGMEM = {8000,4250, 1800,600, 600,1750, 650,1700, 650,1700, 650,1750, 650,1700, 700,1700, 650,1700, 1800,600, 650,1700, 650,1700, 650,1750, 650,1750, 600,1750, 1800,600, 600,1750, 600,1750, 650,1700, 650,1750, 650,1700, 1800,600, 1750,650, 600,1750, 600,1750, 650,1750, 600,1750, 650,1750, 600,1750, 600,1750, 650,1750, 600,1750, 600,1800, 600,1750, 650,1700, 650,1750, 600,1750, 650,1750, 600,1750, 600,1750, 650,1750, 600,1750, 650,1750, 600,1750, 600,1750};
- const unsigned int ACTurnOff PROGMEM = {650,1700, 650,1750, 1800,550, 1800,600, 600,1750, 650,1750, 600,1750, 600,1750, 650,1750, 600,1750, 1800,550, 1800,600, 1750,600, 650,1750, 600,1750, 650,1750, 600,1750, 1800,550};
- const unsigned int ACEnd PROGMEM = {z, o, z, o, o};
- IRsend irsend; //Create an Infra-red LED object
- //name the buttons:
- #define a 8
- #define b 4
- #define up 5
- #define down 6
- #define cr 7
- String cmd = "12:24,25|"; //This string containses the commands, this way: hour, :, minute, ,, temperature, |
- unsigned long int timer = millis();//Set last millisecond which had writen
- unsigned long int lightTime;//Set an integer which be the number of milliseconds until the screen turns off
- int second = 0; //Set current second
- int minute = 0;//Set current minute
- int hour = 0;//Set current hour
- //int hom = 0; /*Delete it*/
- int temperature = 24;//Set current temperature
- void setup() {
- Serial.begin(9600); //Start serial
- //Serial.println(string_delete(cmd, 26, 9)); /*Delete it*/
- //Set all the buttons pins as input pullup pins
- pinMode(a, INPUT_PULLUP);
- pinMode(b, INPUT_PULLUP);
- pinMode(up, INPUT_PULLUP);
- pinMode(down, INPUT_PULLUP);
- pinMode(cr, INPUT_PULLUP);
- //Start the LCD
- lcd.init();
- lcd.init();
- lcd.backlight();
- lcd.setCursor(0,0);/*
- lcd.print("Time not setted");
- lcd.backlight(); //Turn on LCD lights
- while(hom < 2) { /*Delete this while*//*
- while(Serial.available() < 1) {}
- int data = Serial.parseInt();
- if(hom == 0) {
- hour = data;
- hom++;
- }
- else {
- minute = data;
- hom++;
- }
- }*/
- timer = millis(); //Set timer as current millisecond
- lightTime = millis(); //Set lightTime as current millisecond
- lcd.clear();
- //Serial.println("Start"); /*Delete it*/
- }
- int hmt = 0;
- boolean light = true; //Set current light mode
- void loop() {
- check_command(); //Check if there is a command for this current time, and if it's do it
- set_time(true); //Set current time and show information on the LCD
- if(millis() - lightTime > 20000) { //If was been crossen 20 seconds from the last button pressing
- lcd.noBacklight(); //Turn off the LCD light
- lcd.noDisplay();
- light = false; //Set current light mode off
- }
- if(light == true) { //If the light is on
- //Get input data from the buttons
- if(digitalRead(a) == 0) {
- //lcd.backlight();
- //lcd.display();
- lcd.clear();
- show_programm();
- lightTime = millis();
- }
- if(digitalRead(b) == 0) {
- //lcd.backlight();
- //lcd.display();
- lcd.clear();
- add_command_scr();
- lightTime = millis();
- }
- if(digitalRead(cr) == 0) {
- //lcd.backlight();
- //lcd.display();
- lcd.clear();
- lightTime = millis();
- while(digitalRead(cr) == 0);
- hmt++;
- if(hmt == 3) {hmt = 0;}
- }
- if(digitalRead(up) == 0) {
- //lcd.backlight();
- //lcd.display();
- lcd.clear();
- lightTime = millis();
- while(digitalRead(up) == 0);
- if(hmt == 0) {
- hour++;
- if(hour == 24) {hour = 0;}
- }
- else if(hmt == 1) {
- minute++;
- if(minute == 60) {minute = 0;}
- }
- else {temperature++;}
- }
- if(digitalRead(down) == 0) {
- lightTime = millis();
- //lcd.backlight();
- //lcd.display();
- lcd.clear();
- while(digitalRead(down) == 0);
- if(hmt == 0) {
- hour--;
- if(hour == -1) {hour = 24;}
- }
- else if(hmt == 1) {
- minute--;
- if(minute == -1) {minute = 59;}
- }
- else {temperature--;}
- }
- }
- else {
- if(digitalRead(a) == 0 or digitalRead(b) == 0 or digitalRead(cr) == 0 or digitalRead(up) == 0 or digitalRead(down) == 0) {
- while(digitalRead(a) == 0 or digitalRead(b) == 0 or digitalRead(cr) == 0 or digitalRead(up) == 0 or digitalRead(down) == 0);
- light = true;
- lightTime = millis();
- lcd.backlight();
- lcd.display();
- }
- }
- }
- void check_command() {
- String str = cmd;
- for(int i = 0; i < char_count(cmd ,'|'); i++) {
- int h = string_copy(str, 0, 1).toInt();
- int m = string_copy(str, 3, 4).toInt();
- int t = string_copy(str, 6, 7).toInt();
- if(h == hour && m == minute) {
- /*if(h == string_copy(cmd, i*9, i*9 + 1).toInt() && m == string_copy(cmd, i*9 + 3, i*9 + 4).toInt() && t == string_copy(cmd, i*9 + 6, i*9 + 7).toInt()) {
- cmd = string_delete(cmd, i*9 - 1, 9);
- }*/
- set_temperature_new(t);
- break;
- }
- else {
- str.remove(0, 9);
- }
- }
- }
- void add_command(int h, int m, int t) {
- if(h < 10) {cmd += "0";} cmd += String(h);
- cmd += ":";
- if(m < 10) {cmd += "0";} cmd += String(m);
- cmd += ",";
- if(t < 10) {cmd += "0";} cmd += String(t);//If it will be 0 (=ON/OFF) it will write "00"
- cmd += "|";
- }
- int lastT = -1;
- void set_temperature_new(int t) {
- if(t != lastT) {
- Serial.println(t);
- if(t > 1) {
- ac_command(t);
- }
- else {
- irsend.sendRaw(ACIntro, 90, 38);
- irsend.sendRaw(ACTurnOff, 36, 38);
- irsend.sendRaw(ACEnd, 5, 38);
- }
- lastT = t;
- }
- }
- //This is a string controls library for arduino by Eitan Rabinovitch
- /*char string_char_at(String str, int index) {
- return str.charAt(index);
- }*/
- String string_copy(String data, int index, int count) {
- String str = "";
- int number = 0;
- for(int i = index; i < count + 1; i++) {
- str += data.charAt(i);
- }
- return str;
- }
- /*String string_replace_all(String data, String o, String n) {
- data.replace(o, n);
- return data;
- }
- String string_delete(String data, int index, int count) {
- data.remove(index, count);
- return data;
- }
- int char_pos(String data, char nano) {
- int n = 0;
- while(n < data.length() + 1) {
- if(data.charAt(n) == nano) {
- return n;
- }
- n++;
- }
- return 0;
- }*/
- int char_count(String data, char nano) {
- int n = 0;
- int count = 0;
- while(n < data.length() + 1) {
- if(data.charAt(n) == nano) {
- count++;
- }
- n++;
- }
- return count;
- }
- /*String string_insert(String data, int index, String insert) {
- String str = string_copy(data, 0, index) + insert + string_copy(data, index + 1, data.length());
- return str;
- }*/
- void set_time(boolean p) {
- if(millis() - timer >= 1000) {
- second++;
- timer += 1000;
- }
- if(second == 60) {
- minute++;
- second = 0;
- }
- if(minute == 60) {
- hour++;
- minute = 0;
- second--; //Because of the delay
- }
- if(hour == 24) {
- hour = 0;
- }
- if(p == true) {
- lcd.setCursor(3, 0);
- lcd.print("Time " + String(hour) + ":" + String(minute) + " "); //Space because if hour/minute < 10 one digit of the temperature won't be changed
- lcd.setCursor(1, 1); //And then the temperature will look like 3 digit number
- lcd.print(F("A Show B Add"));
- }
- }
- //--Screens---------------------------------------
- void show_programm() {
- String str = cmd;
- int number = 0;
- boolean c = false;
- while(digitalRead(a) == 0);
- while(number < char_count(cmd, '|')) {
- set_time(false);
- lcd.setCursor(0, 0);
- lcd.print("At " + string_copy(str, 0, 1) + ":" + string_copy(str, 3, 4) + " temp " + string_copy(str, 6, 7));
- lcd.setCursor(1, 1);
- lcd.print(F("A Next B Del"));
- if(digitalRead(a) == 0) {
- c = true;
- }
- if(digitalRead(a) == 1 && c == true) {
- if(str.length() >= 9) {
- str.remove(0, 9);
- number++;
- }
- else {break;}
- c = false;
- }
- if(digitalRead(b) == 0) {
- while(digitalRead(b) == 0);
- if(number != 0) {
- cmd.remove(number*9 - 1, 9);
- } else {
- cmd.remove(0, 9);
- }
- str = cmd;
- }
- }
- lcd.clear();
- }
- void add_command_scr() {
- int h = 0;
- int m = 0;
- int t = 24;
- while(digitalRead(b) == 0);
- while(digitalRead(b) == 1) {
- set_time(false);
- if(digitalRead(up) == 0) {
- while(digitalRead(up) == 0);
- h++;
- if(h == 24) {h = 0;}
- }
- if(digitalRead(down) == 0) {
- while(digitalRead(down) == 0);
- h--;
- if(h == -1) {h = 24;}
- }
- lcd.setCursor(0, 0);
- lcd.print("At " + String(h) + ":00 temp 24 ");
- lcd.setCursor(0, 1);
- lcd.print(F("Press B to min"));
- }
- while(digitalRead(b) == 0);
- lcd.clear();
- while(digitalRead(b) == 1) {
- set_time(false);
- if(digitalRead(up) == 0) {
- while(digitalRead(up) == 0);
- m++;
- if(m == 60) {m = 0;}
- }
- if(digitalRead(down) == 0) {
- while(digitalRead(down) == 0);
- m--;
- if(m == -1) {m = 59;}
- }
- lcd.setCursor(0, 0);
- lcd.print("At " + String(h) + ":" + String(m) + " temp 24 ");
- lcd.setCursor(0, 1);
- lcd.print(F("Press B to temp"));
- }
- while(digitalRead(b) == 0);
- lcd.clear();
- while(digitalRead(b) == 1) {
- set_time(false);
- if(digitalRead(up) == 0) {
- while(digitalRead(up) == 0);
- t++;
- }
- if(digitalRead(down) == 0) {
- while(digitalRead(down) == 0);
- t--;
- }
- lcd.setCursor(0, 0);
- lcd.print("At " + String(h) + ":" + String(m) + " temp " + String(t) + " ");
- lcd.setCursor(0, 1);
- lcd.print(F("Press B to save"));
- }
- add_command(h, m, t);
- while(digitalRead(b) == 0);
- lcd.clear();
- }
- unsigned int basecode[131] = {8000,4250, 1800,600, 600,1750, 650,1700, 650,1700, 650,1750, 650,1700, 650,1750, 650,1700, 1800,600, 600,1750, 650,1700, 650,1750, 1800,600, 700,1700, 600,1800, 600,1750, 600,1750, 1800,550, 650,1750, 600,1750, 650,1750, 1750,650, 600,1750, 600,1750, 600,1800, 600,1750, 600,1750, 650,1750, 600,1800, 550,1800, 600,1750, 600,1800, 550,1800, 600,1750, 650,1750, 600,1800, 550,1800, 600,1750, 600,1800, 550,1800, 600,1750, 600,1800, 600,1750, 600,1800, 1700,650, 1750,600, 600,1800, 550,1850, 550,1800, 600,1750, 600,1800, 550,1800, 600,1800, 550,1800, 1750,600, 1750,650, 1700,700, 550,1800, 550,1800, 1750,650, 1700,650, 550,1850/*, 550,1800, 550,1850, 1550*/};
- void ac_command(int temperature) {
- switch(temperature) {
- case 16: //Confirmed
- basecode[36] = z; basecode[37] = o; basecode[38] = z; basecode[39] = o; basecode[40] = z; basecode[41] = o; basecode[42] = z; basecode[43] = o;
- basecode[114] = z; basecode[115] = o; basecode[116] = z; basecode[117] = o; basecode[118] = o; basecode[119] = z; basecode[120] = z; basecode[121] = o; basecode[122] = o; basecode[123] = z; basecode[124] = z; basecode[125] = o; break;
- case 17: //Confirmed
- basecode[36] = o; basecode[37] = z; basecode[38] = z; basecode[39] = o; basecode[40] = z; basecode[41] = o; basecode[42] = z; basecode[43] = o;
- basecode[114] = z; basecode[115] = o; basecode[116] = o; basecode[117] = z; basecode[118] = o; basecode[119] = z; basecode[120] = z; basecode[121] = o; basecode[122] = o; basecode[123] = z; basecode[124] = z; basecode[125] = o; break;
- case 18: //Confirmed
- basecode[36] = z; basecode[37] = o; basecode[38] = o; basecode[39] = z; basecode[40] = z; basecode[41] = o; basecode[42] = z; basecode[43] = o;
- basecode[114] = z; basecode[115] = o; basecode[116] = z; basecode[117] = o; basecode[118] = z; basecode[119] = o; basecode[120] = o; basecode[121] = z; basecode[122] = o; basecode[123] = z; basecode[124] = z; basecode[125] = o; break;
- case 19: //Confirmed
- basecode[36] = o; basecode[37] = z; basecode[38] = o; basecode[39] = z; basecode[40] = z; basecode[41] = o; basecode[42] = z; basecode[43] = o;
- basecode[114] = z; basecode[115] = o; basecode[116] = o; basecode[117] = z; basecode[118] = z; basecode[119] = o; basecode[120] = o; basecode[121] = z; basecode[122] = o; basecode[123] = z; basecode[124] = z; basecode[125] = o; break;
- case 20: //Confirmed
- basecode[36] = z; basecode[37] = o; basecode[38] = z; basecode[39] = o; basecode[40] = o; basecode[41] = z; basecode[42] = z; basecode[43] = o;
- basecode[114] = z; basecode[115] = o; basecode[116] = z; basecode[117] = o; basecode[118] = o; basecode[119] = z; basecode[120] = o; basecode[121] = z; basecode[122] = o; basecode[123] = z; basecode[124] = z; basecode[125] = o; break;
- case 21: //Confirmed
- basecode[36] = o; basecode[37] = z; basecode[38] = z; basecode[39] = o; basecode[40] = o; basecode[41] = z; basecode[42] = z; basecode[43] = o;
- basecode[114] = z; basecode[115] = o; basecode[116] = o; basecode[117] = z; basecode[118] = o; basecode[119] = z; basecode[120] = o; basecode[121] = z; basecode[122] = o; basecode[123] = z; basecode[124] = z; basecode[125] = o; break;
- case 22: //Confirmed
- basecode[36] = z; basecode[37] = o; basecode[38] = o; basecode[39] = z; basecode[40] = o; basecode[41] = z; basecode[42] = z; basecode[43] = o;
- basecode[114] = z; basecode[115] = o; basecode[116] = z; basecode[117] = o; basecode[118] = z; basecode[119] = o; basecode[120] = z; basecode[121] = o; basecode[122] = z; basecode[123] = o; basecode[124] = o; basecode[125] = z; break;
- case 23: //Confirmed
- basecode[36] = o; basecode[37] = z; basecode[38] = o; basecode[39] = z; basecode[40] = o; basecode[41] = z; basecode[42] = z; basecode[43] = o;
- basecode[114] = z; basecode[115] = o; basecode[116] = o; basecode[117] = z; basecode[118] = z; basecode[119] = o; basecode[120] = z; basecode[121] = o; basecode[122] = z; basecode[123] = o; basecode[124] = o; basecode[125] = z; break;
- case 24: //Confirmed
- basecode[36] = z; basecode[37] = o; basecode[38] = z; basecode[39] = o; basecode[40] = z; basecode[41] = o; basecode[42] = o; basecode[43] = z;
- basecode[114] = o; basecode[115] = z; basecode[116] = z; basecode[117] = o; basecode[118] = o; basecode[119] = z; basecode[120] = z; basecode[121] = o; basecode[122] = o; basecode[123] = z; basecode[124] = z; basecode[125] = o; break;
- case 25: //Confirmed
- basecode[36] = o; basecode[37] = z; basecode[38] = z; basecode[39] = o; basecode[40] = z; basecode[41] = o; basecode[42] = o; basecode[43] = z;
- basecode[114] = o; basecode[115] = z; basecode[116] = o; basecode[117] = z; basecode[118] = o; basecode[119] = z; basecode[120] = z; basecode[121] = o; basecode[122] = o; basecode[123] = z; basecode[124] = z; basecode[125] = o; break;
- case 26: //Confirmed
- basecode[36] = z; basecode[37] = o; basecode[38] = o; basecode[39] = z; basecode[40] = z; basecode[41] = o; basecode[42] = o; basecode[43] = z;
- basecode[114] = o; basecode[115] = z; basecode[116] = z; basecode[117] = o; basecode[118] = z; basecode[119] = o; basecode[120] = o; basecode[121] = z; basecode[122] = o; basecode[123] = z; basecode[124] = z; basecode[125] = o; break;
- case 27: //Confirmed
- basecode[36] = o; basecode[37] = z; basecode[38] = o; basecode[39] = z; basecode[40] = z; basecode[41] = o; basecode[42] = o; basecode[43] = z;
- basecode[114] = o; basecode[115] = z; basecode[116] = o; basecode[117] = z; basecode[118] = z; basecode[119] = o; basecode[120] = o; basecode[121] = z; basecode[122] = o; basecode[123] = z; basecode[124] = z; basecode[125] = o; break;
- }
- irsend.sendRaw(basecode, 126, 38);
- irsend.sendRaw(ACEnd, 5, 38);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement