#include "HX711.h" #include #include #include #include LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display HX711 scale; BLEService ledService("19B10000-E8F2-537E-4F6C-D104768A1214"); // BLE LED Service // BLE LED Switch Characteristic - custom 128-bit UUID, read and writable by central BLEByteCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite); BLEUnsignedCharCharacteristic WeightLevelChar("2A19", // standard 16-bit characteristic UUID BLERead | BLENotify); // remote clients will be able to get notifications if this characteristic changes int oldBatteryLevel = 0; // last battery level reading from analog input long previousMillis = 0; // last time the battery level was checked, in ms const int ledPin = LED_BUILTIN; // pin to use for the LED double val; void setup() { lcd.begin(); lcd.backlight(); lcd.clear(); lcd.print("Power on! :)"); lcd.setCursor (0,1); lcd.print("calibrating... "); Serial.begin(9600); scale.begin(A1, A0); //Initialized scale on these pins while (!Serial); scale.set_scale(432.f); // this value is obtained by calibrating the scale with known weights; see the README for details scale.tare(); // reset the scale to 0 // set LED pin to output mode pinMode(ledPin, OUTPUT); // begin initialization if (!BLE.begin()) { Serial.println("starting BLE failed!"); while (1); } // set advertised local name and service UUID: BLE.setLocalName("COFFEE"); BLE.setAdvertisedService(ledService); // add the characteristic to the service ledService.addCharacteristic(switchCharacteristic); ledService.addCharacteristic(WeightLevelChar); // add the battery level characteristic // add service BLE.addService(ledService); // set the initial value for the characeristic: switchCharacteristic.writeValue(0); // start advertising BLE.advertise(); Serial.println("BLE LED Peripheral"); } void loop() { // listen for BLE peripherals to connect: BLEDevice central = BLE.central(); // if a central is connected to peripheral: if (central) { Serial.print("Connected to central: "); // print the central's MAC address: Serial.println(central.address()); // while the central is still connected to peripheral: while (central.connected()) { // Battery Monitor // scale.read(); long currentMillis = millis(); // if 200ms have passed, check the battery level: if (currentMillis - previousMillis >= 200) { previousMillis = currentMillis; // scale.read(); // updateBatteryLevel(); // scale.get_offset(); // scale.get_scale(); outputScale(); } // if the remote device wrote to the characteristic, // use the value to control the LED: if (switchCharacteristic.written()) { if (switchCharacteristic.value()) { // any value other than 0 Serial.println("LED on"); digitalWrite(ledPin, HIGH); // will turn the LED on } else { // a 0 value Serial.println(F("LED off")); digitalWrite(ledPin, LOW); // will turn the LED off } } } // when the central disconnects, print it out: Serial.print(F("Disconnected from central: ")); Serial.println(central.address()); }else{ lcd.clear(); lcd.print("Awaiting BLE... "); lcd.setCursor (0,1); lcd.print("Connect smartphone"); } } void updateBatteryLevel() { /* Read the current voltage level on the A0 analog input pin. This is used here to simulate the charge level of a battery. */ int battery = analogRead(A3); int batteryLevel = map(battery, 0, 1023, 0, 100); if (batteryLevel != oldBatteryLevel) { // if the battery level has changed // Serial.print("Battery Level % is now: "); // print it Serial.println(batteryLevel); WeightLevelChar.writeValue(batteryLevel); // and update the battery level characteristic oldBatteryLevel = batteryLevel; // save the level for next comparison } } void outputScale() { int t, i, n, T; n=7; i=0; double val, sum, sumsq, mean; float stddev; float oldVal; while (i=n){ i=0; } Serial.print(val); Serial.print("\n"); lcd.clear(); lcd.print("weight: "); lcd.print(val); lcd.setCursor (0,1); lcd.print("mean: "); lcd.print(mean); /* n = 20; t = millis(); i = sum = sumsq = 0; while (i