Program Arduino to

So I have a laser that I want Arduino to turn ON and OFF without using delay. I want to be able to use a function, plug in the time values I want to in order to turn the laser ON and OFF without stopping the cycle. I have gotten this far, but I do not know how to complete the code in order for the program to run continuously. Thanks for the help in advance!

int ledState = LOW;
int ledPin = 13;
unsigned long current;
unsigned long ONtime;
unsigned long previous;
const int interval = 1000;
const int ONinterval = 200;

void setup() {
pinMode(13, OUTPUT);

}

void loop() {
current = millis();
if(current - previous >= interval)
{
previous = current;
ONtime =current;
digitalWrite(13, HIGH);

digitalWrite(13, LOW);
}

}

So I have a laser that I want Arduino to turn ON and OFF without using delay

So why have you got a delay?

Please remember to use code tags when posting code

Do you know how to write a function?

Do you know how to call a function and pass a variable to it!

What triggered the LED to turn on? Time of day, when a switch is pushed, when a variable reaches a certain value, it just turns on for a time then turns off for a time . . .

Perhaps, you'd want 2 millis() variables. Let's say m1 and m2. The laser is off, m1 is loaded with a value that will expire in short order and m2 is loaded with a value far out there, initially. m1 times out, energizes the laser and fills in a new m2 value that will expire in a short time. m2 times out and does the laser thing and sets m1 expire time. m1 expires, does the laser thing and sets m2 to expire in short order. When m2 expires, do the laser thing and set m1 to expire and on and on and on and on and on. Sort of like m1 can be tick and m2 can be tock.

Or not.

The demo Several Things at a Time illustrates the use of millis() to manage timing without blocking. It may help with understanding the technique.

Have a look at Using millis() for timing. A beginners guide if you need more explanation.

Also have a look at Planning and Implementing a Program

...R

I think adafruit's Flasher class is exactly what you want.

//usage:
Flasher led1(12, 100, 400); //pin, on, off

neiklot:

Flasher led1(12, 100, 400); //pin, on, off

on and off -- are they times? If so, what are the units -- ms or us or ?

GolamMostafa:
on and off – are they times? If so, what are the units – ms or us or ?

All explained in the tutorial I linked

neiklot:
All explained in the tutorial I linked**.**

Yes! I have found; these are times and the units are in milliseconds; but, at the cost of 'time' which could have been saved should your post be kind to include that information in the comment field.

I used a template that can change the frequency of the LED, but I want to be able to control for the LED duration of being ON and OFF at a certain frequency. I want to use a “do… while” or “if…else” syntax in order to fulfill this part of the code. Let’s say I want the LED to turn ON for 3 minutes, turn OFF for 5 minutes, then turn ON for 3 minutes. Thanks!

/code/

sketch_apr01e.ino (1.63 KB)

Is your code too long to post as described in the how to use this forum guidelines? If not, posting your code in code tags will let more members see your code and be able to help you.

Let's say I want the LED to turn ON for 3 minutes, turn OFF for 5 minutes, then turn ON for 3 minutes.

I would use something like this

const byte ledPin =  13;
unsigned long previousMillis = 0;
const byte states[] = {HIGH, LOW};
const unsigned long intervals[] = {200, 200, 200, 800};
const byte NO_OF_INTERVALS = sizeof(intervals) / sizeof(intervals[0]);
byte state;

void setup()
{
  pinMode(ledPin, OUTPUT);
}

void loop()
{
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= intervals[state % NO_OF_INTERVALS])
  {
    previousMillis = currentMillis;
    digitalWrite(ledPin, states[state++ % 2]);
  }
}

with the appropriate periods in the intervals array

I used a template that can change the frequency of the LED, but I want to be able to control for the LED duration of being ON and OFF at a certain frequency. I want to use a “do… while” or “if…else” syntax in order to fulfill this part of the code. Let’s say I want the LED to turn ON for 3 minutes, turn OFF for 5 minutes, then turn ON for 3 minutes. Thanks!

int ledPin =  13;      // the number of the LED pin
int ledState = LOW;             // ledState used to set the LED
unsigned long previousMillis = 0;        // will store last time LED was updated
long OnTime = 600;           // milliseconds of on-time (frequency)
long OffTime = 300;          // milliseconds of off-time (frequency)
const int Era1 = 1; // minutes on laser
const int Era2 = 3; //minutes off laser
const int Era3 = 3; //minutes on laser


void setup() 
{
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);
  pinMode(12, OUTPUT);      
}

void loop()
  {
    {
  if (Era1 >= x);// Turn on laser for desired amount of time

  else if (Era2 < (60)); // Turn off laser for desired amount of time

  else (Era3); // Turn on laser for desired amount of time
        
  }
  unsigned long currentMillis = millis();
  if((ledState == HIGH) && (currentMillis - previousMillis >= OnTime))  // check to see if it's time to change the state of the LED
  {
    ledState = LOW;  // Turn it off
    if (ledState == LOW)
      {
      digitalWrite(12,HIGH);
      }
    previousMillis = currentMillis;  // Remember the time
    digitalWrite(ledPin, ledState);  // Update the actual LED
    }
    
  else if ((ledState == LOW) && (currentMillis - previousMillis >= OffTime))
    {
    ledState = HIGH;  // turn it on
    previousMillis = currentMillis;   // Remember the time
    digitalWrite(ledPin, ledState);    // Update the actual LED
    if (ledState == HIGH) {
      digitalWrite( 12, LOW);
    }
    }
    
}
 
  
 [code]

Why are you starting a new thread for the same question as here?