Advertisement
2bam

Arduino: LFO Triangular PWM multiple

Mar 30th, 2015
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.22 KB | None | 0 0
  1. //CC-0 - Dominio público - Martín Sebastián Wain
  2.  
  3.  
  4. //Configuracion de cantidad de salidas
  5. #define NUM_IO          2
  6.  
  7. struct InfoIO {
  8.     byte pinLed;
  9.     byte pinPote;
  10.     long proxPaso;
  11.     long periodoPaso;
  12.     char incremento;
  13.     byte valor;
  14.  
  15.     InfoIO() {
  16.         valor = 0;
  17.         proxPaso = 0;
  18.         periodoPaso = 0;
  19.         incremento = 1;
  20.     }
  21. };
  22.  
  23. InfoIO inOut[NUM_IO];
  24.  
  25. void setup() {
  26.     inOut[0].pinLed =...
  27.     inOut[0].pinPote =...
  28.     inOut[1].pinLed =...
  29.     inOut[1].pinPote =...
  30. }
  31.  
  32.  
  33. void loop() {
  34.     long ahora = micros(); 
  35.     for(int i = 0; i < NUM_OUTPUTS; i++) {
  36.         InfoIO &io = inOut[i];
  37.  
  38.         int valPote = analogRead(io.pinPote);
  39.         //Seteamos el periodo y lo dividimos por 256 por los pasos para todo el gradiente PWM
  40.         io.periodoPaso = {{acá iria el mapeo del pote al periodo q quieras en microsegundos}} / 256;
  41.  
  42.         //Si el tiempo actual superó el del "io", cambiar estado
  43.         if(io.proxPaso < ahora) {
  44.             io.proxPaso += io.periodoPaso;      //Ajustamos para el próximo paso
  45.  
  46.             //Rebotes si llegamos al límite
  47.             if(io.valor == 255 && io.incremento>0 || io.valor == 0 && io.incremento<0)
  48.                 io.incremento *= -1;
  49.  
  50.             //Actualizamos el valor y lo mandamos por PWM
  51.             io.valor += io.incremento;
  52.             analogWrite(io.pinLed, io.valor);
  53.         }
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement