Millis help.

Trying to turn on a led time the on time then after X amount of time turn off led. just cant get it.

unsigned long interval = 5000;
unsigned long startTime;
int ledPin=13;
int myColor;
void setup() {

Serial.begin(9600);
pinMode(ledPin,OUTPUT);
}

void loop() {
if(Serial.available()==0){
}
myColor=Serial.parseInt();

if (myColor==1){
digitalWrite(ledPin,HIGH);
startTime = millis();
Serial.println("led is on");

if (millis() - startTime >= interval){
digitalWrite(ledPin,LOW);
} } }

Firstly, please follow the advice on posting a programming question given in Read this before posting a programming question

In particular note the advice to Auto format code in the IDE and to use code tags when posting code here

Here is your code posted in the preferred way

unsigned long interval = 5000;
unsigned long startTime;
int ledPin = 13;
int myColor;
void setup()
{
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
}

void loop()
{
  if (Serial.available() == 0)
  {
  }
  myColor = Serial.parseInt();
  if (myColor == 1)
  {
    digitalWrite(ledPin, HIGH);
    startTime = millis();
    Serial.println("led is on");
    if (millis() - startTime >= interval)
    {
      digitalWrite(ledPin, LOW);
    }
  }
}

As to your problem, if myColor becomes 1 due to serial input then its value will not change unless/until further serial input is received. If no further input is received then startTime will be reset to millis() each time through loop() so the comparison with interval will always fail

Another consideration is what you have the Line ending set to in the Serial monitor as if it set to anything apart from No line ending then there will be bytes in the Serial buffer after the number that you enter so Serial.available() will be greater than zero. What do you have it set to ?

if

 myColor = Serial.parseInt();
  if (myColor == 1)

is true every time through loop(), you are setting a new startTime everytime.
If it isn't true, you are never getting to the

   if (millis() - startTime >= interval)
    {
      digitalWrite(ledPin, LOW);
    }

part.

Hi Bilbo,

I'm not sure what you are trying do do. There is something with serial inside your code.

Please describe in normal words and detailed what the functionality shall be.

here is a demo code showing how to blink with millis()

unsigned long MyTimer;
unsigned long interval = 1000;
int ledPin = 13;

int LED_State = LOW;
unsigned long MyCounter = 0;

// function based on millis() for blinking
// a oneshot-timer needs additional logic
boolean TimePeriodIsOver (unsigned long &expireTime, unsigned long TimePeriod) {
  unsigned long currentMillis  = millis();
  if ( currentMillis - expireTime >= TimePeriod )
  {
    expireTime = currentMillis; // set new expireTime
    return true;                // more time than TimePeriod) has elapsed since last time if-condition was true
  } 
  else return false;            // not expired
}


void setup() {
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
  Serial.println("Setup Start");
}

void loop() {
  MyCounter++; // increment counter by one each time loop repeats
  
  if (TimePeriodIsOver (MyTimer,interval)  ) {
    if (LED_State == LOW) {
      LED_State = HIGH;
    }
    else {
      LED_State = LOW;      
    }
    
    digitalWrite (ledPin, LED_State);
    
    // Demo for detailed serial output
    Serial.print("digitalWrite ("); // print first part of fixed text
    Serial.print(ledPin);           // print content of constant "ledPin"
    Serial.print(", ");             // print fixed text 
    Serial.print(LED_State);        // print content of variable "LED_State"
    Serial.print(") MyCounter:" );  // print fixed text 
    Serial.print(MyCounter);        // print value of variable MyCounter
    Serial.println();               // new line
    // the lines above print out 
    // digitalWrite (13, 0) MyCounter: nnnnnnn
    // digitalWrite (13, 1) MyCounter: nnnnnnn ...
    // nnnnnn is the rapidly increasing number of executed loops
    // which shows the code outside the if-condition runs very fast
    // and the if-condition gets true only once per defined value of interval
  }
}


/* shortest possible code for blinking
  if (TimePeriodIsOver (MyTimer,interval)  ) {
    digitalWrite (ledPin, !digitalRead(ledPin) );
  }

read actual status of IO-Pin:  digitalRead(ledPin)
invert status through not-operator the  "!"
write the result with digitalWrite

all in all:   digitalWrite (ledPin, !digitalRead(ledPin) );
*/

you should always start from a very simple code.
Best a working example and then start from there changing the code in small steps

In this case a small testcode for receiving serial characters without anything else. Make the serial receiving work
and if it is working add the next part. Otherwise it stays unclear which part of the code has a bug.

best regards Stefan

I am trying to figure out the Millis function, so I can implement it on another program I'm working on. My objective is to get rid of the delay and replace with millis. I just can't wrap my head around it I guess thankyou for the help.

#include <Wire.h> 
int flora;       
int buttonPin;              
long int bucketVal;
int ledState=1;
int ledPin=3;
int ledPin1=4;
int ledPin2=6;
int buttonOld;
int buttonNew=1;
int calm=0;
int calg=0;
int calb=0;
int pump;
long int Ml;
long int dt;
void setup() {
Serial.begin(9600);
    
 

pinMode(buttonPin,INPUT);
digitalWrite(buttonPin,HIGH);

pinMode(ledPin,OUTPUT);
pinMode(ledPin1,OUTPUT);
pinMode(ledPin2,OUTPUT);
}

void loop() {
  
  

//Serial.println(Ml);
if(Serial.available()>0){               
   String Received = Serial.readString(); 

if(int(Received[0]) ==0x0C){
  bucketVal=12;}
  if(int(Received[0]) ==0x0B){
  bucketVal=11;}
  if(int(Received[0]) ==0x0A){
  bucketVal=10;}
  if(int(Received[0]) ==0x09){
  bucketVal=9;}
  if(int(Received[0]) ==0x08){
  bucketVal=8;}
  if(int(Received[0]) ==0x07){
  bucketVal=7;}
  if(int(Received[0]) ==0x06){
  bucketVal=6;}
  if(int(Received[0]) ==0x05){
  bucketVal=5;}
  if(int(Received[0]) ==0x04){
  bucketVal=4;}
  if(int(Received[0]) ==0x03){
  bucketVal=3;}
  if(int(Received[0]) ==0x02){
  bucketVal=2;}
  if(int(Received[0]) ==0x01){
  bucketVal=1;}


    
if(int(Received[0]) == 0x17){
if (ledState==0){
    ledState=1;
   Ml=0;
    pump=1;}  
}

if(int(Received[0]) == 0x15){
if (ledState==0){
    ledState=1;
    Ml=0;
    pump=4;}  
}

if(int(Received[0]) == 0x1F){
if (ledState==0){
    ledState=1;
    Ml=0;
    pump=7;}  
}

if(int(Received[0]) == 0x29){
if (ledState==0){
    ledState=1;
    Ml=0;
    pump=10;}  
}

if(int(Received[0]) == 0){
  digitalWrite(ledPin,LOW);
  digitalWrite(ledPin1,LOW);
  digitalWrite(ledPin2,LOW);  
    ledState=0;
    pump=0;
    Ml=0; 
    Serial.print("t4.txt=\"NEWTS\"") ;
    Serial.write(0xff);
    Serial.write(0xff);
    Serial.write(0xff); 
    Serial.print("n1.val=");
    Serial.print(Ml);
    Serial.write(0xff);
    Serial.write(0xff);
    Serial.write(0xff);
    Serial.print("n2.val=");
   Serial.print(Ml);
    Serial.write(0xff);
    Serial.write(0xff);
    Serial.write(0xff);
    Serial.print("n0.val=");
   Serial.print(Ml);
    Serial.write(0xff);
    Serial.write(0xff);
    Serial.write(0xff);
  }}
  
 // Veg stage
if(pump==1){  
 // floraMicro pump
 dt=5503.6*bucketVal;
 Ml=dt/137.59;
    Serial.print("t4.txt=\"MICRO\"") ;
    Serial.write(0xff);
    Serial.write(0xff);
    Serial.write(0xff);  
  digitalWrite(ledPin,HIGH);
    Serial.print("n0.val=");
    Serial.print(Ml);
    Serial.write(0xff);
    Serial.write(0xff);
    Serial.write(0xff);
    delay(dt);
  digitalWrite(ledPin,LOW);
    pump=2;
   
}  
//  // floraGrow pump 

if(pump==2){
  dt=5503.6*bucketVal;
 Ml=dt/137.59;
     Serial.print("t4.txt=\"GROW\"") ;
     Serial.write(0xff);
     Serial.write(0xff);
     Serial.write(0xff);
   digitalWrite(ledPin1,HIGH);
     Serial.print("n1.val=");
     Serial.print(Ml);
     Serial.write(0xff);
     Serial.write(0xff);
     Serial.write(0xff);
     delay(dt);
   digitalWrite(ledPin1,LOW);
     pump=3;    
   }
      //floraBloom pump 
      
if(pump==3){
     dt=2751.8*bucketVal;
 Ml=dt/137.59;
      Serial.print("t4.txt=\"BLOOM\"") ;
      Serial.write(0xff);
      Serial.write(0xff);
      Serial.write(0xff);
    digitalWrite(ledPin2,HIGH);
      Serial.print("n2.val=");
      Serial.print(Ml);
      Serial.write(0xff);
      Serial.write(0xff);
      Serial.write(0xff);
      delay(dt);
    digitalWrite(ledPin2,LOW);
      pump=0;
      ledState=0;
     Serial.print("t4.txt=\"NEWTS\"") ;
      Serial.write(0xff);
      Serial.write(0xff);
      Serial.write(0xff); 
    }

Take a look at Using millis() for timing. A beginners guide, Several things at the same time and the BlinkWithoutDelay example in the IDE

Thank you for your help.

Quite apart from using millis(), instead of this section of code

if(int(Received[0]) ==0x0C){
  bucketVal=12;}
  if(int(Received[0]) ==0x0B){
  bucketVal=11;}
  if(int(Received[0]) ==0x0A){
  bucketVal=10;}
  if(int(Received[0]) ==0x09){
  bucketVal=9;}
  if(int(Received[0]) ==0x08){
  bucketVal=8;}
  if(int(Received[0]) ==0x07){
  bucketVal=7;}
  if(int(Received[0]) ==0x06){
  bucketVal=6;}
  if(int(Received[0]) ==0x05){
  bucketVal=5;}
  if(int(Received[0]) ==0x04){
  bucketVal=4;}
  if(int(Received[0]) ==0x03){
  bucketVal=3;}
  if(int(Received[0]) ==0x02){
  bucketVal=2;}
  if(int(Received[0]) ==0x01){
  bucketVal=1;}

why not just

if (int(Received[0]) <= 0x0C && int(Received[0]) >= 1)
{
  bucketval = int(Received[0]
}

even better, get rid of Strings, but maybe change just one thing at a time, but do take a look at Serial input basics - updated

UKHeliBob:

  bucketval = int(Received[0]
  bucketval = int(Received[0]); // !

Thanks for tip.