Guest User

LED load cell scale

a guest
Apr 26th, 2024
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.09 KB | Source Code | 0 0
  1. #include <Arduino.h>
  2. #include "HX711.h" // library for load cell amplifier
  3. #include <FastLED.h> // led library
  4.  
  5. #define NUM_LEDS 27 //amount of leds
  6. #define LED_PIN 6 //data pin for leds
  7.  
  8.  
  9. const int LOADCELL_DOUT_PIN = 2;  // HX711 circuit wiring
  10. const int LOADCELL_SCK_PIN = 3;
  11.  
  12.  
  13. int target_weight = 230; //weight at which I want the leds to be a full brightness
  14. int brightness = 0;
  15. int mappedVal;
  16. int i;
  17.  
  18.  
  19. CRGB leds[NUM_LEDS];
  20. HX711 scale;
  21.  
  22. void setup() {    // intializing scale and LEDS
  23.  
  24.   FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
  25.  
  26.   Serial.begin(57600);
  27.   Serial.println("HX711 Demo");
  28.   Serial.println("Initializing the scale");
  29.  
  30.   scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
  31.  
  32.   Serial.println("Before setting up the scale:");
  33.   Serial.print("read: \t\t");
  34.   Serial.println(scale.read());      // print a raw reading from the ADC
  35.  
  36.   Serial.print("read average: \t\t");
  37.   Serial.println(scale.read_average(20));   // print the average of 20 readings from the ADC
  38.  
  39.   Serial.print("get value: \t\t");
  40.   Serial.println(scale.get_value(5));   // print the average of 5 readings from the ADC minus the tare weight (not set yet)
  41.  
  42.   Serial.print("get units: \t\t");
  43.   Serial.println(scale.get_units(5), 1);  // print the average of 5 readings from the ADC minus tare weight (not set) divided
  44.             // by the SCALE parameter (not set yet)
  45.            
  46.   scale.set_scale(1029.911); // depending on the base that the load cell is attached to, this will change and have to be reset
  47.                       // this value is obtained by calibrating the scale with known weights; see the README for details
  48.   scale.tare();               // reset the scale to 0
  49.  
  50.   Serial.println("After setting up the scale:");
  51.  
  52.   Serial.print("read: \t\t");
  53.   Serial.println(scale.read());                 // print a raw reading from the ADC
  54.  
  55.   Serial.print("read average: \t\t");
  56.   Serial.println(scale.read_average(20));       // print the average of 20 readings from the ADC
  57.  
  58.   Serial.print("get value: \t\t");
  59.   Serial.println(scale.get_value(5));   // print the average of 5 readings from the ADC minus the tare weight, set with tare()
  60.  
  61.   Serial.print("get units: \t\t");
  62.   Serial.println(scale.get_units(5), 1);        // print the average of 5 readings from the ADC minus tare weight, divided
  63.             // by the SCALE parameter set with set_scale
  64.  
  65.   Serial.println("Readings:");
  66.  
  67.   }
  68.  
  69.  
  70. void loop() {
  71.   int scale_value = (scale.get_units()); // assigns scale reading to variable
  72.   Serial.print("Scale Value is: ");
  73.   Serial.print(scale_value);  //prints scale reading after calibration and tare
  74.   Serial.print("\n");
  75.  
  76.   byte mappedVal = map(scale_value, 0, target_weight, 0, 255); //map scale reading data to brightness level
  77.  
  78.   Serial.print("Brightness Level is:\t");
  79.   Serial.println(mappedVal);
  80.   Serial.print("\n");
  81.  
  82.   delay(100);
  83.  
  84.   if (scale_value >= 1){
  85.     for(int i = 0; i < NUM_LEDS; i++){
  86.       leds[i] = CHSV(0,255,brightness);
  87.     }
  88.   }
  89.   FastLED.show();
  90.   brightness = brightness + mappedVal;
  91.   if (scale_value == 0){
  92.     FastLED.clear();
  93.   }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment