Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Definição dos PINOS
- #define POT A0
- #define LED1 10
- #define LED2 11
- #define LED3 12
- #define BOTAO 3
- #define ECHO 8
- #define TRIG 7
- #define LIGADO 0
- #define DESLIGADO 1
- #define SERIAL_BAUD 115200
- //Cria um buffer global - modo RAIZ
- #define LIMITE_BUFFER 10
- String buffer_msg;
- int estado_sistema;
- void configuraHardware(){
- pinMode(LED1, OUTPUT);
- pinMode(LED2, OUTPUT);
- pinMode(LED3, OUTPUT);
- pinMode(BOTAO, INPUT_PULLUP);
- pinMode(TRIG, OUTPUT);
- digitalWrite(TRIG, LOW);
- pinMode(ECHO, INPUT);
- Serial.begin(SERIAL_BAUD);
- }
- void setup(){
- configuraHardware();
- buffer_msg = "";
- Serial.println("*************************");
- Serial.println("* SISTEMA FATEC *");
- Serial.println("*************************");
- estado_sistema = 0;
- }
- void inverterLed(int LED){
- digitalWrite(LED, !digitalRead(LED));
- delay(300);
- }
- void atingiuLimite(int saida, int valor, int limite){
- if(valor > limite)
- digitalWrite(saida, LIGADO);
- else
- digitalWrite(saida, DESLIGADO);
- }
- void escalaPot(int valor){
- atingiuLimite(LED1, valor, 300);
- atingiuLimite(LED2, valor, 600);
- atingiuLimite(LED3, valor, 900);
- }
- void estavaNaLoop(){
- DisparaPulsoUltrassonico();
- float TempoEcho = pulseIn(ECHO, HIGH);
- float distancia_metros = CalculaDistancia(TempoEcho);
- Serial.print("Distancia:");
- Serial.println(distancia_metros, DEC);
- delay(2000);
- }
- void loop(){
- if(estado_sistema == 0){
- mostrarMenu();
- estado_sistema = 1;
- } else if(estado_sistema == 1){
- if(Serial.available()>0){
- buffer_msg = Serial.readStringUntil('#');
- if(buffer_msg.equals("1")){
- estado_sistema = 2;
- } else if(buffer_msg.equals("2")){
- estado_sistema = 3;
- } else if(buffer_msg.equals("3")){
- estado_sistema = 8;
- }
- }
- } else if(estado_sistema == 2){
- exibeEstadoSistema();
- estado_sistema = 0;
- } else if(estado_sistema == 3){
- exibeMenuEstado3();
- estado_sistema = 4;
- } else if(estado_sistema == 4){
- if(Serial.available()>0){
- buffer_msg = Serial.readStringUntil('#');
- if(buffer_msg.equals("1")){
- estado_sistema = 5;
- } else if(buffer_msg.equals("2")){
- estado_sistema = 6;
- } else if(buffer_msg.equals("3")){
- estado_sistema = 7;
- } else if(buffer_msg.equals("0")){
- estado_sistema = 0;
- }
- }
- } else if(estado_sistema == 5){
- inverterLed(LED1);
- estado_sistema = 3;
- } else if(estado_sistema == 6){
- inverterLed(LED2);
- estado_sistema = 3;
- } else if(estado_sistema == 7){
- inverterLed(LED3);
- estado_sistema = 3;
- }
- }
- void exibeMenuEstado3(){
- Serial.println("1 - Inverte LED1");
- Serial.println("2 - Inverte LED2");
- Serial.println("3 - Inverte LED3");
- }
- void mostrarMenu(){
- Serial.println("1 - Exibe Estado do Sistema");
- Serial.println("2 - Controla Saidas");
- Serial.println("3 - Logger Distância");
- }
- void exibeEstadoSistema(){
- Serial.println("ESTADO SISTEMA!");
- }
- void DisparaPulsoUltrassonico(){
- digitalWrite(TRIG, HIGH);
- delayMicroseconds(10);
- digitalWrite(TRIG, LOW);
- }
- float CalculaDistancia(float tempo_us){
- const float VelocidadeSom_mporus = 0.000340;
- return((tempo_us*VelocidadeSom_mporus)/2);
- }
Advertisement
Add Comment
Please, Sign In to add comment