skizziks_53

Reddit LCD bar graph voltmeter 3.0

Oct 11th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.92 KB | None | 0 0
  1. /*
  2.   Reddit bargraph LCD voltmeter - version 3.0 - 11 October 2019
  3.  
  4.   This should print the volts value on line 1.
  5.   The second line should be a left-to-right bar graph between two left and right pointers:
  6.   >>###########<<
  7.  
  8. */
  9.  
  10.  
  11. #include <LiquidCrystal.h>
  12. int Vpin = 0;
  13. float voltaje;
  14. float V;
  15. LiquidCrystal lcd(7, 6, 5, 4, 3, 2);  // pines RS, E, D4, D5, D6, D7 de modulo 1602A
  16.  
  17.  
  18.  
  19. byte DIV_0_OF_5[8] = { // This is really the same as printing a space [ ]
  20.   0b00000,
  21.   0b00000,
  22.   0b00000,
  23.   0b00000,
  24.   0b00000,
  25.   0b00000,
  26.   0b00000,
  27.   0b00000
  28. }; // 0 / 5
  29.  
  30. byte DIV_1_OF_5[8] = {
  31.   0b10000,
  32.   0b10000,
  33.   0b10000,
  34.   0b10000,
  35.   0b10000,
  36.   0b10000,
  37.   0b10000,
  38.   0b10000
  39. }; // 1 / 5
  40.  
  41. byte DIV_2_OF_5[8] = {
  42.   0b11000,
  43.   0b11000,
  44.   0b11000,
  45.   0b11000,
  46.   0b11000,
  47.   0b11000,
  48.   0b11000,
  49.   0b11000
  50. }; // 2 / 5
  51.  
  52. byte DIV_3_OF_5[8] = {
  53.   0b11100,
  54.   0b11100,
  55.   0b11100,
  56.   0b11100,
  57.   0b11100,
  58.   0b11100,
  59.   0b11100,
  60.   0b11100
  61. }; // 3 / 5
  62.  
  63. byte DIV_4_OF_5[8] = {
  64.   0b11110,
  65.   0b11110,
  66.   0b11110,
  67.   0b11110,
  68.   0b11110,
  69.   0b11110,
  70.   0b11110,
  71.   0b11110
  72. }; // 4 / 5
  73.  
  74. byte DIV_5_OF_5[8] = {
  75.   0b11111,
  76.   0b11111,
  77.   0b11111,
  78.   0b11111,
  79.   0b11111,
  80.   0b11111,
  81.   0b11111,
  82.   0b11111
  83. }; // 5 / 5
  84.  
  85. byte right_pointer[8] = {
  86.   0b11000,
  87.   0b01100,
  88.   0b00110,
  89.   0b00011,
  90.   0b00011,
  91.   0b00110,
  92.   0b01100,
  93.   0b11000
  94. }; // A character like this >>
  95.  
  96. byte left_pointer[8] = {
  97.   0b00011,
  98.   0b00110,
  99.   0b01100,
  100.   0b11000,
  101.   0b11000,
  102.   0b01100,
  103.   0b00110,
  104.   0b00011
  105. }; // A character like this <<
  106.  
  107.  
  108.  
  109. /**
  110.    Fonction de configuration de l'écran LCD pour la barre de progression.
  111.    Utilise les caractéres personnalisés de 0 à 5 (6 et 7 restent disponibles).
  112. */
  113.  
  114. // You can use the function below, but it must be called in setup() after the lcd.begin() call.
  115. void setup_progressbar() {
  116.  
  117.   /* Enregistre les caractères personnalisés dans la mémoire de l'écran LCD */
  118.   lcd.createChar(0, DIV_0_OF_5);
  119.   lcd.createChar(1, DIV_1_OF_5);
  120.   lcd.createChar(2, DIV_2_OF_5);
  121.   lcd.createChar(3, DIV_3_OF_5);
  122.   lcd.createChar(4, DIV_4_OF_5);
  123.   lcd.createChar(5, DIV_5_OF_5);
  124.   lcd.createChar(6, right_pointer);
  125.   lcd.createChar(7, left_pointer);
  126. }
  127.  
  128.  
  129. void setup() {
  130.   lcd.begin(16, 2);     // inicializa a display de 16 columnas y 2 lineas
  131.   lcd.clear(); // to clear any possible random chars from starting up
  132.   setup_progressbar();
  133.  
  134.   // Writing to an LCD takes time, so you want to avoid re-writing characters that will not change.
  135.  
  136.   // The characters below will not change, so they don't need to be re-written.
  137.   lcd.setCursor(0, 0);
  138.   lcd.print("SondaV= "); // The start position for V value is (8,0)
  139.   lcd.setCursor(0, 1); // This should be the left-most character on line 2.
  140.   lcd.write(byte(6));
  141.   lcd.setCursor(13, 1); // This should be the char at the right end of the bar code range.
  142.   lcd.write(byte(7));
  143. }
  144.  
  145. void loop() {
  146.  
  147.   voltaje = analogRead (Vpin);
  148.   V = voltaje / 1024 * 5.0;
  149.   lcd.setCursor(8, 0);
  150.   lcd.print("       ");
  151.   lcd.setCursor(8, 0);
  152.   lcd.print(V);
  153.   display_bargraph1();
  154.   delay(500);
  155.  
  156. }
  157.  
  158.  
  159. void display_bargraph1() {
  160.   lcd.setCursor(1, 1);
  161.   // 1.1 voltage, .2 char divisions, displayed over 11 chars,,,,
  162.   float base_voltage = 0;
  163.   for (int i = 1; i <= 11; i++) {
  164.     if (V >= (base_voltage + 1)) {
  165.       lcd.write(byte(5));
  166.     }
  167.     else if (V >= (base_voltage + .08)) {
  168.       lcd.write(byte(4));
  169.     }
  170.     else if (V >= (base_voltage + .06)) {
  171.       lcd.write(byte(3));
  172.     }
  173.     else if (V >= (base_voltage + .04)) {
  174.       lcd.write(byte(2));
  175.     }
  176.     else if (V >= (base_voltage + .02)) {
  177.       lcd.write(byte(1));
  178.     }
  179.     else {
  180.       lcd.write(" "); // This just writes a regular space.
  181.       // Or, you could use custom character (zero) below--
  182.       //lcd.write(byte(0)); // ------------------> but this is the same as printing a space.
  183.     }
  184.     base_voltage = base_voltage + .1;
  185.   }
  186. }
  187.  
  188.  
  189.  
  190.  
  191. // ~~~~~~~~ the end ~~~~~~~~~
Add Comment
Please, Sign In to add comment