Using counters

Hey everyone, new to arduino's and this forum as well! I am working on a project in which i need 3 different inputs all of which are going high and low at different intervals. I am using counters, but am not sure how to specifically have an input go high at a specific time. For instance, i need my first input to be high for 1 sec, then low for 1 sec. During the 1 sec high phase of this i need my 2nd input to be turned on for 100ms (this needs to happen before the 1 second of low happens), so im trying to initiate the 2nd input at 800ms of the 1st high input. Finally my 3rd input needs to be turned on at the start of the 1st inputs 1 second Low. I have seen counters just count the amount of times the input pin goes off, but am not sure as to how to initiate it at a specific time. Any help would be appreciated, and if i was too confusing(which i probably was newbie here) please dont be afraid to ask questions. Thanks!

The demo several things at a time is an extended example of the BWoD technique.

...R

Hey thanks to both of you guys, i have my program up and running to the specifications that i needed, i may create a new thread if i have trouble tweaking my current code to what i need next. Again thanks you guys really appreciate the feedback :smiley:

Okay! so after further investigation im finding that my pin 12 and 11 inputs start to be off a little bit in terms of when i want it to initiate. Here is my code:
int ledPin1 = 12;
int ledState1 = LOW;
unsigned long previousMillis1 = 0; //Will store last time LED was updated
long OnTime1 = 100; //milliseconds of on-time
long OffTime1 = 1800; //milliseconds of off-time

int ledPin2 = 11;
int ledState2 = HIGH;
unsigned long previousMillis2 = 0; //will store last time LED was updated
long OnTime2 = 900; //milliseconds of on-time
long OffTime2 = 1050; //milliseconds of off-time

int ledPin3 = 13;
int ledState3 = LOW;
unsigned long previousMillis3 = 0; //will store last time LED was updated
long OnTime3 = 1000; //milliseconds of on-time
long OffTime3 = 1000; //milliseconds of off-time

void setup()
{
//Set the digital pins as outputs
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
}
void loop()
{
//check to see if it's time to change the state of LED
unsigned long currentMillis = millis();

if((ledState1 == HIGH) && (currentMillis - previousMillis1 >= OnTime1))
{
ledState1 = LOW; //Turn off
previousMillis1 = currentMillis; //Remember the time
digitalWrite(ledPin1, ledState1); //update the actual LED
}
else if ((ledState1 == LOW) && (currentMillis - previousMillis1 >= OffTime1))
{
ledState1 = HIGH; //Turn on
previousMillis1 = currentMillis; //remember the time
digitalWrite(ledPin1, ledState1); //update the LED
}

if((ledState2 == HIGH) && (currentMillis - previousMillis2 >= OnTime2))
{
ledState2 = LOW; //Turn off
previousMillis2 = currentMillis; //remember the time
digitalWrite(ledPin2, ledState2); //update the LED
}
else if((ledState2 == LOW) && (currentMillis - previousMillis2 >= OffTime2))
{
ledState2 = HIGH; //turn on
previousMillis2 = currentMillis; //remember the time
digitalWrite(ledPin2, ledState2); //update the LED
}

if((ledState3 == HIGH) && (currentMillis - previousMillis3 >= OnTime3))
{
ledState3 = LOW; //turn off
previousMillis3 = currentMillis; //remember the time
digitalWrite(ledPin3, ledState3); //update the LED
}
else if((ledState3 == LOW) && (currentMillis - previousMillis3 >= OffTime3))
{
ledState3 = HIGH; //turn on
previousMillis3 = currentMillis; //remember the time
digitalWrite(ledPin3, ledState3); //update the LED
}
}

my question is, how am i able to reset the time back to 0 milli after 2 seconds, so that way i get the same initiate time, everytime. Again i am trying to get pin 12 to go high when pin 13 is High. pin 13 is high for 1000ms, and i am trying to get pin 12 to go off at 800ms of that 1000ms. It only needs to be on for 100ms. Same goes for pin 11, i am trying to have it go off when pin 13 is on its low 1000ms. Im trying to initiate pin 11 high at 50ms after pin13 goes low, and then have pin 11 go Low 50ms before pin 13 goes back high

Welcome to the Forum. It would be helpful if you read Nick Gammon's two posts at the top of this Forum as to guidelines for posting here, especially the use of code tags when posting code. You might also want to reformat your code in the IDE using Crtl-T before posting...it makes it easier for us to help you.

Delta_G:
OK, so it comes on when pin 13 goes HIGH. And it goes off 800ms later. But it only needs to be on for 100ms. I'm not following. Do you want it to be on for 800ms or 100ms?

Can you draw a simple timing diagram of what you want? That alone may help you answer your own question.

Low High
|1 sec| |1sec|
13 ------------
12
--__
11 -------
------
_

pin 12 is supposed to go high at 800ms and be on for 100ms(so pin 12 goes high at 800, then low at 900 during 1000ms High phase of pin 13). So pin 12 should be going off 1 time during pin 13 high phase. the only problem is, as the program continues the offtime for pin 12 becomes different. Example being the first time it comes on is at 1800ms, the second time it comes on is 3600ms. This is a problem because i need pin 12 to go off at that exact moment everytime. Im wondering if there is a way i can reset the currentMillis() to 0 after running the program once. Thanks again for all the help! and here is my code again, hopefully i formatted it correctly and used the tags right! sorry about that!!!

int ledPin1 = 12;
int ledState1 = LOW;
unsigned long previousMillis1 = 0;        //Will store last time LED was updated
long OnTime1 =  100;                      //milliseconds of on-time
long OffTime1 = 1800;                      //milliseconds of off-time

int ledPin2 = 11;
int ledState2 = HIGH;
unsigned long previousMillis2 = 0;        //will store last time LED was updated
long OnTime2 = 900;                       //milliseconds of on-time
long OffTime2 = 1050;                     //milliseconds of off-time

int ledPin3 = 13;
int ledState3 = LOW;
unsigned long previousMillis3 = 0;        //will store last time LED was updated
long OnTime3 = 1000;                      //milliseconds of on-time
long OffTime3 = 1000;                     //milliseconds of off-time

void setup()
{
  //Set the digital pins as outputs
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
}
void loop()
{
  //check to see if it's time to change the state of LED
  unsigned long currentMillis = millis();

  if ((ledState1 == HIGH) && (currentMillis - previousMillis1 >= OnTime1))
  {
    ledState1 = LOW; //Turn off
    previousMillis1 = currentMillis;   //Remember the time
    digitalWrite(ledPin1, ledState1);  //update the actual LED
  }
  else if ((ledState1 == LOW) && (currentMillis - previousMillis1 >= OffTime1))
  {
    ledState1 = HIGH;  //Turn on
    previousMillis1 = currentMillis;    //remember the time
    digitalWrite(ledPin1, ledState1);   //update the LED
  }

  if ((ledState2 == HIGH) && (currentMillis - previousMillis2 >= OnTime2))
  {
    ledState2 = LOW;  //Turn off
    previousMillis2 = currentMillis;     //remember the time
    digitalWrite(ledPin2, ledState2);    //update the LED
  }
  else if ((ledState2 == LOW) && (currentMillis - previousMillis2 >= OffTime2))
  {
    ledState2 = HIGH;  //turn on
    previousMillis2 = currentMillis;     //remember the time
    digitalWrite(ledPin2, ledState2);    //update the LED
  }

  if ((ledState3 == HIGH) && (currentMillis - previousMillis3 >= OnTime3))
  {
    ledState3 = LOW;  //turn off
    previousMillis3 = currentMillis;     //remember the time
    digitalWrite(ledPin3, ledState3);    //update the LED
  }
  else if ((ledState3 == LOW) && (currentMillis - previousMillis3 >= OffTime3))
  {
    ledState3 = HIGH;  //turn on
    previousMillis3 = currentMillis;     //remember the time
    digitalWrite(ledPin3, ledState3);    //update the LED
  }
}

If you want to maintain accurate and synchronized timing you should change line 4

if ((ledState1 == HIGH) && (currentMillis - previousMillis1 >= OnTime1))
  {
    ledState1 = LOW; //Turn off
    previousMillis1 = currentMillis;   //Remember the time
    digitalWrite(ledPin1, ledState1);  //update the actual LED
  }

to

previousMillis1 += OnTime1;

There is a very full explanation in the Thread several things at a time

If that is not the problem can you explain is as much detail as possible what your code actually does

...R

It seems to me, what you want are "outputs", not "inputs".

It's a repeating sequence. You only need to save one timestamp, which you reset at the end of the sequence. All the other times should be detected as offsets from it. It is because you are not doing this, that your program has complex interactions that you can't manage.