Go Bears !

Ever since I came to Berkeley and became a part of this beautiful campus and amazing community, I have been fascinated by the Campanile. With this lab, I have tried to recreate the lights part of the Light and Music Show at the Campanile.

It very exciting to think about how much diverse and cutting edge work is happening on these 1232 acres of land and yet at the end of every hour these bells unify us (our awareness)  across the diversity, thus strengthening the sense of community. Go Bears!

Components:

  • Arduino Uno
  • Breadboard
  • 3 LEDs (rgb)
  • 3 220Ω Resistors
  • jumper wires
  • USB cable
  • laptop

Code:

/*
* Serial RGB LED
* —————
* Serial commands control the brightness of R,G,B LEDs
*
* Command structure is “<colorCode(1)><colorCode(n)>”, where “colorCode” is
* one of “r”,”g”,or “b”.
* E.g. “rr” sets the red LED to 20% brightness.
* “gggg” sets the green LED to 40% brightness
* “bbb” sets the blue LED to 30% brightness
*
* Created 13 Sep 2016
*
*/

char serInString[100]; // array that will hold the different bytes of the string. 100=100characters;
// -> you must state how long the array will be else it won’t work properly
char colorCode;
int colorVal;

int redPin = 9; // Red LED, connected to digital pin 9
int greenPin = 10; // Green LED, connected to digital pin 10
int bluePin = 11; // Blue LED, connected to digital pin 11
int lenStr = 0;

void setup() {
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
analogWrite(redPin, 127); // set them all to mid brightness
analogWrite(greenPin, 127); // set them all to mid brightness
analogWrite(bluePin, 127); // set them all to mid brightness
Serial.println(“enter color command (e.g. ‘r43’) :”);
}

void loop () {
//Serial.println(“in the loop”);
// clear the string
memset(serInString, 0, 100);

//read the serial port and create a string out of what you read
readSerialString(serInString);
//Serial.println(serInString);
lenStr = strlen(serInString);
//Serial.println(lenStr);

colorCode = serInString[0];
if( colorCode == ‘r’ || colorCode == ‘g’ || colorCode == ‘b’ ) {
colorVal = int(lenStr * 10 * 2.55);
//Serial.println(colorVal);
Serial.print(“setting color “);
Serial.print(colorCode);
Serial.print(” to “);
Serial.print(colorVal);
Serial.println();
if(colorCode == ‘r’)
analogWrite(redPin, colorVal);
else if(colorCode == ‘g’)
analogWrite(greenPin, colorVal);
else if(colorCode == ‘b’)
analogWrite(bluePin, colorVal);
}

delay(100); // wait a bit, for serial data
}

//read a string from the serial and store it in an array
//you must supply the array variable
void readSerialString (char *strArray) {
int i = 0;
if(!Serial.available()) {
return;
}
while (Serial.available()) {
strArray[i] = Serial.read();
i++;
}
}

Leave a Reply