Hi! I am a beginner Arduino user. Recently I write a code by using Millis function. But it is not working. can you help me find out my fault in coding. I am sharing my code below:
unsigned long currentTime = 0;
unsigned long previousTime1 = 0;
unsigned long previousTime2 = 0;
other issues are the button switch is typically connected between the pin and ground and the pin is configure as INPUT_PULLUP. the makes it HIGH until it is depressed. the condition should check for it being LOW.
void loop() {
unsigned long currentTime = millis();
if (digitalRead(12) == HIGH && currentTime-previousTime1>=2000) // when pressure switch is low which detected by arduino as high input & 2 sec delay
{
digitalWrite(2, HIGH); // compressor-01 start command
previousTime1 = currentTime;
if (digitalRead(12) == HIGH && currentTime-previousTime2>=6000) // compressor-01 remain running for from 2 sec to 6 sec
{
digitalWrite(2, LOW); // after 6 sec compressor-01 stop command
previousTime2 = currentTime;
}
else {
digitalWrite(2, LOW); // if pressure switch is high which detected by arduino as low input for compressor-01 stop command.
}
that's not much different from what you originally posted. and as already explained, the logic is flawed.
it would be more helpful if you explained in words what you want the code to do. seems like you want it to flash an LED on/off every 6 seconds when a button is pressed
thanks all for your help. I am new here. let me explain what i want to execute through code:
there will be one digital input signal
two digital output signal
condition_1: **when digital input is high ** : digital output_1 will be high from 0 to 10 seconds & that time digital output_2 will be low. Then from 11 to 20 seconds digital output_2 will be high and that time digital output_1 will be low. This loop will continue as long as digital input is high.
condition_2: when digital input is low : both digital output_1 and digital output_2 will be low.
** I am trying to learn "Auto Format the code in the IDE". I will post the code in that format after a while.
Have you tested JUST the digital input to make sure it is stable? What is the source of the digital input?
Values you are testing against millis() against and number of outputs do not correspond with the following description:
condition_1: **when digital input is high ** : digital output_1 will be high from 0 to 10 seconds & that time digital output_2 will be low. Then from 11 to 20 seconds digital output_2 will be high and that time digital output_1 will be low. This loop will continue as long as digital input is high.
condition_2: when digital input is low : both digital output_1 and digital output_2 will be low.
If the input goes LOW and then HIGH again should condition_1 timings start all over again or resume?
Then a single start time is sufficient. During interval 1 (up to 10s) output_1 will be high, else during interval 2 (up to 20s) output_2 will be high, and afterwards the loop continues with the new start time.
For the change of the input see the StateChangeDetection example. If the button goes on the loop is started and going off ends the loop.
thanks for your help. code is working. Only one flow, (that because may be i couldn't make it clear to you. ) .
when input goes from LOW to HIGH, output_1 take a 10 second delay (outputTime = 10000) to give HIGH . Instead i wanted output_1 will be high immediately after input goes from LOW to HIGH.
well, now it is okay. but another problem is like that: after switch on Arduino power and when input goes high then immediately output_1 goes to High. But after that if i press the Arduino reset button then it wait one cycle Output_1 as low although input is HIGH. other than that code is working perfectly. thanks for your help.