Guest User

Untitled

a guest
Feb 24th, 2018
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.38 KB | None | 0 0
  1. #include <Q2HX711.h>
  2. #include <LiquidCrystal_I2C.h>
  3.  
  4. const int resolution=1000;
  5. const byte hx711_data_pin = A3;
  6. const byte hx711_clock_pin = A2;
  7. const int MaxChars=15;
  8.  
  9. String commandChars="~CWL";
  10. char strValue[MaxChars+1];
  11. char serialInputType=' ';
  12. float itemWeight=0.00;
  13. int index=0;
  14. long total=0;
  15. long average = 0;
  16. long tareWeight=0;
  17. String LCDLine1,LCDLine2;
  18. String itemLabel;
  19. bool doTare=false;
  20. bool doCount=false;
  21.  
  22. Q2HX711 hx711(hx711_data_pin, hx711_clock_pin);
  23. LiquidCrystal_I2C lcd(0x3F,16,2);
  24.  
  25. void updateLCD () {
  26. lcd.clear();
  27. lcd.setCursor(0,0);
  28. lcd.print(LCDLine1);
  29. Serial.print("A");
  30. Serial.println(LCDLine1);
  31.  
  32. lcd.setCursor(0,1);
  33. lcd.print(LCDLine2);
  34. Serial.print("B");
  35. Serial.println(LCDLine2);
  36. }
  37.  
  38. long readingAverage(int samples=25,long t=0) {
  39. total=0;
  40.  
  41. for (int i=0; i<samples;i++) {
  42. total=total+((hx711.read()/resolution)-t);
  43. delay(10);
  44. }
  45. return (total / samples);
  46. }
  47.  
  48. void _doTare() {
  49. doTare=true;
  50. }
  51.  
  52. void setTare() {
  53. doTare=false;
  54. LCDLine1="TARE ...";
  55. LCDLine2="";
  56. updateLCD();
  57. digitalWrite(13, !digitalRead(13)); // Toggle LED on pin 13
  58. tareWeight=readingAverage(25,0);
  59. digitalWrite(13, !digitalRead(13)); // Toggle LED on pin 13
  60. }
  61.  
  62. void _doCount() {
  63. doCount=true;
  64. }
  65. void getItemWeight(float c=25.00)
  66. {
  67. doCount=false;
  68. LCDLine1="COUNT ...";
  69. LCDLine2="";
  70. updateLCD();
  71. digitalWrite(13, !digitalRead(13)); // Toggle LED on pin 13
  72. itemWeight=readingAverage(25,tareWeight) / c;
  73. Serial.print('I');
  74. Serial.println(int(itemWeight*100));
  75. digitalWrite(13, !digitalRead(13)); // Toggle LED on pin 13
  76. }
  77.  
  78. void setup() {
  79. Serial.begin(38400);
  80. Serial.println("Counting Scale - Version 0.9");
  81. pinMode(13, OUTPUT);
  82. pinMode(2, INPUT_PULLUP);
  83. pinMode(3, INPUT_PULLUP);
  84. attachInterrupt(0, _doTare, CHANGE);
  85. attachInterrupt(1, _doCount, CHANGE);
  86. lcd.init(); //initialize the lcd
  87. lcd.backlight(); //open the backlight
  88. LCDLine1="Initializing ...";
  89. LCDLine2="";
  90. updateLCD();
  91. tareWeight=readingAverage(25,0);
  92. }
  93.  
  94. void setLabel(char ch) {
  95. if(index < MaxChars && (isDigit(ch) or isAlpha(ch) or isWhitespace(ch))) {
  96. strValue[index++] = ch;
  97. }
  98. else
  99. {
  100. itemLabel=strValue;
  101. index = 0;
  102. // strValue[0]=(char)0;
  103. memset(strValue, 0, sizeof strValue);
  104. serialInputType=' ';
  105. }
  106. }
  107.  
  108. void setItemWeight(char ch) {
  109. int tempWeight;
  110. if(index < MaxChars && isDigit(ch)) {
  111. strValue[index++] = ch;
  112. }
  113. else
  114. {
  115. tempWeight =atoi(strValue);
  116. if(tempWeight > 0){
  117. itemWeight=tempWeight/100.00;
  118.  
  119. // lcd.print(itemWeight);
  120. }
  121. index = 0;
  122. memset(strValue, 0, sizeof strValue);
  123. serialInputType=' ';
  124. }
  125. }
  126.  
  127. void readCommand(char ch) {
  128. serialInputType=' ';
  129. switch (ch) {
  130.  
  131. case '2' : // Count 25
  132. getItemWeight(25);
  133. break;
  134.  
  135. case '5' : // Count 50
  136. getItemWeight(50);
  137. break;
  138.  
  139. case '7' : // Count 75
  140. getItemWeight(75);
  141. break;
  142.  
  143. case '1' : // Count 100
  144. getItemWeight(100);
  145. break;
  146.  
  147. case 'T' : // Tare
  148. setTare();
  149. break;
  150. }
  151. }
  152.  
  153. void serialEvent()
  154. {
  155. while(Serial.available())
  156. {
  157. char ch = Serial.read();
  158.  
  159. switch (serialInputType) {
  160. case 'W' :
  161. setItemWeight(ch);
  162. break;
  163.  
  164. case 'L' :
  165. setLabel(ch);
  166. break;
  167.  
  168. case 'C' :
  169. readCommand(ch);
  170. break;
  171. case 'V' :
  172. Serial.println("Counting Scale - Version 0.9");
  173. break;
  174. case ' ' :
  175. if (commandChars.indexOf(ch)>0) {
  176. serialInputType=ch;
  177. }
  178. break;
  179. }
  180. }
  181. }
  182. void loop() {
  183. if (doTare) {
  184. setTare();
  185. }
  186.  
  187. if (doCount) {
  188. getItemWeight(25);
  189. }
  190.  
  191. average=readingAverage(25,tareWeight);
  192. Serial.println(average);
  193. LCDLine1=String(average*0.715)+"g";
  194.  
  195. // if item weight has been set, calculate and show quantity
  196. if (itemWeight>0) {
  197.  
  198. // Do not want to show quantity as negative
  199. if (average>0) {
  200. LCDLine2=String(average / itemWeight,0);
  201. }
  202. else {
  203. LCDLine2="0";
  204. }
  205.  
  206. // if there is an associated label, show it. Otherwise show pcs
  207. if (itemLabel!="") {
  208. LCDLine2=LCDLine2+" "+itemLabel;
  209. }
  210. else {
  211. LCDLine2=LCDLine2+" Pcs";
  212. }
  213. }
  214. updateLCD();
  215. }
Advertisement
Add Comment
Please, Sign In to add comment