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);
}
}
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
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.
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
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.