While loop before if-else statement not working?

Viewed 25

I have a code illuminating an LED using a few different inputs. I'm not sure why but the (while) command to turn off the light when the battery is low, isn't executing. Currently it's draining below the 3v level all the way down to 1.9v. Could it be because of my code structure?

While- measures battery and shuts off when under 3v

if (rf95.available())- listens for a radio signal and switch command

if(digitalRead(SWITCH))- listens for physical switch command else- leaves the LED on if no commands are sent

Watchdog.sleep- deep sleep command to save battery

void loop(){
    Serial.begin(115200);
    
    uint8_t buf[RH_RF95_MAX_MESSAGE_LEN];      
    uint8_t len = sizeof(buf);  
   
     float measuredvbat = analogRead(VBATPIN);
     measuredvbat *= 2; // we divided by 2, so multiply back
     measuredvbat *= 3.3; // Multiply by 3.3V, our reference voltage
     measuredvbat /= 1024; // convert to voltage
     measuredvbat /=1.61; //  fudge factor
    
     Serial.print("VBat: " ); 
     Serial.println(measuredvbat);

         while(measuredvbat < 3.00){
           analogWrite(LED,0);
           Serial.print("VBat: " ); 
           Serial.println(measuredvbat);
           Serial.println("Battery Low");
           
           int sleepMS = Watchdog.sleep(3600000);
         }
 
         if (rf95.available()){                       
          if (rf95.recv(buf, &len)) {
           if(strncmp(buf, "TurnOn", strlen(buf)) == 0){
            Serial.println("Turn Up For WAT !!");
            analogWrite(LED,75);
           }
          }
         }
          
         if(digitalRead(SWITCH)){
          Serial.println("SWITCH-ON-12");
          analogWrite(LED, 75);           
         }
         
         else{
          Serial.println("SWITCHED ON-12");
          analogWrite(LED, 112);
         }
  }
0 Answers
Related