Advertisement
Eto2112

arduino project

Apr 25th, 2024 (edited)
763
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.26 KB | None | 0 0
  1. // include the library code:
  2. #include <Wire.h>
  3. #include <ezButton.h>
  4. #include "Waveshare_LCD1602_RGB.h"
  5.  
  6. // variable red, green, blue for LCD (can change)
  7. int r, g, b;
  8.  
  9.  
  10. // State of LCD
  11. // if isOn = true, it mean the LCD is ON
  12. // if isOn = false, it mean the LCD is OFF
  13. bool isOn = true;
  14.  
  15. // define LCD from the library, 16 columns and 2 rows
  16. Waveshare_LCD1602_RGB lcd(16, 2);
  17.  
  18. // create ezButton object that attach to pin
  19. ezButton button1(6);
  20. ezButton button2(7);
  21. ezButton button3(8);
  22. ezButton button4(5);
  23. ezButton button5(4);
  24. ezButton button6(3);
  25.  
  26. // setup function
  27. void setup() {
  28.  
  29.   // set debounce time to 50 milliseconds,
  30.   button1.setDebounceTime(50);
  31.   button2.setDebounceTime(50);
  32.   button3.setDebounceTime(50);
  33.   button4.setDebounceTime(50);
  34.   button5.setDebounceTime(50);
  35.   button6.setDebounceTime(50);
  36.  
  37.   // initializes the display
  38.   lcd.init();
  39.  
  40.   // call LCD welcomeScreen() function
  41.   welcomeScreen();
  42.  
  43. }
  44. void loop() {
  45.    
  46.   // MUST call the button loop() function first
  47.   button1.loop();
  48.   button2.loop();
  49.   button3.loop();
  50.   button4.loop();
  51.   button5.loop();
  52.   button6.loop();
  53.  
  54.   //-----button excute------
  55.  
  56.   // If the LCD is ON then it will work following program:
  57.   if (isOn == true) {
  58.      
  59.     // If button 1 is press then it will call function happy()
  60.     if (button1.isPressed()) {
  61.       happy();
  62.       delay(75);
  63.     }
  64.  
  65.     // If button 2 is press then it will call function sad()
  66.     else if (button2.isPressed()) {
  67.       sad();
  68.       delay(75);
  69.     }
  70.  
  71.     // If button 3 is press then it will call function confused()
  72.     else if (button3.isPressed()) {
  73.       confused();
  74.       delay(75);
  75.     }
  76.  
  77.     // If button 4 is press then it will call function angry()
  78.     else if (button4.isPressed()) {
  79.       angry();
  80.       delay(75);
  81.     }
  82.  
  83.     // If button 5 is press then it will call function nervous()
  84.     else if (button5.isPressed()) {
  85.       nervous();
  86.       delay(75);
  87.     }
  88.    
  89.     // If button 6 is press then it will
  90.     // turn off the LCD screen.
  91.     else if (button6.isPressed()) {
  92.       lcd.clear();
  93.       lcd.setRGB(0, 0, 0);
  94.       isOn = false;
  95.     }
  96.    
  97.   }
  98.  
  99.   // If the LCD is OFF and user press
  100.   // BUTTON 6 then it will turn ON the LCD
  101.   // and call function welcomeScreen()
  102.   else if (button6.isPressed()) {
  103.     if (isOn == false) {
  104.       lcd.clear();
  105.       welcomeScreen();
  106.       isOn = true;
  107.       delay(200);
  108.     }
  109.    
  110.   }
  111.  
  112. }
  113.  
  114. // create welcome screen function
  115. void welcomeScreen() {
  116.   // change rgb for LCD text
  117.   r = 255;
  118.   g = 255;
  119.   b = 255;
  120.  
  121.   // send text to LCD
  122.   lcd.setRGB(r, g, b);
  123.   lcd.setCursor(0, 0);
  124.   lcd.send_string("Power On!");
  125.   lcd.setCursor(0, 1);
  126.   lcd.send_string("Emotions Present");
  127. }
  128.  
  129. // ====== emoji function =======
  130.  
  131. // create sad function
  132. void sad() {
  133.   // change rgb for LCD text to blue
  134.   r = 112;
  135.   g = 150;
  136.   b = 255;
  137.  
  138.   // send text to LCD
  139.   lcd.clear();
  140.   lcd.setRGB(r, g, b);
  141.   lcd.setCursor(0, 1);
  142.   lcd.send_string("I'm sad");
  143.   lcd.setCursor(12, 0);
  144.   lcd.send_string("T_T");
  145. }
  146.  
  147. // create happy function
  148. void happy() {
  149.   // change rgb for LCD text yellow
  150.   r = 221;
  151.   g = 255;
  152.   b = 0;
  153.  
  154.   // send text to LCD
  155.   lcd.clear();
  156.   lcd.setCursor(0, 1);
  157.   lcd.send_string("I'm happy");
  158.   lcd.setCursor(12, 0);
  159.   lcd.setRGB(r, g, b);
  160.   lcd.send_string("^_^");
  161. }
  162.  
  163. // create confused function
  164. void confused() {
  165.   // change rgb for LCD text to green
  166.   r = 0;
  167.   g = 255;
  168.   b = 0;
  169.  
  170.   //send text to LCD
  171.   lcd.clear();
  172.   lcd.setCursor(0, 1);
  173.   lcd.send_string("I'm confused");
  174.   lcd.setCursor(12, 0);
  175.   lcd.setRGB(r, g, b);
  176.   lcd.send_string("@_@");
  177. }
  178.  
  179. // create nervous function
  180. void nervous() {
  181.   // change rgb for LCD text to white
  182.   r = 255;
  183.   g = 255;
  184.   b = 255;
  185.  
  186.   // send text to LCD
  187.   lcd.clear();
  188.   lcd.setCursor(0, 1);
  189.   lcd.send_string("I'm nervous");
  190.   lcd.setCursor(12, 0);
  191.   lcd.setRGB(r, g, b);
  192.   lcd.send_string(">_<");
  193. }
  194.  
  195. // create angry function
  196. void angry() {
  197.   // change rgb for LCD text to red
  198.   r = 255;
  199.   g = 0;
  200.   b = 0;
  201.  
  202.   // create uint8_t array/list for custom character.
  203.   // Each LCD block (there are 32 blocks total
  204.   // from 2x16) have 40 pixels (8x4)
  205.  
  206.   /*
  207.    NOTE* (how to create custom
  208.    character for Waveshare library)
  209.    
  210.    To convert pixel to binary,
  211.    if pixel is ON then binary = 1,
  212.    if pixel is OFF then binary = 0,
  213.    each row have 5 columns.
  214.    so it is ##### with # is 1 or 0.
  215.    Convert binary to Hex.
  216.    [!] Hexadecimal, requiring the prefix 0x or 0X.
  217.    */
  218.  
  219.   uint8_t angry1[] = {
  220.     0x04,
  221.     0x02,
  222.     0x03,
  223.     0x03,
  224.     0x00,
  225.     0x00,
  226.     0x01,
  227.     0x02
  228.   };
  229.   uint8_t angry2[] = {
  230.     0x00,
  231.     0x00,
  232.     0x00,
  233.     0x00,
  234.     0x00,
  235.     0x1F,
  236.     0x00,
  237.     0x00
  238.   };
  239.   uint8_t angry3[] = {
  240.     0x04,
  241.     0x08,
  242.     0x18,
  243.     0x18,
  244.     0x00,
  245.     0x00,
  246.     0x10,
  247.     0x08
  248.   };
  249.  
  250.   // create custom characters, maximum is 7
  251.   // characters(1-7, with Hex prefix)
  252.   lcd.customSymbol(0x01, angry1);
  253.   lcd.customSymbol(0x02, angry2);
  254.   lcd.customSymbol(0x03, angry3);
  255.  
  256.   // send text to LCD
  257.   lcd.setRGB(r, g, b);
  258.   lcd.clear();
  259.   lcd.setCursor(0, 1);
  260.   lcd.send_string("I'm angry");
  261.   lcd.setCursor(12, 0);
  262.   lcd.write_char(0x01);
  263.   lcd.write_char(0x02);
  264.   lcd.write_char(0x03);
  265. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement