setup()
setup() runs once when the page loads. It prepares all UI elements—button, status text, and sensor displays—by creating them and applying styles. Every element gets positioned near the center of the screen (width/2) and is pushed into arrays or stored as globals so they can be updated later in draw() or event handlers.
function setup() {
createCanvas(windowWidth, windowHeight);
textAlign(CENTER, CENTER);
// Botón conectar
connectBtn = createButton('🔗 Conectar Bluetooth');
connectBtn.position(width/2 - 100, 80);
connectBtn.size(200, 50);
connectBtn.mousePressed(toggleBluetooth);
connectBtn.style('font-size', '18px');
connectBtn.style('border-radius', '10px');
connectBtn.style('background-color', '#4CAF50');
connectBtn.style('color', 'white');
connectBtn.style('border', 'none');
connectBtn.style('cursor', 'pointer');
// Estado
statusLabel = createDiv('Estado: Desconectado');
statusLabel.position(width/2 - 150, 150);
statusLabel.style('font-size', '20px');
statusLabel.style('color', '#333');
statusLabel.style('text-align', 'center');
statusLabel.style('width', '300px');
// Labels de distancias
for (let i = 0; i < 4; i++) {
let label = createDiv(`Sensor ${i+1}: -- cm`);
label.position(width/2 - 150, 220 + i * 80);
label.style('font-size', '24px');
label.style('font-weight', 'bold');
label.style('color', '#2196F3');
label.style('background-color', '#E3F2FD');
label.style('padding', '15px');
label.style('border-radius', '8px');
label.style('width', '300px');
label.style('text-align', 'center');
label.style('box-shadow', '0 2px 4px rgba(0,0,0,0.1)');
distanceLabels.push(label);
}
}
Line-by-line explanation (9 lines)
🔧 Subcomponents:
connectBtn = createButton('🔗 Conectar Bluetooth');
Creates an interactive button that will trigger toggleBluetooth() when clicked
connectBtn.style('font-size', '18px');
Sets visual properties of the button (font size, colors, border, cursor)
statusLabel = createDiv('Estado: Desconectado');
Displays current connection state and error messages to the user
for (let i = 0; i < 4; i++) {
Creates and styles four identical display boxes—one for each ultrasonic sensor
createCanvas(windowWidth, windowHeight);- Creates a canvas that fills the entire browser window; p5.js will draw the title and connection indicator here
textAlign(CENTER, CENTER);- Centers all text both horizontally and vertically, used for the title drawn in draw()
connectBtn = createButton('🔗 Conectar Bluetooth');- Creates a clickable button with Bluetooth emoji; mousePressed attaches toggleBluetooth() as its callback
connectBtn.position(width/2 - 100, 80);- Centers the button horizontally and places it 80 pixels from the top
connectBtn.mousePressed(toggleBluetooth);- When clicked, the button calls toggleBluetooth() to initiate or end Bluetooth connection
connectBtn.style('background-color', '#4CAF50');- Sets button background to green; will change to red (#f44336) after connection
statusLabel = createDiv('Estado: Desconectado');- Creates a text box that displays connection status and error messages; updates dynamically
for (let i = 0; i < 4; i++) {- Loops four times to create one display box for each of the four ultrasonic sensors
distanceLabels.push(label);- Adds each newly created sensor display to the distanceLabels array so it can be updated later