How do i stop blink with out delay with a input trigger?

So I'm using blink without delay in my sketch which is trigger by a sensor input. Everything works great until the sensor want to tell the loop to stop blinking, while the led is high, the led stay HIGH. Is there a good way to exit the loop?

I've tried standard blink without delay sketch controlled by a switch and a "ledFlasher" library they all do the same thing.

easterly81: So I'm using blink without delay in my sketch which is trigger by a sensor input. Everything works great until the sensor want to tell the loop to stop blinking, while the led is high, the led stay HIGH. Is there a good way to exit the loop?

I've tried standard blink without delay sketch controlled by a switch and a "ledFlasher" library they all do the same thing.

We're supposed to be able to tell you what's wrong without seeing your code? We're not mindreaders.

while the led is high, the led stay HIGH.

So write to the pin to turn the LED off.

Anyway without code there's little point suggesting anything more.


Rob

Sorry

/* Blink without Delay
 
 
 http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
 */

// constants won't change. Used here to 
// set pin numbers:
const int ledPin =  12;      // the number of the LED pin

// Variables will change:
int ledState = LOW;             // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated
int sw1=3;
int sw1State=0;
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 1000;           // interval at which to blink (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(ledPin, OUTPUT);      
  pinMode(sw1,INPUT);
Serial.begin(9600);
}

void loop()
{
  Serial.println(sw1State);
  
  
  sw1State=digitalRead(sw1);
  unsigned long currentMillis = millis();
 
  if (sw1State==1)
  {
   

    if(currentMillis - previousMillis > interval) {
      // save the last time you blinked the LED 
      previousMillis = currentMillis;   

      // if the LED is off turn it on and vice-versa:
      if (ledState == LOW)
        ledState = HIGH;
      else
        ledState = LOW;

      // set the LED with the ledState of the variable:
      digitalWrite(ledPin, ledState);
    }
  }
}

or

#include <LedFlasher.h>
LedFlasher Relay (12, 2000, 500);
LedFlasher Relay2 (12, 200, 2000);
LedFlasher RelayOff (12, 0, 0);
int LED =12;
int sw1 =3;
int sw2 = 8;
int sw1State=0;
int sw2State=0;
int comsw=0;
void setup ()
{
Relay.begin();
Relay2.begin();
RelayOff.begin();
pinMode(sw1,INPUT);
pinMode(sw2,INPUT);
pinMode(LED,OUTPUT);
Serial.begin(9600);
}
void loop()
{
Serial.print(sw1State);

Serial.print(" , ");
  
  Serial.println(sw2State);
sw1State=digitalRead(sw1);
sw2State=digitalRead(sw2);

if (sw1State==1)
Relay.update();


if (sw2State==1)
Relay.update();
}
  Serial.println(sw1State);
  
  
  sw1State=digitalRead(sw1);

Print the state of the switch. Then, read the state of the switch. Why?

I am trying to use sw1State as a trigger for the loop.

I found this and it works but i haven't been able to successfully add a second blink schedule.

#define LED_PIN 12
#define LED_ON 1000       //milliseconds
#define LED_OFF 5000
#define sw 3
unsigned long ms;        //time from millis()
unsigned long msLast;    //last time the LED changed state
boolean ledState;        //current LED state
boolean swState;
void setup(void)
{
  pinMode(LED_PIN, OUTPUT);
  pinMode (sw,INPUT);
}

void loop(void)
{
  swState=digitalRead(sw);
  ms = millis();
  blinkLED();
}

void blinkLED(void)
{
  if (swState==HIGH)
    if (ms - msLast > (ledState ? LED_ON : LED_OFF)) {
      digitalWrite(LED_PIN, ledState = !ledState);
      msLast = ms;
    }
  if (swState==LOW)
  {
    digitalWrite(LED_PIN, ledState = swState);
  }
}

I guess i dont understand whats going on here

void blinkLED(void)
{
  if (swState==HIGH)
    if (ms - msLast > (ledState ? LED_ON : LED_OFF)) {
      digitalWrite(LED_PIN, ledState = !ledState);
      msLast = ms;
    }
  if (swState==LOW)
  {
    digitalWrite(LED_PIN, ledState = swState);
  }

Pretend like we don't know what you're trying to do. What is a "blink schedule"?

When you start with poor code, it’s hard to extend it. What makes that poor code, you might ask. Let’s start with the fact that there is a function called blinkLED() that does more than toggle the LED on and off. It cares about the state of the switch.

It should not. It should ONLY care about the pin, the last time it did something, and the interval between events. It should be called only when the switch is pressed. It should NOT use ANY global variables. It should be responsible for determining “now”, not the caller. It should be told the interval, the pin number, and the last time it did something. It should return the time it did something, or 0, if it did nothing.

That way, you could use the function to blink another LED on a different schedule, instead of having to write another function to blink an LED on another pin on another schedule.

Same led two switches(which are eventually going to be sensor controlled) scheduled one, switch one, on 1 sec off 5 sec, and schedule two, switch two, one 1 sec off 10 sec.

This seems to work

#define LED_PIN 12
#define LED_ON 1000       //milliseconds
#define LED_OFF 5000
#define LED2_ON 2000       //milliseconds
#define LED2_OFF 10000
#define sw 3
#define sw2 8
unsigned long ms;        //time from millis()
unsigned long msLast;    //last time the LED changed state
unsigned long ms2;        //time from millis()
unsigned long msLast2;
boolean ledState;        //current LED state
boolean ledState2;        //current LED state

boolean swState;
boolean swState2;

void setup(void)
{
  pinMode(LED_PIN, OUTPUT);
  pinMode (sw,INPUT);
  pinMode (sw2,INPUT);
Serial.begin(9600);
}

void loop(void)
{
  Serial.println(swState+swState2);

  swState=digitalRead(sw);
  swState2=digitalRead(sw2);

  ms = millis();
  blinkLED();
  blinkLED2();
}

void blinkLED(void)
{
  if (swState==1)
    if (ms - msLast > (ledState ? LED_ON : LED_OFF)) {
      digitalWrite(LED_PIN, ledState = !ledState);
      msLast = ms;
    }
  if (swState+swState2==0)
  {
    digitalWrite(LED_PIN, ledState = swState);
  }
}
void blinkLED2(void)
{
  if (swState2==1)
    if (ms - msLast > (ledState ? LED2_ON : LED2_OFF)) 
    {
      digitalWrite(LED_PIN, ledState = !ledState);
      msLast = ms;
    }

}

whats the deal with the "void loop(void)" and what is the

void blinkLED(void)

just trying to understand it.

so more like this

#define LED_PIN 12
#define LED_ON 1000       //milliseconds
#define LED_OFF 5000
#define LED2_ON 2000       //milliseconds
#define LED2_OFF 10000
#define sw 3
#define sw2 8
unsigned long ms;        //time from millis()
unsigned long msLast;    //last time the LED changed state
unsigned long ms2;        //time from millis()
unsigned long msLast2;
boolean ledState;        //current LED state
boolean ledState2;        //current LED state

boolean swState;
boolean swState2;

void setup(void)
{
  pinMode(LED_PIN, OUTPUT);
  pinMode (sw,INPUT);
  pinMode (sw2,INPUT);
Serial.begin(9600);
}

void loop(void)
{
  Serial.println(swState+swState2);

  swState=digitalRead(sw);
  swState2=digitalRead(sw2);

  ms = millis();


  if (swState==HIGH)
  blinkLED();
 if (swState2==HIGH)
  blinkLED2();
}

void blinkLED(void)
{
     if (ms - msLast > (ledState ? LED_ON : LED_OFF)) {
      digitalWrite(LED_PIN, ledState = !ledState);
      msLast = ms;
    }

}
void blinkLED2(void)
{

    if (ms - msLast > (ledState ? LED2_ON : LED2_OFF)) 
    {
      digitalWrite(LED_PIN, ledState = !ledState);
      msLast = ms;
    }

}

Sorry super noob!

whats the deal with the "void loop(void)"

What is the question? If you are wondering about the void in the parentheses, it is not needed, but some people like to put it there it remind themselves that the function takes no arguments.

the code in reply 9 starts the schedules but they stay on when the switch is off(while the led is on). And is this code a better approach?

but they stay on when the switch is off

If that's not what you want, turn the pins off if the switches are not pressed.

And is this code a better approach?

Not really. You have two functions doing the same thing, for different pins, times, and intervals. You only need one function with arguments.

If that's not what you want, turn the pins off if the switches are not pressed.

then only one switch works. Mostly cause the initial code is flawed

so i just need the one blink function("void blinkLED(void)") and i need to write code with if statements controlled by the switches to vary the LED_ON and LED_OFF intervals, Is this correct?

so i just need the one blink function("void blinkLED(void)")

Yes, you only need one function to toggle a pin. No, it does not take no arguments.

and i need to write code with if statements controlled by the switches to vary the LED_ON and LED_OFF intervals

That's a definite maybe.

You need a function like so:

unsigned long togglePin(byte pin, unsigned long lastTime, unsigned long interval)
{
}

You need to call it like so:

Well, frankly, I'm stumped. I don't understand what you are doing. You have two switch pins and one LED pin. I think at this point you need to define the requirements better if we are to help you.

The end result is to have a photo cell and a dh11 control a relay. I want the sensors to vary the on and off duration without delays. I’m also using the Arduino to log data.

The end result is to have a photo cell and a dh11 control a relay.

What is the relay going to be doing?

Write out, in English, what you want the code to do, in detail. Why will the relay be toggling off and on, like a blinking LED? That's the part that doesn't yet make sense.

Having the relay turn on under certain circumstances, and stay on a while, makes sense. Having the relay turn off under certain circumstances, and stay off a while, makes sense. Toggling on and off at regular intervals does not.

this seems to be working is this a good approach?

#define LED_PIN 12
#define LED_ON 1000       //milliseconds
#define LED_OFF 5000
#define LED2_ON 2000       //milliseconds
#define LED2_OFF 10000
#define sw 3
#define sw2 8
unsigned long ms;        //time from millis()
unsigned long msLast;    //last time the LED changed state
unsigned long ms2;        //time from millis()
unsigned long msLast2;
boolean ledState;        //current LED state
boolean ledState2;        //current LED state

int swState=0;
int swState2=0;

void setup(void)
{
  pinMode(LED_PIN, OUTPUT);
  pinMode (sw,INPUT);
  pinMode (sw2,INPUT);
}

void loop(void)
{
  swState=digitalRead(sw);
  swState2=digitalRead(sw2);

  ms = millis();
  ms2=millis();

  if (swState==1)
    blinkLED();
  else if (swState2==1)
    blinkLED2();
  else
    digitalWrite(LED_PIN,LOW);
  
  
  if (swState2==1)
    blinkLED2();
  else if (swState==1)
    blinkLED();
  else
    digitalWrite(LED_PIN,LOW);

}


void blinkLED(void)
{
  if (ms - msLast > (ledState ? LED_ON : LED_OFF))
  {
    digitalWrite(LED_PIN, ledState = !ledState);
    msLast = ms;
  }

}
void blinkLED2(void)
{

  if (ms2 - msLast2 > (ledState2 ? LED2_ON : LED2_OFF)) 
  {
    digitalWrite(LED_PIN, ledState2 = !ledState2);
    msLast2 = ms2;
  }

}

I am trying to make the switches change the blinking rate of the same led.

is this a good approach?

No. You still have two functions doing the same thing. The only difference between them is the interval. Make a damned function that accepts an argument, and get rid of half the code you have (and half the global variables). You don't need two different last-time-the-LED-toggled variable, for instance.

how exactly do i do that, do i just remove the voids and and the if statements to change the relay_on and relay_off intervals?

Like This?

#define LED_PIN 12
long LED_ON =0;       //milliseconds
long LED_OFF= 0;

#define sw 3
#define sw2 8
unsigned long ms;        //time from millis()
unsigned long msLast;    //last time the LED changed state

boolean ledState;        //current LED state

int swState=0;
int swState2=0;

void setup(void)
{
  pinMode(LED_PIN, OUTPUT);
  pinMode (sw,INPUT);
  pinMode (sw2,INPUT);
}

void loop(void)
{
  swState=digitalRead(sw);
  swState2=digitalRead(sw2);

  ms = millis();

  if (swState==1)
  {
   LED_ON =1000;
   LED_OFF =5000;
    blinkLED();
  }
  else if (swState2==1)
 {  
   LED_ON =2000;
   LED_OFF =10000;
   blinkLED();
}
  else
    digitalWrite(LED_PIN,LOW);
  
  if (swState2==1)
  {
   LED_ON =LED_ON+2000;
   LED_OFF =LED_OFF+10000;
    blinkLED();
  }
  else if (swState==1)
 {  
   LED_ON =LED_ON+1000;
   LED_OFF =LED_OFF+5000;
   blinkLED();
}
  else
    digitalWrite(LED_PIN,LOW);  
  

}


int blinkLED()
{
  if (ms - msLast > (ledState ? LED_ON : LED_OFF))
  {
    digitalWrite(LED_PIN, ledState = !ledState);
    msLast = ms;
  }

}