skizziks_53

Reddit LCD custom chars v2.0

Oct 11th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.64 KB | None | 0 0
  1. /*
  2. October 11, 2019
  3. This is a demonstration of how to make custom characters for a 16x2 LCD on an Arduino (Uno).
  4.  
  5. */
  6. #include <LiquidCrystal.h>
  7. int Vpin = 0;
  8. float voltaje;
  9. float V;
  10. LiquidCrystal lcd(7, 6, 5, 4, 3, 2);  // pines RS, E, D4, D5, D6, D7 de modulo 1602A
  11. //custom character https://lastminuteengineers.com/arduino-1602-character-lcd-tutorial/
  12. /*
  13.   byte DIV_0_OF_5[8] = {
  14.   B00000,
  15.   B00000,
  16.   B00000,
  17.   B00000,
  18.   B00000,
  19.   B00000,
  20.   B00000,
  21.   B00000
  22.   }; // 0 / 5
  23.  
  24.   byte DIV_1_OF_5[8] = {
  25.   B10000,
  26.   B10000,
  27.   B10000,
  28.   B10000,
  29.   B10000,
  30.   B10000,
  31.   B10000,
  32.   B10000
  33.   }; // 1 / 5
  34.  
  35.   byte DIV_2_OF_5[8] = {
  36.   B11000,
  37.   B11000,
  38.   B11000,
  39.   B11000,
  40.   B11000,
  41.   B11000,
  42.   B11000,
  43.   B11000
  44.   }; // 2 / 5
  45.  
  46.   byte DIV_3_OF_5[8] = {
  47.   B11100,
  48.   B11100,
  49.   B11100,
  50.   B11100,
  51.   B11100,
  52.   B11100,
  53.   B11100,
  54.   B11100
  55.   }; // 3 / 5
  56.  
  57.   byte DIV_4_OF_5[8] = {
  58.   B11110,
  59.   B11110,
  60.   B11110,
  61.   B11110,
  62.   B11110,
  63.   B11110,
  64.   B11110,
  65.   B11110
  66.   }; // 4 / 5
  67.  
  68.   byte DIV_5_OF_5[8] = {
  69.   B11111,
  70.   B11111,
  71.   B11111,
  72.   B11111,
  73.   B11111,
  74.   B11111,
  75.   B11111,
  76.   B11111
  77.   }; // 5 / 5
  78. */
  79.  
  80. // The Arduino pages say to prefix a byte value declaration with capital B,
  81. // but many other places use 0b (zero & b) instead?
  82. // The capital B seems to be an Arduino-specific instruction.
  83. // The sketches I have used (that worked) always used 0b ...
  84. byte DIV_0_OF_5[8] = {
  85.   0b00000,
  86.   0b00000,
  87.   0b00000,
  88.   0b00000,
  89.   0b00000,
  90.   0b00000,
  91.   0b00000,
  92.   0b00000
  93. }; // 0 / 5
  94.  
  95. byte DIV_1_OF_5[8] = {
  96.   0b10000,
  97.   0b10000,
  98.   0b10000,
  99.   0b10000,
  100.   0b10000,
  101.   0b10000,
  102.   0b10000,
  103.   0b10000
  104. }; // 1 / 5
  105.  
  106. byte DIV_2_OF_5[8] = {
  107.   0b11000,
  108.   0b11000,
  109.   0b11000,
  110.   0b11000,
  111.   0b11000,
  112.   0b11000,
  113.   0b11000,
  114.   0b11000
  115. }; // 2 / 5
  116.  
  117. byte DIV_3_OF_5[8] = {
  118.   0b11100,
  119.   0b11100,
  120.   0b11100,
  121.   0b11100,
  122.   0b11100,
  123.   0b11100,
  124.   0b11100,
  125.   0b11100
  126. }; // 3 / 5
  127.  
  128. byte DIV_4_OF_5[8] = {
  129.   0b11110,
  130.   0b11110,
  131.   0b11110,
  132.   0b11110,
  133.   0b11110,
  134.   0b11110,
  135.   0b11110,
  136.   0b11110
  137. }; // 4 / 5
  138.  
  139. byte DIV_5_OF_5[8] = {
  140.   0b11111,
  141.   0b11111,
  142.   0b11111,
  143.   0b11111,
  144.   0b11111,
  145.   0b11111,
  146.   0b11111,
  147.   0b11111
  148. }; // 5 / 5
  149.  
  150.  
  151.  
  152.  
  153. /**
  154.    Fonction de configuration de l'écran LCD pour la barre de progression.
  155.    Utilise les caractéres personnalisés de 0 à 5 (6 et 7 restent disponibles).
  156. */
  157.  
  158. // You can use the function below, but it must be called in setup() after the lcd.begin() call.
  159. void setup_progressbar() {
  160.  
  161.   /* Enregistre les caractères personnalisés dans la mémoire de l'écran LCD */
  162.   lcd.createChar(0, DIV_0_OF_5);
  163.   lcd.createChar(1, DIV_1_OF_5);
  164.   lcd.createChar(2, DIV_2_OF_5);
  165.   lcd.createChar(3, DIV_3_OF_5);
  166.   lcd.createChar(4, DIV_4_OF_5);
  167.   lcd.createChar(5, DIV_5_OF_5);
  168. }
  169.  
  170.  
  171. void setup() {
  172.   lcd.begin(16, 2);     // inicializa a display de 16 columnas y 2 lineas
  173.   lcd.clear(); // to clear any possible random chars from starting up
  174.   setup_progressbar();
  175. }
  176.  
  177. void loop() {
  178.  
  179.   lcd.setCursor(0, 0);
  180.   lcd.write(byte(0)); // This line is how to write one of the custom characters.
  181.   lcd.write(byte(1));
  182.   lcd.write(byte(2));
  183.   lcd.write(byte(3));
  184.   lcd.write(byte(4));
  185.   lcd.write(byte(5));
  186.  
  187.   lcd.setCursor(0, 1);
  188.   lcd.write(byte(5));
  189.   lcd.write(byte(4));
  190.   lcd.write(byte(3));
  191.   lcd.write(byte(2));
  192.   lcd.write(byte(1));
  193.   lcd.write(byte(0));
  194.  
  195.   delay(3000);
  196.  
  197.   lcd.clear();
  198.  
  199.   delay(1000);
  200.  
  201.   // Below is commented out just for testing the custom chars.
  202.   /*
  203.     voltaje = analogRead (Vpin);
  204.     V = voltaje / 1024 * 5.0;
  205.     lcd.print("SondaV= ");
  206.     lcd.print(V);
  207.     delay(500);
  208.     lcd.clear();
  209.   */
  210. }
  211.  
  212.  
  213.  
  214.  
  215. // ~~~~~~~~ the end ~~~~~~~~~
Add Comment
Please, Sign In to add comment