User login

Powered by Drupal, an open source content management system

Theory and Practice of Tangible User Interfaces

Stay Away! and Keypress Controlled LEDs

Submitted by bobacita on Wed, 09/17/2008 - 20:56

Assignment: Digital I/O with Arduino Boards + Diffuser

Collaborators:

Description

Designed a few diffusers for the RGB LEDs and updated the code to control the RGB values with multiple key presses.

For the diffusers I experimented with fabric softener, which initially seemed to be a good option, until I spotted the top of a bathtub scrubber head and twisted it off, fastening two drywall screws in the holes to create legs to stand around the breadboard.  The fabric softener on top of the scrubber head didn't add much, so I scrapped that idea, and played with the inside of an empty toilet paper roll.

The code for the required portion of the assignment takes in a series of consecutive keypresses and increases or decreases the brightness of the LEDs depending on whether the letters 'r', 'g', and 'b' are uppercase (increase) or lowercase (decrease).

For the optional portion, I changed the program to work as a health indicator, shining green on a person's desk if they're healthy and red if they're sick (contagious).  This way, the sick person can warn other people that he or she is germy.

Components Used

 

  • 3 LEDs
  • 3 resistors
  • 3 wires for power
  • 1 wire for ground
  • 1 bathtub scrubber top half
  • 1 empty toilet paper roll
  • 2 drywall screws

 

Arduino Code

Keypress Program

 

/* 

 * Serial RGB LED

 * ---------------

 * Serial commands control the brightness of R,G,B LEDs 

 *

 * Command structure is "<colorCode>*", where "colorCode" is

 * one of "R", "r", "G", "g", "B", or "b"

 *

 * Created 15 September 2008

 * copyright 2008 Carol Chen

 */

 

#include <string.h>

 

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 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);   // set them all to mid brightness

  analogWrite(greenPin, blueValue);   // set them all to mid brightness

  analogWrite(bluePin,  greenValue);   // set them all to mid brightness

  Serial.println("Enter color command for any combo of the colors red, green, and blue.");

  Serial.print("Uppercase for increasing brightness,");

  Serial.println(" lowercase for dimming.  (e.g. 'rr', 'RRGbbR') :"); 

}

 

void loop () {

  //read the serial port and create a string out of what you read

  readSerialString(serInString);

 

  processRepeatKeyCommands(serInString, 100); 

 

  //Erase anything left in the serial string, preparing it for the 

  //next loop

  resetSerialString(serInString);

 

  delay(100);  // wait a bit, for serial data

}

 

void processRepeatKeyCommands(char *strArray, int maxLength) {

  int i = 0;

 

  //loop through the string (strArray)

  //i = the current position in the string

  //Stop when either (a) i reaches the end of the string or

  //                 (b) there is an empty character '\0' in the string

  while (i < maxLength && strArray[i] != '\0') {

    //Read in the character at position i in the string

    colorCode = serInString[i];

 

    //If the character is R (increase red)...

    if (colorCode == 'R') {

      //Increase the current red value by 25, and if you surpass 255 stay at 255

      redValue = redValue + 25;

      if (redValue > 255) {

        redValue = 255;

      }

      analogWrite(redPin, redValue);

      Serial.print("Increasing red to ");

      Serial.println(redValue);

 

  //If the character is r (dim red)    

    } else if (colorCode == 'r') {

      // Decrease the current red value by 25, and if you dip below 0, set the value to 0

      redValue = redValue - 25;

      if (redValue < 0) {

        redValue = 0;

      }

      analogWrite(redPin, greenValue);

      Serial.print("Dimming red to ");

      Serial.println(redValue);

 

    //If the character is G (increase green)...

    } else if (colorCode == 'G') {

      greenValue = greenValue + 25;

      if (greenValue > 255) {

        greenValue = 255;

      }

      analogWrite(greenPin, greenValue);

      Serial.print("Increasing green to ");

      Serial.println(greenValue);

 

    //If the character is g (dim green)...

    } else if (colorCode == 'g') {

      greenValue = greenValue - 25;

      if (greenValue < 0) {

        greenValue = 0;

      }

      analogWrite(greenPin, greenValue);

      Serial.print("Dimming green to ");

      Serial.println(greenValue);

 

    //If the character is B (increase blue)...

    } else if (colorCode == 'B') {

      blueValue = blueValue + 25;

      if (blueValue > 255) {

        blueValue = 255;

      }

      analogWrite(bluePin, blueValue);

      Serial.print("Increasing blue to ");

      Serial.println(blueValue);

 

    //If the character is b (dim blue)..S.

    } else if (colorCode == 'b') {

      blueValue = blueValue - 25;

      if (blueValue < 0) {

        blueValue = 0;

      }

      analogWrite(bluePin, blueValue);

      Serial.print("Dimming blue to ");

      Serial.println(blueValue);

    }

    //Move on to the next character in the string

    //From here, the code continues executing from the "while" line above...

    i++;

  }

}

 

void resetSerialString (char *strArray) {

  for (int i = 0; i < 11; i++) {

    strArray[i] = '\0';

  }

}

 

//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++;

  }

}

 

Health Indicator Program

 

/* 

 * Serial RGB LED - Health Indicator

 * ---------------------------------

 * Serial commands send visual cues to others about whether you are sick

 * and they should keep their distance

 *

 * Command structure is "set sick" or "set healthy"

 *

 * Created 16 September 2008

 * copyright 2008 Carol Chen

 */

 

#include <string.h>

 

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

 

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, 0);

  analogWrite(greenPin, 0);

  analogWrite(bluePin, 0);

  Serial.println("What is your health level?  'set sick' or 'set healthy'? : ");

}

 

void loop () {

  //read the serial port and create a string out of what you read

  readSerialString(serInString);

 

  // Set LEDs to indicate whether you are contagious

  processHealthCommand(serInString, 100);

 

  //Erase anything left in the serial string, preparing it for the 

  //next loop

  resetSerialString(serInString);

 

  delay(100);  // wait a bit, for serial data

}

 

// "set sick" sets the red LED to full brightness (stop metaphor)

// "set healthy" sets the green LED to full brightness (go metaphor)

void processHealthCommand(char *strArray, int length) {

  if( stringsEqual(strArray, "set sick", 9)) {

    analogWrite(redPin, 255);

    analogWrite(greenPin, 0);

    analogWrite(bluePin, 0);

    Serial.println("Change to sick status complete.");

  } else if( stringsEqual(strArray, "set healthy", 12)) {

    analogWrite(redPin, 0);

    analogWrite(greenPin, 255);

    analogWrite(bluePin, 0);

    Serial.println("Change to healthy status complete.");

  }

}

 

void resetSerialString (char *strArray) {

  for (int i = 0; i < 11; i++) {

    strArray[i] = '\0';

  }

}

 

//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++;

  }

}

 

//compare two strings to see if they are equal

//compares the first 'numCharacters' characters of string1 and string2 to

//see if they are the same

//

//E.g. stringsEqual("hello","hello",5) => true

//     stringsEqual("hello","helaabbnn",3) => true

//     stringsEqual("hello","helaa",5) => false

boolean stringsEqual(char *string1, char *string2, int numCharacters) {

  if (strncmp(string1, string2, numCharacters) == 0) {

    return true;

  } else {

    return false;

  }

}

 

Photos attached