Temps on loop whilst running everything else

Hi there,

I'm making an advanced timer, that constantly reads two ds18b20 sensors and reports back their temps to a 7 segment display, with another 7 segment for the timer, and an LCD that shows instructions etc during the timer.

The timer also runs a servo at several regular instances during the timer.

Issue
So far, I have got as far as getting the timer side/lcd all sorted but when Ive put the temp sensors in, they stop working once I have pressed a button (button press is waiting in loop).
Is there a way to keep them going, whilst the Arduino is working it's way through the timer commands?
Could I put the button presses in setup? They only need to be pressed once to set off the timer program.

And here is my loop code at present

void loop() {

  if (digitalRead(4) == LOW) {
    //Button one is pressed!
    lcd.clear();
    lcd.print("C41 Process");
    digitalWrite(5, LOW);
    digitalWrite(6, HIGH);
    digitalWrite(7, LOW);
    delay(5000);
    Program1();

    if (digitalRead(11) == LOW) {
      //Button two is pressed!
      lcd.clear();
      lcd.print("E6 Process");
      digitalWrite(5, LOW);
      digitalWrite(6, LOW);
      digitalWrite(7, HIGH);
      delay(5000);
      Program1();

    }

    delay(100);

  }
  //temp loop stuff
  sensors.requestTemperatures();
  tempC = sensors.getTempCByIndex(0);
  tempC1 = sensors.getTempCByIndex(1);
  delay(500);
  Serial.println(tempC);
  Serial.println(tempC1);

}

As you can see, at present the servo commands don't exist yet, Ive just got them to turn a led on until I can get past this issue.

On top of that for reference, the relay references in the script will be used for lighting up certain buttons with 12v, so Ive left them in, albeit unused at present.

Entire script is in next post due to character limit

Is there a way to keep them going, whilst the Arduino is working it's way through the timer commands?

Yes. Do not write blocking code. That means NO for loops. Let loop() do all the looping required. It means ABSOLUTELY NO delay()s.

Entire script code so far

#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#include "SevenSegmentTM1637.h"
#include "SevenSegmentExtended.h"
#include <OneWire.h>
#include <DallasTemperature.h>

// All the LCD STUFF

#define I2C_ADDR    0x27  // Define I2C Address where the PCF8574A is
#define BACKLIGHT_PIN     3
#define En_pin  2
#define Rw_pin  1
#define Rs_pin  0
#define D4_pin  4
#define D5_pin  5
#define D6_pin  6
#define D7_pin  7

LiquidCrystal_I2C  lcd(I2C_ADDR, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin);

//Temperature sTUFF
#define ONE_WIRE_BUS 6

OneWire oneWire(ONE_WIRE_BUS);

DallasTemperature sensors(&oneWire);

float tempC = 0;
float tempC1 = 0;
int oilWarningTemp = 27;

//7Segment stuff
//1st Display
const byte PIN_CLK = 10;   // define CLK pin (any digital pin)
const byte PIN_DIO = 11;   // define DIO pin (any digital pin)
SevenSegmentExtended      display1(PIN_CLK, PIN_DIO);
//2nd Display
const byte PIN_CLK1 = 12;   // define CLK pin (any digital pin)
const byte PIN_DIO1 = 13;   // define DIO pin (any digital pin)
SevenSegmentExtended      display2(PIN_CLK1, PIN_DIO1);

void setup() {
  //Buttons
  pinMode(4, INPUT_PULLUP);
  // pinMode(11, INPUT_PULLUP);
  // pinMode(10, INPUT_PULLUP);


  //7 Segment Setup stuff
  //Display1
  display1.begin();            // initializes the display
  display1.setBacklight(100);  // set the brightness to 100 %
  display1.print("HIYA");      // display INIT on the display
  delay(1000);                // wait 1000 ms
  //Display1
  display2.begin();            // initializes the display
  display2.setBacklight(100);  // set the brightness to 100 %
  display2.print("LOVE");      // display INIT on the display
  delay(1000);                // wait 1000 ms

  //LED's
  pinMode(5, OUTPUT); //-Go Button
  pinMode(7, OUTPUT); //-B2 Button
  pinMode(9, OUTPUT); //-B2 Button

  //Initialise LCD
  lcd.begin (20, 4);

  // Switch on the lcd backlight
  lcd.setBacklightPin(BACKLIGHT_PIN, POSITIVE);
  lcd.setBacklight(HIGH);
  lcd.home ();                   // go home


  //Relay Startup stuff
#define RELAY_ON 0
#define RELAY_OFF 1

  //Relay Pin Numbers
#define Relay_1  2  // Arduino Digital I/O pin number
#define Relay_2  3  // Arduino Digital I/O pin number

  //-------( Initialize Pins so relays are inactive at reset)----
  digitalWrite(Relay_1, RELAY_OFF);
  digitalWrite(Relay_2, RELAY_OFF);


  //---( THEN set pins as outputs )----
  pinMode(Relay_1, OUTPUT);
  pinMode(Relay_2, OUTPUT);
  delay(1000); //Check that all relays are inactive at Reset

  //Temp setup stuff
  sensors.begin();
  pinMode(3, OUTPUT);
  analogWrite(3, 0);
  Serial.begin(9600);

  // LCD First Power Up Messages
  lcd.print("   The Developist");
  lcd.setCursor ( 0, 2 );        // go to the 2nd line
  lcd.print("  *Select Program*");
  lcd.setCursor ( 0, 3 );        // go to the 2nd line
  lcd.print("  C41-E6-BW-Stand");

  delay(500);
  //Turn on Button LED's

  digitalWrite(5, HIGH);   // Turn on the LED
  digitalWrite(6, HIGH);   // Turn on the LED
  digitalWrite(7, HIGH);   // Turn on the LED

}

void loop() {

  if (digitalRead(4) == LOW) {
    //Button one is pressed!
    lcd.clear();
    lcd.print("C41 Process");
    digitalWrite(5, LOW);
    digitalWrite(6, HIGH);
    digitalWrite(7, LOW);
    delay(5000);
    Program1();

    if (digitalRead(11) == LOW) {
      //Button two is pressed!
      lcd.clear();
      lcd.print("E6 Process");
      digitalWrite(5, LOW);
      digitalWrite(6, LOW);
      digitalWrite(7, HIGH);
      delay(5000);
      Program1();

    }

    delay(100);

  }
  //temp loop stuff
  sensors.requestTemperatures();
  tempC = sensors.getTempCByIndex(0);
  tempC1 = sensors.getTempCByIndex(1);
  delay(500);
  Serial.println(tempC);
  Serial.println(tempC1);

}

// - Start of all the programs


void Program1() {
  lcd.clear();
  lcd.print("Set Temp to 38c");
  lcd.setCursor ( 0, 1 );        // go to the 2nd line
  lcd.print("Press when Ready");
  digitalWrite(5, HIGH);
  while (digitalRead(4) == HIGH) {
    // Do nothing
  }
  digitalWrite(5, LOW);
  lcd.clear();
  lcd.print("Sequence GO!");
  delay(3000);
  lcd.clear();
  lcd.setCursor ( 0, 0 );
  lcd.print("Developing Cycle");
  c41DevCycle();
  delay(10000);
  lcd.clear();
  digitalWrite(6, LOW);   // Turn on the LED
  lcd.print("Program Finished");
}



void c41DevCycle() {
  uint8_t c41Devcountdown = 180;
  uint8_t c41relayCountdown = 15;

  bool runningc41Dev = true;
  bool updateDisplayc41Dev = true;
  uint32_t countdownTimec41Dev = millis();
  uint32_t relayOnTimec41Dev = 5;
  uint32_t relayOffTimec41Dev = 25;
  bool relayStatec41Dev = true;

  //-enter the servo function below
  functionOn();
  while (runningc41Dev) {
    if (updateDisplayc41Dev) {
      updateDisplayc41Dev = false;
      lcd.setCursor(0, 2);
      char m[3];
      char s[3];
      sprintf(m, "%02d", int(c41Devcountdown / 60));
      sprintf(s, "%02d", c41Devcountdown % 60);
      lcd.print(m);
      lcd.print(":");
      lcd.print(s);
      //Below is the countdown in minutes and seconds for 7segment display
      byte c41Devminutes    = (c41Devcountdown / 60);
      byte c41Devseconds  = (c41Devcountdown % 60);
      display1.printTime(c41Devminutes, c41Devseconds, true);  // display time


      if (relayStatec41Dev) {
        lcd.print("  Agitating");
      }
      else {
        lcd.print("   Standing");
      }

      lcd.setCursor(0, 4);
      if (relayStatec41Dev) {
        lcd.print("AGI stops in ");
      }
      else {
        lcd.print("Next AGI in  ");
      }
      sprintf(s, "%02d", c41relayCountdown);
      lcd.print(s);
    }

    // Every second: count down.
    if (millis() - countdownTimec41Dev > 1000) {
      if (c41Devcountdown == 0) { // Finished! (this happens after the last second has passed).
        runningc41Dev = false;
      }
      c41Devcountdown--;
      c41relayCountdown--;
      countdownTimec41Dev += 1000;
      updateDisplayc41Dev = true;
      if (c41relayCountdown == 0) { // Switch the relay.
        if (relayStatec41Dev) {
          //enter the servo off function below
          functionOff();
          relayStatec41Dev = false;
          c41relayCountdown = relayOffTimec41Dev;
        }
        else {
          //enter the servo on function below
          functionOn();
          relayStatec41Dev = true;
          c41relayCountdown = relayOnTimec41Dev;
        }
      }
    }
  }
  lcd.clear();
  display1.clear();
}


void e6DevCycle() {
  uint8_t e6Devcountdown = 120;
  uint8_t e6relayCountdown = 5;

  bool runninge6Dev = true;
  bool updateDisplaye6Dev = true;
  uint32_t countdownTimee6Dev = millis();
  uint32_t relayOnTimee6Dev = 5;
  uint32_t relayOffTimee6Dev = 25;
  bool relayStatee6Dev = true;

  digitalWrite(Relay_1, RELAY_ON);  //--Insert here if you want the relay turned on at the start
  while (runninge6Dev) {
    if (updateDisplaye6Dev) {
      updateDisplaye6Dev = false;
      lcd.setCursor(0, 2);
      char m[3];
      char s[3];
      sprintf(m, "%02d", int(e6Devcountdown / 60));
      sprintf(s, "%02d", e6Devcountdown % 60);
      lcd.print(m);
      lcd.print(":");
      lcd.print(s);

      if (relayStatee6Dev) {
        lcd.print(" Agitating");
      }
      else {
        lcd.print("  Standing");
      }

      lcd.setCursor(0, 4);
      if (relayStatee6Dev) {
        lcd.print("AGI stops in ");
      }
      else {
        lcd.print("Next AGI in  ");
      }
      sprintf(s, "%02d", e6relayCountdown);
      lcd.print(s);
    }

    // Every second: count down.
    if (millis() - countdownTimee6Dev > 1000) {
      if (e6Devcountdown == 0) { // Finished! (this happens after the last second has passed).
        runninge6Dev = false;
      }
      e6Devcountdown--;
      e6relayCountdown--;
      countdownTimee6Dev += 1000;
      updateDisplaye6Dev = true;
      if (e6relayCountdown == 0) { // Switch the relay.
        if (relayStatee6Dev) {
          digitalWrite(Relay_1, RELAY_OFF);
          relayStatee6Dev = false;
          e6relayCountdown = relayOffTimee6Dev;
        }
        else {
          digitalWrite(Relay_1, RELAY_ON);
          relayStatee6Dev = true;
          e6relayCountdown = relayOnTimee6Dev;
        }
      }
    }
  }
  lcd.clear();
}
void functionOn() {
  digitalWrite(9, HIGH);
}

void functionOff() {
  digitalWrite(9, LOW);
}
float tempC = 0;
float tempC1 = 0;

Do you count uh-huh, two, three? Or, do you count one, two, three? When you start numbering variables that seem related, number ALL of them. Or, get smart and use an array.

  pinMode(7, OUTPUT); //-B2 Button
  pinMode(9, OUTPUT); //-B2 Button

You have two -B2 buttons?

    delay(5000);

That's got to go.

      delay(5000);

And that.

      delay(100);

And that.

      delay(500);

And that.

void Program1() {

That is a meaningless name for a function. Can you imagine the Arduino team replacing digitalRead() with program128()? Exercise your gray cells. Come up with a better name.

  while (digitalRead(4) == HIGH) {
    // Do nothing
  }

NO, absolutely NOT.

You must learn to write code that is event driven, NOT code that waits for something to happen.

      char m[3];
      char s[3];
      sprintf(m, "%02d", int(c41Devcountdown / 60));
      sprintf(s, "%02d", c41Devcountdown % 60);
      lcd.print(m);
      lcd.print(":");
      lcd.print(s);

sprintf() is a sledgehammer where you need a flyswatter.

      byte c41Devminutes    = (c41Devcountdown / 60);
      byte c41Devseconds  = (c41Devcountdown % 60);

(Didn't) (you) (just) (calculate) (those) (values) (?)

Get rid of the useless parentheses.

That's enough. You need to google "state machine" and completely restructure your code.

You have many valid points, regarding delays etc

The generic program names etc, are just until I get stuff working, then they will be refined.

The generic program names etc, are just until I get stuff working, then they will be refined.

"I'm going to useless meaningless names until the code is working perfectly. Then, I'll fix them". Interesting approach. Not one that I recommend. YMMV.