how to run two loops at the same time

Hi, not sure how to name my problem, so if there is a topic with the same discussion,
just redirect me there, thank you.

Simply I want arduino to check every second temperature from a sensor and
keep track of highest and lowest temperature. At the same time I want the
current temperature, highest and lowest to be displayed on a display. Since the
display has only 2 lines, I want to use buttons to scroll through the display.

My problem is, how to write the code in such a way, that while Im going through
the display menu with buttons, arduino still keeps reading the temperature every
second (plus minus few millisec).

It is a simple task, but I dont see a simple solution how to implement it. Do I need
some parallel programing for this?

Thank you

Do I need some parallel programing for this?

No. You need some code. Presumably, you already have some that you didn't post.

Im sorry, I see this more as a general problem, than one special code...But let me give an example so Im clearer about what I want.

sketch of code:

#include
initialize display and sensor communication;
define global variables;

// define functions

checkbuttonstatus; //checks which button was pressed or none

readtemperature//reads temperature from sensor

renewdata // takes temperature, compares it to the lowest and highest temperature it has stored, if it is higher rewrites highest, if its lower rewrites lowest

printmainmenu // prints on display current temperature, highest temp and lowest temperature

void setup() { }

void loop
{
boolean buttonnotpressed=1;

while(buttonnotpressed)
{
checkbuttonstatus; \if button was pressed buttonnotpressed becomes 0 and while loop stops, otherwise it is 1
readtemperature; \read sensor data
renewdata; \compare with stored data
printmainmenu;
delay(1000); }

//to get here somebody had to press a button

switch(buttonpressed) // we get different screens depending on which button was pressed
{
case button1: // show menu 1 for 2 seconds

case button2: //show menu 2 for 2 seconds
}

}

so I have actually two problems:
1st: when in the while(buttonnotpressed) loop, during 1s delay arduino ignores buttons input
2nd: when in the switch(buttonpressed) , during 2s delay arduino doesnt measure current temperature and doesnt store min and max temperature

First problem I could resolve by 1ms delay in while loop and counter to keep track how many times the loop went and then call readtemperature; renewdata; printmainmenu; only after 100loops went.

For the second problem I could write delay(1000); readtemperature; renewdata; delay(1000) instead of 2 second delay

But I find both of these solutions silly and adding lots of code. When in future I will add some submenus or want to prolong the display times, I find it very uncomfortable. Isnt there some nicer logical way to do this? Thanks

plus minus few millisec

Your question hints at the solution. Look at the BlinkWithoutDelay example in the IDE to see how to use millis() to execute code at a given interval whilst not blocking other code from running.

Do I need some parallel programing for this?

No such thing on an Arduino but you can do several things in series very quickly using non blocking code as in the example referred to above.

Was it something like this you where requesting?
This example is built on my statemachine library wich supports concurrency and timing. The library can be found here:

http://playground.arduino.cc/Code/SMlib

I did not have a LCD so im using serial in this example, it should be easy to adapt

#include <SM.h>

SM Temperature(Tread, Twait);
int Tmax;
int Tmin;
int TCurrent;

SM Menu(MainDisplay, MainResponse);
const int Btn1 = 2;
const int Btn2 = 3;
int Btn1State;
int Btn2State;

void setup(){
  Serial.begin(115200);
}//setup()

void loop(){
  EXEC(Temperature);
  EXEC(Menu);
}//loop()

State MainDisplay(){
  Serial.print("Main\t");
  displayTemp();
  Serial.println("Press: btn1 for menu1, btn2 for menu2");
}//MainDisplay()

State MainResponse(){
  if(RE(digitalRead(Btn1), Btn1State)) Menu.Set(Sub1Display, Sub1Response);
  if(RE(digitalRead(Btn2), Btn2State)) Menu.Set(Sub2Display, Sub2Response);
  if(Menu.Timeout(1000)) Menu.Set(MainDisplay, MainResponse);//refresh menu
}//MainResponse()

State Sub1Display(){
  Serial.print("Menu1\t");
  displayTemp();
  Serial.println("Press btn1 for main menu");
}//Sub1Display()

State Sub1Response(){
  if(RE(digitalRead(Btn1), Btn1State)) Menu.Set(MainDisplay, MainResponse);
  if(Menu.Timeout(1000))  Menu.Set(Sub1Display, Sub1Response);//refresh menu
}//Sub1Resopnse()

State Sub2Display(){
  Serial.print("Menu2\t");
  displayTemp();
  Serial.println("Press btn2 for main menu");
}//Sub2Display()

State Sub2Response(){
  if(RE(digitalRead(Btn2), Btn2State)) Menu.Set(MainDisplay, MainResponse);
  if(Menu.Timeout(1000))  Menu.Set(Sub2Display, Sub2Response);//refresh menu
}//Sub2Resonse()

State Tread(){
  TCurrent = analogRead(2);//once every second
  Tmax = max(Tmax, TCurrent);
  Tmin = min(Tmin, TCurrent);
}//Tread()

State Twait(){
  if(Temperature.Timeout(1000)) Temperature.Set (Tread, Twait);//repeat after 1s
}//Twait()

void displayTemp(){
  Serial.print("Current temp: ");
  Serial.print(TCurrent);
  Serial.print("\tMax temp: ");
  Serial.print(Tmax);
  Serial.print("\tMin temp: ");
  Serial.println(Tmin);
}//displaytemp()

kingbean:
My problem is, how to write the code in such a way, that while Im going through
the display menu with buttons, arduino still keeps reading the temperature every
second (plus minus few millisec).

Thanks much for response. I read blink and this basic stuff, never noticed this non-blocked blink.
Seems thats possible way to go. Thank you again.