#include <Wire.h> #include <LiquidCrystal_I2C.h> LiquidCrystal_I2C lcd( 0x27,20,4); boolean enter=false; int menus =0; int salida=0; int salidados=0; int rele=6; int rele_dos=7; int encoderPin1 = 2; int encoderPin2 = 3; int boton=A0 ; int valor_boton=0; boolean h = false; volatile int lastEncoded = 0; volatile long encoderValue = 0; boolean uno = false; long lastencoderValue = 0; int ultimoencoder=0; int lastMSB = 0; int lastLSB = 0; int valor_encendido=0; const int pinBuzzer = 12; unsigned long tiempodiferencia =0; int tiempodiferenciados=500; int led_nueve=9; int led_diez=10; unsigned long tiempomillis,tiempoactual=0,retardo=0; int led_ocho=8; int led_once=11; /* int menus =0; int salida=0; int salidados=0; int rele=6; int rele_dos=7; int encoderPin1 = 2; int encoderPin2 = 3; int boton=A0 ; int valor_boton=0; boolean h = false; volatile int lastEncoded = 0; volatile long encoderValue = 0; boolean uno = false; long lastencoderValue = 0; int ultimoencoder=0; int lastMSB = 0; int lastLSB = 0; int valor_encendido=0; const int pinBuzzer = 12; unsigned long tiempodiferencia =0; int tiempodiferenciados=500; LiquidCrystal_I2C lcd( 0x27,16,2); boolean enter=false; */ void setup() { pinMode(encoderPin1, INPUT_PULLUP); pinMode(encoderPin2, INPUT_PULLUP); attachInterrupt(0, updateEncoder, CHANGE); attachInterrupt(1, updateEncoder, CHANGE); lcd.init(); lcd.backlight(); encoderValue=0; pinMode(rele,OUTPUT); pinMode(rele_dos,OUTPUT); pinMode(boton,INPUT); pinMode(led_diez,OUTPUT); pinMode(led_nueve,OUTPUT); pinMode(pinBuzzer,OUTPUT); pinMode(led_ocho,OUTPUT); pinMode(led_once,OUTPUT); lcd.setCursor(3,0); lcd.print("EN PROCESO"); /////////////////////////////////// delay(100); tone(12,293.66,200); delay(200); tone(12,293.66,100); delay(100); tone(12,293.66,100); delay(100); tone(12,440,100); delay(100); tone(12,523.25,100); delay(100); tone(12,587.33,100); delay(200); tone(12,587.33,100); delay(200); tone(12,587.33,100); delay(100); tone(12,659.25,100); delay(100); tone(12,698.45,100); delay(200); tone(12,698.45,100); delay(200); tone(12,698.45,100); delay(100); tone(12,783.99,100); delay(100); tone(12,659.25,100); delay(200); tone(12,659.25,100); delay(200); tone(12,587.33,100); delay(100); tone(12,523.25,100); delay(100); tone(12,523.25,100); delay(100); tone(12,587.33,100); delay(300); tone(12,440,100); delay(100); tone(12,523.25,100); delay(100); tone(12,587.33,100); delay(200); tone(12,587.33,100); delay(200); tone(12,587.33,100); delay(100); tone(12,659.25,100); delay(100); tone(12,698.45,100); delay(200); tone(12,698.45,100); delay(200); tone(12,698.45,100); delay(100); tone(12,783.99,100); delay(100); tone(12,659.25,100); delay(200); tone(12,659.25,100); delay(200); tone(12,587.33,100); delay(100); tone(12,523.25,100); delay(100); tone(12,587.33,100); delay(400); /////////////////////////////// } void loop() { if(encoderValue<0){ encoderValue=0; } ///////////////////////////////////////////////////PRIMER MENU if(encoderValue==0){ lcd.setCursor(0,0); lcd.print(">MOTOR ON "); lcd.setCursor(0,1); lcd.print(" MOTOR OFF "); lcd.setCursor(0,2); lcd.print(" VENTILADOR ON "); lcd.setCursor(0,3); lcd.print(" VENTILADOR OFF "); valor_boton=analogRead(boton) ; if(valor_boton==LOW){ digitalWrite(rele,HIGH); digitalWrite(led_diez,HIGH); digitalWrite(led_nueve,LOW); beepon(); } } //////////////////////////////////////////////////////////////////////FIN PRIMER MENU if(encoderValue==4){ lcd.setCursor(0,0); lcd.print(" MOTOR ON "); lcd.setCursor(0,1); lcd.print(">MOTOR OFF "); lcd.setCursor(0,2); lcd.print(" VENTILADOR ON "); lcd.setCursor(0,3); lcd.print(" VENTILADOR OFF "); valor_boton=analogRead(boton) ; if(valor_boton==LOW){ digitalWrite(rele,LOW); digitalWrite(led_nueve,HIGH); digitalWrite(led_diez,LOW); beepoff(); } } ////////////////////////////////////////////////////////////////////SEGUNDO MENU if(encoderValue==8){ lcd.setCursor(0,0); lcd.print(" MOTOR ON "); lcd.setCursor(0,1); lcd.print(" MOTOR OFF "); lcd.setCursor(0,2); lcd.print(">VENTILADOR ON "); lcd.setCursor(0,3); lcd.print(" VENTILADOR OFF "); valor_boton=analogRead(boton) ; if(valor_boton==LOW){ digitalWrite(rele_dos,HIGH); digitalWrite(led_ocho,LOW); digitalWrite(led_once,HIGH); beepon(); } } ////////////////////////////////////////////////////////////////////// if(encoderValue==12){ lcd.setCursor(0,0); lcd.print(" MOTOR ON "); lcd.setCursor(0,1); lcd.print(" MOTOR OFF "); lcd.setCursor(0,2); lcd.print(" VENTILADOR ON "); lcd.setCursor(0,3); lcd.print(">VENTILADOR OFF "); valor_boton=analogRead(boton) ; if(valor_boton==LOW){ digitalWrite(rele_dos,LOW); digitalWrite(led_ocho,HIGH); digitalWrite(led_once,LOW); beepoff(); } } ////////////////////////////////////////////////////////////////////////FIN SEGUNDO MENU if(encoderValue>12){ encoderValue=12; } } void updateEncoder(){ int MSB = digitalRead(encoderPin1); //MSB = most significant bit int LSB = digitalRead(encoderPin2); //LSB = least significant bit int encoded = (MSB << 1) |LSB; //converting the 2 pin value to single number int sum = (lastEncoded << 2) | encoded; //adding it to the previous encoded value if(sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011){ encoderValue ++;} if(sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000) {encoderValue --;} lastEncoded = encoded; //store this value for next time menus=encoderValue; delay(5); } void beepon(){ int i; for(i=0;i<350;i++){ digitalWrite(pinBuzzer,HIGH); delayMicroseconds(200); digitalWrite(pinBuzzer,LOW); delayMicroseconds(100); } } void beepoff(){ int i; for(i=0;i<350;i++){ digitalWrite(pinBuzzer,HIGH); delayMicroseconds(200); digitalWrite(pinBuzzer,LOW); delayMicroseconds(100); } for(i=0;i<350;i++){ digitalWrite(pinBuzzer,HIGH); delayMicroseconds(300); digitalWrite(pinBuzzer,LOW); delayMicroseconds(100); } }