Advertisement
Guest User

Untitled

a guest
Feb 3rd, 2015
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. Picture of The Code
  2. // Arduino as load cell amplifier
  3. // by Christian Liljedahl
  4. // christian.liljedahl.dk
  5.  
  6. // Load cells are linear. So once you have established two data pairs, you can interpolate the rest.
  7.  
  8. // Step 1: Upload this sketch to your arduino board
  9.  
  10. // You need two loads of well know weight. In this example A = 10 kg. B = 30 kg
  11. // Put on load A
  12. // read the analog value showing (this is analogvalA)
  13. // put on load B
  14. // read the analog value B
  15.  
  16. // Enter you own analog values here
  17. float loadA = 10; // kg
  18. int analogvalA = 200; // analog reading taken with load A on the load cell
  19.  
  20. float loadB = 30; // kg
  21. int analogvalB = 600; // analog reading taken with load B on the load cell
  22.  
  23. // Upload the sketch again, and confirm, that the kilo-reading from the serial output now is correct, using your known loads
  24.  
  25. float analogValueAverage = 0;
  26.  
  27. // How often do we do readings?
  28. long time = 0; //
  29. int timeBetweenReadings = 200; // We want a reading every 200 ms;
  30.  
  31. void setup() {
  32. Serial.begin(9600);
  33. }
  34.  
  35. void loop() {
  36. int analogValue = analogRead(0);
  37.  
  38. // running average - We smooth the readings a little bit
  39. analogValueAverage = 0.99*analogValueAverage + 0.01*analogValue;
  40.  
  41. // Is it time to print?
  42. if(millis() > time + timeBetweenReadings){
  43. float load = analogToLoad(analogValueAverage);
  44.  
  45. Serial.print("analogValue: ");Serial.println(analogValueAverage);
  46. Serial.print(" load: ");Serial.println(load,5);
  47. time = millis();
  48. }
  49. }
  50.  
  51. float analogToLoad(float analogval){
  52.  
  53. // using a custom map-function, because the standard arduino map function only uses int
  54. float load = mapfloat(analogval, analogvalA, analogvalB, loadA, loadB);
  55. return load;
  56. }
  57.  
  58. float mapfloat(float x, float in_min, float in_max, float out_min, float out_max)
  59. {
  60. return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement