AngyalRobert

HX711

Dec 7th, 2020
98
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "HX711.h"
  2. #include <EEPROM.h>
  3. //#include "nextion.h"
  4.  
  5. // HX711 circuit wiring
  6. const int LOADCELL_DOUT_PIN = A1;
  7. const int LOADCELL_SCK_PIN = A0;
  8.  
  9. const char commandStart= '@';
  10. const char commandEnd= ';';
  11. unsigned char commandPtr= 0;
  12. char command[20];
  13. unsigned long serTimeout;
  14.  
  15. HX711 scale;
  16.  
  17. long adTable[ 30];
  18. unsigned char firstRead= 1;
  19. char setPoint[20];
  20.  
  21. long avgAd;
  22.  
  23. long zeroAd;
  24. float spanAd;
  25.  
  26. unsigned char avgCount= 15;
  27. unsigned char decPoint= 1;
  28. unsigned char nyugFelt= 10;
  29. unsigned char lepes= 1;
  30.  
  31. void setup() {
  32. Serial.begin( 9600);
  33. scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
  34.  
  35. EEPROM.get( 0, zeroAd);
  36. Serial.print( "zeroAd: ");
  37. Serial.println( zeroAd);
  38.  
  39. EEPROM.get( 4, spanAd);
  40. Serial.print( "spanAd: ");
  41. Serial.println( spanAd);
  42.  
  43. EEPROM.get( 12, avgCount);
  44. if ((avgCount>99) || (avgCount<3)){
  45. avgCount= 15;
  46. EEPROM.put( 12, avgCount);
  47. }
  48. Serial.print( "avgCount: ");
  49. Serial.println( avgCount);
  50.  
  51. EEPROM.get( 13, decPoint);
  52. if (decPoint>5){
  53. decPoint= 1;
  54. EEPROM.put( 13, decPoint);
  55. }
  56. Serial.print( "decPoint: ");
  57. Serial.println( decPoint);
  58.  
  59. EEPROM.get( 14, nyugFelt);
  60. if (nyugFelt>99){
  61. nyugFelt= 10;
  62. EEPROM.put( 14, nyugFelt);
  63. }
  64. Serial.print( "nyugFelt: ");
  65. Serial.println( nyugFelt);
  66.  
  67. EEPROM.get( 15, lepes);
  68. if (lepes!=1 && lepes!=2 && lepes!=5 && lepes!=10 && lepes!=20 && lepes!=50){
  69. Serial.print( "lepes: ");
  70. Serial.println( lepes);
  71. lepes= 1;
  72. EEPROM.put( 15, lepes);
  73. }
  74. Serial.print( "lepes: ");
  75. Serial.println( lepes);
  76. }
  77.  
  78. float weight( long avgAd)
  79. {
  80. /* Serial.print( "zeroAd: ");
  81. Serial.print( zeroAd);
  82. Serial.print( " endAd: ");
  83. Serial.print( endAd);
  84. Serial.print( " endValue: ");
  85. Serial.print( endValue);
  86. */
  87.  
  88. char s[20];
  89. float x= (float )avgAd;
  90. float zA= (float )zeroAd;
  91.  
  92. // Serial.print( " avgAd: ");
  93. // dtostrf( x, 10, 10, s);
  94. // Serial.print( s);
  95.  
  96. x-= zA;
  97. /* Serial.print( " avg-zero: ");
  98. dtostrf( x, 10, 10, s);
  99. Serial.print( s);*/
  100.  
  101. float sA= (float )spanAd;
  102. x*= sA;
  103. /* Serial.print( " *eV:");
  104. dtostrf( x, 10, 10, s);
  105. Serial.print( s);
  106. Serial.print( " weight: ");*/
  107.  
  108. float l= (float)lepes;
  109. unsigned char i;
  110. for (i=0; i<decPoint; i++) x*=10;
  111. x= round( x/l)*l;
  112. for (i=0; i<decPoint; i++) x/=10;
  113. return x;
  114. }
  115.  
  116. unsigned long last_millis= millis()+ 99;
  117.  
  118. void loop() {
  119.  
  120. // -- HX711 beolvasás kb 100 ms-onként.
  121. if (millis()>= last_millis){
  122. if (scale.wait_ready_retry(10)) {
  123. long reading = scale.read();
  124. unsigned char i;
  125.  
  126. if (firstRead) {
  127. firstRead= 0;
  128. for (i= 0; i<30; i++) adTable[i]= reading;
  129. }
  130.  
  131. for (i=avgCount-1; i>0; i--) {
  132. adTable[i]= adTable[i-1];
  133. }
  134. adTable[0]= reading;
  135.  
  136. long avg= 0;
  137. for (i=0; i< avgCount; i++) {
  138. avg+= adTable[i];
  139. // Serial.print( weight( adTable[i]));
  140. //Serial.print( " ");
  141. }
  142. avg/= avgCount;
  143. avgAd= avg;
  144. // Serial.println( avgAd);
  145.  
  146. // Serial.print("AVG:");
  147. //Serial.print( avgAd);
  148. unsigned char nyugalom= 1;
  149. float nyugF= nyugFelt;
  150. for (i=0; i< decPoint; i++)
  151. nyugF/=10;
  152. float w= weight( avgAd);
  153. for (i=0; i< avgCount; i++){
  154. if ( abs( weight(adTable[i])- w)> nyugF){
  155. nyugalom= 0;
  156. }
  157. }
  158.  
  159. if (nyugalom) Serial.print( "W");
  160. else Serial.print( "w");
  161. char s[20];
  162. dtostrf( weight( avgAd), 8, decPoint, s);
  163. Serial.println( s);
  164. //Serial.print("\x03");
  165. } else {
  166. Serial.println("HX711 not found.");
  167. }
  168. last_millis= millis()+ 100;
  169. }
  170.  
  171. //-- soros port kezelése
  172. while (Serial.available() > 0) {
  173. char serRead= Serial.read();
  174. switch (commandPtr){
  175. case 0: if (serRead== commandStart) {
  176. commandPtr++;
  177. serTimeout= millis()+ 100;
  178. Serial.println( "START");
  179. }
  180. break;
  181. case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: case 10: case 11:
  182. // parancsok
  183. // nullázás ZERO
  184. // végérték megadás ENDP004000
  185. // átalagolás száma AVGC30 01..99
  186. // tizedes pontok DECP2 0..5
  187. // lepes nagysag LEPS01 01 02 05 10 20 50
  188. // nyug feltétel NYUG01 01..99 tizedosztás
  189. if (serRead== commandEnd ) {
  190. commandPtr= 0;
  191. serTimeout= 0;
  192. if (strncmp( command, "ZERO", 4)== 0){
  193. if (strlen( command)== 4){
  194. zeroAd= avgAd;
  195. Serial.print( "ZERO: ");
  196. Serial.println( avgAd);
  197. EEPROM.put( 0, zeroAd);
  198. }else{
  199. Serial.print( "LENGTH ERROR ");
  200. Serial.println( command);
  201. }
  202. }else
  203. if (strncmp( command, "ENDP", 4)== 0){
  204. if (strlen( command)== 10){
  205. strncpy( setPoint, command+ 4, 6);
  206. setPoint[ 6]= 0;
  207. long endValue= strtol( setPoint, NULL, 10);
  208. long endAd= avgAd- zeroAd;
  209. spanAd= (float)endValue/ (float) endAd;
  210. Serial.print( "endAd: ");
  211. Serial.print( endAd);
  212. Serial.print( " endValue: ");
  213. Serial.print( endValue);
  214. Serial.print( " spanAd: ");
  215. Serial.println( spanAd);
  216. EEPROM.put( 4, spanAd);
  217. }else{
  218. Serial.print( "LENGTH ERROR ");
  219. Serial.println( command);
  220. }
  221. }else
  222. if (strncmp( command, "AVGC", 4)== 0){
  223. if (strlen( command)== 6){
  224. strncpy( setPoint, command+ 4, 2);
  225. setPoint[ 2]= 0;
  226. avgCount= strtol( setPoint, NULL, 10);
  227. Serial.print( "avgCount: ");
  228. Serial.println( avgCount);
  229. EEPROM.put( 12, avgCount);
  230. }else{
  231. Serial.print( "LENGTH ERROR ");
  232. Serial.println( command);
  233. }
  234. }else
  235. if (strncmp( command, "DECP", 4)== 0){
  236. if (strlen( command)== 5){
  237. strncpy( setPoint, command+ 4, 1);
  238. setPoint[ 1]= 0;
  239. decPoint= strtol( setPoint, NULL, 10);
  240. Serial.print( "decP: ");
  241. Serial.println( decPoint);
  242. EEPROM.put( 13, decPoint);
  243. }else{
  244. Serial.print( "LENGTH ERROR ");
  245. Serial.println( command);
  246. }
  247. }else
  248. if (strncmp( command, "NYUG", 4)== 0){
  249. if (strlen( command)== 6){
  250. strncpy( setPoint, command+ 4, 2);
  251. setPoint[ 2]= 0;
  252. nyugFelt= strtol( setPoint, NULL, 10);
  253. Serial.print( "nyugFelt: ");
  254. Serial.println( nyugFelt);
  255. EEPROM.put( 14, nyugFelt);
  256. }else{
  257. Serial.print( "LENGTH ERROR ");
  258. Serial.println( command);
  259. }
  260. }else
  261. if (strncmp( command, "LEPS", 4)== 0){
  262. if (strlen( command)== 6){
  263. strncpy( setPoint, command+ 4, 2);
  264. setPoint[ 2]= 0;
  265. lepes= strtol( setPoint, NULL, 10);
  266. Serial.print( "lepes: ");
  267. Serial.println( lepes);
  268. EEPROM.put( 15, lepes);
  269. }else{
  270. Serial.print( "LENGTH ERROR ");
  271. Serial.println( command);
  272. }
  273. }else
  274. {
  275. Serial.print( "ERROR: ");
  276. Serial.println( command);
  277. }
  278. }else{
  279. command[ commandPtr-1]= serRead;
  280. command[ commandPtr]= 0;
  281. commandPtr++;
  282. if (commandPtr> 11) {
  283. commandPtr= 0;
  284. serTimeout= 0;
  285. Serial.println( "ERROR 11");
  286. }
  287. }
  288. break;
  289. default:
  290. break;
  291. }
  292. }
  293. if (serTimeout>0 && millis()> serTimeout){
  294. serTimeout= 0;
  295. commandPtr= 0;
  296. Serial.println( "TIMEOUT");
  297. }
  298.  
  299. delay(1);
  300. }
RAW Paste Data