1

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());
}
2
  • Sorry, but your question is very unclear. Can you edit your post using correct prepositions? Like: "IN the kitchen", "IN the bedroom". Also, "SWITCH ON the light bulb". Remember, saying "switching light FROM x room" means: "Switching light by pressing button which is located IN room x".
    – smajli
    Commented Jun 10, 2019 at 10:00
  • whenever the button is pressed, check the room number that it is operating on, then act accordingly
    – jsotola
    Commented Jun 10, 2019 at 18:50

3 Answers 3

1

Difficult to advise without more clear information but here's a couple of ideas to play around with (currently typing on my phone, will edit later).

If using a physical menu and buttons then you need some way to track location. You could try adding proximity sensors to check physical location/proximity to the light source and let that be the deciding variable for which bulb gets activated. You could use a "universal remote" approach, using separate arduinos (per light) with IR receivers.

If using an app menu, like on a phone, try declaring a variable "room" and have the button pass the frame name or ID number based on the frame ("bedroom" or "kitchen", "1" or "2" etc) as that variable via your communication line to the arduino. On the arduino side, declare the same variable and process the received data from the app i.e. String room = your-room-ID. Ensure you have separate pinout declarations for each bulb you want to control i.e. #define kitchen 14 # define livingroom 15... setPin(output, kitchen), setPin(output, livingroom) then modify your bulb activation line to be pinout(room, HIGH);

4
  • Reading your answer i understand mostly that i was not so clear in my post. And i am agree with you on that. Now.. imagine that i have an glcd which is divided by 2. On the left is room 1 light and on the right is ventilation for room1. That in the Frame 1. In the Frame 2(btw i scroll into the frames using another button) so, in Frame 2 the image is also divided by 2. On the left light for room2 and on the right is the temp for room2. Now, besides the button that i use for switching the frames, i have another 2 that i want to use for "current" frame that i am in. Commented Jun 10, 2019 at 10:14
  • So.. if i press the button wich is positioned under the left side lf the screen. I could turn on or off the light in the room 1. But the problem is that the light will be turned on even in the room 2 Commented Jun 10, 2019 at 10:21
  • My app code notes still apply, you are controlling the glcd with the same arduino? If so then just add an extra line of code to set your room variable when the frame is changed and use the rest of the pin coding as I described. This is all assuming there are no errors in your current coding that are making this result happen, without your sketch we will never be able to tell.
    – Dan Bailey
    Commented Jun 10, 2019 at 10:50
  • here is a part of it Commented Jun 10, 2019 at 10:56
1

It's really very simple, I think... If I understood properly...

global frame, lightRoom1, lightRoom2

function changeFrame ( ) {

// if frame == 1, change it to 2

    if ( frame == 1 ) {
        <commands to refresh display>;
        frame = 2;
    } else {
        <commands to refresh display>
        frame = 1;
    }

}

function switchLight ( ) {

// roomno = frameno.
// if light on, switch off; if off, switch on.

    if ( frame == 1 ) {

        if ( lightRoom1 == false ) {
            <code to switch light on >;
            lightRoom1 == true;
        } else {
            <code to switch light off >;
            lightRoom1 == false;
        }

    }

    if ( frame == 2 ) {

        // same as above but for room 2.

    }

}


if ( digitalRead ( framePin ) == LOW ) {
    changeFrame ( );
}

if ( digitalRead ( lightPin ) == LOW ) {
    switchLight ( );
}
0

The code you show is indeed too long. However, a cursory look seems to indicate that you are not using any debouncing library. This is then the first thing you should do. There are probably many options. I will suggest Bounce2 which, in addition to debouncing, handles the edge detection. Then you have the fell() method that tells you whether the input signal just fell since the last time it was tested, which is a way to detect the button press.

Then, within loop(), you would add something like this:

if (mainButton.update() && mainButton.fell()) {

    // Find the pin that controls the appropriate light.
    uint8_t lightPin;
    if (currentFrame == FRAME1) {
        lightPin = PIN_LIGHT_ROOM_1;
    } else {  // FRAME2
        lightPin = PIN_LIGHT_ROOM_2;
    }

    // Toggle the light.
    if (digitalRead(lightPin) == HIGH) {
        digitalWrite(lightPin, LOW);
    } else {
        digitalWrite(lightPin, HIGH);
    }
}

The assumptions I am making should be pretty obvious:

  • currentFrame is a variable updated by your menu system. Its value can only be one of the constants FRAME_1 or FRAME_2
  • PIN_LIGHT_ROOM_1 and PIN_LIGHT_ROOM_2 are constants holding the pin numbers which control the corresponding lights.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.