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