Convert from Delay to millis, How?

Hi
As described in header. I wants to switch from delay witch in my case isn't going to make my day ... I've read several posts and reference material about it, but either I'm plain stupid or something...

Could someone point me in the right direction, I post my code that I use now. I Wants to read the Dallas sensor, then show that on the LCD, after 5 seconds I wants to show the FAN rpm for 5 seconds. I also need to change the void fanRPM function and remove delay from that one to.

void loop()
{
  showTemp();
  delay(5000);
  fanRPM();
  delay(4000);
}
 
void showTemp()
{
  sensors.requestTemperatures();
  tempDevice0 = sensors.getTempCByIndex(0);
  lcd.setCursor(0, 0);
  lcd.print("Showing Temp    ");
  lcd.setCursor(0, 1);
  lcd.print(tempDevice0);
  lcd.setCursor(5, 1);
  lcd.print(strThree);
}
 
void fanRPM()
{
  half_revolutions = 0;
  attachInterrupt(0, rpm_fan, FALLING);
  delay(1000);
  detachInterrupt(0);
  rpm = half_revolutions * 30;
  lcd.setCursor(0, 0);
  lcd.print("Showing FAnspeed    ");
  lcd.setCursor(0, 1);
  lcd.print(rpm + strTwo);
   
}

 // this code will be executed every time the interrupt 0 (pin2) gets low.

void rpm_fan()
{
  half_revolutions++;
}

Before someone will advise you not to use interrupt, do some reading on it.
Than start with how to generate the interrupt at 5 seconds interval. Using delay will do.
Than figure out what code you are interrupting and what process you want to execute and when.
You have decent code base you just need to “plug” in ( and modify) into interrupt driven process.
Good luck.

Don't use interrupts. You don't need interrupts. You most certainly don't need interrupts to make a 5 second timer.

Study the concept in the Blink Without Delay example sketch. Then look at the longer example in the demo sketch I wrote in this Thread Demonstration code for several things at the same time - Project Guidance - Arduino Forum

...R

Than start with how to generate the interrupt at 5 seconds interval. Using delay will do

No, I didn't understand that either.
I think it is supposed to read "Rather than starting with how the generate the interrupt at 5 second intervals", but then it all goes wrong.
If you look at the blink without delay example, you will find a technique to schedule events without the added complexity of interrupts or having to

figure out what code you are interrupting

(which is another bit I didn't understand)

Vaclav:
Using interrupts could be useful for me in the future. I've learning Arduino now for the major project to my water chilled computer. But here I only needed some thing to get ride of delay so I can process several things almost at the same time...

Robin2:
Thanxs for putting me in the right direction, I'm still learning why and how it works but slowly like a pwm signal it will be lit someday :stuck_out_tongue: . I rewrote the code like this, can't figure out thou why I don't receive any transmission from the Arduino but I will solve it ( hopefully)...

void loop()
{
  showTemp();
  if(Serial.available() > 0)
  {
    serByte = Serial.read();
    if(serByte == 2)
    {
      Serial.println(tempDevice0);
    }
    
  }
  
  
}
 
void showTemp()
{
  if(millis() - Timer1 > interval)
  {
    sensors.requestTemperatures();
    tempDevice0 = sensors.getTempCByIndex(0);
    lcd.setCursor(0, 0);
    lcd.print("Showing Temp    ");
    lcd.setCursor(0, 1);
    lcd.print(tempDevice0);
    lcd.setCursor(5, 1);
    lcd.print(strThree);
    Timer1 = millis();
    
  }
}

You haven't shown all of your code.

I guess tempDevice0 is a global variable but I can't guess if it is an INT or BYTE or FLOAT

Change the code in loop() to the following as a temporary test

void loop()
{
  showTemp();
  Serial.println(tempDevice0);
  delay(1000);  // don't worry, this is just for testing to slow the output to the monitor
}

If that works then look carefully at how you are trying (unsuccessfully) to deal with the Serial stuff. Go through your code line by line and write down what happens at each step. Then consider whether you have considered every possibility.

...R

What is sending serial data? Is it REALLY sending binary data? Or, should you be comparing to '2', rather than 2?

Robin2:
Well I got it :slight_smile: but it took a while before i did... Rewrote the code again to better handle timers and function calls...

// Read and display temperatures with SPI based LCD and to serial monitor
// Johnny Lindberg
// Libs used
#include <SPI.h>
#include <LiquidCrystal.h>
#include <DallasTemperature.h>
#include <OneWire.h>
// Define witch Pin Dallas temp is connected to
#define ONE_WIRE_BUS 8

// Variables and sensors, timers and other
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
LiquidCrystal lcd(10);
String strOne, strTwo, strThree;
unsigned long Timer1;
unsigned long Timer2;
int lcdTimerIntVal = 5000;
int tempTimerIntVal = 2500;
char serByte;
float tempDevice0;
boolean coldStart = true;

// Setup all start
void setup()
{
  lcd.begin(16, 2);
  sensors.begin();
  Serial.begin(9600);
  strOne= String("JonLee Cooler   ");
  strTwo = String("C kylaren    ");
  strThree= String("Starting up...");
  lcd.print(strThree);
}

// Main loop
void loop()
{
  // Do a coldstart routine to display temp without delay
  if(coldStart == true)
  {
    delay(3000);
    readTemp();
    lcdPrintTemp();
    coldStart = !coldStart;
  }
  // Call Timers to read and display temperature on LCD
  tempTimer();
  lcdTimer();
  // Check if Serial read is incoming and send back temp value
  if(Serial.available())
  {
    serByte = Serial.read();
    // Check if it is the correct number recived
    if(serByte == '1')
    {
      // Send temp back to Serial monitor
      Serial.print(tempDevice0);
      Serial.println("C kylare!");
    }
  }
}
    
// LCD Timer
void lcdTimer()
{
  if(millis() - Timer1 > lcdTimerIntVal)
  {
    lcdPrintTemp();
    Timer1 = millis();
  }
}

// Temp Timer
void tempTimer()
{
  if(millis() - Timer2 > tempTimerIntVal)
  {
    readTemp();
    Timer2 = millis();
  }
}

// Read Dallas temperature module
void readTemp()
{
  sensors.requestTemperatures();
  tempDevice0 = sensors.getTempCByIndex(0);
}

// LCD Printer function
void lcdPrintTemp()
{
  lcd.setCursor(0, 0);
  lcd.print(strOne);
  lcd.setCursor(0, 1);
  lcd.print(tempDevice0);
  lcd.setCursor(5, 1);
  lcd.print(strTwo);
}