Hi I'm working on a project connecting 3 Arduinos nano RP 2040 connect with Bluetooth and I had 3 working codes, I tried it this morning and they don't work anymore. I can't figure out why
Please help me I'm just trying to pass my class
First code:
/////////////////////////////////////////////////////
// Libraries
/////////////////////////////////////////////////////
#include <ArduinoBLE.h> // Bluetooth Low Energy library.
/////////////////////////////////////////////////////
// Initialize Variables
/////////////////////////////////////////////////////
// Noise Level
const int noisePin = A0; // Pin used to read the noise value.
int sensorValue = 0; // Stores the noise value.
bool tooMuchNoise = false; // Tells either if there is too much noise or not.
String noiseLevel = ""; // Used for the message that will be sent on BLE.
String dataToSend = ""; // Used for the message that will be sent on BLE.
bool noise = false; // Tells either if there is too much noise or not.
int noiseState;
// Used for time elapsed.
unsigned long currentTime = 0; // Used to set the current time ellapsed.
unsigned long extendTime = 0; // Used for a Retriggerable Timer.
unsigned long previousTime = 0; // Stores the time when the event occurred.
const unsigned long interval1 = 1500; // Time to wait (in milliseconds) = 10 seconds for phase 2 to start.
// Used for servomotor of the ceiling lamp.
const int servoPin = 9; // Pin 9 will be used for the servomotor.
int standardPose = 80; // What is the standard position of the servomotor.
int pos = 0; // Actual position of the servomotor.
int servoSpeed = 1; // The speed of the servomotor. The higher the value the faster it is.
bool moveRight = true; // To know if the servo is moving right during the pendulum movement.
// Counters. I didnt use any of them lol
int i = 0;
int j = 0;
int k = 0;
// BLE Variables
BLEService myService("1234"); // BLE service
BLECharacteristic myCharacteristic("5678", BLERead | BLENotify, 25); // Increased to 25 bytes
/////////////////////////////////////////////////////
// Set Up
/////////////////////////////////////////////////////
void setup() {
Serial.begin(9600); // Used to communicate with arduino through serial monitor.
while (!Serial);
pinMode(2, INPUT);
if (!BLE.begin()) {
Serial.println("Starting BLE failed!");
while (1);
}
BLE.setLocalName("NanoSender2"); // Name of your arduino in BLE.
BLE.setAdvertisedService(myService); // Advertises a service.
myService.addCharacteristic(myCharacteristic); // Creates a characteristic.
BLE.addService(myService); // Creates a Service.
// Set initial value
myCharacteristic.writeValue(""); // Send null value to characteristic.
BLE.advertise(); // Advertises the arduino BLE
Serial.println("Bluetooth device active, waiting for connection...");
}
/////////////////////////////////////////////////////
// Void Loop
/////////////////////////////////////////////////////
void loop() {
BLEDevice central = BLE.central(); // Stores information from the arduinos that are connected to the ceiling lamp.
// If connected to a central (radio or floor lamp).
if (central) {
Serial.print("Connected to: ");
Serial.println(central.address());
while (central.connected()) {
tooMuchNoise = soundLevel(); // We use the custom function to read the level of the noise. If there is noise, tooMuchNoise is true.
noiseState = digitalRead(2);
Serial.println(noiseState);
if (tooMuchNoise) {
dataToSend = "tooMuchNoise";
} else {
dataToSend = "veryDemure";
}
// Phase 1
currentTime = millis(); // Update the current time.
if (tooMuchNoise) {
extendTime = currentTime; // Retriggerable Timer
}
// if there is noise initiate phase 2, otherwise restart counters, timers and light
if (currentTime - extendTime < 2000) {
// if 5 seconds had elapsed, then begin phase 2, if not do nothing.
if (currentTime - previousTime >= interval1) {
phase1(); // Initiate the phase 1, move the servo.
}
dataToSend = "tooMuchNoise";
} else {
previousTime = currentTime; // restart previous time
delay(15); // a little and cute delay
}
// Add string length check for safety
if (dataToSend.length() + 1 <= 25) { // +1 for null terminator
myCharacteristic.writeValue(dataToSend.c_str(), dataToSend.length());
Serial.println("Sent: " + dataToSend);
} else {
Serial.println("Message too long for characteristic!");
}
delay(100); // !!!!!!!!!!!!!
}
Serial.println("Disconnected");
}
}
/////////////////////////////////////////////////////
// Custom Functions
/////////////////////////////////////////////////////
// Reads the noise level.
bool soundLevel() {
noiseState = digitalRead(2);
Serial.println(noiseState);
if (noiseState) {
noiseLevel = "Too much noise!";
noise = true;
} else {
noiseLevel = "Very demure.";
noise = false;
}
// Serial.println("Sound Level: " + String(sensorValue) + " (RAW DATA)");
Serial.println("Sound Level: " + noiseLevel);
return noise;
}
// Activates the phase 1
void phase1() {
Serial.println("Start Phase 1! ╚(•⌂•)╝");
// Serial.println("Phase 1: Moving the Ceiling Lamp.");
// Check the position and adjust direction if needed
if (pos >= 85) {
moveRight = false;
Serial.println("Reached upper limit. Moving the Ceiling Lamp to the left.");
} else if (pos <= 5) {
moveRight = true;
Serial.println("Reached lower limit. Moving the Ceiling Lamp to the right.");
}
// Calculate new position and constrain it
int newPos = pos + (moveRight ? servoSpeed : -servoSpeed);
pos = constrain(newPos, 0, 180);
// Update servo position if it changed
Serial.println(pos);
// Wait briefly to allow smooth movement
delay(20);
}
This is the next one:
////////////////////////////////////////////////
// Libraries
////////////////////////////////////////////////
#include <ArduinoBLE.h>
#include <NeoPixelConnect.h>
////////////////////////////////////////////////
// Variables
////////////////////////////////////////////////
// Pin on the arduino
const int ledPin = 13; // pin to use for the LED
// Noise Check
bool tooMuchNoise = false;
// Used for elapsed time.
unsigned long currentTime = 0; // Used to set the current time ellapsed.
unsigned long extendTime = 0; // Used for a Retriggerable Timer.
unsigned long previousTime = 0; // Stores the time when the event occurred.
const unsigned long interval2 = 5000; // Time to wait (in milliseconds) = 10 seconds for phase 2 to start.
// Counters
int i = 0; // Counter used to dim the light.
int j = 0; // Counter used to dim the light.
// Used to control the Light dimmer
int light = 0; // Value of the RGB
int dimLight = 5; // To what value will it dim, 0 being turned off.
#define MAXIMUM_NUM_NEOPIXELS 60
NeoPixelConnect p(4, MAXIMUM_NUM_NEOPIXELS, pio0, 0);
////////////////////////////////////////////////
// Set Up
////////////////////////////////////////////////
void setup() {
Serial.begin(9600);
while (!Serial);
pinMode(ledPin, OUTPUT);
BLE.begin();
Serial.println("FloorLamp");
BLE.scanForUuid("0701");
}
void loop() {
BLEDevice peripheral = BLE.available();
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() != "ceilingLamp") {
return;
}
BLE.stopScan();
listenNoise(peripheral);
BLE.scanForUuid("0701");
}
}
////////////////////////////////////////////////
// Custom Functions
////////////////////////////////////////////////
void listenNoise(BLEDevice 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("Attributes discovered");
} else {
Serial.println("Attribute discovery failed!");
peripheral.disconnect();
return;
}
BLECharacteristic noiseCharacteristic = peripheral.characteristic("1511");
if (!noiseCharacteristic) {
Serial.println("Peripheral does not have noise Characteristic!");
return;
}
if (!noiseCharacteristic.subscribe()) {
Serial.println("Subscription Failed!");
peripheral.disconnect();
return;
}
while (peripheral.connected()) {
Serial.println("");
if (noiseCharacteristic.valueUpdated()) {
byte d = 0;
noiseCharacteristic.readValue(d);
if (d>0){
tooMuchNoise = true;
Serial.print(" Noise Level: Too Much Noise! ");
} else {
tooMuchNoise = false;
Serial.print(" Noise Level: Very Demure! ");
}
currentTime = millis();
if (tooMuchNoise) {
extendTime = currentTime;
}
if (currentTime - extendTime < 1500) {
if (currentTime - previousTime >= interval2) {
phase2();
}
} else {
previousTime = currentTime;
light = 255;
i = 0;
j = 0;
Serial.print(" Light: " + String(light));
p.neoPixelFill(light, light, light);
delay(15);
}
}
delay(50);
}
Serial.println("Peripheral disconnected");
}
////////////////////////////////////////////////
// Custom Function
////////////////////////////////////////////////
void phase2(){
Serial.print(" Start phase 2! ");
digitalWrite(ledPin, HIGH);
j++;
if (j%1==0) {
i=i+3;
i = constrain(i, 0, 255);
}
light = 255 - i;
light = constrain(light, dimLight, 255);
Serial.print(" Light: " + String(light));
p.neoPixelFill(light, light, light, true); // Tell the light to dim using the libraryt=
delay(15);
}
////////////////////////////////////////////////
//
////////////////////////////////////////////////
An finally:
////////////////////////////////////////////////
// Libraries
////////////////////////////////////////////////
# include <ArduinoBLE.h>
#include <DFRobotDFPlayerMini.h> // Used to control the df player mini
////////////////////////////////////////////////
// Variables
////////////////////////////////////////////////
// Pin on the arduino
const int ledPin = 13; // pin to use for the LED
// Noise Check
bool tooMuchNoise = false;
// Used for elapsed time.
unsigned long currentTime = 0; // Used to set the current time ellapsed.
unsigned long extendTime = 0; // Used for a Retriggerable Timer.
unsigned long previousTime = 0; // Stores the time when the event occurred.
const unsigned long interval2 = 10000; // Time to wait (in milliseconds) = 10 seconds for phase 2 to start.
// Used for the DF Player Mini
DFRobotDFPlayerMini myDFPlayer;
int volume=30;
////////////////////////////////////////////////
// Set Up
////////////////////////////////////////////////
void setup() {
Serial.begin(9600);
while (!Serial);
pinMode(ledPin, OUTPUT);
// Init the serial communication with the DF player mini.
Serial1.begin(9600);
if (!myDFPlayer.begin(Serial1)) {
Serial.println("DFPlayer mini initialization failed!");
while(1);
}
BLE.begin();
Serial.println("Radio");
BLE.scanForUuid("0701");
}
void loop() {
BLEDevice peripheral = BLE.available();
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() != "ceilingLamp") {
return;
}
BLE.stopScan();
listenNoise(peripheral);
BLE.scanForUuid("0701");
}
}
////////////////////////////////////////////////
// Custom Functions
////////////////////////////////////////////////
void listenNoise(BLEDevice 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("Attributes discovered");
} else {
Serial.println("Attribute discovery failed!");
peripheral.disconnect();
return;
}
BLECharacteristic noiseCharacteristic = peripheral.characteristic("1511");
if (!noiseCharacteristic) {
Serial.println("Peripheral does not have noise Characteristic!");
return;
}
if (!noiseCharacteristic.subscribe()) {
Serial.println("Subscription Failed!");
peripheral.disconnect();
return;
}
while (peripheral.connected()) {
Serial.println("");
if (noiseCharacteristic.valueUpdated()) {
byte d = 0;
noiseCharacteristic.readValue(d);
if (d>0){
tooMuchNoise = true;
Serial.print(" Noise Level: Too Much Noise! ");
} else {
tooMuchNoise = false;
Serial.print(" Noise Level: Very Demure! ");
}
currentTime = millis();
if (tooMuchNoise) {
extendTime = currentTime;
}
if (currentTime - extendTime < 1500) {
if (currentTime - previousTime >= interval2) {
phase3();
previousTime = currentTime;
}
} else {
previousTime = currentTime;
delay(15);
}
}
delay(50);
}
Serial.println("Peripheral disconnected");
}
////////////////////////////////////////////////
// Custom Function
////////////////////////////////////////////////
void phase3(){
Serial.print(" Start phase 3! ");
digitalWrite(ledPin, HIGH);
myDFPlayer.volume(volume);
myDFPlayer.next();
Serial.print(" Producing a sound! ");
delay(15);
}
////////////////////////////////////////////////
//
////////////////////////////////////////////////
I tried reuploading all of them and can't seem to make it work
Thank you!