Use millis arduino uno

I'm a beginner please I want to change the code below using millis :

   digitalWrite(RELAY1, HIGH);  
   for (int x=0; x < 8000; x++) {            
      delay(1);
      if (x==6000) {
         digitalWrite(RELAY1, LOW); 
      }
   }
   digitalWrite(RELAY2, HIGH);    
   for (int x=0; x < 8000; x++) {           
      delay(1);
      if (x==6000) {
            digitalWrite(RELAY2, LOW); 
      }
   }

@emtekpro,
Unfortunately there is no Indonesian section of the forum for you to use. There are various international sections in various languages, you can use whichever one suits your language knowledge. Otherwise please post in English in the English part of the forum, you may use Google translate if you need to.

Thank you.

ok thanks

Halo emtekpro

Did you check the BLINKWITHOUTDELAY example of the IDE.
This timer function is the mother of all arduino timers coded.

Do you want to observe that RELAY1 is ON for 6000 ms and 2000 ms silent; RELAY2 is ON for 6000 ms and 2000 ms silent?

Your code is very complex in order to do a very simple sequence. It is equivalent to this much simpler code:

digitalWrite(RELAY1, HIGH);      
delay(6000);
digitalWrite(RELAY1, LOW); 
delay(2000);
digitalWrite(RELAY2, HIGH);
delay(6000);
digitalWrite(RELAY2, LOW); 
delay(2000);

Is there a reason why your code was written in that way?

when relay 1 ON for 8000 ms relay 2 OFF
when relay 1 OFF relay 2 still OFF then delay 2000 ms then relay 2 ON, otherwise

Halo emtekpro
Post a simple timing diagram to see what shall happens.

Digital timing diagram - Wikipedia.

i want use millis for that code

Did you run the BLINKWITHOUTDELAY example of the IDE yet?

You may get a clue from the following excerpt and sketch:

1. Working Principle of millis() Function: The millis() function and millisCounter can be used to account for the elapse of 1000 ms ( for example) time period. The millisCounter starts with initial value of 0 (zero) when the uploading process of a sketch is finished in the UNO. After that it is incremented by the elapse of every 1ms time. The execution of millis() function reads the current content of the millisCounter, which says how much ms (millisecond) time has elapsed since the UNO has started. The millisCounter is a 32-bit unsigned counter (composed of four consecutive RAM locations) and can hold 232 ms time (49-day 17-hour 2-minute 47-second 296-millisecond) before overflow occurs. For example: The millis() function can be used in the following ways to observe if 2000 ms (2 sec) has elapsed from the current instant.

unsigned long prMillis = millis();   //present/current content of millisCounter
	do
	{
			;   //do nothing or some other task
	}
	while(millis – prMillis < 2000)!  

//prMillis is fixed; millis() gives current time of millisCounter 
//which continuously advances by 1 ms.

2. The following sketch uses millis() function to keep L (built0in LED of UNO) ON for 1000 ms and OFF for 5000 ms.

unsigned long prMillis = 0;
byte ledState = HIGH;
bool flag = true;


void setup()
{
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  digitalWrite(13, ledState);   //LED is ON
  prMillis = millis();
}

void loop()
{
  if (flag == true) //ON
  {
    callDelay(1000);
  }
  else //OFF
  {
    callDelay(5000);
  }
}

void callDelay(unsigned long interval)
{
  if (millis() - prMillis >= interval)//5-sec is over
  {
    ledState = !ledState;
    digitalWrite(13, ledState); //LED toggles
    flag = !flag;
    prMillis = millis();
  }
}

ok thank you, I will try to understand it

thanks for the code :slight_smile:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.