Dimmer per strisce led con Arduino
Codice per la versione senza display:
/**************************************
* Dimmer per strisce led *
* *
* Esperienze Elettroniche 2021 *
**************************************/
#define pwmPin 3
#define adcPin A0
int adcValue = 0;
int pwmValue = 0;
void setup() {
analogWrite(pwmPin, 0);
}
void loop() {
adcValue = analogRead(adcPin);
pwmValue = map(adcValue, 0, 1024, 0, 255);
analogWrite(pwmPin, pwmValue);
delay(50);
}
Codice per la versione con display (occorre installare la libreria LiquidCrystal_I2C):
/**************************************
* Dimmer per strisce led *
* *
* Esperienze Elettroniche 2021 *
**************************************/
#include <LiquidCrystal_I2C.h>
#define adcPin A0
#define pwmPin 3
// Change the address according with the device in use
LiquidCrystal_I2C lcd(0x3F, 16, 2);
// Custom haracters definition (rectangles used for progress bar)
uint8_t full[8] =
{
0b11111,
0b11111,
0b11111,
0b11111,
0b11111,
0b11111,
0b11111,
0b11111,
};
uint8_t empty[8] =
{
0b11111,
0b10001,
0b10001,
0b10001,
0b10001,
0b10001,
0b10001,
0b11111,
};
int adcValue = 0;
int pwmValue = 0;
void setup() {
// Set mosfet control pin as output
analogWrite(pwmPin, 0);
// Initialize display
lcd.backlight();
lcd.init();
lcd.print(" PWM Led Dimmer");
// Create custom chars
lcd.createChar(0, full);
lcd.createChar(1, empty);
delay(3000);
lcd.clear();
lcd.setCursor(3, 0);
lcd.print("LUMINOSITA'");
}
void loop() {
// Read analog value from potentiometer
adcValue = analogRead(adcPin);
// Truncate value to 8 bit
pwmValue = map(adcValue, 0, 1023, 0, 255);
// Set pwm output
analogWrite(pwmPin, pwmValue);
// Display pwm value
printBar(pwmValue);
delay(50);
}
void printBar(int value){
int i=0, j=16;
lcd.setCursor(0, 1);
unsigned char b=(value+1)/16;
while(b > i){
lcd.write(0);
i++;
j--;
}
while(j > 0){
lcd.write(1);
j--;
}
}
Commenti
Posta un commento