I'm stuck and don't know how to get over it. The problem: i want to use a push button for a menu with 2 screens(frames) Frame1: if i push the button i want to turn on the light from the room1 if i press it again i want to turn the light off in the room1
Frame 2: using the same button: i want to turn on the light in the room2 pressing it again i want to turn the light off in the room2
Now.. the problem i encounter... if i'm in frame 1 and press the button then the light from room1 will turn on but also the light from the room2 will turn on. and i don't want that. All i want is to tell somehow to uC that in the frame1 it should turn on only the light from the room1 Please, someone smarter than me ..give me an ideea how can i do that. i cannot post the code because there are over 3000lines and would be time consuming for you to read it all.
for the buttons i use a counter to see if the state has changed and if so then increment and do what it has to do. Also i think that the problem is here because i use the same counter in both functions.
int btn1 = 46,starebtn1, stareVechebtn1 = 0, btn2 = 50, starebtn2, stareVechebtn2 = 0;
int btn3 = 42, starebtn3, stareVechebtn3 = 0,btn4 = 40, starebtn4, stareVechebtn4 = 0,btn5 = 44, starebtn5, stareVechebtn5 = 0;
int cntbtn1 = 0, cntbtn2 = 0,cntbtn2_1 = 0, cntbtn3 = 0, cntbtn4 = 0, cntbtn5 = 0;
int soilMoisture = A0;
int buzzer = 3;
boolean frame1 = false, frame2 = false;
int procentajUmiditate(){
int mSensorValue = analogRead(soilMoisture);
mSensorValue = map(mSensorValue,0,1023,180,0);
return mSensorValue;
}
void setup()
{
Serial.begin(9600);
dht.begin();
rtc.begin();
pinMode(ledVerde,OUTPUT);
pinMode(buzzer,OUTPUT);
pinMode(soilMoisture,INPUT_PULLUP);
pinMode(btn1,INPUT);
pinMode(btn2,INPUT);
pinMode(btn3,INPUT);
pinMode(btn4,INPUT);
pinMode(btn5,INPUT);
imagineSistemDomotic();
delay(3000);
imagineCasaWifi();
delay(4000);
tone(buzzer,1000);
delay(500);
noTone(buzzer);
delay(500);
digitalWrite(ledVerde,HIGH);
}
void loop()
{
starebtn1 = digitalRead(btn1);
starebtn2 = digitalRead(btn2);
starebtn3 = digitalRead(btn3);
starebtn4 = digitalRead(btn4);
starebtn5 = digitalRead(btn5);
int h = dht.readHumidity();
int t = dht.readTemperature()-2;
int f = dht.readTemperature(true);
temp = t;
hum = h;
houR = rtc.getTimeStr();
dateR = rtc.getDateStr();
//conditii procentaj umiditate si pornire automata irigare
if(procentajUmiditate()<20)
{
//relayOn
}
else
{
//relayOff
}
//btn1
if(starebtn1 != stareVechebtn1)
{
if(starebtn1 == HIGH)
{
Serial.println(cntbtn1);
cntbtn1++;
}
stareVechebtn1 = starebtn1;
}//end btn1
//btn2
if(starebtn2 != stareVechebtn2)
{
if(starebtn2 == HIGH)
{
cntbtn2 = cntbtn2+1;
}
stareVechebtn2 = starebtn2;
}//endbtn2
//btn3
if(starebtn3 != stareVechebtn3)
{
if(starebtn3 == HIGH)
{
cntbtn3 = cntbtn3+1;
}
stareVechebtn3 = starebtn3;
}//endbtn3
//btn4
if(starebtn4 != stareVechebtn4)
{
if(starebtn4 == HIGH)
{
cntbtn4 = cntbtn4+1;
}
stareVechebtn4 = starebtn4;
}//endbtn4
//btn5
if(starebtn5 != stareVechebtn5)
{
if(starebtn5 == HIGH)
{
cntbtn5 = cntbtn5+1;
}
stareVechebtn5 = starebtn5;
}//endbtn5
//ecran1
if(cntbtn1 == 1)
{
displayInteriorTempHum();
//u8g.drawStr(23,58,temp.c_str());
// u8g.drawStr(99,58,hum.c_str());
}
//ecran2
if(cntbtn1 == 2)
{
displayInteriorLights();
}
//ecran3
if(cntbtn1 == 3)
{
displayExteriorValues();
}
if(cntbtn1 == 4) cntbtn1 = 0;
if(cntbtn2 == 3) cntbtn2 = 1;
if(cntbtn2_1 == 3) cntbtn2_1 = 1;
if(cntbtn3 == 3) cntbtn3 = 1;
} //end loop
void imagineSistemDomotic()
{
u8g.firstPage();
do{
u8g.drawFrame(0,0,128,64);
u8g.setFont(u8g_font_courB10);
u8g.drawStr(0,30,"Sistem Domotic");
}while(u8g.nextPage());
}
void imagineCasaWifi()
{
u8g.firstPage();
do{
u8g.drawFrame(0,0,128,64);
u8g.drawBitmapP( 0, 1, 16, 64, CasaDomoticaImage2);
}while(u8g.nextPage());
}
void tempInterior()
{
u8g.firstPage();
do{
u8g.setFont(u8g_font_courB10);
u8g.drawStr(32,10,"Interior");
u8g.drawFrame(0,15,128,49);
u8g.setFont(u8g_font_5x7);
u8g.drawStr(2,24,dateR.c_str());
u8g.drawStr(101,24,houR.c_str());
u8g.setFont(u8g_font_courB10);
u8g.drawBitmapP( -3, 4, 16, 64, Temp1);
u8g.drawCircle(40,46,1);
u8g.drawStr(23,58,"26");
u8g.setFont(u8g_font_10x20);
u8g.drawStr(43,58,"C");
u8g.setFont(u8g_font_courB10);
u8g.drawBitmapP( 66, 10, 16, 64, UmidLogo);
u8g.drawStr(99,58,"57");
u8g.drawStr(117,58,"%");
}while(u8g.nextPage());
}
void displayExteriorValues()
{
frame1 = false;
frame2 = true;
u8g.firstPage();
do{
u8g.setFont(u8g_font_courB10);
u8g.drawStr(32,10,"Exterior");
u8g.drawFrame(0,12,128,51);
u8g.drawLine(64,12,64,62);
if(cntbtn2 == 1)
{
//u8g.drawBitmapP( 15, 30, 16, 64, robinetApaOprita);
u8g.setFont(u8g_font_5x8);
u8g.drawStr(78,23,"Irigare");
u8g.drawStr(78,30,"pornita");
u8g.drawBitmapP( 72, 8, 16, 64, robinetApaPornita);
u8g.setPrintPos(15,40);
u8g.setFont(u8g_font_courB10);
u8g.print(procentajUmiditate());
if(procentajUmiditate()<100)
{
u8g.setPrintPos(34,40);
u8g.print("%");
}
else
{
u8g.setPrintPos(44,40);
u8g.print("%");
}
}
else{
//u8g.drawBitmapP( 15, 30, 16, 64, robinetApaOprita);
u8g.setFont(u8g_font_5x8);
u8g.drawStr(78,23,"Irigare");
u8g.drawStr(78,30,"pornita");
u8g.drawBitmapP( 72, 8, 16, 64, robinetApaPornita);
// u8g.drawBitmapP( 20, -20, 16, 64, LightOff);
// valoare buna FanOffLOGO u8g.drawStr(10,23,"Ventilatie");
// valoare buna FanOffLOGO u8g.drawStr(20,30,"oprita");
// u8g.drawBitmapP( -15, 24, 16, 64, fanOffLogo);
u8g.drawStr(10,23,"Umid. Sol");
u8g.drawBitmapP( 12, -1, 16, 64, UmiditateSol);
}
}while(u8g.nextPage());
cntbtn2 = 0;
}
void displayInteriorLights()
{
u8g.firstPage();
do{
u8g.setFont(u8g_font_courB10);
u8g.drawStr(32,10,"Interior");
u8g.drawFrame(0,12,128,51);
u8g.drawLine(64,12,64,62);
u8g.setFont(u8g_font_5x8);
if(cntbtn2 == 1)
{
u8g.drawStr(73,23,"Dormitor");
u8g.drawStr(75,60,"L.aprinsa");
u8g.drawBitmapP( 78, 7, 16, 64, LightOn);
}
else if(cntbtn2 == 2)
{
u8g.drawStr(73,23,"Dormitor");
u8g.drawStr(75,60,"L.stinsa");
u8g.drawBitmapP( 78, 7, 16, 64, LightOff);
}
else{
u8g.drawStr(73,23,"Dormitor");
u8g.drawStr(75,60,"L.stinsa");
u8g.drawBitmapP( 78, 7, 16, 64, LightOff);
}
if(cntbtn3 == 1)
{
u8g.drawStr(10,23,"Sufragerie");
u8g.drawStr(15,60,"L.aprinsa");
u8g.drawBitmapP( 20, 8, 16, 64, LightOn);
}
else if(cntbtn3 == 2)
{
u8g.drawStr(10,23,"Sufragerie");
u8g.drawStr(15,60,"L.stinsa");
u8g.drawBitmapP( 20, 8, 16, 64, LightOff);
}
//else if(digitalRead(relay)
else{
u8g.drawStr(10,23,"Sufragerie");
u8g.drawStr(15,60,"L.stinsa");
u8g.drawBitmapP( 20, 8, 16, 64, LightOff);
}
}while(u8g.nextPage());
}
void displayInteriorTempHum()
{
u8g.firstPage();
do{
u8g.setFont(u8g_font_courB10);
u8g.drawStr(32,10,"Interior");
u8g.drawFrame(0,15,128,49);
u8g.setFont(u8g_font_5x7);
u8g.drawStr(2,24,dateR.c_str());
u8g.drawStr(101,24,houR.c_str());
u8g.setFont(u8g_font_courB10);
u8g.drawBitmapP( -3, 4, 16, 64, Temp1);
u8g.drawCircle(40,46,1);
u8g.drawStr(23,58,temp.c_str());
u8g.setFont(u8g_font_10x20);
u8g.drawStr(43,58,"C");
u8g.setFont(u8g_font_courB10);
u8g.drawBitmapP( 66, 10, 16, 64, UmidLogo);
u8g.drawStr(99,58,hum.c_str());
u8g.drawStr(117,58,"%");
digitalWrite(ledVerde,LOW);
}while(u8g.nextPage());
}