I am using millis () function but after one iteration it does not check the condition and enters the loop. What should I do?

I am using mq7 sensor where I have to toggle vcc between 5 to 1.5 V for 1 min and 1.5 min. For this I am using millis function.But after 1st iteration it skips the condition and enters the loop.

The code is as follows:

    //the number of the LED pin
    const int ledPin = 14;  
     float sensorPin = 34;
    // setting PWM properties
    const int freq = 5000;
    const int ledChannel = 0;
    const int resolution = 8;
    unsigned long previousMillis = 0;  
    
     
    void setup(){
      // configure LED PWM functionalitites
      Serial.begin(115200);
      Serial.println("started");
      ledcAttach(ledPin, freq, resolution);
      ledcAttachChannel(ledPin, freq, resolution, 0);
    }
     
    void loop(){
      // increase the LED brightness
     unsigned long  currentMillis = millis();
      Serial.println("hii");
    // changing the LED brightness with PWM
        ledcWrite(ledPin, 255);
       if (currentMillis - previousMillis > 60000) 
       {
        previousMillis = currentMillis;
        // save the last time you blinked the LED
        Serial.println("switching");  
        // changing the LED brightness with PWM
        ledcWrite(ledPin, 95);   
        delay(90000);
        Serial.println("switching"); 
         // we need to read the sensor at 5V, but must not let it heat up. So hurry!  
        ledcWrite(ledPin, 255);
        delay (50); //don't know how long to wait without heating up too much. Getting an analog read apparently takes 100uSec  
       // read the value from the sensor:  
       Serial.println("switching"); 
        int sensorValue = analogRead(sensorPin);  
        Serial.println(sensorValue);  
       }
    }

The output is as follows:

enter image description here

That is how loop() works it will execute the code over and over.
What do you want it to do?

//the number of the LED pin
const int ledPin = 14;
const int sensorPin = 34;
// setting PWM properties
const int freq = 5000;
const int ledChannel = 0;
const int resolution = 8;

void setup() {
  // configure LED PWM functionalitites
  Serial.begin(115200);
  Serial.println("started");
  ledcAttach(ledPin, freq, resolution);
  ledcAttachChannel(ledPin, freq, resolution, 0);
}

void loop() {
  Serial.println("hii");
  ledcWrite(ledPin, 255); // changing the LED brightness with PWM
  delay (60000);
  Serial.println("switching95");
  ledcWrite(ledPin, 95);
  delay(90000);
  Serial.println("switching255");
  Serial.println(analogRead(sensorPin));
}

What I want to do is toggle vcc between 5 and 1.5 V for 1 min and 1.5 min. It should be done ideally after executing the loop, I want vcc to be 5v which done by pwm but here the loop is getting executed continuously

Anything in loop() will execute over and over forever.
If you want something to only execute once, you can either put it in setup() or use a flag to indicate that it has already been done.

Please describe, succinctly, what the output does. So far, it looks like
setup()
initialize output
loop()
set output to 255
wait 1 minute
set output to 95
wait 1.5 minutes
<and do what now, set the output to 0?> <<<<this, you haven't defined

1 Like

Another problem with the code is that you are checking that the difference between currentMillis and previousMillis is greater than 60000mS, then after setting previousMillis to currentMillis you delay for 90000mS, guaranteeing that the difference will always be greater than 60000 the next time the if statement executes.

Also, I doubt you're actually changing VCC, rather you're changing an output voltage which is VCC for some downstream device, such as a sensor, correct?

Be aware, if this is feeding a sensor, a PWM output isn't a suitable source of a sensor power supply.

After loop executes...loop executes. If you want execution to stop, you need to end loop() with an endless loop, such as:
while(1){};
which never finishes, so loop stalls out.

By the way, what processor? ESP32, Uno, ??
Your code doesn't compile; is it missing a #include line? Please post a complete code listing. That is best done by

  • in the IDE, press ctrl-T to format the code
  • in the IDE, press ctrl-shft-C, to copy the code FOR FORUM
    • in a new forum message in this thread, press ctrl-V to place the entire code(will have code tags added automatically), then add any commentary you feel is needed outside the code block.
      Thank you for cooperating, list members appreciate when you make it easier to help you.

Mixing delay function with millis timer, not so elegant..
Could use one millis timer and add a simple state machine..

Maybe something like..

//the number of the LED pin
const byte ledPin = 14;
const byte sensorPin = 34;
// setting PWM properties
const int freq = 5000;
const byte ledChannel = 0;
const byte resolution = 8;
unsigned long previousMillis = 0;
unsigned long intervalDelay = 0;

byte StateSys = 0;

void setup() {
  // configure LED PWM functionalitites
  Serial.begin(115200);
  Serial.println("started");
  ledcAttach(ledPin, freq, resolution);
  ledcAttachChannel(ledPin, freq, resolution, 0);
}

void loop() {
  unsigned long  currentMillis = millis();
  int sensorValue = 0;
  //Serial.println("hii");
  if (currentMillis - previousMillis > intervalDelay)
  {
    previousMillis = currentMillis;
    switch (StateSys) {
      case 0:  // increase the LED brightness
        Serial.println("increasing LED brightness..");
        ledcWrite(ledPin, 255);
        intervalDelay = 60000ul;
        StateSys = 1;
        break;
      case 1: Serial.println("LED brightness 95");
        // changing the LED brightness with PWM
        ledcWrite(ledPin, 95);
        intervalDelay = 90000ul;
        StateSys = 2;
        break;
      case 2: Serial.println("LED brightness 255");
        // we need to read the sensor at 5V, but must not let it heat up.So hurry!
        ledcWrite(ledPin, 255);
        intervalDelay = 50ul;
        StateSys = 3;
        break;
      case 3:  Serial.print("Reading Sensor :");
        sensorValue = analogRead(sensorPin);
        Serial.println(sensorValue);
        intervalDelay = 0;
        StateSys = 0;
        break;
    }
  }
}

good luck.. ~q

This is what you want to do
You need to read the sensor at 1.5V NOT 5V

// Jim
# define sixty 60000
# define ninty 90000

const int ledPin = 14;
bool flag1, flag2;
unsigned long time1, time2;

void setup()
{
  Serial.begin(115200);
  time1 = millis();
  flag1 = true;
  flag2 = false;
  Serial.println("Running");
  Serial.println("");
  analogWrite(ledPin, 255);
}

void loop()
{

  if ((millis() - time1 > sixty) && flag1)
  {
    time2 = millis();
    flag1 = false;
    flag2 = true;
    Serial.println(" 60 sec @ 5V done");
    analogWrite(ledPin, 77);
  }

  if ((millis() - time2 > ninty) && flag2)
  {
    analogRead(A0);
    time1 = millis();
    flag1 = true;
    flag2 = false;
    Serial.println(" 90 sec @ 1.5V done");
    Serial.print("Gas:  ");
    Serial.println (A0); //Read the sensor
    delay(10);
    Serial.println("-----------");
    Serial.println("");
    analogWrite(ledPin, 255);

  }

}
//

As your topic does not relate directly to the installation or operation of the IDE it has been moved to the Programming Questions category of the forum

This is already answered at ASE.

OP. Please give the folks on one site a chance before you pepper the same question all over the internet. Asking on multiple sites at the same time wastes the time of those trying to help you and it a bit disrespectful. It's mostly the same people on both sites, so you're not increasing your audience. You're actually hurting your chances like that.

2 Likes

Sorry, about it. I am new so didn't knew about it.

I am using esp32

Ohk, will remove the delay.

Did you try the code in post#12 ?
I don't see that the information linked in post#14 was of any use.

Hey, I tried your code. It worked out properly.
Thank you so much for your help.

Glad it worked
Have a nice day!