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:
1 Answer 1
The millis()
-based programming idiom you are using is intended to
handle delays without ever blocking the program in a delay()
call.
Then you are completely defeating its purpose by calling delay(90000)
.
After this 1.5 min delay, of course one minute will have elapsed, and
the test currentMillis - previousMillis > 60000
will always be true.
If you want to embrace the non-blocking paradigm millis()
allows, you
should completely ban delay()
from your vocabulary (as well as other
blocking loops).
Edit: Answering the question in the comment "Can you suggest as to how can I modify the code for 1.5 minutes delay". As suggested by JRobert in another comment, you should follow the "Blink Without Delay" Arduino example. There is only one caveat: this Arduino example keeps the LED on for one second and off for another second, while you want to toggle your system between two states with different durations.
The solution is to remember what state you are currently on, and write
separate code paths for handling each state. As an example, the code
below toggles the on-board LED, keeping it on for 100 ms and off
for 900 ms. This is achieved by remembering the current state (in
the boolean variable is_on
below) and conditioning the code execution
on the current state (e.g. if (is_on && ...)
).
/*
* Toggle the on-board LED on for 100 ms every second.
*/
const unsigned long time_on = 100; // on time = 100 ms
const unsigned long time_off = 900; // off time = 900 ms
unsigned long previousMillis; // previous time the LED was toggled
bool is_on; // is the LED currently on?
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
is_on = false;
}
void loop() {
unsigned long currentMillis = millis();
// If the LED has been on long enough, turn it off.
if (is_on && currentMillis - previousMillis >= time_on) {
digitalWrite(LED_BUILTIN, LOW);
is_on = false;
previousMillis = currentMillis;
}
// If the LED has been off long enough, turn it on.
if (!is_on && currentMillis - previousMillis >= time_off) {
digitalWrite(LED_BUILTIN, HIGH);
is_on = true;
previousMillis = currentMillis;
}
}
-
Can you suggest as to how can I modify the code for 1.5 minutes delay should I write millis loopYukta Sanghvi– Yukta Sanghvi2024年06月30日 12:49:21 +00:00Commented Jun 30, 2024 at 12:49
-
See the Examples in the Arduino IDE. In the second group, 'Digital', the 'Blink Without Delay' sketch shows a good example of how to do that.JRobert– JRobert2024年06月30日 14:17:02 +00:00Commented Jun 30, 2024 at 14:17
-
1@YuktaSanghvi your code already contains a 1 minute delay using millis() ... what is preventing you from changing it to 1.5 minutes?jsotola– jsotola2024年06月30日 21:56:38 +00:00Commented Jun 30, 2024 at 21:56