Hello Arduino community
I made a while ago a program that calculated a calibration value of my sensor. The calculations are based on the max. and min. values during the time I push a pushbutton. You can see the code below (I'm sorry, some words are written in Dutch):
/*
In dit programma wordt er een kalibratie van een sensor uitgevoerd. Dit door voor een onbepaalde tijd de drukknop in te duwen.
In die tijd zal het programma zoeken naar de maximale en minimale analoge waarde die de sensor binnenleest. De referentiewaarde
die de "kalibratie" aangeeft is dan gelijk aan het gemiddelde van die twee extreme waarden.
*/
#include <LiquidCrystal.h>
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
const int sensorPin = A0;
const int indicatorLedPin = 13;
const int buttonPin = 0;
int sensorMin = 1023;
int sensorMax = 0;
int sensorValue = 0;
int Kalibratiewaarde;
void setup() {
Serial.begin(9600);
pinMode(indicatorLedPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop() {
while (digitalRead(buttonPin) == HIGH) {
calibrate();
}
digitalWrite(indicatorLedPin, LOW);
Kalibratiewaarde = (sensorMin + sensorMax) / 2;
Serial.println(Kalibratiewaarde);
lcd.setCursor(0, 0);
lcd.print(Kalibratiewaarde);
delay(500);
}
void calibrate() {
sensorMin = 1023;
sensorMax = 0;
while (digitalRead(buttonPin) == HIGH) {
digitalWrite(indicatorLedPin, HIGH);
sensorValue = analogRead(sensorPin);
if (sensorValue > sensorMax) {
sensorMax = sensorValue;
}
if (sensorValue < sensorMin) {
sensorMin = sensorValue;
}
}
}
This program works fine, but I want to work with BLE (two arduino's: one arduino connected to a pushbutton and an LCD and the other arduino connected to a sensor).
I want to set up the arduino with the pushbutton and LCD as a peripheral, and the arduino with the sensor as a central.
Now, I have two questions ... The first question is witch kind of characteristic I will have to use to write values from the peripheral to the central (or is that even possible?). In need this to say to my central arduino that he has to calibrate while pushing the pushbutton.
The second one is maybe stupid, but how do you send a value other from 0 or 1, from a central to a peripheral (for example a calibration value of 500)? I need this to send the calibration value to my peripheral and display this value on an LCD.
I would be very pleased with help!
Kind regards
Stan
first of all there are no stupid questions. Just maybe simple questions.
most bluetooth-modules for Arduino work as a wireless serial connection.
You use them almost like direct serial connection.
And with a serial connection you can transfer a lot of bytes in both directions.
So it is possible to do. Even on various ways.
For further advice it is important what kind of bluetooth-moduls you want to use.
Or would you like to have suggestions on that too?
So maybe other users who have experience with the different modules can chime in.
It is good that you have posted your code. Nonetheless
you should describe the functionality that you need in normal words.
This will make it much easier to understand what your code does and reduce the analysing time.
If you haven't bought yet the bluetooth-moduls or not even the arduinos
another possability for wireless communication are ESP8266 microcontrollers or ESP32
WIth ESP8266 you could use a wireless connection called "ESP-NOW" ESP32 has ESP-NOW and additionally even bluetooth onboard. They can be programmed with the arduino-IDE.
So post the informations asked above and then we will go on.
What I exactly want is when I push a pushbutton on an arduino(1), the other arduino(2) has to start calibrating his values for a sensor during the pushbutton-press. When stop pushing the pushbutton, the calculated calibration value has to be send from arduino(2) to arduino(1) where the value is displayed on an LCD. The communication has to be done via BLE.
I use two arduino nano 33 IoT's to communicate with each other. And I am making use of the ArduinoBLE library (ArduinoBLE - Arduino Reference).
My experience with programming with BLE is very small ... The only thing I have done is controlling a LED with a pusbutton via BLE. You can see the code below for that program:
PERIPHERAL (MASTER)
#include <ArduinoBLE.h>
BLEService testprogrammaLED("19B10000-E8F2-537E-4F6C-D104768A1214");
BLEByteCharacteristic switchCharacteristic("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite);
const int LED = LED_BUILTIN;
void setup() {
Serial.begin(9600);
while (!Serial);
pinMode(LED, OUTPUT);
if (!BLE.begin()) {
Serial.println("Het opstarten van BLE is mislukt!");
while (1);
}
BLE.setLocalName("STAN");
BLE.setAdvertisedService(testprogrammaLED);
testprogrammaLED.addCharacteristic(switchCharacteristic);
BLE.addService(testprogrammaLED);
BLE.advertise();
Serial.println("BLE LED Periperal");
}
void loop() {
BLEDevice central = BLE.central();
if (central) {
Serial.print("Verbonden met central");
Serial.println(central.address());
while (central.connected()) {
if (switchCharacteristic.written()) {
if (switchCharacteristic.value()) {
Serial.println("LED aan");
digitalWrite(LED, HIGH);
}
else {
Serial.println(F("LED uit"));
digitalWrite(LED, LOW);
}
}
}
Serial.print(F("Losgekoppeld van central"));
Serial.println(central.address());
}
}
CENTRAL (SLAVE)
#include <ArduinoBLE.h>
const int knop3 = 3;
int Oknopwaarde = LOW;
void setup() {
Serial.begin(9600);
while (!Serial);
pinMode(knop3, INPUT);
BLE.begin();
Serial.println("BLE centrale | LED-bediening");
BLE.scanForUuid("19b10000-e8f2-537e-4f6c-d104768a1214");
}
void loop() {
BLEDevice peripheral = BLE.available();
if (peripheral) {
Serial.print("Gevonden ");
Serial.print(peripheral.address());
Serial.print(" '");
Serial.print(peripheral.localName());
Serial.print("' ");
Serial.print(peripheral.advertisedServiceUuid());
Serial.println();
if (peripheral.localName() != "STAN") {
return;
}
BLE.stopScan();
controlLed(peripheral);
BLE.scanForUuid("19b10000-e8f2-537e-4f6c-d104768a1214");
}
}
void controlLed(BLEDevice peripheral) {
Serial.println("Verbinding maken ...");
if (peripheral.connect()) {
Serial.println("Verbonden!");
} else {
Serial.println("Verbinding maken mislukt!");
return;
}
Serial.println("Zoeken van attributen ...");
if (peripheral.discoverAttributes()) {
Serial.println("Attributen gevonden");
} else {
Serial.println("Attributen zoeken mislukt!");
peripheral.disconnect();
return;
}
BLECharacteristic LEDKarakteristiek = peripheral.characteristic("19b10001-e8f2-537e-4f6c-d104768a1214");
if (!LEDKarakteristiek) {
Serial.println("De peripheral heeft geen LED-karakteristiek!");
peripheral.disconnect();
return;
} else if (!LEDKarakteristiek.canWrite()) {
Serial.println("De peripheral heeft een aanpasbaar karakteristiek!");
peripheral.disconnect();
return;
}
while (peripheral.connected()) {
int knopwaarde = digitalRead(knop3);
if (Oknopwaarde != knopwaarde) {
Oknopwaarde = knopwaarde;
if (knopwaarde) {
Serial.println("Drukknop is ingedrukt");
LEDKarakteristiek.writeValue((byte)0x01);
} else {
Serial.println("Drukknop is losgelaten");
LEDKarakteristiek.writeValue((byte)0x00);
}
}
}
Serial.println("Peripheral disconnected");
}
I'm not certain how it is supported through the Arduino library you are using. I used this UART service on an esp32 where there was a BLE uart example.
Why do you need BLE instead of the original Bluetooth. The SPP communication protocol is far more simple.