Description
In this lab we explored some of the digital and serial features of the Arduino Uno. Specifically, we looked at Pulse Width Modulation (PWM). The three LED’s brightness were controlled using PWM (R = pin9, G= pin10, B= pin11). The USB port was used for serial communication between the Arduino and computer (serial monitor). By adjusting the delay time between the square pulses sent we are able to adjust the LED brightness through serial communication. Example sketches were used to fade three LED’s in parallel; from this we changed the codes for different effects on the brightness of the LED’s through serial communication. A diffuser was made from a sports bottle cap (see attached image).
For the HW and optional assignment I edited the code to create a light show using the 3 LED’s and serial communication. The USER uses the “A” and “D” key for left and right movement respectively and “W” and “S” for increasing and decreasing brightness. The user enters a sequence of letters using those 4 keys in any order up to 100 characters long. Once ENTER has been pressed the commands are read and initiated. The RED light is the start point and the light selections move left and right and brightness is adjusted up and down in accordance to the command string entered by the user.
An example input is “AAAWWWSDDS”. This would be Left, Left, Left, Bright, Bright, Bright, Dim, Right, Right, Dim.
Components
1- Arduino Uno
1- Breadboard
1- USB Cable
3- 220 ohm resistors
3 – LEDs (Red, Green, Blue)
1- Sports Water Bottle Top (diffuser)
4 – Jumper wires (3 white, 1 red)
Code
/*
* Serial RGB LED
* ---------------
* Serial commands control the selection and brightness of R,G,B LEDs
*
* Command structure is "<direction><colorVal_step>", where "direction" is
* one of "a","d" for LEFT and RIGHT and "colorVal_step" is a "w", "s" stepping 0 to 255.
* E.g. "aasdwsww" Move left 2 LED's, dim down, move right an LED, bright, dim, bright, bright.
* Software will read a input of a 100 commands
*
* Created 21 September 2013
* Michael Hemati
*/
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 command;
char LED_status = 'r';
int LEDstep = 25;
int comm_Length = 0;
int rval = 127;
int gval = 127;
int bval = 127;
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);
int k = 0;
for (k=0;k<4;k++){
analogWrite(redPin, 255); // set them all to no brightness
analogWrite(greenPin, 0); // set them all to no brightness
analogWrite(bluePin, 0); // set them all to no brightness
delay (150);
analogWrite(redPin, 0); // set them all to no brightness
analogWrite(greenPin, 255); // set them all to no brightness
analogWrite(bluePin, 0); // set them all to no brightness
delay (150);
analogWrite(redPin, 0); // set them all to no brightness
analogWrite(greenPin, 0); // set them all to no brightness
analogWrite(bluePin, 255); // set them all to no brightness
delay (150);
analogWrite(redPin, 255); // set them all to no brightness
analogWrite(greenPin, 0); // set them all to no brightness
analogWrite(bluePin, 0); // set them all to no brightness
}
Serial.println("Use (A Left), (D Right), (W Bright), (S Dim) , (X, RESET)");
}
void loop () {
// clear the string
memset(serInString, 0, 100);
//read the serial port and create a string out of what you read
readSerialString(serInString);
comm_Length = strlen(serInString);
if (comm_Length > 100) {
Serial.println("Error: Too long of a sequence");
}
else {
int j;
for (j=0;j<comm_Length+1;j++) {
command = serInString[j];
if( command == 'd' && LED_status == 'r') {
analogWrite(greenPin, gval);
analogWrite(redPin, 0);
LED_status = 'g';
delay (250);
}
else if (command == 'd' && LED_status == 'g'){
analogWrite(greenPin, 0);
analogWrite(bluePin,bval);
LED_status = 'b';
delay (250);
}
else if (command == 'd' && LED_status == 'b'){
analogWrite(bluePin, 0);
analogWrite(redPin, rval);
LED_status = 'r';
delay (250);
}
else if( command == 'a' && LED_status == 'r') {
analogWrite(bluePin, bval);
analogWrite(redPin, 0);
LED_status = 'b';
delay (250);
}
else if (command == 'a' && LED_status == 'g'){
analogWrite(greenPin, 0);
analogWrite(redPin,rval);
LED_status = 'r';
delay (250);
}
else if (command == 'a' && LED_status == 'b'){
analogWrite(bluePin, 0);
analogWrite(greenPin, gval);
LED_status = 'g';
delay (250);
}
else if (command == 'w' && LED_status == 'g'){
gval = gval+LEDstep;
if (gval > 255){
gval = 0;
}
if (gval < 0) {
gval = 255;
}
analogWrite(greenPin, gval);
LED_status = 'g';
delay (250);
}
else if (command == 'w' && LED_status == 'r'){
rval = rval + LEDstep;
if (rval > 255){
rval = 0;
}
if (rval < 0) {
rval = 255;
}
analogWrite(redPin, rval);
LED_status = 'r';
delay (250);
}
else if (command == 'w' && LED_status == 'b'){
bval = bval+LEDstep;
if (bval > 255){
bval = 0;
}
if (bval < 0) {
bval = 255;
}
analogWrite(bluePin, bval);
LED_status = 'b';
delay (250);
}
else if (command == 's' && LED_status == 'g'){
gval = gval-LEDstep;
if (gval > 255){
gval = 0;
}
if (gval < 0) {
gval = 255;
}
analogWrite(greenPin, gval);
LED_status = 'g';
delay (250);
}
else if (command == 's' && LED_status == 'r'){
rval = rval-LEDstep;
if (rval > 255){
rval = 0;
}
if (rval < 0) {
rval = 255;
}
analogWrite(redPin, rval);
LED_status = 'r';
delay (250);
}
else if (command == 's' && LED_status == 'b'){
bval = bval-LEDstep;
if (bval > 255){
bval = 0;
}
if (bval < 0) {
bval = 255;
}
analogWrite(bluePin, bval);
LED_status = 'b';
delay (250);
}
}
}
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