Color Mixing & Diffuser
Description
In this lab, we explore some of the digital features of the Arduino Board. Specifically, we’ll be looking at:
- Pulse Width Modulation (PWM) which “fakes” analog behavior using digital signals
- Serial communication with the laptop allowing for greater design flexibility
In exploring these features, we will use Arduino to not just blink a single LED, but to control and fade several LEDs at once.
For the first component of the lab, I created a diffuser using a kleenex box, tracing paper, cotton, and tape.
For the second part of the lab, I altered the code so that pressing 'r' enter will increase the value by 30. Entering a value of the same color will increase the value by that amount. Entering a value for a different color will set that color to input value.
Components Used
1 - Arduino Uno
3 - 220 ohm resistors
1 - Red LED
1 - Blue LED
1 - Green LED
1 - Breadboard
1 - Tracing Paper
1- Kleenex Box
Cotton
Scissors
Tape
Code
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 newVal;
char lastColor;
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. 'r43') :");
}
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];
if( colorCode == 'r' || colorCode == 'g' || colorCode == 'b' ) {
//my code
// getting new color value
newVal = atoi(serInString+1);
// set color value to 30 if no input
if(newVal == 0){
newVal = 30;
}
// check if the last color code is the same and increment value
// if not reset color val to the input
if(lastColor == colorCode){
colorVal += newVal;
} else {
colorVal = newVal;
}
// save last used color code so we can check every loop
lastColor = colorCode;
// don't let color value go over 255
if(colorVal > 255){
colorVal = 255;
}
Serial.print("setting your awesome color ");
Serial.print(colorCode);
Serial.print(" to ");
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++;
}
}
- Login to post comments
Drupal theme by Kiwi Themes.