// EMULA COMMUTTATHORE A 4 POSIZIONI
// 2024.02.28
//lo stato contiene il numero del pin da accendere
//il pin 9 non è collegato e corrisponde al caso in cui tutti
//i led sono spenti
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define tasto 2
bool first=true;
int stato = 9;
void setup()
{
lcd.init();
lcd.backlight();
Serial.begin(9600);
for (int i=10; i<14; i++) {
pinMode (i, OUTPUT);
}
//pinMode(10, OUTPUT);
//pinMode(11, OUTPUT);
//pinMode(12, OUTPUT);
//pinMode(13, OUTPUT);
pinMode(tasto, INPUT);
lcd.setCursor(0, 0);
lcd.print(" DEMO");
lcd.setCursor(0, 1);
lcd.print("COMMUTATORE 4pos");
delay(2000);
lcd.clear();
}
void loop()
{
switch (stato) {
case 9:
// POSIZIONE 0
comm_pos0();
break;
case 10:
// POSIZIONE 1
comm_pos1();
break;
case 11:
// POSIZIONE 2
comm_pos2();
break;
case 12:
// POSIZIONE 3
comm_pos3();
break;
case 13:
// POSIZIONE4
comm_pos4();
break;
}
if (digitalRead(tasto) == HIGH)
{
stato++;
first=true;
if (stato > 13)
{
stato = 9;
}
delay(200);
}
//spengo tutti i led
for (int i=10; i<14; i++) {
digitalWrite (i, LOW);
}
//digitalWrite(10, LOW);
//digitalWrite(11, LOW);
//digitalWrite(12, LOW);
//digitalWrite(13, LOW);
//accendo il led selezionato
digitalWrite(stato, HIGH);
}
void comm_pos0(void) {
if (first) {
first=false;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("commuttatore a 0");
}
}
void comm_pos1(void) {
if (first) {
first=false;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("commuttatore a 1");
}
}
void comm_pos2(void) {
if (first) {
first=false;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("commuttatore a 2");
}
}
void comm_pos3(void) {
if (first) {
first=false;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("commuttatore a 3");
}
}
void comm_pos4(void) {
if (first) {
first=false;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("commuttatore a 4");
}
}