Skip to content

Instantly share code, notes, and snippets.

@AnnaGerber
Created June 17, 2013 12:19
Show Gist options
  • Save AnnaGerber/5796459 to your computer and use it in GitHub Desktop.
Save AnnaGerber/5796459 to your computer and use it in GitHub Desktop.
#include <Wire.h>
// pwm pins for red, green and blue
int rled = 9;
int gled = 10;
int bled = 11;
char currentColor = 'r';
/*
TODO: support hex values for rgb
int rledval = 255;
int gledval = 255;
int bledval = 255;
*/
void setup()
{
// set up pwm pins for RGB LED
pinMode(rled, OUTPUT);
pinMode(bled, OUTPUT);
pinMode(gled, OUTPUT);
// join i2c bus with address #2 - change for each cell
Wire.begin(2);
Wire.onReceive(receiveEvent);
}
void loop()
{
delay(100);
}
/*
* operations are:
* o 0|1 off/on
* c <color> set colour
* l 0|1 lock
*/
void receiveEvent(int howMany)
{
char op = Wire.read(); // read op
int arg = Wire.read(); // read argument(s)
switch(op) {
case 'o': rgbOn(arg); break;
case 'c': rgbSetColor(arg); break;
case 'l': doorLock(arg); break;
default: break;
}
}
void doorLock(char lock){
if (lock == 0){
// control solenoid to unlock door
} else {
// control solenoid to lock door
}
}
void rgbSetColor(char color){
currentColor = color;
digitalWrite(rled, LOW);
digitalWrite(gled, LOW);
digitalWrite(bled, LOW);
switch(color){
case 'r': digitalWrite(rled, HIGH); break;
case 'g': digitalWrite(gled, HIGH); break;
case 'b': digitalWrite(bled, HIGH); break;
default: break;
}
}
void rgbOn(char on) {
if (on == 0){
digitalWrite(rled, LOW);
digitalWrite(gled, LOW);
digitalWrite(bled, LOW);
} else {
rgbSetColor(currentColor);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment