Guest User

Arduino

a guest
Nov 11th, 2013
1,758
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. /*
  2. Voltmeter with graph
  3. Coded by Ollie Jeffcoate
  4. */
  5. #include <LiquidCrystal.h>
  6.  
  7. LiquidCrystal lcd(7, 8, 9, 10, 11, 12); //initialize the library, numbers of the interface pins
  8.  
  9. int analogInput = 1; //analog pin A1
  10. float VoltageOut = 0.0;
  11. float VoltageIn = 0.0;
  12. int refresh = 50; //refresh rate
  13. int i = 0; //variable used for clearing our byte 'graph'
  14. int value = 0; //variable to store value
  15. byte graph[8] =
  16. {
  17. B11111,
  18. B11111,
  19. B11111,
  20. B11111,
  21. B11111,
  22. B11111,
  23. B11111,
  24. B11111,
  25. };
  26.  
  27. void setup()
  28. {
  29. pinMode(analogInput, INPUT); //setting analog pin mode to input
  30.  
  31. lcd.begin(16, 2); //setting up the LCD screens rows and colums
  32. lcd.print("Voltage=");
  33. }
  34.  
  35. void loop(){
  36. value = analogRead(analogInput); //reading the value on A1
  37.  
  38. //VoltageOut = (value * 5.0) / 1024.0;
  39. VoltageIn = (value * 5.0) / 1024.0;
  40. //VoltageOut / (R2/(R1+R2));
  41.  
  42. lcd.setCursor(8, 0); //printing the result to the LCD display
  43. lcd.print(VoltageIn);
  44. lcd.print(" V");
  45. delay(refresh); //refreshing the screen
  46.  
  47. voltmetergraph(); //calling my function voltmetergraph
  48. }
  49.  
  50. void voltmetergraph()
  51. {
  52. if (VoltageIn >= 0.3125) //setting up the bar graph
  53. {
  54. lcd.createChar(0, graph);
  55. lcd.setCursor(0, 2);
  56. lcd.write(byte(0));}
  57. else {LcdClearByte(0);}
  58.  
  59. if (VoltageIn >= 0.625)
  60. {
  61. lcd.createChar(0, graph);
  62. lcd.setCursor(1, 2);
  63. lcd.write(byte(0));}
  64. else {LcdClearByte(1);}
  65.  
  66. if (VoltageIn >= .9375)
  67. {
  68. lcd.createChar(0, graph);
  69. lcd.setCursor(2, 2);
  70. lcd.write(byte(0));}
  71. else {LcdClearByte(2);}
  72.  
  73. if (VoltageIn >= 1.25){
  74. lcd.createChar(0, graph);
  75. lcd.setCursor(3, 2);
  76. lcd.write(byte(0));}
  77. else {LcdClearByte(3);}
  78.  
  79. if (VoltageIn >= 1.5625)
  80. {
  81. lcd.createChar(0, graph);
  82. lcd.setCursor(4, 2);
  83. lcd.write(byte(0));}
  84. else {LcdClearByte(4);}
  85.  
  86. if (VoltageIn >= 1.875)
  87. {
  88. lcd.createChar(0, graph);
  89. lcd.setCursor(5, 2);
  90. lcd.write(byte(0));}
  91. else {LcdClearByte(5);}
  92.  
  93. if (VoltageIn >= 2.1875)
  94. {
  95. lcd.createChar(0, graph);
  96. lcd.setCursor(6, 2);
  97. lcd.write(byte(0));}
  98. else {LcdClearByte(6);}
  99.  
  100. if (VoltageIn >= 2.5)
  101. {
  102. lcd.createChar(0, graph);
  103. lcd.setCursor(7, 2);
  104. lcd.write(byte(0));}
  105. else {LcdClearByte(7);}
  106.  
  107. if (VoltageIn >= 2.8125)
  108. {
  109. lcd.createChar(0, graph);
  110. lcd.setCursor(8, 2);
  111. lcd.write(byte(0));}
  112. else {LcdClearByte(8);}
  113.  
  114. if (VoltageIn >= 3.125)
  115. {
  116. lcd.createChar(0, graph);
  117. lcd.setCursor(9, 2);
  118. lcd.write(byte(0));}
  119. else {LcdClearByte(9);}
  120.  
  121. if (VoltageIn >= 3.4375)
  122. {
  123. lcd.createChar(0, graph);
  124. lcd.setCursor(10, 2);
  125. lcd.write(byte(0));}
  126. else {LcdClearByte(10);}
  127.  
  128. if (VoltageIn >= 3.75)
  129. {
  130. lcd.createChar(0, graph);
  131. lcd.setCursor(11, 2);
  132. lcd.write(byte(0));}
  133. else {LcdClearByte(11);}
  134.  
  135. if (VoltageIn >= 4.0625)
  136. {
  137. lcd.createChar(0, graph);
  138. lcd.setCursor(12, 2);
  139. lcd.write(byte(0));}
  140. else {LcdClearByte(12);}
  141.  
  142. if (VoltageIn >= 4.375)
  143. {
  144. lcd.createChar(0, graph);
  145. lcd.setCursor(13, 2);
  146. lcd.write(byte(0));}
  147. else {LcdClearByte(13);}
  148.  
  149. if (VoltageIn >= 4.6875)
  150. {
  151. lcd.createChar(0, graph);
  152. lcd.setCursor(14, 2);
  153. lcd.write(byte(0));}
  154. else {LcdClearByte(14);}
  155.  
  156. if (VoltageIn >= 4.99)
  157. {
  158. lcd.createChar(0, graph);
  159. lcd.setCursor(15, 2);
  160. lcd.write(byte(0));}
  161. else {LcdClearByte(15);}
  162. }
  163.  
  164. void LcdClearByte(int c) //clearbyte function
  165. {
  166. lcd.setCursor(c,1);
  167. for (i = 0; i < 16; i = i + 1) {
  168. lcd.print(" ");
  169. }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment