Can I keep logging a sensors data to an sd card while the arduino is in delay

I an using an uno to log data from a sensor, but i an also using the senor to turn on and off a device in 30 minutes intervals. Is there a way to turn they device on and off with out delay. I saw a tutorial on led blink without a delay but i am running and output 3 min on 30 min off,not sure if that will work for this.

easterly81:
I saw a tutorial on led blink without a delay but i am running and output 3 min on 30 min off,not sure if that will work for this.

That's how you would do it. If you're having issues doing it that way, post what you have.

Maybe i need to look into it more but it seems from the example it would on for the same time its off. Is there a way to write it so its on for x amount of time and off for y amount of time.

easterly81:
Maybe i need to look into it more but it seems from the example it would on for the same time its off. Is there a way to write it so its on for x amount of time and off for y amount of time.

Yes. The interval is a variable and can be changed. Some simple logic can be implemented within the if statement to check the current interval value and change it.

It's an example that demonstrates the basics; you can't just look at the examples and go "Nope, it doesn't do exactly what I want it to do, so it probably won't work."

is this a good approach to my problem?

//DEFINING CONSTANTS & VARIABLES
 
 
  // Which pins are connected to which LED
const byte LED1 = 6;

 
 
  // Assigning ON and OFF interval constants.
const unsigned long LED1_ON_interval = 3000; //
const unsigned long LED1_OFF_interval = 6000;

 
 
  // Declaring the variables holding the timer value, i.e. time of last state change.
unsigned long LED1_statechange_Timei;

unsigned long LED1_statechange_Timeii;

 
//SETUP
 
 
  // Setting 3 digital pins as LED output pins and starting millisecond timer
void setup ()
  {
  pinMode (LED1, OUTPUT);

  LED1_statechange_Timei = millis ();

  }  
  
 
  // LED1 loop that turns LED ON if it is OFF
void toggle_LED1i ()
  {
   if (digitalRead (LED1) == LOW)
      digitalWrite (LED1, HIGH);
      LED1_statechange_Timei = millis (); // Remember when LED1's state was changed from ON to OFF
  }  // End of toggle_LED1i
 
 
  // LED1 loop that turns LED OFF if it is ON
void toggle_LED1ii ()
  {
    if (digitalRead (LED1) == HIGH);
       digitalWrite (LED1, LOW);
       LED1_statechange_Timei = millis (); // Remember when LED1's state was changed from OFF to ON
  } // End of toggle_LED1ii

void loop () // Start of loop
  {
    //LED 1
      // If the time since the last change in state from OFF to ON is equal or greater than the ON interval
      //then run the loop toggle_LED1i
  if ( (millis () - LED1_statechange_Timei) >= LED1_ON_interval)
     toggle_LED1i ();
      // If the time since the last change in state from ON to OFF is equal or greater than the OFF interval
      //then run the loop toggle_LED1ii
  if ( (millis () - LED1_statechange_Timeii) >= LED1_OFF_interval)
     toggle_LED1ii ();

}

Well, that's pretty close. I can see two problems.

LED1_statechange_Timeii is never set.

You should probably only check for the ON_interval when the LED is on. When it is off, only check the OFF_interval. Otherwise you might get the other if block executing again and again.

it that like this:

// Declaring the variables holding the timer value, i.e. time of last state change.
unsigned long LED1_statechange_Timei = 0;

unsigned long LED1_statechange_Timeii = 0;

or(from blink without delay example)

long previousMillis = 0; // will store last time LED was updated

sorry i'm a newbie can you provide an example.

Thanks

easterly81:
is this a good approach to my problem?

No, there is no reason to have two independent time comparisons:

  if ( (millis () - LED1_statechange_Timei) >= LED1_ON_interval)
     toggle_LED1i ();
      // If the time since the last change in state from ON to OFF is equal or greater than the OFF interval
      //then run the loop toggle_LED1ii
  if ( (millis () - LED1_statechange_Timeii) >= LED1_OFF_interval)
     toggle_LED1ii ();

}

The right side of the equation can change, they don't have to be constants. In your if block, you can change interval based on whether the LED was just turned on or off.

sorry i'm lost can i use the rtc to turn the relay for 3 off for 30 would that be simpler?

LED1_statechange_Timeii is never set.

I guess I should have been more direct. Both toggle_LED1i() and toggle_LED1ii() are setting LED1_statechange_Timei. Neither of them are setting LED1_statechange_Timeii. Is that what you had intended?

Actually, the toggle functions are a bit of a misnomer. You're not really toggling the led. You're either turning it on or off.

You can check the actual state of the LED in the loop() or, if you want to be more efficient, you could make a state variable to keep track for you.

Try this:

//DEFINING CONSTANTS & VARIABLES
 
 
  // Which pins are connected to which LED
const byte LED1 = 6;

 
 
  // Assigning ON and OFF interval constants.
const unsigned long LED1_ON_interval = 3000; //
const unsigned long LED1_OFF_interval = 6000;

 
 
  // Declaring the variables holding the timer value, i.e. time of last state change.
unsigned long LED1_statechange_Timei;

unsigned long LED1_statechange_Timeii;

 
//SETUP
 
 
  // Setting 3 digital pins as LED output pins and starting millisecond timer
void setup ()
  {
  pinMode (LED1, OUTPUT);

  LED1_statechange_Timei = millis ();
  digitalWrite(LED1, HIGH);

  }  
  
 
  // LED1 loop that turns LED ON if it is OFF
void toggle_LED1i ()
  {
   if (digitalRead (LED1) == LOW)
      digitalWrite (LED1, HIGH);
      LED1_statechange_Timei = millis (); // Remember when LED1's state was changed from ON to OFF
  }  // End of toggle_LED1i
 
 
  // LED1 loop that turns LED OFF if it is ON
void toggle_LED1ii ()
  {
    if (digitalRead (LED1) == HIGH);
       digitalWrite (LED1, LOW);
       LED1_statechange_Timeii = millis (); // Remember when LED1's state was changed from OFF to ON
  } // End of toggle_LED1ii

void loop () // Start of loop
  {
    //LED 1
      // If the time since the last change in state from OFF to ON is equal or greater than the ON interval
      //then run the loop toggle_LED1i
  if (digitalRead(LED1) == HIGH)
  {
    if ( (millis () - LED1_statechange_Timei) >= LED1_ON_interval)
       toggle_LED1ii ();
  }
  else
  {
      // If the time since the last change in state from ON to OFF is equal or greater than the OFF interval
      //then run the loop toggle_LED1ii
    if ( (millis () - LED1_statechange_Timeii) >= LED1_OFF_interval)
       toggle_LED1i ();
  }
}

Sorry about the indentation. I couldn't figure out your system.

easterly81:
sorry i'm lost can i use the rtc to turn the relay for 3 off for 30 would that be simpler?

No, not really.

Well, you have to arrange for the pin to get both the HIGH and the LOW signal. You might be able to program a timer to reset the pin automatically, depending...

If you want the pin to alternately go HIGH and LOW repeatedly at the same rates, that's what PWM is for. The frequencies are limited, and the pins you can use are limited as well. I don't think you can do PWM at 30 minute intervals. 5 seconds max.

And, it's kinda complex. It involves a lot of code like TCCR2A = _BV(COM2A0) | _BV(COM2B1) | _BV(WGM20);

So can some one tell me how to modify this example to make the led go on for second off from 2 secounds

/* Blink without Delay
 
 Turns on and off a light emitting diode(LED) connected to a digital  
 pin, without using the delay() function.  This means that other code
 can run at the same time without being interrupted by the LED code.
 
 The circuit:
 * LED attached from pin 13 to ground.
 * Note: on most Arduinos, there is already an LED on the board
 that's attached to pin 13, so no hardware is needed for this example.
 
 
 created 2005
 by David A. Mellis
 modified 8 Feb 2010
 by Paul Stoffregen
 
 This example code is in the public domain.

 
 http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
 */

// constants won't change. Used here to 
// set pin numbers:
const int ledPin =  13;      // 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

// 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);      
}

void loop()
{
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the 
  // difference between the current time and last time you blinked 
  // the LED is bigger than the interval at which you want to 
  // blink the LED.
  unsigned long currentMillis = millis();
 
  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);
  }
}

Sure. Change 2 things. Change this line:

long interval = 1000;           // interval at which to blink (milliseconds)

to this:

long interval = 2000;           // interval at which to blink (milliseconds)

and add this line:

    interval = 3000 - interval;

right after this line:

    previousMillis = currentMillis;

So this seems to work but it starts in an off state is there a way to flip it, the led is off 30min then on for 3min i want on for 3 then off 30.

easterly81:
So this seems to work but it starts in an off state is there a way to flip it, the led is off 30min then on for 3min i want on for 3 then off 30.

Set ledState to HIGH where it is declared, instead of LOW as you have it now.

copy the digitalWrite statement and put it at the end of the setup function.

So like this?

/* Blink without Delay
 
 Turns on and off a light emitting diode(LED) connected to a digital  
 pin, without using the delay() function.  This means that other code
 can run at the same time without being interrupted by the LED code.
 
 The circuit:
 * LED attached from pin 13 to ground.
 * Note: on most Arduinos, there is already an LED on the board
 that's attached to pin 13, so no hardware is needed for this example.
 
 
 created 2005
 by David A. Mellis
 modified 8 Feb 2010
 by Paul Stoffregen
 
 This example code is in the public domain.

 
 http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
 */

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

// Variables will change:
int ledState = HIGH;             // ledState used to set the LED
long previousMillis = 0;        // will store last time LED was updated

// 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 = 180000;           // interval at which to blink (milliseconds)

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

void loop()
{
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the 
  // difference between the current time and last time you blinked 
  // the LED is bigger than the interval at which you want to 
  // blink the LED.
  unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   
    interval = 1980000 - interval;
    // 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);
  }
}

And if i want it activated with a senor input, would i leave out the digitalWrite in the setup and add an if statement above the loop?

digitalWrite(ledPin, HIGH);

TanHadron:

digitalWrite(ledPin, HIGH);

?

I'm about to give up and by another uno.lol
one to turn on and off my device and one to log data

Never mind. I mis-read your code and hadn't realized you added a global state variable. That should work as is. Did you try it? Did it work for you?