Running a temperature sensor and a servo motor at the same time

I was wondering if there is any way that I can run a temperature sensor to switch a relay on and off while moving a Servo motor at a certain speed. I’ve tried playing with the code to run both at the same time but the problem that I have is one runs after the other and not together. The servo motor has nothing to do with the temperature sensor but I still want both of them to run at the same time. Thank you in advance for everyone that will give me more information about this. I put the code below

Servo myservo;
int pos = 10;
const int relay = 7;
int thermoCLK = 9;
int thermoCS = 8;
int thermoDO = 10;
Adafruit_MAX31855 thermocouple(thermoCLK, thermoCS, thermoDO);
LiquidCrystal lcd(12, 11, 6, 5, 4, 2);

void actuation (int int_pos, int end_pos, double time)

int delay_time_ms = abs(time / ((end_pos - int_pos) / 1) * 60000);

for (pos = int_pos; pos < end_pos; pos += 1)
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in ‘pos’
delay(delay_time_ms); // waits for the servo to reach position

for (pos = int_pos; pos > end_pos; pos -= 1)


void setup() {
// set up the LCD’s number of columns and rows:
lcd.begin(16, 2);

lcd.print(“MAX31855 test”);
// wait for MAX chip to stabilize

pinMode(relay, OUTPUT);



void loop() {

lcd.setCursor(0, 0);
lcd.print("Int. Temp = “);
lcd.print(” ");

double c = thermocouple.readCelsius() - 1.8;
lcd.setCursor(0, 1);
if (isnan(c))
lcd.print(“T/C Problem”);
lcd.print(" ");
double f = (c * 9) / 5 + 32;
lcd.print (“F=”);

if (c >= 23)
digitalWrite(relay, LOW);
digitalWrite(relay, HIGH);

If you want things to happen at the same time you cannot use delay(). The demo several things at a time illustrates how to use millis() to manage timing.

As well as that you should not use a FOR loop to move your servo because that must complete before anything else can happen. You need to move the servo one step at a time using loop() to produce the repeats. That is also illustrated in several things at a time.