// This runs when the phone sends a number via Bluetooth
bluetooth.onUartDataReceived(serial.delimiters(Delimiters.NewLine), function () {
receivedData = bluetooth.uartReadUntil(serial.delimiters(Delimiters.NewLine))
money = parseFloat(receivedData)
if (money > 0) {
// Calculate volume based on money
milliliters = money * mlPerDollar
// Calculate how long the pump needs to run (in milliseconds)
pumpingTime = milliliters * msPerMl
runPumpSequence()
}
})
function runPumpSequence () {
// Show amount received
basic.showNumber(money)
basic.pause(500)
// Status: Pumping
basic.showIcon(IconNames.SmallDiamond)
// TURN ON PUMP (Connected to Pin 0)
pins.digitalWritePin(DigitalPin.P0, 1)
// Wait for the calculated duration
basic.pause(pumpingTime)
// AUTO-STOP PUMP
pins.digitalWritePin(DigitalPin.P0, 0)
// Status: Finished
basic.showIcon(IconNames.Yes)
// Reset for next transaction
money = 0
}
let pumpingTime = 0
let milliliters = 0
let money = 0
let receivedData = ""
let msPerMl = 0
let mlPerDollar = 0
// --- CALIBRATION SETTINGS ---
// Example: $1 gets you 100ml
mlPerDollar = 100
// Example: It takes 250ms to pump 1ml
msPerMl = 250
// Setup Bluetooth
bluetooth.startUartService()
basic.showString("CONNECT APP")
Post a Comment