Dimming and Keyboard Inputs

Description:

I connected 3 LEDs in parallel and covered them with a diffuser made of styrofoam and bubblewrap. The initial code changes the brightness of the LEDs one at a time through a keyboard input and pulse width modulation (eg: r127). I modified the code so that a user could enter the letters ‘r’, ‘g’ or ‘b’ a number of times to indicate the percentage of brightness that they want that LED to be at. The minimum is 0% and the maximum is 100%, so the users can enter a letter 1 to 10 times to change the value (‘ggg’ corresponds to 30%). I adjusted the code so that entering a letter once turns the LED off. In addition I added the functionality of changing the intensity of multiple LEDs in one line input. The user can specify colors by entering different letters at once (eg: rrgggbbb). If the user doesn’t enter a specific color at all, then that color doesn’t change with that input.

diffuserLED

Components:

  • 1 Arduino
  • 1 Breadboard3 LEDs (blue, red, green)
  • 3 Resistors (220 ohms each)
  • 4 Connecting wires
  • 1 Diffuser (3 styrofoam peanuts + 1 bubble wrap sheet)
  • 1 USB Cable
  • 1 Laptop

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;

float colorVal;

float redVal = 0, blueVal = 0, greenVal = 0;

int redCode, greenCode, blueCode;

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. ‘rrrrgggbb’) to indicate percentage of brightness(1-10). Single letter for turning off:”);

}

void loop () {

  // clear the string

  memset(serInString, 0, 100);

  redVal = 0, blueVal = 0, greenVal = 0;

  redCode = 0, blueCode = 0, greenCode = 0;

  //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’ ) {

  for (int i = 0; i < strlen(serInString); i++) {

    if (serInString[i] == ‘r’) {

      redVal++;

      redCode = 1;

    }

    else if (serInString[i] == ‘g’) {

      greenVal++;

      greenCode = 1;

    }

    else if (serInString[i] == ‘b’) {

      blueVal++;

      blueCode = 1;

    }

  }

  Serial.print(redVal);

  Serial.print(” “);

  Serial.print(greenVal);

  Serial.print(” “);

  Serial.println(blueVal);

  redVal = (redVal/10)*255;

    if (redVal < 26) {

      redVal = 0;

  }

  greenVal = (greenVal/10)*255;

    if (greenVal < 26) {

      greenVal = 0;

  }

  blueVal = (blueVal/10)*255;

    if (blueVal < 26) {

      blueVal = 0;

  }

  Serial.print(“setting colors to “);

  Serial.print(redVal);

  Serial.print(” “);

  Serial.print(greenVal);

  Serial.print(” “);

  Serial.print(blueVal);

  Serial.println();

  serInString[0] = 0;

  if (redCode > 0)

    analogWrite(redPin, redVal);

  if (greenCode > 0)

    analogWrite(greenPin, greenVal);

  if (blueCode > 0)

    analogWrite(bluePin, blueVal);

  }

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

  }

}

 

Leave a Reply