Is is possible to run two loops simultaneously??

Hi again,

I have two sketches that i wish to combine together, however i believe due to a delay i have written in one, that they cannot operate independently, is there a way around this??

Code 1 - Clock timer

int ledPin = 9;
int clock = 1000;
int hour = 0;
int min = 0;
int sec = 0;
void setup() {
  
  pinMode(ledPin, OUTPUT);
}
void loop() {
  delay(clock);

  sec = sec + 1;
  // basic conditionals
  if (hour >= 1) {            // after 1 hour pin is high
    digitalWrite(ledPin, HIGH);
  }
    
  // time keeping stuff
  if (sec > 59) {
    sec = 0;
    min = min + 1;
  }
  if (min > 59) {
    min = 0;
    hour = hour + 1;
  }
  if (hour > 23) {
    hour = 0;
  }
  }

Code 2 - Temperature Sensor and data recording

#include <EEPROM.h>
#include "EEPROManything.h"

void setup()
{
Serial.begin(9600);

}

int currentAddress = 0;
float sensor = 0;
float celsius = 0;
float voltage = 0;

void loop()
{
  sensor = analogRead (0);
  voltage = (sensor*5000)/1024; // convert raw sensor value to millivolts
  voltage = voltage-500;        // remove voltage offset
  celsius = voltage/10;         // converts to celsius
  
  
  currentAddress += EEPROM_writeAnything(currentAddress, celsius);  //Writing to EEPROM
  
   Serial.print (celsius); Serial.println(" degrees C");
   Serial.print (currentAddress); Serial.println("EEPROM address");
   
   if (currentAddress == 1024)  //Checking the EEPROM isnt overflowing
   delay (18000000);
   
  delay (45000);                //Delay of 45 seconds to ensure that EEPROM will not fill up from oversampling.
} // end of loop

So i will be sampling temperature while, having a countdown timer that flicks a siren on after 1 hour.
The delay of 45 seconds in the second code, will prevent the first code from doing its job, so i will be flicking the siren on after 1 hour and 45 secs??

Is this the only way around things??

Thanks
Zac

Yes, there is a way around this. Read and experiment with the blink without delay example in the IDE.

So does this mean, that i can loose my countdown timer, and simply put an if statement in, and if it is true, set the siren to high?? Forgive me, but what about my delay of 45 seconds in my temperature writing code, it seems that i would be no better off.

Zac

It usually takes a little longer than 30 minutes to play and experiment with the blink without delay tutorial.

You need to use timers and maybe interrupts (though this doesn't sound timing-critical so maybe ditch the interrupts). It's covered in the example sketches as suggested.

Oh believe me i have been experimenting alot longer than 30mins. I have read about this before earlier today, and was trying to get someone to shed a wee light on it, or if there was any other way around it.

I got it doing what i was after

#include <EEPROM.h>
#include "EEPROManything.h"

int ledPin = 9;
long previousMillis = 0; 
long interval = 12600000;

void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT);

}

int currentAddress = 0;
float sensor = 0;
float celsius = 0;
float voltage = 0;

void loop()
{
  sensor = analogRead (0);
  voltage = (sensor*5000)/1024; // convert raw sensor value to millivolts
  voltage = voltage-500;        // remove voltage offset
  celsius = voltage/10;         // converts to celsius
  
  
  currentAddress += EEPROM_writeAnything(currentAddress, celsius);  //Writing to EEPROM
  
   Serial.print (celsius); Serial.println(" degrees C");
   Serial.print (currentAddress); Serial.println("EEPROM address");
   
   if (currentAddress == 1024)  //Checking the EEPROM isnt overflowing
   delay (18000000);
   
   delay (45000);                //Delay of 45 seconds to ensure that EEPROM will not fill up from oversampling.
   
  unsigned long currentMillis = millis();
  if (currentMillis + (45000 - previousMillis) > interval){
    previousMillis = currentMillis;
    
    digitalWrite(ledPin, HIGH);
  }
    
   
   
                  //Delay of 45 seconds to ensure that EEPROM will not fill up from oversampling.
} // end of loop

You should probably be looking @ using PWM with the ATMEGA timer peripherals for this kind of stuff -

http://arduino.cc/en/Tutorial/SecretsOfArduinoPWM

http://appusajeev.wordpress.com/tag/atmega/