Advertisement
hidromotic

EfectoBarraLILOControlado

May 28th, 2024
697
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.61 KB | Software | 0 0
  1. /*
  2.  * ITES - MICROCONTROLADORES II
  3.  * by Alejo S. Giles
  4.  *
  5.  * - BlinkAsimetrico
  6.  * - EfectoBarraLILO (6 LEDS)
  7.  * - Boton para incrementar (avanzar en el efecto)
  8.  * - Boton para decrementar (retrocecder en el efecto)
  9.  * - Antirrebote para los botones
  10.  * - Puerto Serie
  11.  * -- Comando para incrementar (+)
  12.  * -- Comando para decrementar (-)
  13.  * -- Comando para reiniciar el efecto (0)
  14.  */
  15.  
  16. #define PIN_LED1 2
  17. #define PIN_LED2 3
  18. #define PIN_LED3 4
  19. #define PIN_LED4 5
  20. #define PIN_LED5 6
  21. #define PIN_LED6  7
  22. #define CANT_LEDS 6
  23. const int PIN_LEDS[]={PIN_LED1,PIN_LED2,PIN_LED3,PIN_LED4,PIN_LED5,PIN_LED6};
  24.  
  25. #define CONFIG_SALIDA(x) pinMode(x,OUTPUT)
  26. #define AJUSTAR_LED(x,y) digitalWrite(PIN_LEDS[x],y)
  27.  
  28. //BLINK
  29. #define PIN_BLINK         13
  30. #define CONFIG_BLINK      pinMode(PIN_BLINK, OUTPUT)
  31. #define ENCENDER_BLINK    digitalWrite(PIN_BLINK, 1)
  32. #define APAGAR_BLINK      digitalWrite(PIN_BLINK, 0)
  33. #define AJUSTAR_BLINK(x)  digitalWrite(PIN_BLINK, x)
  34. #define MS_BLINK_ON 50
  35. #define MS_BLINK_OFF 2000
  36.  
  37. //BOTONES
  38. #define MS_ANTIREBOTE             100
  39. #define PIN_BTN_INC               8
  40. #define CONFIG_BTN_INC            pinMode(PIN_BTN_INC, INPUT)
  41. #define BTN_INCREMENTA_PRESIONADO digitalRead(PIN_BTN_INC)
  42.  
  43. #define PIN_BTN_DEC               9
  44. #define CONFIG_BTN_DEC            pinMode(PIN_BTN_DEC, INPUT)
  45. #define BTN_DECREMENTA_PRESIONADO digitalRead(PIN_BTN_DEC)
  46.  
  47. #define MS_ANTIRREBOTE 100
  48.  
  49. char accion=0;
  50.  
  51. void setup()
  52.   {
  53.   Serial.begin(9600);
  54.   Serial.print("EfectoBarraLILOControlado");
  55.   CONFIG_BTN_INC;
  56.   CONFIG_BTN_DEC;
  57.   CONFIG_BLINK;
  58.  
  59.   //LEDS DEL EFECTO
  60.   for(int i=0; i<CANT_LEDS; i++)  CONFIG_SALIDA(PIN_LEDS[i]);
  61.   }
  62.  
  63. void loop()
  64.   {
  65.   BlinkAsimetrico();
  66.   CtrlBotones();
  67.   RecepcionSerie();
  68.  
  69.   EfectoBarraLILO();  //Debe actuar de acuerdo a la variable accion
  70.   if(accion) accion=0;
  71.   }
  72.  
  73. void RecepcionSerie(void)
  74.   {
  75.   if(!Serial.available()) return;
  76.  
  77.   switch(Serial.read())
  78.     {
  79.     case '-': accion=-1;  break;
  80.     case '+': accion=1;   break;
  81.     }
  82.   }
  83.  
  84. void CtrlBotones(void)  
  85.   {
  86.   static unsigned long millis_ini=0;
  87.   static char btn_incrementa_presionado_ant=0;
  88.   static char btn_decrementa_presionado_ant=0;
  89.  
  90.   if(millis_ini>0)
  91.       {
  92.       if(millis() - millis_ini < MS_ANTIREBOTE) return;
  93.       millis_ini=0;
  94.       }
  95.  
  96.   if(BTN_INCREMENTA_PRESIONADO != btn_incrementa_presionado_ant)
  97.     {
  98.     btn_incrementa_presionado_ant=BTN_INCREMENTA_PRESIONADO;
  99.     if(BTN_INCREMENTA_PRESIONADO)
  100.       {
  101.       millis_ini= millis(); //Preparar para esperar MS_ANTIREBOTE
  102.       accion=1;
  103.       }
  104.     }
  105.  
  106.   if(BTN_DECREMENTA_PRESIONADO != btn_decrementa_presionado_ant)
  107.     {
  108.     btn_decrementa_presionado_ant=BTN_DECREMENTA_PRESIONADO;
  109.     if(BTN_DECREMENTA_PRESIONADO)
  110.       {
  111.       millis_ini= millis(); //Preparar para esperar MS_ANTIREBOTE
  112.       accion=-1;
  113.       }
  114.     }
  115.   }
  116.  
  117. void EfectoBarraLILO(void) //1=avanzar / 0=no hacer nada / -1=retroceder
  118.   {
  119.   static unsigned int paso=2*CANT_LEDS-1;
  120.  
  121.   if(accion==0)  return;
  122.  
  123.   if(accion>0)
  124.     {
  125.     paso++;
  126.     if(paso>=2*CANT_LEDS) paso=0;
  127.     }
  128.  
  129.   if(paso<CANT_LEDS)  AJUSTAR_LED(paso, accion>0);
  130.   else                AJUSTAR_LED(paso-CANT_LEDS, !(accion>0));
  131.  
  132.   if(accion<0)
  133.     {
  134.     if(paso>0) paso--;
  135.     else paso=2*CANT_LEDS-1;
  136.     }
  137.   }
  138.  
  139. void BlinkAsimetrico(void)
  140.   {
  141.   static unsigned long millis_ant=0;
  142.   static bool estado=0;
  143.   static unsigned int espera=0;
  144.  
  145.   if(millis() - millis_ant < espera) return;
  146.   millis_ant=millis();
  147.  
  148.   estado=!estado;
  149.   AJUSTAR_BLINK(estado);
  150.  
  151.   espera= estado ? MS_BLINK_ON : MS_BLINK_OFF;  //Ajustar el tiempo de espera
  152.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement