Lab 2

Submitted by zwasson on Tue, 02/12/2013 - 18:46

Description

The purpose of this lab was to create an LED diffuser using red, green, and blue LEDs. A diffuser was created using a packing peanut and a ping pong ball as seen in the pictures below (cyan.png and violet.png). Using PWM, the intensity of each LED is varied, which is mixed due to the diffuser. The Arduino is controlled via Serial commands from the host computer.

The supported commands can be seen in the picture below (help.png), but basically you can control the colors by setting red, green, or blue to a specific value or by setting all LEDs at once to a predefined color. Other possible commands can be seen in the image. There are various error checks for invalid commands and invalid colors; however, it may be possible to get unpredictable behavior with certain inputs. There's also an undocumented "strobe" command that pulses random colors every 100ms, perfect for your classroom rave.

Components

  • Arduino Uno
  • Red LED
  • Green LED
  • Blue LED
  • 3 220ohm resistors
  • Packing Peanut
  • Ping Pong Ball

Code

/*
  Lab2
  This lab uses serial communication to set RGB values for 3 LEDs.
  
  Zach Wasson
 */
 
// LED definitions with associated pins
int red_led = 11;
int green_led = 10;
int blue_led = 9;
 
// RGB values
int red_val = 128;
int green_val = 128;
int blue_val = 128;
 
// color strings and RGB values
// note the index of the string and RGB value must be the same
char * color_string[] = {"red", "green", "blue", 
                         "magenta", "yellow", "cyan", 
                         "orange", "chartreuse_green", "spring_green",
                         "azure", "violet", "rose",  
                         "\0"};
char * color_rgb[] = {"(255,0,0)", "(0,255,0)", "(0,0,255)", 
                      "(255,0,255)", "(255,255,0)", "(0,255,255)", 
                      "(255,127,0)", "(127,255,0)", "(0,255,127)",
                      "(0,127,255)", "(127,0,255)", "(255,0,127)",
                      "\0"};
 
char input[64];
 
// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pins as outputs.
  pinMode(red_led, OUTPUT);  
  pinMode(green_led, OUTPUT); 
  pinMode(blue_led, OUTPUT); 
  
  Serial.begin(9600);
  Serial.println("Enter color command: ");
}
 
// the loop routine runs over and over again forever:
void loop() {
  Serial.print("> ");
  
  // read a string from serial port
  int i = 0;
  char c;
  while(true) {
    if(Serial.available()) {
      c = Serial.read();
      if(c == '\n')
        break;
      input[i++] = c;
    }
  }
  input[i] = 0;
  Serial.println(input);  
  
  // parse command
  char command[64];
  int j = 0;
  i = 0;
  while(input[i] != '=' && input[i] != '\n') {
    if(input[i] != ' ')
      command[j++] = input[i];
    i++;
  }
  command[j] = 0;      // null-terminate string
  
  // parse value
  char value[64];
  j = 0;
  i++;                  // skip '='
  while(input[i] != '\0') {
    if(input[i] != ' ')
      value[j++] = input[i];
    i++;
  }
  value[j] = 0;         // null-terminate string
 
  // execute command
  if(!strcmp(command, "red") || !strcmp(command, "r")) {
    red_val = atoi(value);
    if(red_val >= 0 && red_val <= 255)
      analogWrite(red_led, red_val);
    else
      Serial.println("\nError: value out of range (0 to 255)\n");
      
  } else if(!strcmp(command, "green") || !strcmp(command, "g")) {
    green_val = atoi(value);
    if(green_val >= 0 && green_val <= 255)
      analogWrite(green_led, green_val);
    else
      Serial.println("\nError: value out of range (0 to 255)\n");
      
  } else if(!strcmp(command, "blue") || !strcmp(command, "b")) {
    blue_val = atoi(value);
    if(blue_val >= 0 && blue_val <= 255)
      analogWrite(blue_led, blue_val);
    else
      Serial.println("\nError: value out of range (0 to 255)\n");
      
  } else if(!strcmp(command, "rgb")) {
    parse_tuple(value);
    // set LEDs
    if(red_val >= 0 && red_val <= 255 && green_val >= 0 && 
       green_val <= 255 && blue_val >= 0 && blue_val <= 255) {
      analogWrite(red_led, red_val);
      analogWrite(green_led, green_val);
      analogWrite(blue_led, blue_val);
    } else {
      Serial.println("\nError: value out of range (0 to 255)\n");
    }
  
  } else if(!strcmp(command, "color")) {
    int i = 0;
    boolean found = false;
    while(strcmp(color_string[i], "\0")) {
      if(!strcmp(value, color_string[i])) {
        found = true;
        break;
      }
      i++;
    }
    if(found == true) {
      parse_tuple(color_rgb[i]);
      if(red_val >= 0 && red_val <= 255 && green_val >= 0 && 
         green_val <= 255 && blue_val >= 0 && blue_val <= 255) {
        analogWrite(red_led, red_val);
        analogWrite(green_led, green_val);
        analogWrite(blue_led, blue_val);
      } else {
        Serial.println("\nError: value out of range (0 to 255)\n");
      }
    } else {
      Serial.println("\nError: color not found (type 'help' to see the available colors)\n");  
    }
    
  } else if(!strcmp(command, "random")) {
    red_val = random(0, 256);
    green_val = random(0, 256);
    blue_val = random(0, 256);
    analogWrite(red_led, red_val);
    analogWrite(green_led, green_val);
    analogWrite(blue_led, blue_val);
    
  } else if(!strcmp(command, "strobe")) {
    for(int i = 0; i < 100; i++) {
      red_val = random(0, 256);
      green_val = random(0, 256);
      blue_val = random(0, 256);
      analogWrite(red_led, red_val);
      analogWrite(green_led, green_val);
      analogWrite(blue_led, blue_val);
      delay(100);
    }
    
  } else if(!strcmp(command, "off")) {
    analogWrite(red_led, 0);
    analogWrite(green_led, 0);
    analogWrite(blue_led, 0);
    
  } else if(!strcmp(command, "help")) {
    int i = 0;
    Serial.println();
    Serial.println("Usage: <command> = <value>");
    Serial.println("Available commands:");
    Serial.println("  'red' or 'r'\t  Set the red value. <value> should be an integer in [0, 255].");
    Serial.println("  'green' or 'g'  Set the green value. <value> should be an integer in [0, 255].");
    Serial.println("  'blue' or 'b'\t  Set the blue value. <value> should be an integer in [0, 255].");
    Serial.println("  'rgb'\t\t  Set the RGB value. <value> should be an RGB tuple of the form '(r,g,b)'.");
    Serial.println("  'color'\t  Set the color. <value> should be an available color.");
    Serial.println("  'random'\t Set the color to something random. No <value> is specified.");
    Serial.println("  'off'\t\t  Turn off all LEDs. No <value> is specified.");
    Serial.println("Available colors:");
    while(strcmp(color_string[i], "\0")) {
      Serial.print("  ");
      Serial.println(color_string[i++]);
    }
    Serial.println();
    
  } else {
    Serial.println();
    Serial.println("Error: unrecognized command (type 'help' for a list of commands)");
    Serial.println();
  }
}
 
// parses RGB tuple of the form '(r,g,b)'
void parse_tuple(char *tuple) {
  char temp[10];
  int i = 1;          // skip '('
  int j = 0;
  // parse red
  while(tuple[i] != ',') {
    temp[j++] = tuple[i++];
  }
  temp[j] = 0;
  red_val = atoi(temp);
  i++;
  j = 0;
  // parse green
  while(tuple[i] != ',') {
    temp[j++] = tuple[i++];
  }
  temp[j] = 0;
  green_val = atoi(temp);
  i++;
  j = 0;
  // parse blue
  while(tuple[i] != ')') {
    temp[j++] = tuple[i++];
  }
  temp[j] = 0;
  blue_val = atoi(temp);
}
cyan.jpg
help.png
violet.jpg
0
Your rating: None
Drupal theme by Kiwi Themes.