Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //CC-0 - Dominio público - Martín Sebastián Wain
- //Configuracion de cantidad de salidas
- #define NUM_IO 2
- struct InfoIO {
- byte pinLed;
- byte pinPote;
- long proxPaso;
- long periodoPaso;
- char incremento;
- byte valor;
- InfoIO() {
- valor = 0;
- proxPaso = 0;
- periodoPaso = 0;
- incremento = 1;
- }
- };
- InfoIO inOut[NUM_IO];
- void setup() {
- inOut[0].pinLed =...
- inOut[0].pinPote =...
- inOut[1].pinLed =...
- inOut[1].pinPote =...
- }
- void loop() {
- long ahora = micros();
- for(int i = 0; i < NUM_OUTPUTS; i++) {
- InfoIO &io = inOut[i];
- int valPote = analogRead(io.pinPote);
- //Seteamos el periodo y lo dividimos por 256 por los pasos para todo el gradiente PWM
- io.periodoPaso = {{acá iria el mapeo del pote al periodo q quieras en microsegundos}} / 256;
- //Si el tiempo actual superó el del "io", cambiar estado
- if(io.proxPaso < ahora) {
- io.proxPaso += io.periodoPaso; //Ajustamos para el próximo paso
- //Rebotes si llegamos al límite
- if(io.valor == 255 && io.incremento>0 || io.valor == 0 && io.incremento<0)
- io.incremento *= -1;
- //Actualizamos el valor y lo mandamos por PWM
- io.valor += io.incremento;
- analogWrite(io.pinLed, io.valor);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement