xxxxxxxxxx
144
/*
In this code, the background color changes depending on the random number between 0 and 255 transmitted from arduino nano BLE, Moreover different emojis are displayed as per the values.
The code can be used to track the button state.
When you press the connect button, wait till all charactristics are found
*/
const serviceU = "9EF4C2D8-41AD-4EA6-BB1A-1F532C3B6F46"; // UUID for the service
const buttonU = "8BCD218F-9C34-40AE-A13D-D607CE92EE94"; // UUID for the button
const randomInt = "7701D4B0-9EC1-4299-80ED-E7CC838A280C"; // UUID for random number
const service_UUID = serviceU.toLowerCase();
const button_UUID = buttonU.toLowerCase();
const random_UUID = randomInt.toLowerCase();
let randomN;
let button;
let BLEdevice;
let randomValue; // to hold the random value
//This contains emojis to be shown depending on the random value
const emojis =
[
"😊", // Emoji for value 0-42
"😄", // Emoji for value 43-85
"😃", // Emoji for value 86-127
"😎", // Emoji for value 128-170
"🤩", // Emoji for value 171-213
"😲", // Emoji for value 214-255
];
function setup() {
createCanvas(500, 500);
BLEdevice = new p5ble();
const connectButton = createButton("Connect Device");// create a button that when pressed it connects to the devices
connectButton.position(50, 50);
connectButton.mousePressed(connectDevice);//when presse connect to the Device
const getValue = createButton("GET NEW VALUE");
getValue.position(50, 150);
getValue.mousePressed(controlButton);
}
function draw() {
const backColor = map(randomValue, 0, 255, 0, 100);//mapping the values of the random values to values between 0 and 100 to achieve the combination below without having invalid values
background(backColor + 155, 255 - backColor, 50 + backColor);
textSize(20);
textAlign(CENTER, CENTER);
if (randomValue !== undefined) {
const emojiIndex = Math.floor(map(randomValue, 0, 255, 0, emojis.length));
const emojiToShow = emojis[emojiIndex];
text(emojiToShow, width/2, height/2); // Display the emoji corresponding to randomValue
}
}
function searchForCharacteristics(error, characteristics) {
if (error) {
console.log("Error occurred: ", error);
return;
}
for (let i = 0; i < characteristics.length; i++) {
if (characteristics[i].uuid == button_UUID) {
button = characteristics[i];
console.log("Button Found");
}
if (characteristics[i].uuid == random_UUID) {
randomN = characteristics[i];
console.log("Random Int Found");
}
}
if (!randomN) {
console.log("Random Int Not Found");
}
if (!button) {
console.log("Button Not Found");
}
}
// Function to connect
function connectDevice() {
BLEdevice.connect(service_UUID, searchForCharacteristics);
}
// Function to toggle the button status
function changeButtonStatus(error, data) {
if (error)
{
console.log("An error happened: ", error);
return;
}
let newData = !data;
BLEdevice.write(button, newData, checkError);
//console.log("The button is set to: ",!data);
retrieveValue();
}
function checkError(error)
{
if(error)
{
console.log("Error writing button value");
return;
}
}
// Function to control the button
function controlButton() {
if (!button || !randomN) {
console.log("Characteristics not found yet");
return;
}
BLEdevice.read(button, changeButtonStatus); // Read the button status and toggle it
}
// Function to retrieve the random value
function retrieveValue() {
BLEdevice.read(randomN, checkValue); // Read the random number
}
// Function to collect the value
function checkValue(error, value) {
if (error) {
console.log("Error occurred:", error);
return;
}
randomValue = value; // Read the value to the declared vaiable
console.log("Random Value: ", randomValue);
}
//function to retrieve the button value
function checkButtonValue(error, value) {
if (error) {
console.log("Error occurred:", error);
return;
}
randomValue = value; // Read the value as Uint32
console.log("The button is set to: ", randomValue);
}