I2C
https://github.com/fdebrabander/Arduino-LiquidCrystal-I2C-library es pot instal·lar directament des delgestor de llibreries
https://magnusglad.wordpress.com/2013/03/16/arduino-lcd/
2 €
/* aug13e * Control Led RGB amb teclat matricial I2C i display * 16x2 I2C amb adaptador de nivells I2C * Per a ESP8266 NodeMcu 1.0 */#include <Keypad_I2C.h>#include <Keypad.h>#include <Wire.h>#define I2CADDR 0x38#include <LiquidCrystal_I2C.h>LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address
// to 0x27 for a 16 chars and 2 line display
const int ledR = D8;const int ledG = D7;const int ledB = D6;int r,g,b;const byte ROWS = 4; //four rowsconst byte COLS = 4; //four columns
char keys[ROWS][COLS] = {
{'1', '2', '3', 'S'},
{'4', '5', '6', 'G'},
{'7', '8', '9', 'L'},
{'*', '0', '#', 'P'}
};
// Digitran keypad, bit numbers of PCF8574 i/o port
byte rowPins[ROWS] = {0, 1, 2, 3}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {7, 6, 5, 4}; //connect to the column pinouts of the keypad
Keypad_I2C kpd( makeKeymap(keys), rowPins, colPins, ROWS, COLS, I2CADDR, PCF8574 );
void setup() { r=255; g=255; b=255; color(r,g,b); Wire.begin( ); kpd.begin( makeKeymap(keys) );
lcd.init(); // initialize the lcd
lcd.init();
// Print a message to the LCD.
lcd.backlight();
lcd.setCursor(1,0);
lcd.print("R");
lcd.setCursor(6,0);
lcd.print("G");
lcd.setCursor(11,0);
lcd.print("B");
pantalla (r,g,b);
}void loop() { char key = kpd.getKey(); if (key) { switch(key){ case '1':
r++;
if (r>255) r=255;
break;
case '4':
r--;
if (r<0) r=0;
break;
case '2': g++; if (g>255) g=255; break; case '5': g--; if (g<0) g=0; break;
case '3':
b++;
if (b>255) b=255;
break;
case '6': b--; if (b<0) b=0; break; case 'S': r=0; g=0; b=0; break; case 'G':
r=255; g=255; b=255;
break;
case '7':
r=255; g=0; b=0;
break;
case '8':
r=255; g=50; b=0;
break;
case '9':
r=255; g=100; b=0;
break;
case 'L':
r=0; g=255; b=0;
break;
case '*':
r=0; g=0; b=255;
break;
case '0':
r=100; g=0; b=255;
break;
case '#':
r=255; g=0; b=255;
break;
case 'P':
r=0; g=255; b=255;
break;
}
color (r,g,b);
pantalla (r,g,b);
}
}
void color(int r,int g,int b) {
float lr, lg, lb;
lr=r*180/255;
lg=g*180/255;
lb=b*180/255;
analogWrite (ledR,lr);
analogWrite (ledG,lg);
analogWrite (ledB,lb);
}
void pantalla(int r, int g,int b) {
lcd.setCursor(1,1);
lcd.print(" ");
lcd.setCursor(6,1);
lcd.print(" ");
lcd.setCursor(11,1);
lcd.print(" ");
lcd.setCursor(1,1);
lcd.print(r);
lcd.setCursor(6,1);
lcd.print(g);
lcd.setCursor(11,1);
lcd.print(b);
}