Beverage cabinet update!
Our interactive beverage cabinet is coming along... slowly! Alex and I have been working on the Arduino code for the past ten days or so, but we've hit a number of obstacles along the way and are still working through the kinks.
The cabinet has five switches, one in each compartment. When a bottle is removed, a switch is flipped in that compartment, triggering a music track and light sequence corresponding to that bottle.
Initially, we figured that the code would be relatively straightforward - it's just five switches, a few lights and some tunes, right? Well, yes, but using multiple switches to control multiple light sequences through the MIDI dimmer was a little trickier than anticipated.
Our first step was getting the MIDI dimmer to communicate with the Arduino. We tested this using Rory Nugent's test code.
Next, we programmed the dimmer to begin the light sequence when a switch is turned on, and stop the sequence then the switch is turned off, without delays (which cause problems as the Arduino stops).
When we tested the code using the lightGo() and lightHalt() functions, the light turned on as hoped when the switch is pressed, but didn't begin the incremental dimming up & down.
So, we sought advice and learned that the parts of code involving (switchState ! == lastSwitchState) and if (millis() - previousMillis > interval) were the source of our issues. So, we added arrays for brightness values, switchState, lastSwitchState, previousMillis and interval variables for each switch, ending up with the following code:
MIDI Dimmer code
int midiPin = 1; // digital output pin for the midi dimmer
int lastSwitchState[5]; // previous state of the switch
int lightToggle[5]; //DOES THIS NEED TO BE AN ARRAY? Variable for toggling the light at max and min values
int switchState[5]; // current state of each switch
int lastswitchState[5]; // previous state of each switch
int previousMillis[5]; //DOES THIS NEED TO BE LONG?
int brightness[5]; // array of brightnesses for each light
int switchPins[] = {
2,3,4,5,6}; // digital input array to hold the switch pin numbers
// brightness goes from 1-127...not sure what goes in the curly brackets below
//int brightness[] = { ? }
void setup() {
for (int thisChannel = 0; thisChannel < 5; thisChannel++) {
//initialize switch pins as inputs
pinMode(switchPins[thisChannel], INPUT);
pinMode(midiPin, OUTPUT); //initialize midi dimmer as output
Serial.begin(31250); // set MIDI baud rate
}
}
void loop() {
for (int thisChannel = 0; thisChannel < 5; thisChannel++){
// read the switch:
switchState[thisChannel] = digitalRead(switchPins[thisChannel]);
//if the switch has changed,
// compare the switchState to its previous state
if (switchState[thisChannel] != lastswitchState[thisChannel]) {
// if the switch has changed from off to on:
if (switchState[thisChannel] == HIGH) {
// if the current state is HIGH then the switch
// went from off to on:
lightGo(thisChannel, 10); //REPLACE 10 with "interval"
}
else { // else the switch changed, going from on to off:
lightGo(thisChannel, 3); //REPLACE 10 with "interval"
}
// save the current state as the last state,
//for next time through the loop
lastswitchState[thisChannel] = switchState[thisChannel];
}
else { // else the switch didn't change.
// dim all the light channels:
brightness[thisChannel]--;
lightGo(brightness[thisChannel], 30);
}
}
//if the switch is off:
if (switchState == LOW) {
//call the lightHalt function
lightHalt (0);
//lightHalt (1);
//lightHalt (2);
}
*/
}
// data1 should be from 0-5, and tells the dimmer which light group
// data2 should be from 0-127 and represents the brightness
void noteOn(char cmd, char data1, char data2) {
Serial.print(cmd, BYTE);
Serial.print(data1, BYTE);
Serial.print(data2, BYTE);
}
void lightGo (int thisChannel, long interval) { //
if ((millis() - previousMillis[thisChannel]) > interval) {
//if the light is on at all
if (brightness[thisChannel] == 127) {
lightToggle[thisChannel] = 0;
// Serial.println("here");
}
//if light is off
else if (brightness[thisChannel] == 0) {
lightToggle[thisChannel] = 1;
}
if (lightToggle[thisChannel]==1) {
brightness[thisChannel]++;
}
else {
brightness[thisChannel]--;
}
//turn on the light
noteOn(0x90, thisChannel, brightness[thisChannel]);
// save the last time you blinked the light
previousMillis[thisChannel] = millis();
}
}
void lightHalt(int thisChannel) {
//if the switch is off, turn off the light:
noteOn(0x90, thisChannel, 0);
//save the last time the light blinked:
previousMillis[thisChannel] = millis();
}
We're still working on figuring out and finishing this code and making it work. In the meantime, we've hard-coded everything in the hope of making it work.
MP3 Trigger
The MP3 Trigger allowed two ways for us to play MP3 files through Arduino: we could either use the 7 trigger pins on the board to directly trigger 7 pre-selected tracks, or use serial communication through Arduino to enable remote triggering of all tracks on the board, with the added advantage of volume control. We opted for the latter - however, the MIDI dimmer was already using the serial port, so we got the MP3 Trigger functioning through software serial.
We loaded our tracks onto the MP3 Trigger, and used the XX library to link the songs to specific switches.
Switches = Wicked Witches!
We tried out one switch to see if the music and lights would work together and after a bit of troubleshooting, it worked! However, when we tried the cabinet with more than one switch, everything went a bit crazy.
And that's where we are right now. We've worked our posteriors off, we've learned a TON, and we're pretty certain that we're almost there.
Images/video of our progress shall be forthcoming. But first, sleep. And breakfast.