Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // include the library code:
- #include <Wire.h>
- #include <ezButton.h>
- #include "Waveshare_LCD1602_RGB.h"
- // variable red, green, blue for LCD (can change)
- int r, g, b;
- // State of LCD
- // if isOn = true, it mean the LCD is ON
- // if isOn = false, it mean the LCD is OFF
- bool isOn = true;
- // define LCD from the library, 16 columns and 2 rows
- Waveshare_LCD1602_RGB lcd(16, 2);
- // create ezButton object that attach to pin
- ezButton button1(6);
- ezButton button2(7);
- ezButton button3(8);
- ezButton button4(5);
- ezButton button5(4);
- ezButton button6(3);
- // setup function
- void setup() {
- // set debounce time to 50 milliseconds,
- button1.setDebounceTime(50);
- button2.setDebounceTime(50);
- button3.setDebounceTime(50);
- button4.setDebounceTime(50);
- button5.setDebounceTime(50);
- button6.setDebounceTime(50);
- // initializes the display
- lcd.init();
- // call LCD welcomeScreen() function
- welcomeScreen();
- }
- void loop() {
- // MUST call the button loop() function first
- button1.loop();
- button2.loop();
- button3.loop();
- button4.loop();
- button5.loop();
- button6.loop();
- //-----button excute------
- // If the LCD is ON then it will work following program:
- if (isOn == true) {
- // If button 1 is press then it will call function happy()
- if (button1.isPressed()) {
- happy();
- delay(75);
- }
- // If button 2 is press then it will call function sad()
- else if (button2.isPressed()) {
- sad();
- delay(75);
- }
- // If button 3 is press then it will call function confused()
- else if (button3.isPressed()) {
- confused();
- delay(75);
- }
- // If button 4 is press then it will call function angry()
- else if (button4.isPressed()) {
- angry();
- delay(75);
- }
- // If button 5 is press then it will call function nervous()
- else if (button5.isPressed()) {
- nervous();
- delay(75);
- }
- // If button 6 is press then it will
- // turn off the LCD screen.
- else if (button6.isPressed()) {
- lcd.clear();
- lcd.setRGB(0, 0, 0);
- isOn = false;
- }
- }
- // If the LCD is OFF and user press
- // BUTTON 6 then it will turn ON the LCD
- // and call function welcomeScreen()
- else if (button6.isPressed()) {
- if (isOn == false) {
- lcd.clear();
- welcomeScreen();
- isOn = true;
- delay(200);
- }
- }
- }
- // create welcome screen function
- void welcomeScreen() {
- // change rgb for LCD text
- r = 255;
- g = 255;
- b = 255;
- // send text to LCD
- lcd.setRGB(r, g, b);
- lcd.setCursor(0, 0);
- lcd.send_string("Power On!");
- lcd.setCursor(0, 1);
- lcd.send_string("Emotions Present");
- }
- // ====== emoji function =======
- // create sad function
- void sad() {
- // change rgb for LCD text to blue
- r = 112;
- g = 150;
- b = 255;
- // send text to LCD
- lcd.clear();
- lcd.setRGB(r, g, b);
- lcd.setCursor(0, 1);
- lcd.send_string("I'm sad");
- lcd.setCursor(12, 0);
- lcd.send_string("T_T");
- }
- // create happy function
- void happy() {
- // change rgb for LCD text yellow
- r = 221;
- g = 255;
- b = 0;
- // send text to LCD
- lcd.clear();
- lcd.setCursor(0, 1);
- lcd.send_string("I'm happy");
- lcd.setCursor(12, 0);
- lcd.setRGB(r, g, b);
- lcd.send_string("^_^");
- }
- // create confused function
- void confused() {
- // change rgb for LCD text to green
- r = 0;
- g = 255;
- b = 0;
- //send text to LCD
- lcd.clear();
- lcd.setCursor(0, 1);
- lcd.send_string("I'm confused");
- lcd.setCursor(12, 0);
- lcd.setRGB(r, g, b);
- lcd.send_string("@_@");
- }
- // create nervous function
- void nervous() {
- // change rgb for LCD text to white
- r = 255;
- g = 255;
- b = 255;
- // send text to LCD
- lcd.clear();
- lcd.setCursor(0, 1);
- lcd.send_string("I'm nervous");
- lcd.setCursor(12, 0);
- lcd.setRGB(r, g, b);
- lcd.send_string(">_<");
- }
- // create angry function
- void angry() {
- // change rgb for LCD text to red
- r = 255;
- g = 0;
- b = 0;
- // create uint8_t array/list for custom character.
- // Each LCD block (there are 32 blocks total
- // from 2x16) have 40 pixels (8x4)
- /*
- NOTE* (how to create custom
- character for Waveshare library)
- To convert pixel to binary,
- if pixel is ON then binary = 1,
- if pixel is OFF then binary = 0,
- each row have 5 columns.
- so it is ##### with # is 1 or 0.
- Convert binary to Hex.
- [!] Hexadecimal, requiring the prefix 0x or 0X.
- */
- uint8_t angry1[] = {
- 0x04,
- 0x02,
- 0x03,
- 0x03,
- 0x00,
- 0x00,
- 0x01,
- 0x02
- };
- uint8_t angry2[] = {
- 0x00,
- 0x00,
- 0x00,
- 0x00,
- 0x00,
- 0x1F,
- 0x00,
- 0x00
- };
- uint8_t angry3[] = {
- 0x04,
- 0x08,
- 0x18,
- 0x18,
- 0x00,
- 0x00,
- 0x10,
- 0x08
- };
- // create custom characters, maximum is 7
- // characters(1-7, with Hex prefix)
- lcd.customSymbol(0x01, angry1);
- lcd.customSymbol(0x02, angry2);
- lcd.customSymbol(0x03, angry3);
- // send text to LCD
- lcd.setRGB(r, g, b);
- lcd.clear();
- lcd.setCursor(0, 1);
- lcd.send_string("I'm angry");
- lcd.setCursor(12, 0);
- lcd.write_char(0x01);
- lcd.write_char(0x02);
- lcd.write_char(0x03);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement