# Code Millis

**URL:** <https://forum.arduino.cc/t/code-millis/685943>\
**Category:** Programming\
**Created:** [December 6, 2020, 7:52pm UTC](https://forum.arduino.cc/t/code-millis/685943 "2020-12-06T19:52:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![louloul](https://avatars.discourse-cdn.com/v4/letter/l/a5b964/32.png) [@louloul](https://forum.arduino.cc/u/louloul)\
**Post date:** [December 6, 2020, 7:52pm UTC](https://forum.arduino.cc/t/code-millis/685943/1 "2020-12-06T19:52:16Z")

</div>

```cpp
hello,

I am a beginner and I am trying to introduce the millis () function in a program but cannot do.
I have no idea how to use it.
Could you show me how to code with the millis () function so that:

If my sensor is less than 200: it is the RED LED ON: // this I succeeded.

If my sensor is greater than 200 for more than 5s since the change of state of my sensor: the GREEN LED lights up
If my sensor is greater than 200 for less than 5s since the change of state of my sensor: the RED LED lights up

Here is my code. I thank you in advance.

```

void setup()  
{  
// initialize serial communications at 9600 bps:  
Serial.begin(9600);  
// initialize l'entrée capteur  
pinMode(9, INPUT\_PULLUP);  
// initialize la sortie led  
pinMode (10, OUTPUT) ; // 10 : LED rouge  
pinMode (12, OUTPUT) ; // 12 : LED vert  
digitalWrite(10,LOW);  
digitalWrite(12,HIGH);

}  
void loop()  
{  
// read the analog in value:  
delay(1000);  
sensorValue = analogRead(analogInPin);  
Serial.println(sensorValue) ;

// si detection  
if (sensorValue \< 200){  
digitalWrite(10,HIGH);  
digitalWrite(12,LOW) ;}

if (sensorValue \> 200) && ((millis() - temps) \< 5000)  
{ temps = millis();  
digitalWrite(10,HIGH);  
digitalWrite(12,LOW) ;}

// Si pas de detection  
else {  
digitalWrite(10,LOW);  
digitalWrite(12

---

<div class="post-metadata">

**Author:** ![anon73444976](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon73444976](https://forum.arduino.cc/u/anon73444976)\
**Post date:** [December 6, 2020, 8:00pm UTC](https://forum.arduino.cc/t/code-millis/685943/2 "2020-12-06T20:00:05Z")

</div>

Your code is incomplete.

Please remember to use code tags when posting code

---

<div class="post-metadata">

**Author:** ![groundFungus](https://avatars.discourse-cdn.com/v4/letter/g/73ab20/32.png) [@groundFungus](https://forum.arduino.cc/u/groundFungus)\
**Post date:** [December 6, 2020, 8:01pm UTC](https://forum.arduino.cc/t/code-millis/685943/3 "2020-12-06T20:01:09Z")

</div>

Using millis() for timing tutorials:  
[Several things at a time.](https://forum.arduino.cc/index.php?topic=223286.0)  
[Beginner's guide to millis().](https://forum.arduino.cc/index.php?topic=503368.0)  
[Blink without delay().](https://www.arduino.cc/en/tutorial/BlinkWithoutDelay)

---

<div class="post-metadata">

**Author:** ![Koepel](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/koepel/32/637546_2.png) [@Koepel](https://forum.arduino.cc/u/Koepel)\
**Post date:** [December 6, 2020, 10:07pm UTC](https://forum.arduino.cc/t/code-millis/685943/4 "2020-12-06T22:07:57Z")

</div>

Please modify your first post and remove the [left] and [pre] tags and use code-tags for the sketch.

---

<div class="post-metadata">

**Author:** ![Blackfin](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/blackfin/32/238871_2.png) [@Blackfin](https://forum.arduino.cc/u/Blackfin)\
**Post date:** [December 6, 2020, 11:27pm UTC](https://forum.arduino.cc/t/code-millis/685943/5 "2020-12-06T23:27:34Z")

</div>

Here's an untested (but compiles) example of how you might do such things with a state machine.

```cpp
enum
{
    WAIT_LESS_THAN_200 = 0,
    WAIT_FOR_MORE_THAN_200,
    CHECK_TIME_ELAPSED_SINCE_CHANGE,        
};

const uint8_t pinSensorInput = 9;      //unused?
const uint8_t pinRougeLED = 10;
const uint8_t pinVertLED = 12;
const uint8_t analogInPin = A0;        //correct pin? was not previously defined

int sensorValue;

void setup()
{
    // initialize serial communications at 9600 bps:
    Serial.begin(9600);
    
    // initialize l'entrée capteur
    pinMode( pinSensorInput, INPUT_PULLUP );  
    
    // initialize la sortie led
    pinMode( pinRougeLED, OUTPUT ) ; //  10 : LED rouge
    pinMode( pinVertLED, OUTPUT ) ; // 12 : LED vert
    digitalWrite( pinRougeLED,LOW );
    digitalWrite( pinVertLED, HIGH );
    
}//setup

void loop()
{
    static uint8_t
        state = WAIT_LESS_THAN_200;
    static uint32_t
        timeStateChange,
        timeReadSensor = 0;
    uint32_t
        timeNow = millis();
        
    // si detection 20x/sec
    if( (timeNow - timeReadSensor) >= 50ul )
    {
        timeReadSensor = timeNow;
        sensorValue = analogRead(analogInPin);  
        Serial.println(sensorValue) ;                
        
    }//if

    switch( state )
    {
        case    WAIT_LESS_THAN_200:
            //we wait here for the sensor to read < 200
            if( sensorValue < 200 )
            {
                //If my sensor is less than 200: it is the RED LED ON                
                digitalWrite( pinVertLED, LOW );
                digitalWrite( pinRougeLED, HIGH );

                //then go to the state waiting for the sensor to read >= 200
                state = WAIT_FOR_MORE_THAN_200;
                
            }//if
            
        break;

        case    WAIT_FOR_MORE_THAN_200:
            //sensor is less than 200 now; here we wait for it to go >= 200
            if( sensorValue >= 200 )
            {            
                //If my sensor is greater than 200 for less than 5s since the change of state of my sensor: the RED LED lights up
                //
                //this is same LED state as <200 (?)
                digitalWrite( pinVertLED, LOW );
                digitalWrite( pinRougeLED, HIGH );

                //get the time we saw this transition to >= 200
                timeStateChange = timeNow;
                //and move to time the 5-sec period
                state = CHECK_TIME_ELAPSED_SINCE_CHANGE;
                
            }//if
        
        break;

        case    CHECK_TIME_ELAPSED_SINCE_CHANGE:
            //value had increased to >=200; if it falls below 200 again, go back to the first state
            if( sensorValue < 200 )
                state = WAIT_LESS_THAN_200;
            //otherwise check to see if 5-second has elapsed...
            else if( (timeNow - timeStateChange) >= 5000ul )
            {
                //If my sensor is greater than 200 for more than 5s since 
                //the change of state of my sensor: the GREEN LED lights up
                digitalWrite( pinVertLED, HIGH );
                digitalWrite( pinRougeLED, LOW );

                //I'm guessing you can go back to wait for <200 again
                state = WAIT_LESS_THAN_200;
                
            }//else
            
        break;
                
    }//switch
    
}//loop

```

---

<div class="post-metadata">

**Author:** ![louloul](https://avatars.discourse-cdn.com/v4/letter/l/a5b964/32.png) [@louloul](https://forum.arduino.cc/u/louloul)\
**Post date:** [December 11, 2020, 2:30pm UTC](https://forum.arduino.cc/t/code-millis/685943/6 "2020-12-11T14:30:17Z")

</div>

```cpp
Hello Blackfin,

Thanks so much for the code.
This works !!! I'm happy.
Have a good day.

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 6, 2021, 12:53am UTC](https://forum.arduino.cc/t/code-millis/685943/7 "2021-05-06T00:53:12Z")

</div>

Closed on May 6, 2021, 12:53am UTC


