Night light

Description

I altered the code to dim LEDs so it now takes input in form of ‘rrr’ or ‘gggggg’. I also tried different diffusers like paper and cloth, in addition to the cotton and styrofoam that we worked with in class. Finally, I settled on an LED lamp – which I removed the batteries from – and held it on top of my breadboard LEDs for the purposes of this assignment. This lamp can be kept in a corner and used as a nightlight, the color of which can be changed on your command.

Components

1 Arduino Uno
3 LEDs
3 220Ω Resistors
1 Breadboard
Jumper wires
USB cable


char serInString[100]; // array that will hold the different bytes of the string. 100=100characters;
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

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. 'rrrr') :");
}

void loop () {
// clear the string
memset(serInString, 0, 100);
//read the serial port and create a string out of what you read
readSerialString(serInString);

colorCode = serInString[0];
colorVal = 0;
if( colorCode == 'r' || colorCode == 'g' || colorCode == 'b' ) {
for (int i=0; i < strlen(serInString); i++) {
if( colorCode == 'r' || colorCode == 'g' || colorCode == 'b' )
{
colorVal += 1;
}
}

colorVal = (colorVal/200.0)*255;
Serial.print("setting color ");
Serial.print(colorCode);
Serial.print(" to intensity: ");
Serial.print(colorVal);
Serial.println();
serInString[0] = 0; // indicates we've used this string
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++;
}
}

IMG_20160912_025841

IMG_20160912_025517

IMG_20160912_025502

Leave a Reply