Glazzzzz

This is a project in collaboration with Oryan Inbar and Natalia Cabrera.

We created a musical interface using five glass cups, each on an individual holder equipped with an LED and a tiny Vibrating Motor Disc, controlled wirelessly through a smartphone. We implemented Bluetooth LE capabilities based on Don Coleman’s code and guidance and then created a simple iOS app using Phonegap and Cordova that controls the sounds/lights/vibes. The glasses have different levels of liquid in them in an attempt to create different sounds. The LEDs correspond the colors of the buttons in the app.

K47JLb
JvM45T

Here is the repo with the the RFduino code as well as the phonegap code: https://github.com/smorad-/led-sound-system

Arduino code:

#define LED1_PIN 2 // RedBear Blend & RFduino
#define LED2_PIN 3
#define LED3_PIN 4
#define LED4_PIN 5
#define LED5_PIN 6

// #define LED_PIN A3 // RedBear Nano

// Import libraries (BLEPeripheral depends on SPI)
#include <SPI.h>
#include <BLEPeripheral.h>

// define pins (varies per shield/board)
// https://github.com/sandeepmistry/arduino-BLEPeripheral#pinouts
// Blend
#define BLE_REQ 9
#define BLE_RDY 8
#define BLE_RST 5

// create peripheral instance, see pinouts above
BLEPeripheral blePeripheral = BLEPeripheral(BLE_REQ, BLE_RDY, BLE_RST);

// create service
BLEService ledService = BLEService(“AAB4”);

// create switch characteristic
BLECharCharacteristic switchCharacteristic 
 = BLECharCharacteristic(“AAB5”, BLERead | BLEWrite);
BLEDescriptor switchDescriptor = BLEDescriptor(“2901”, “Pattern”);
void setup() {
Serial.begin(9600);

// set LED pin to output mode
pinMode(LED1_PIN, OUTPUT);
pinMode(LED2_PIN, OUTPUT);
pinMode(LED3_PIN, OUTPUT);
pinMode(LED4_PIN, OUTPUT);
pinMode(LED5_PIN, OUTPUT);

// set advertised local name and service UUID
blePeripheral.setLocalName(“COSO”);
blePeripheral.setDeviceName(“COSO”);
blePeripheral.setAdvertisedServiceUuid(ledService.uuid());

// add service and characteristics
blePeripheral.addAttribute(ledService);
blePeripheral.addAttribute(switchCharacteristic);
blePeripheral.addAttribute(switchDescriptor);
// assign event handlers for characteristic
switchCharacteristic.setEventHandler(BLEWritten, switchCharacteristicWritten);

// begin initialization
blePeripheral.begin();

Serial.println(F(“Bluetooth LED”));
}

void loop() {
// Tell the bluetooth radio to do whatever it should be working on
blePeripheral.poll();
}

void switchCharacteristicWritten(BLECentral& central, 
BLECharacteristic& characteristic) {
// central wrote new value to characteristic, update LED
// Serial.print(F(“Characteristic event, written: “));
if (switchCharacteristic.value() == 1) {
Serial.println(switchCharacteristic.value());
Serial.println(F(“LED 1 on”));
digitalWrite(LED1_PIN, HIGH);
digitalWrite(LED2_PIN, LOW);
digitalWrite(LED3_PIN, LOW);
digitalWrite(LED4_PIN, LOW);
digitalWrite(LED5_PIN, LOW);
delay(1000);
digitalWrite(LED1_PIN, LOW);

} else if (switchCharacteristic.value() == 2) {
Serial.println(switchCharacteristic.value());
Serial.println(F(“LED 2 on”));
digitalWrite(LED2_PIN, HIGH);
digitalWrite(LED1_PIN, LOW);
digitalWrite(LED3_PIN, LOW);
digitalWrite(LED4_PIN, LOW);
digitalWrite(LED5_PIN, LOW);
delay(1000);
digitalWrite(LED2_PIN, LOW);

} else if (switchCharacteristic.value() == 3) {
Serial.println(switchCharacteristic.value());
Serial.println(F(“LED 2 on”));
digitalWrite(LED3_PIN, HIGH);
digitalWrite(LED1_PIN, LOW);
digitalWrite(LED2_PIN, LOW);
digitalWrite(LED4_PIN, LOW);
digitalWrite(LED5_PIN, LOW);
delay(1000);
digitalWrite(LED3_PIN, LOW);

} else if (switchCharacteristic.value() == 4) {
Serial.println(switchCharacteristic.value());
Serial.println(F(“LED 2 on”));
digitalWrite(LED4_PIN, HIGH);
digitalWrite(LED1_PIN, LOW);
digitalWrite(LED2_PIN, LOW);
digitalWrite(LED3_PIN, LOW);
digitalWrite(LED5_PIN, LOW);
delay(1000);
digitalWrite(LED4_PIN, LOW);

} else if (switchCharacteristic.value() == 5) {
Serial.println(switchCharacteristic.value());
Serial.println(F(“LED 2 on”));
digitalWrite(LED5_PIN, HIGH);
digitalWrite(LED1_PIN, LOW);
digitalWrite(LED2_PIN, LOW);
digitalWrite(LED3_PIN, LOW);
digitalWrite(LED4_PIN, LOW);
delay(1000);
digitalWrite(LED5_PIN, LOW);
}
}
  Phonegap HTML code:
<html>
<head>
<meta charset=”utf-8″>
<meta name=”viewport” content=”initial-scale=1, >
<maximum-scale=1, user-scalable=no, width=device-width”>
<link rel=”stylesheet” type=”text/css” href=”css/index.css” />
<title>Patterns</title>
</head>
<body>
<div>
<h2>Patterns</h2>
<div id=”mainPage”>
<ul id=”deviceList”>
</ul>
<button id=”refreshButton”>Refresh</button>
</div>
<div id=”detailPage”>
<button id=”button_1″ onclick=”app.turnOn(1)”>&nbsp;</button>
<button id=”button_2″ onclick=”app.turnOn(2)”>&nbsp;</button>
<button id=”button_3″ onclick=”app.turnOn(3)”>&nbsp;</button>
<button id=”button_4″ onclick=”app.turnOn(4)”>&nbsp;</button>
<button id=”button_5″ onclick=”app.turnOn(5)”>&nbsp;</button>
<button id=”disconnectButton”>Disconnect</button>
</div>
</div>
<script type=”text/javascript” src=”cordova.js”></script>
<script type=”text/javascript” src=”js/fastclick.js”></script>
<script type=”text/javascript” src=”js/disconnector.js”></script>
<script type=”text/javascript” src=”js/index.js”></script>
</body>
</html>

 

Javascript code:

var LED_SERVICE = ‘AAB0’; var SWITCH_CHARACTERISTIC = ‘AAB1’; var app = { initialize: function() { this.bindEvents(); this.showMainPage(); }, bindEvents: function() { document.addEventListener(‘deviceready’, this.onDeviceReady, false); document.addEventListener(‘backbutton’, this.onBackButton, false); deviceList.addEventListener(‘click’, this.connect, false); refreshButton.addEventListener(‘click’, this.refreshDeviceList, false); // onButton.addEventListener(‘click’, this.switchOn, false); // offButton.addEventListener(‘click’, this.switchOff, false); disconnectButton.addEventListener(‘click’, this.disconnect, false); }, onDeviceReady: function() { FastClick.attach(document.body); // https://github.com/ftlabs/fastclick app.refreshDeviceList(); }, refreshDeviceList: function() { deviceList.innerHTML = ”; // empty the list ble.scan([LED_SERVICE], 5, app.onDiscoverDevice, app.onError); }, onDiscoverDevice: function(device) { var listItem = document.createElement(‘li’); listItem.innerHTML = device.name + ‘
’ + device.id + ‘
’ + ‘RSSI: ‘ + device.rssi; listItem.dataset.deviceId = device.id; deviceList.appendChild(listItem); }, connect: function(e) { var deviceId = e.target.dataset.deviceId; ble.connect(deviceId, app.onConnect, app.onError); }, onConnect: function(peripheral) { app.peripheral = peripheral; app.showDetailPage(); }, disconnect: function(e) { if (app.peripheral && app.peripheral.id) { ble.disconnect(app.peripheral.id, app.showMainPage, app.onError); } }, turnOn: function(number) { app.setSwitchValue(number); }, // switchOn: function() { // app.setSwitchValue(2); // }, // switchOff: function() { // app.setSwitchValue(0); // }, setSwitchValue: function(value) { var success = function() { console.log(‘Set switch value to ‘ + value); }; if (app.peripheral && app.peripheral.id) { var data = new Uint8Array(1); data[0] = value; ble.write( app.peripheral.id, LED_SERVICE, SWITCH_CHARACTERISTIC, data.buffer, success, app.onError ); } }, showMainPage: function() { mainPage.hidden = false; detailPage.hidden = true; }, showDetailPage: function() { mainPage.hidden = true; detailPage.hidden = false; }, onBackButton: function() { if (mainPage.hidden) { app.disconnect(); } else { navigator.app.exitApp(); } }, onError: function(reason) { navigator.notification.alert(reason, app.showMainPage, ‘Error’); } }; app.initialize();

Leave a Reply