Hi guys! I am trying to send a simple switch signal from one arduino nano 33 BLE to the other one to activate a voice recording module externally. The module I am using is a ISD1820. When i have the module and the switch connected to the same arduino it works fine but as soon as i try to move it off to the other breadboard it sends no signal. I am quite new to Arduino so could someone please help me to figure out if there is a possible error in my code that could be causing this? Thank you so much in advance!
This is the central code:
#include <ArduinoBLE.h>
// Variables for sensors
const int sensorPin1 = A0;
const int sensorPin2 = A1;
const int buttonPin = 2;
int oldButtonState = LOW;
// Distance constants for sensor calibration
const int sensorReadingAt10cm = 730;
const int sensorReadingAt30cm = 220;
// Distance thresholds for motor control
const int motorThreshold1Low = 10; // Lower distance threshold for motor 1 (cm)
const int motorThreshold1High = 20; // Higher distance threshold for motor 1 (cm)
const int motorThreshold2Low = 20; // Lower distance threshold for motor 2 (cm)
const int motorThreshold2High = 30; // Higher distance threshold for motor 2 (cm)
BLEDevice peripheral; // Declare BLE peripheral device
void setup() {
Serial.begin(9600);
while (!Serial);
pinMode(buttonPin, INPUT);
BLE.begin();
Serial.println("Bluetooth® Low Energy Central - Distance Sensor");
BLE.scanForUuid("19b10000-e8f2-537e-4f6c-d104768a1214"); // Start scanning for peripherals
BLE.scanForUuid("19b10000-e8f2-537e-4f6c-d104768a1215"); // Start scanning for peripherals
}
void loop() {
peripheral = BLE.available(); // Check if a peripheral has been discovered
if (peripheral) {
Serial.print("Found ");
Serial.print(peripheral.address());
Serial.print(" '");
Serial.print(peripheral.localName());
Serial.print("' ");
Serial.print(peripheral.advertisedServiceUuid());
Serial.println();
if (peripheral.localName() != "motor") {
return;
}
BLE.stopScan(); // Stop scanning
controlMotors(); // Control motors based on sensor readings
BLE.scanForUuid("19b10000-e8f2-537e-4f6c-d104768a1214"); // Start scanning again
BLE.scanForUuid("19b10000-e8f2-537e-4f6c-d104768a1215"); // Start scanning again
}
}
void controlMotors() {
// Connect to the peripheral
Serial.println("Connecting ...");
if (peripheral.connect()) {
Serial.println("Connected");
} else {
Serial.println("Failed to connect!");
return;
}
// Discover peripheral attributes
Serial.println("Discovering attributes ...");
if (!peripheral.discoverAttributes()) {
Serial.println("Attribute discovery failed!");
peripheral.disconnect();
return;
}
// Retrieve motor characteristics
BLECharacteristic motorCharacteristic1 = peripheral.characteristic("19b10001-e8f2-537e-4f6c-d104768a1214");
BLECharacteristic motorCharacteristic2 = peripheral.characteristic("19b10002-e8f2-537e-4f6c-d104768a1214");
BLECharacteristic speakerCharacteristic = peripheral.characteristic("19b10003-e8f2-537e-4f6c-d104768a1215");
if (!motorCharacteristic1 || !motorCharacteristic2 || !motorCharacteristic1.canWrite() || !motorCharacteristic2.canWrite()) {
Serial.println("Peripheral does not have the required motor characteristics!");
peripheral.disconnect();
return;
}
if (!speakerCharacteristic) {
Serial.println("Peripheral does not have speaker characteristic!");
peripheral.disconnect();
return;
} else if (!speakerCharacteristic.canWrite()) {
Serial.println("Peripheral does not have a writable speaker characteristic!");
peripheral.disconnect();
return;
}
while (peripheral.connected()) {
// Read distance values from sensors
int distanceValue1 = analogRead(sensorPin1);
int distanceValue2 = analogRead(sensorPin2);
// Convert sensor readings to distances (cm)
int distanceInCm1 = map(distanceValue1, sensorReadingAt10cm, sensorReadingAt30cm, 10, 30);
int distanceInCm2 = map(distanceValue2, sensorReadingAt10cm, sensorReadingAt30cm, 10, 30);
Serial.print("Distance Sensor 1: ");
Serial.print(distanceInCm1);
Serial.println(" cm");
Serial.print("Distance Sensor 2: ");
Serial.print(distanceInCm2);
Serial.println(" cm");
// Control motor 1
if (distanceInCm1 > motorThreshold1Low && distanceInCm1 <= motorThreshold1High) {
motorCharacteristic1.writeValue((byte)250); // Turn motor 1 on at PWM 200
} else if (distanceInCm1 > motorThreshold2Low && distanceInCm1 <= motorThreshold2High) {
motorCharacteristic1.writeValue((byte)190); // Turn motor 1 on at PWM 130
} else {
motorCharacteristic1.writeValue((byte)0); // Turn motor 1 off
}
// Control motor 2
if (distanceInCm2 > motorThreshold1Low && distanceInCm2 <= motorThreshold1High) {
motorCharacteristic2.writeValue((byte)250); // Turn motor 2 on at PWM 200
} else if (distanceInCm2 > motorThreshold2Low && distanceInCm2 <= motorThreshold2High) {
motorCharacteristic2.writeValue((byte)190); // Turn motor 2 on at PWM 130
} else {
motorCharacteristic2.writeValue((byte)0); // Turn motor 2 off
}
delay(1000); // Delay before next sensor readings
}
int buttonState = digitalRead(buttonPin);
if (oldButtonState != buttonState) {
// button changed
oldButtonState = buttonState;
if (buttonState) {
Serial.println("button pressed");
// button is pressed, write 0x01 to turn the speaker on
speakerCharacteristic.writeValue((byte)0x01);
} else {
Serial.println("button released");
// button is released, write 0x00 to turn the speaker off
speakerCharacteristic.writeValue((byte)0x00);
}
}
}
And this is the peripheral code:
#include <ArduinoBLE.h>
BLEService motorService("19B10000-E8F2-537E-4F6C-D104768A1214"); // Bluetooth® Low Energy motor service
BLEService speakerService("19B10000-E8F2-537E-4F6C-D104768A1215");
BLEByteCharacteristic pwmCharacteristic1("19B10001-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite); // PWM characteristic for motor 1
BLEByteCharacteristic pwmCharacteristic2("19B10002-E8F2-537E-4F6C-D104768A1214", BLERead | BLEWrite); // PWM characteristic for motor 2
BLEByteCharacteristic switchCharacteristic("19B10003-E8F2-537E-4F6C-D104768A1215", BLERead | BLEWrite); // Switch characteristic
const int motorPin1 = 2; // pin for motor 1
const int motorPin2 = 4; // pin for motor 2
const int speakerPin1 = 6; // pin for speaker 1
const int speakerPin2 = 7; // pin for speaker 2
void setup() {
Serial.begin(9600);
while (!Serial);
pinMode(motorPin1, OUTPUT); // set motor pin to output mode
pinMode(motorPin2, OUTPUT);
pinMode(speakerPin1, OUTPUT); // set speaker pin to output mode
pinMode(speakerPin2, OUTPUT);
if (!BLE.begin()) {
Serial.println("Starting Bluetooth® Low Energy module failed!");
while (1);
}
BLE.setLocalName("motor");
BLE.setAdvertisedService(motorService);
BLE.setAdvertisedService(speakerService);
motorService.addCharacteristic(pwmCharacteristic1);
motorService.addCharacteristic(pwmCharacteristic2);
speakerService.addCharacteristic(switchCharacteristic);
BLE.addService(motorService);
BLE.addService(speakerService);
pwmCharacteristic1.writeValue(0); // Set initial PWM value to 0
pwmCharacteristic2.writeValue(0);
switchCharacteristic.writeValue(0);
BLE.advertise();
Serial.println("BLE motor Peripheral");
}
void loop() {
BLEDevice central = BLE.central();
if (central) {
Serial.print("Connected to central: ");
Serial.println(central.address());
while (central.connected()) {
if (pwmCharacteristic1.written()) {
int pwmValue1 = pwmCharacteristic1.value();
Serial.print("PWM value for motor 1: ");
Serial.println(pwmValue1);
analogWrite(motorPin1, pwmValue1); // Write PWM value to motor 1 pin
}
if (pwmCharacteristic2.written()) {
int pwmValue2 = pwmCharacteristic2.value();
Serial.print("PWM value for motor 2: ");
Serial.println(pwmValue2);
analogWrite(motorPin2, pwmValue2); // Write PWM value to motor 2 pin
}
if (switchCharacteristic.written()) {
if (switchCharacteristic.value()) { // any value other than 0
Serial.println("Switch characteristic written!");
Serial.println("speaker on");
digitalWrite(speakerPin1, HIGH);
digitalWrite(speakerPin2, HIGH); // will turn the LED on
} else { // a 0 value
Serial.println(F("speaker off"));
digitalWrite(speakerPin1, LOW);
digitalWrite(speakerPin2, LOW); // will turn the LED off
}
}
}
// when the central disconnects, print it out:
Serial.print(F("Disconnected from central: "));
Serial.println(central.address());
}
}