Advertisement
Guest User

Untitled

a guest
Jun 7th, 2024
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.61 KB | None | 0 0
  1.  
  2. /*
  3.   Blink without Delay
  4.  
  5.   Turns on and off a light emitting diode (LED) connected to a digital pin,
  6.   without using the delay() function. This means that other code can run at the
  7.   same time without being interrupted by the LED code.
  8.  
  9.   The circuit:
  10.   - Use the onboard LED.
  11.   - Note: Most Arduinos have an on-board LED you can control. On the UNO, MEGA
  12.     and ZERO it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN
  13.     is set to the correct LED pin independent of which board is used.
  14.     If you want to know what pin the on-board LED is connected to on your
  15.     Arduino model, check the Technical Specs of your board at:
  16.     https://www.arduino.cc/en/Main/Products
  17.  
  18.   created 2005
  19.   by David A. Mellis
  20.   modified 8 Feb 2010
  21.   by Paul Stoffregen
  22.   modified 11 Nov 2013
  23.   by Scott Fitzgerald
  24.   modified 9 Jan 2017
  25.   by Arturo Guadalupi
  26.  
  27.   This example code is in the public domain.
  28.  
  29.   https://www.arduino.cc/en/Tutorial/BuiltInExamples/BlinkWithoutDelay
  30. */
  31.  
  32. // adding your headers and functions
  33. #include <EEPROM.h>
  34. #include <HX711.h>
  35. #include <Wire.h>
  36. #include <LiquidCrystal_I2C.h>
  37.  
  38. // Pin definitie voor HX711
  39. const int LOADCELL_DOUT_PIN = 13;
  40. const int LOADCELL_SCK_PIN = 12;
  41.  
  42. // HX711 object
  43. HX711 scale;
  44.  
  45. // LCD object
  46. LiquidCrystal_I2C lcd(0x27, 16, 2);
  47.  
  48. // EEPROM address voor kalibratiefactor
  49. const int CALIBRATION_FACTOR_ADDRESS = 0;
  50.  
  51. // Kalibratiefactor variabele
  52. float calibration_factor;
  53.  
  54. // Functie om kalibratiefactor uit EEPROM te lezen
  55. float readCalibrationFactor() {
  56.   float calFactor;
  57.   EEPROM.get(CALIBRATION_FACTOR_ADDRESS, calFactor);
  58.   if (isnan(calFactor)) {
  59.     calFactor = 1.0; // Standaard kalibratiefactor als er nog geen data is opgeslagen
  60.   }
  61.   Serial.print("Kalibratiefactor gelezen: ");
  62.   Serial.println(calFactor);
  63.   return calFactor;
  64. }
  65.  
  66. // Functie om gewicht af te ronden op dichtstbijzijnde 0.5 kg
  67. float roundToNearestHalfKg(float weight) {
  68.   return round(weight * 2) / 2.0;
  69. }
  70.  
  71.  
  72.  
  73.  
  74.  
  75. // constants won't change. Used here to set a pin number:
  76. //const int ledPin =  LED_BUILTIN;// the number of the LED pin
  77.  
  78. // Variables will change:
  79. //int ledState = LOW;             // ledState used to set the LED
  80.  
  81. // Generally, you should use "unsigned long" for variables that hold time
  82. // The value will quickly become too large for an int to store
  83. unsigned long previousMillis = 0;        // will store last time LED was updated
  84.  
  85. // constants won't change:
  86. const long interval = 1000;           // interval at which to blink (milliseconds)
  87.  
  88. void setup() {
  89.   // set the digital pin as output:
  90. //  pinMode(ledPin, OUTPUT);
  91.  
  92. // adding your init here
  93.   Serial.begin(9600);
  94.  
  95.   // LCD initialisatie
  96.   lcd.init();
  97.   lcd.backlight();
  98.   lcd.setCursor(0, 0);
  99.   lcd.print("Initialiseren...");
  100.   Serial.println("Initialiseren...");
  101.  
  102.   // HX711 initialisatie
  103.   scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
  104.  
  105.   // Lees de kalibratiefactor uit de EEPROM
  106.   calibration_factor = readCalibrationFactor();
  107.   scale.set_scale(calibration_factor);
  108.   scale.tare(); // Zet de huidige leeswaarde op 0
  109.  
  110.   lcd.clear();
  111.   lcd.setCursor(0, 0);
  112.   lcd.print("Klaar voor meting");
  113.   Serial.println("Klaar voor meting");
  114.   delay(2000);
  115.   lcd.clear();
  116.  
  117. }
  118.  
  119. void loop() {
  120.   // here is where you'd put code that needs to be running all the time.
  121.  
  122.   // check to see if it's time to blink the LED; that is, if the difference
  123.   // between the current time and last time you blinked the LED is bigger than
  124.   // the interval at which you want to blink the LED.
  125.   unsigned long currentMillis = millis();
  126.  
  127.  
  128.  
  129.  
  130.   if (currentMillis - previousMillis >= interval) {
  131.     // save the last time you blinked the LED
  132.     previousMillis = currentMillis;
  133.  
  134.   // sticking the event that needs to heppen every second here:
  135.  
  136.   // Lees het gewicht en converteer naar kg
  137.   float gewicht = scale.get_units(10) / 1000.0;
  138.  
  139.   // Zorg ervoor dat het gewicht nooit negatief is
  140.   if (gewicht < 0) {
  141.     gewicht = 0;
  142.   }
  143.  
  144.   // Rond het gewicht af op de dichtstbijzijnde 0.5 kg
  145.   float afgerond_gewicht = roundToNearestHalfKg(gewicht);
  146.  
  147.   // Toon het gewicht op de LCD
  148.   lcd.setCursor(0, 0);
  149.   lcd.print("Gewicht: ");
  150.   lcd.print(afgerond_gewicht);
  151.   lcd.print(" kg");
  152.  
  153.   // Toon het gewicht op de seriële monitor
  154.   Serial.print("Gewicht: ");
  155.   Serial.print(afgerond_gewicht);
  156.   Serial.println(" kg");
  157.  
  158.  
  159.     // if the LED is off turn it on and vice-versa:
  160. //    if (ledState == LOW) {
  161. //      ledState = HIGH;
  162. //    } else {
  163. //      ledState = LOW;
  164. //    }
  165.  
  166.     // set the LED with the ledState of the variable:
  167. //    digitalWrite(ledPin, ledState);
  168.   }
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177. }
  178.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement