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
Here's an untested (but compiles) example of how you might do such things with a state machine.
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