#include <ArduinoBLE.h>
const int solenoidPin = 2; // Replace with actual Arduino pin connected to the solenoid control transistor
bool isPaused = true; // Start with the solenoid paused
BLEService solenoidService("12345678-1234-1234-1234-123456789ABC"); // Custom UUID for the service
BLEStringCharacteristic commandCharacteristic("87654321-4321-4321-4321-210987654321", BLERead | BLEWrite, 20); // Custom UUID for the characteristic
void setup() {
pinMode(solenoidPin, OUTPUT);
Serial.begin(9600);
if (!BLE.begin()) {
Serial.println("starting BLE failed!");
while (1);
}
BLE.setLocalName("SolenoidControl");
BLE.setAdvertisedService(solenoidService);
solenoidService.addCharacteristic(commandCharacteristic);
BLE.addService(solenoidService);
commandCharacteristic.writeValue("Paused"); // Initial value
BLE.advertise();
Serial.println("Bluetooth device active, waiting for connections...");
}
void loop() {
BLEDevice central = BLE.central();
if (central) {
Serial.print("Connected to central: ");
Serial.println(central.address());
while (central.connected()) {
if (commandCharacteristic.written()) {
String command = commandCharacteristic.value();
if (command == "Start") {
isPaused = false;
Serial.println("Solenoid started");
} else if (command == "Pause") {
isPaused = true;
Serial.println("Solenoid paused");
}
}
if (!isPaused) {
digitalWrite(solenoidPin, HIGH); // Activate solenoid
delay(1000); // Keep solenoid activated for 1 second
digitalWrite(solenoidPin, LOW); // Deactivate solenoid
delay(1000); // Keep solenoid deactivated for 1 second
}
}
Serial.print("Disconnected from central: ");
Serial.println(central.address());
}
}