cycle timer using millis question

So ive been studying the millis sticky and have a question. Towards the top the op said " You could change the on/off periods by simply changing the value of the period variable when you change the state of the LED."

My question is this? How would on do that

Im creating a cycle timer that is on for 1 min then off for 5 min.

Tried doing it with delays and that didnt work at all. It mucked up my temp readings and from what ive read is a bad way to do it. Hence the millis crash course. Thx in advance for the help.

How would on do that

Use an assignment statement.

If you have a variable, period, that defines the on time, and it is set to 500 (milliseconds), and you want the off time to be 250 (milliseconds), and you have a variable, ledState, that is HIGH or LOW,

void loop()
{
   if(millis() - lastTime >= period)
   {
      if(ledState)
         period = 250;
      else
         period = 500;
      ledState = !ledState;

      digitalWrite(ledPin, ledState);
   }
}

Thanks for the answer. Exactly what i needed. Sorry. Forgot to post the code im using. Ill try to add thatlil snippet. ill updatew.

#include <DHT.h> // include library
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
//#include <Time.h>
//#include <TimeAlarms.h>

const int relayPin = 3;

const int DHTPIN = 12;

unsigned long startMillis;  //some global variables available anywhere in the program
unsigned long currentMillis;
const unsigned long period = 30000;  //the value is a number of milliseconds
const byte ledPin = 4;    //using the built in LED

#define DHTTYPE DHT11   // DHT 11
DHT dht(DHTPIN, DHTTYPE);

#define I2C_ADDR 0x27
#define BACKLIGHT_PIN 3
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
LiquidCrystal_I2C lcd(I2C_ADDR, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin);



void setup()
{

  Serial.begin(9600);
  pinMode(relayPin, OUTPUT);
  lcd.begin(16, 2);
  lcd.setBacklightPin(BACKLIGHT_PIN, POSITIVE);
  lcd.setBacklight(HIGH);
  dht.begin();
  pinMode(ledPin, OUTPUT);
  startMillis = millis();  //initial start time
}



void loop()  {

  currentMillis = millis();  //get the current "time" (actually the number of milliseconds since the program started)
  if (currentMillis - startMillis >= period)  //test whether the period has elapsed
  {
    digitalWrite(ledPin, !digitalRead(ledPin));  //if so, change the state of the LED.  Uses a neat trick to change the state
    startMillis = currentMillis;  //IMPORTANT to save the start time of the current LED state.
  }

  float h = dht.readHumidity();
  float t = dht.readTemperature();
  float f = dht.readTemperature(true);

  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  Serial.print("Temperature = ");
  Serial.print(t);
  Serial.print(" *C ");
  Serial.print("Temperature = ");
  Serial.print(f);
  Serial.print(" *F ");
  Serial.print("Humidity = ");
  Serial.print(h);
  Serial.print(" %\t ");
  Serial.println();
  delay(2000);

  // lcd
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Air Temp = ");
  lcd.print(f);
  lcd.print("F ");
  lcd.print((char)223);
  lcd.print("C");
  lcd.setCursor(0, 1);
  lcd.print("Humidity = ");
  lcd.print(h);
  lcd.print(" %");

  if (h >= 70) { // you can change humidity value here - h>=66 to your preffered number
    digitalWrite(relayPin, LOW);
  }
  else {
    digitalWrite(relayPin, HIGH);
  }
  if (f >= 78) {// you can change temperature value here - t>= 30 to your preferred number or change from Celsius to Fahrenheit readings
    digitalWrite(relayPin, LOW);
  }
  else {
    digitalWrite(relayPin, HIGH);
  }
}

How would one incorporate it into this sketch? im trying but cant ge tit to compile.

How would one incorporate it into this sketch? im trying but cant ge tit to compile.

Please post what you tried

The difference between Paul's example and your code is that his uses a variable to hold the state of the LED and yours doesn't. It would be easier if you created such a variable and used it instead of the trick of changing the state of the LED by reading its current state and inverting it.

This is one of the times when having a value in a variable comes into its own although you could test the state of the LED using digitalRead() and set the period based on what is returned, but that would involve doing the digitalRead() twice which would be bad practice

trying to get thisone:

unsigned long startMillis;  //some global variables available anywhere in the program
unsigned long currentMillis;
const unsigned long period = (currentMillis - startMillis);  //the value is a number of milliseconds
const byte ledPin = 13;    //using the built in LED

void setup()
{
  Serial.begin(115200);  //start Serial in case we need to print debugging info
  pinMode(ledPin, OUTPUT);
  startMillis = millis();  //initial start time


}


void loop()
{
  currentMillis = millis();  //get the current "time" (actually the number of milliseconds since the program started)

  if (millis() - startMillis >= period)
  {
    if (ledState)
      period = 2500;
    else
      period = 5000;
    ledState = !ledState;

   digitalWrite(ledPin, ledState);
  }
}

Errors when i compile and says:

/home/pi/Desktop/millis_timing/millis_timing.ino: In function 'void loop()':
millis_timing:22:9: error: 'ledState' was not declared in this scope
     if (ledState)
         ^
millis_timing:23:14: error: assignment of read-only variable 'period'
       period = 2500;
              ^
millis_timing:25:14: error: assignment of read-only variable 'period'
       period = 5000;
              ^
millis_timing:26:5: error: 'ledState' was not declared in this scope
     ledState = !ledState;
     ^
/home/pi/Desktop/millis_timing/millis_timing.ino: At global scope:
millis_timing:35:13: error: expected constructor, destructor, or type conversion before '(' token
 digitalWrite(ledPin, !digitalRead(ledPin));  //if so, change the state of the LED.  Uses a neat trick to change the state
             ^
millis_timing:36:1: error: 'startMillis' does not name a type
 startMillis = currentMillis;  //IMPORTANT to save the start time of the current LED state.
 ^
millis_timing:37:1: error: expected declaration before '}' token
 }
 ^
exit status 1
'ledState' was not declared in this scope

I assume i need a variable named led state. cant seem to get the right syntax.

Here's where you declare the variable, period.

const unsigned long period = (currentMillis - startMillis);  //the value is a number of milliseconds

Check what the const keyword means. Try removing it.

Declare ledState as a global byte variable and set it to HIGH at the start of the program

const unsigned long period = (currentMillis - startMillis);  //the value is a number of milliseconds

currentMillis is a global variable, so it's value is 0.
startMillis is a global variable, so it's value is 0.
So, you just set your period to 0, and can't change it. Not particularly useful.

ok. I came up with this. compiles just fine but all it does is turn on the led. No cycling. Please help

unsigned long startMillis;  //some global variables available anywhere in the program
unsigned long currentMillis;
unsigned long period;
unsigned long ledState;
const byte ledPin = 4;   

void setup()
{
  Serial.begin(115200);  //start Serial in case we need to print debugging info
  pinMode(ledPin, OUTPUT);
  startMillis = millis();  //initial start time
  pinMode(ledPin, HIGH);
  
}


void loop()
{
  period = (currentMillis - startMillis); 

  currentMillis = millis();  //get the current "time" (actually the number of milliseconds since the program started)

  if (millis() - startMillis >= period)
  {
    if (ledState)
      period = 2500;
    else
      period = 5000;
    ledState = !ledState;

    digitalWrite(ledPin, ledState);
  }
}

grrr. worked on it last evening and this morninng. im so close. Cant get it to compile. heres the code:

unsigned long startMillis;  //some global variables available anywhere in the program
unsigned long currentMillis;
unsigned long period = 250;  //the value is a number of milliseconds
const byte ledPin = 3;    //using the built in LED
byte ledState;

void setup()
{
  Serial.begin(115200);  //start Serial in case we need to print debugging info
  pinMode(ledPin, OUTPUT);
  startMillis = millis();  //initial start time
  ledState = (3, HIGH);


}


void loop()
{
  currentMillis = millis();  //get the current "time" (actually the number of milliseconds since the program started)

  period = (millis() - startMillis);

  digitalRead(ledState);

  if (millis() - startMillis >= period);
  {
    if (ledState = HIGH);
    period = 2500;
    else;
      period = 5000;
  }
    ledState = !ledState;

    digitalWrite(ledPin, ledState);
  
    delay(500);
 }

Heres the error:

Arduino: 1.8.8 (Linux), Board: "Arduino/Genuino Uno"

/home/pi/Arduino/MILLI_TIMING/MILLI_TIMING_2.ino/MILLI_TIMING_2.ino.ino: In function 'void loop()':
MILLI_TIMING_2.ino:32:5: error: 'else' without a previous 'if'
     else;
     ^
exit status 1
'else' without a previous 'if'

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

Thanks in advance.

Lose the semicolon:

    if (ledState = HIGH);

lose the one after the else too.
Edit: that = should be == too.

  ledState = (3, HIGH);

What do you think that this rubbish is supposed to do?

rubbish huh. Ive been learning this for about a week now. I thought i was doing good. thx for the help.

dougtebo:
rubbish huh. Ive been learning this for about a week now. I thought i was doing good. thx for the help.

You can’t expect us to help you write correct code without knowing what you expect what you wrote to do.

Yes, that statement is rubbish. It doesn’t matter if it was written by someone with 5 minutes or 50 years of experience. Don’t get so defensive.

Not defensive. Just frustrated this morn. sorry.