Lab 2 - Digital I/O with Arduino Board + Diffuser
Description
1. Design a diffuser for your RGB LEDs
2. Change the code so that you can control the RGB values with multiple key presses.
Components Used
- Light Emitting Diodes (LED): red, green, and blue
- Resistors (220 ohms) x 3
- Arduino board
- white plastic cup
- kleenex
Arduino Code
/*
* Serial RGB LED
* ---------------
* Serial commands control the brightness of R,G,B LEDs
*
* Command structure is string emotions.
*
* E.g. "angry" sets the red LED to 255, green LED to 0, and blue LED to 0.
*
* Valid emotions are:
* angry (red; r=255, g=0, b=0)
* sad (blue; r=0, g=0, b=255)
* happy (yellow; r=255, g=255, b=0)
* smitten (pink; r=255, g=20, b=147)
* hungry (orange; r=255, g=165, b=0)
*
* Created 6 February 2011
*
* Adapted from serial_led_rgb by Tod E. Kurt <tod@todbot.com> and
* serial_led_rgb_enhanced by Ryan Airpperspach <ryanaip@alumni.rice.edu>
*
* copyright 2011 Emily Wagner emily@ischool.berkeley.edu
*
*/
//include support for manipulating strings.
#include <stdio.h>
#include <string.h>
#define ANGRY "angry"
#define SAD "sad"
#define HAPPY "happy"
#define SMITTEN "smitten"
#define HUNGRY "hungry"
#define MAX_INPUT_LENGTH 100
char serInString[MAX_INPUT_LENGTH]; // array that will hold the characters of the string. 100=100characters;
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 redValue = 127;
int greenValue = 127;
int blueValue = 127;
void setup() {
pinMode(redPin, OUTPUT); //sets the pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
analogWrite(redPin, redValue); //sets red LED to medium brightness
analogWrite(greenPin, greenValue);
analogWrite(bluePin, blueValue);
}
void loop() {
//read the serial port and create a string out of what you read
readSerialString(serInString, MAX_INPUT_LENGTH);
processEmotionCommands(serInString);
resetSerialString(serInString, MAX_INPUT_LENGTH);
delay(100); //wait a bit, for serial data
}
void readSerialString (char *strArray, int maxLength) { //passing in a pointer to the first character in the array
Serial.println("enter an emotion (e.g. 'happy') :");
while (!Serial.available()) { }
delay(10);
int i = 0;
while (Serial.available() && i < maxLength) {
strArray[i++] = Serial.read();
}
}
void resetSerialString (char *strArray, int length) {
for (int i = 0; i < length; i++) {
strArray[i] = '\0';
}
}
void processEmotionCommands(char *emotion) {
Serial.print("setting emotion to ");
Serial.println(emotion);
if(stringsEqual(emotion, ANGRY)) {
analogWrite(redPin, 255);
analogWrite(greenPin, 0);
analogWrite(bluePin, 0);
}
else if(stringsEqual(emotion, SAD)) {
analogWrite(redPin, 0);
analogWrite(greenPin, 0);
analogWrite(bluePin, 255);
}
else if(stringsEqual(emotion, HAPPY)) {
analogWrite(redPin, 255);
analogWrite(greenPin, 255);
analogWrite(bluePin, 0);
}
else if(stringsEqual(emotion, SMITTEN)) {
analogWrite(redPin, 255);
analogWrite(greenPin, 20);
analogWrite(bluePin, 147);
}
else if(stringsEqual(emotion, HUNGRY)) {
analogWrite(redPin, 255);
analogWrite(greenPin, 165);
analogWrite(bluePin, 0);
}
else {
Serial.println("unknown emotion!");
}
}
boolean stringsEqual(char *string1, char *string2) {
if (strlen(string1) != strlen(string2))
return false;
if (strncmp(string1, string2, strlen(string1)) == 0)
return true;
else
return false;
}
Photo
Creator Comments
The diffuser is a little lame, but I was happy with the user input method. The user enters an emotion, like "happy" or "sad" which changes the LED values. The photo shows "smitten." I ran into some timing troubles when reading the serial input string which I "fixed" by adding a delay in the readSerialString function.
(1 vote)