Motor de passo e sensor de temperatura/umidade

Olá pessoal,
eu faço engenharia elétrica e preciso fazer um programa para o arduino (uso o uno) que aparece no lcd a temperatura,umidade e o relógio (o que já consegui fazer de boa e testei, funcionando perfeitamente) o problema é que agora tenho que incrementar neste programa um motor de passo para quando a temperatura cair e a umidade aumentar ele acionara o motor de passo no sentido horário e quando o inverso acontecer no sentido anti-horário. Basicamente é como se quando a temperatura cair e a umidade aumentar o motor fecharia uma janela automaticamente e quando o inverso acontecer ele abriria, uma climatização do ambiente automatizada.
Podem por defeito. pq não tá compilando, acho q é pq não soube fazer o motor funcionar corretamente.
Meu programa ficou assim:

#include <LiquidCrystal.h>
#include "dht11.h"
#include <Stepper.h>
#define STEPS 100
#define pinSensor 3
Stepper stepper(STEPS, 5, 6, 9, 10);
LiquidCrystal lcd(2, 4, 7, 8, 12, 13);
dht11 sensor;

int Segundos = 0;
int Minutos = 0;
int Hora = 0;
char tempo[8];
byte grau[8] = { 0b00110,
0b01001,
0b01001,
0b00110,
0b00000,
0b00000,
0b00000,
0b00000 };

int flag = 0; // 0-aberta 1-fechado dessa forma a janela encontra-se aberta inicialmente
void abrir();
void fechar();

void setup()
{
stepper.setSpeed(30);

pinMode(3,INPUT);
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);

lcd.begin(16, 2);
Serial.begin(9600);
lcd.createChar(1, grau);

cli();

TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;

OCR1A = 15624;

TCCR1B |= (1 << WGM12);

TCCR1B |= (1 << CS12)|(1 << CS10);

TIMSK1 |= (1 << OCIE1A);

sei();
}

void loop()

{

int estado = sensor.read(pinSensor);
int hum = sensor.read(sensor.humidity);
int temp = sensor.read(sensor.temperature);

Serial.println(estado);

if(estado ==0)
{
lcd.setCursor(0,2);
lcd.print("RH: ");
lcd.print(sensor.humidity);
lcd.print("%");
lcd.setCursor(7,2);
lcd.print("TEMP: ");
lcd.print(sensor.temperature);
lcd.write(1);
lcd.print("C");

Serial.print("RH: ");
Serial.print(sensor.humidity);
Serial.println("%");
Serial.print("TEMP: ");
Serial.print(sensor.temperature);
Serial.write(176);
Serial.println("C");

}

delay(1000);

{
flag=hum;
flag=temp;
if(hum >= 65 && temp <=20)
{
switch (flag)
{
case(0):
fechar();
break;
case(1):
abrir();
break;
}
}
if(hum <=20 && temp >=27)
{
switch (flag)
{
case(0):
abrir();
break;
case(1):
fechar();
break;
}
}
{
void fechar();
{
flag = 0;

digitalWrite(9,HIGH);
delay(100);
digitalWrite(9,LOW);
delay(100);
digitalWrite(10,HIGH);
delay(100);
digitalWrite(10,LOW);
delay(100);
digitalWrite(5,HIGH);
delay(100);
digitalWrite(5,LOW);
delay(100);
digitalWrite(6,HIGH);
delay(100);
digitalWrite(6,LOW);
delay(100);
while (flag=0)
{
flag = (hum >= 65 && temp <=20);
stepper.step(100);
delay(100);
}
}
void abrir();
{
flag = 1;

digitalWrite(9,HIGH);
delay(100);
digitalWrite(9,LOW);
delay(100);
digitalWrite(10,HIGH);
delay(100);
digitalWrite(10,LOW);
delay(100);
digitalWrite(5,HIGH);
delay(100);
digitalWrite(5,LOW);
delay(100);
digitalWrite(6,HIGH);
delay(100);
digitalWrite(6,LOW);
delay(100);
{
flag=(hum <=20 && temp >=27);
stepper.step(-100);
delay(100);
}
}
}
}
}

ISR(TIMER1_COMPA_vect);

{
if (Segundos < 59)
{
Segundos++;
}
else
{
Segundos = 0;
Minutos++;
}

if (Minutos > 59)
{
Minutos = 0;
Hora++;
}
sprintf (tempo, "%02d:%02d:%02d",Hora, Minutos, Segundos);
Serial.println(tempo);
lcd.setCursor(0,0);
lcd.print(tempo);
}

No teu skeetch tens isto:

int flag = 0; // 0-aberta 1-fechado dessa forma a janela encontra-se aberta inicialmente

Depois logo abaixo tens isto:

flag=hum;
flag=temp;
if(hum >= 65 && temp <=20)
{
switch (flag)
{
case(0):
fechar();
break;
case(1):
abrir();
break;
}
}
if(hum <=20 && temp >=27)
{
switch (flag)
{
case(0):
abrir();
break;
case(1):
fechar();
break;
}
}
{

Queres explicar isto?
é que algo muito mal explicado esta a acontecer aqui :slight_smile:
Outra questao é, configuraste os timers para gerarem uma interrupçao tudo bem, mas sabes que dentro da rotina de interrupçao as variaveis que la usas dentro se forem globais devem ser declaradas como volatile?
Outra questao é que axo que nao deves usar Serial.prints dentro da rotina de interrupçao ja que elas sao lentasssss e dentro da rotina de interrupçao deves fazer instruçoes o mais rapido possivel

First ... when do you use "volatile" variables?
A variable should only be marked volatile if it is used both inside an ISR, and outside one.
Variables only used outside an ISR should not be volatile.
Variables only used inside an ISR should not be volatile.
Variables used both inside and outside an ISR should be volatile.

Lê este topico do Nick Gammon sobre interrupções