ESTOU REALIZANDO UM PROJETO  E ESTOU COM UM PEQUENO PROBLEMA NO CÓDIGO ARDUÍNO
MEU PROBLEMA E QUE MEU PROJETO POSSUI DOIS SENSORES INFRAVERMELHOS UM NO INICIO E OUTRO 30CM A FRENTE, FIZ ESSE CÓDIGO E FUNCIONA SO QUE PRECISA  DE UM REPARO.
O SENSOR 1 DETECTA PRESENÇA LIGA O MOTOR, EM SEGUIDA PASSA PELO SENSOR 2 E MANTEM O MOTOR LIGADO SÓ QUE POR CAUSA DO DELAY NO FINAL DO CODIGO, SE O SENSOR 1 TORNAR A DETECTAR PRESENÇA A FUNÇÃO DE CONTINUAR COM MOTOR LIGADO NÃO ESTA FUNCIONANDO.
PRECISO FAZER UMA ALTERAÇÃO NO CÓDIGO PARA QUE QUANDO O DELAY ESTIVER CONTANDO O TEMPO, SE ALGUM SENSOR DETECTAR PRESENÇA CANCELAR O DELAY E EXECUTAR A FUNÇÃO QUE ESTA DETERMINADA NO CODIGO.

CODIGO DO MEU PROJETO

int motor = 3;
int sensor = 4;
int sensor1 = 5;

int estado_sensor = 0;
int estado_sensor1 = 0;

void setup()
{
Serial.begin(9600);
// Define o pino do sensor como entrada
pinMode(sensor, INPUT);
pinMode(sensor1, INPUT);
// Define o pino do MOTOR como saida
pinMode(motor, OUTPUT);
Serial.println("Teste sensor infravermelho Arduino");
}

void loop()
{
estado_sensor = digitalRead(sensor);
if (estado_sensor == 0)
{
// liga o motor
digitalWrite(motor, HIGH);
// Envia mensagem para o Serial Monitor
Serial.println("Movimento detectado!");
}
estado_sensor1 = digitalRead(sensor1);
if (estado_sensor1 == 0)
{
// liga o motor
digitalWrite(motor, HIGH);
// Envia mensagem para o Serial Monitor
Serial.println("Movimento detectado!");
estado_sensor1 = digitalRead(sensor1);
if (estado_sensor1 == 1)
{
delay(2600); (APOS O DELAY DESLIGAR O MOTOR)
digitalWrite(motor,LOW );
}
}

}

Exibições: 303

Responder esta

Respostas a este tópico

Não é possivel parar um delay.

O que vc pode fazer é fragmentar ele.

Use um for para criar um loop() de 260 verificações e delay(10)s. Por ex.

Um do {} while() seria até mais polido pq vc faria os 2 testes (do 260 e da condição).

Ref:

https://www.arduino.cc/en/Reference/For

https://www.arduino.cc/en/Reference/DoWhile

RSS

© 2024   Criado por Marcelo Rodrigues.   Ativado por

Badges  |  Relatar um incidente  |  Termos de serviço