Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Arduino.h>
- #include "HX711.h" // library for load cell amplifier
- #include <FastLED.h> // led library
- #define NUM_LEDS 27 //amount of leds
- #define LED_PIN 6 //data pin for leds
- const int LOADCELL_DOUT_PIN = 2; // HX711 circuit wiring
- const int LOADCELL_SCK_PIN = 3;
- int target_weight = 230; //weight at which I want the leds to be a full brightness
- int brightness = 0;
- int mappedVal;
- int i;
- CRGB leds[NUM_LEDS];
- HX711 scale;
- void setup() { // intializing scale and LEDS
- FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
- Serial.begin(57600);
- Serial.println("HX711 Demo");
- Serial.println("Initializing the scale");
- scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
- Serial.println("Before setting up the scale:");
- Serial.print("read: \t\t");
- Serial.println(scale.read()); // print a raw reading from the ADC
- Serial.print("read average: \t\t");
- Serial.println(scale.read_average(20)); // print the average of 20 readings from the ADC
- Serial.print("get value: \t\t");
- Serial.println(scale.get_value(5)); // print the average of 5 readings from the ADC minus the tare weight (not set yet)
- Serial.print("get units: \t\t");
- Serial.println(scale.get_units(5), 1); // print the average of 5 readings from the ADC minus tare weight (not set) divided
- // by the SCALE parameter (not set yet)
- scale.set_scale(1029.911); // depending on the base that the load cell is attached to, this will change and have to be reset
- // this value is obtained by calibrating the scale with known weights; see the README for details
- scale.tare(); // reset the scale to 0
- Serial.println("After setting up the scale:");
- Serial.print("read: \t\t");
- Serial.println(scale.read()); // print a raw reading from the ADC
- Serial.print("read average: \t\t");
- Serial.println(scale.read_average(20)); // print the average of 20 readings from the ADC
- Serial.print("get value: \t\t");
- Serial.println(scale.get_value(5)); // print the average of 5 readings from the ADC minus the tare weight, set with tare()
- Serial.print("get units: \t\t");
- Serial.println(scale.get_units(5), 1); // print the average of 5 readings from the ADC minus tare weight, divided
- // by the SCALE parameter set with set_scale
- Serial.println("Readings:");
- }
- void loop() {
- int scale_value = (scale.get_units()); // assigns scale reading to variable
- Serial.print("Scale Value is: ");
- Serial.print(scale_value); //prints scale reading after calibration and tare
- Serial.print("\n");
- byte mappedVal = map(scale_value, 0, target_weight, 0, 255); //map scale reading data to brightness level
- Serial.print("Brightness Level is:\t");
- Serial.println(mappedVal);
- Serial.print("\n");
- delay(100);
- if (scale_value >= 1){
- for(int i = 0; i < NUM_LEDS; i++){
- leds[i] = CHSV(0,255,brightness);
- }
- }
- FastLED.show();
- brightness = brightness + mappedVal;
- if (scale_value == 0){
- FastLED.clear();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment