int pir = 4;
int potenciometro = 0;
int buzzer = 18;
int ledR = 21;
int ledG = 20;
int ledB = 19;
int botao = 5;
unsigned long tempo_prensagem = 0;
unsigned long tempo_retorno = 0;
unsigned long tempo_alarme = 0;
int old_value = LOW;
int old_value_botao = HIGH;
void repouso(void);
void prensagem(void);
void retorno(void);
void alarmes(void);
void finish(void);
enum ESTADOS{REST, PRESS, RETURN, ALARM, FINISH};
ESTADOS current_state = REST;
ESTADOS next_state = PRESS;
void loop(){
current_state = next_state;
switch(current_state){
case REST:
repouso();
break;
case PRESS:
prensagem();
break;
case RETURN:
retorno();
break;
case ALARM:
alarmes();
break;
case FINISH:
finish();
break;
default:
next_state = REST;
}
delay(100);
}
void setup(){
Serial.begin(115200);
ledcAttach(ledR, 1000, 8);
ledcAttach(ledG, 1000, 8);
ledcAttach(ledB, 1000, 8);
pinMode(potenciometro, INPUT);
pinMode(pir, INPUT);
pinMode(botao, INPUT_PULLUP);
ledcAttach(buzzer, 1000, 8);
}
void repouso(void){
Serial.println("REST");
ledcWrite(ledG, 255);
int valor_pressao = analogRead(potenciometro);
Serial.println(valor_pressao);
if (valor_pressao <= 1000){
next_state = REST;
}
else{
tempo_prensagem = millis();
ledcWrite(ledG, 255);
next_state = PRESS;
}
}
void prensagem(void){
int valor_pressao = analogRead(potenciometro);
Serial.println("Press");
if (valor_pressao >= 3000){
Serial.println("ERRO! VALOR LIMITE DE PRESSÃO ATINGIDO!");
next_state = ALARM;
}
else{
if (valor_pressao >= 1000){
Serial.println("Alvo atingido!");
ledcWrite(ledR, 255);
ledcWrite(ledG, 255);
ledcWriteTone(buzzer, 300);
unsigned long now = millis();
if (now - tempo_prensagem > 3000){
ledcWrite(ledR, 0);
ledcWrite(ledG, 0);
ledcWriteTone(buzzer, 0);
next_state = RETURN;
}
}
else{
next_state = PRESS;
}
}
}
void retorno(void){
Serial.println("Return");
ledcWrite(ledB, 255);
ledcWriteTone(buzzer, 5000);
int new_value = digitalRead(pir);
unsigned long now = millis();
if (now - tempo_prensagem < 7000){
if (new_value != old_value){
Serial.println("Colisão detetada!");
ledcWriteTone(buzzer, 0);
ledcWrite(ledR, 0);
next_state = FINISH;
}
}
else{
Serial.println("TEMPO PARA RETORNO EXCEDIDO!");
next_state = ALARM;
}
}
void alarmes(void){
Serial.println("ALARM");
ledcWrite(ledR, 255);
ledcWriteTone(buzzer, 1000);
delay(500);
ledcWrite(ledR, 0);
ledcWriteTone(buzzer, 0);
delay(500);
int leituraBotao = digitalRead(botao);
Serial.print("Valor do botao: ");
Serial.println(leituraBotao);
if (leituraBotao == LOW) {
Serial.println("Botão premido!");
ledcWriteTone(buzzer, 0);
ledcWrite(ledR, 0);
next_state = FINISH;
}
}
void finish(void){
Serial.println("Para recomeçar, prima o botão");
int leituraBotao = digitalRead(botao);
Serial.print("Valor do botao: ");
Serial.println(leituraBotao);
if (leituraBotao == LOW) {
next_state = REST;
}
}Loading
esp32-c6-devkitc-1
esp32-c6-devkitc-1