Lab 2

victorpras's picture

Lab 2

By Victor Tjhia

Description

-          Finding a good diffuser for LED

-          Increasing LED’s brightness every time pressing a button in keyboard

-          Showing certain color after pressing a button in keyboard

Components Used

·         3 Light Emitting Diode (LED) RGB

·         Resistor

·         Arduino

·         Solderless Board

·         Plastic cup

·         cotton

Arduino Code

1.diffuser code

/*

 * Serial RGB LED

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

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

 *

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

 * one of "r","g",or "b" and "colorVal" is a number 0 to 255.

 * E.g. "r0"   turns the red LED off. 

 *      "g127" turns the green LED to half brightness

 *      "b64"  turns the blue LED to 1/4 brightness

 *

 * Created 18 October 2006

 * copyleft 2006 Tod E. Kurt <tod@todbot.com

 * http://todbot.com/

 */

 

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

 

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. 'r43') :"); 

}

 

void loop () {

  // clear the string

  memset(serInString, 0, 100);

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

    colorVal = atoi(serInString+1);

    Serial.print("setting color ");

    Serial.print(colorCode);

    Serial.print(" to ");

    Serial.print(colorVal);

    Serial.println();

    serInString[0] = 0;                   // indicates we've used this string

    if(colorCode == 'r')

      analogWrite(redPin, colorVal);

    else if(colorCode == 'g')

      analogWrite(greenPin, colorVal);

    else if(colorCode == 'b')

      analogWrite(bluePin, colorVal);

  }

 

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

  }

}

2. increase brightness by keypress

/*

 * RGBKeypress

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

 * by Victor Tjhia

 * modified from Serial RGB

 * every time 'r' or 'g' or 'b' b button in keyboard is pressed, the brightness increase by 10% until it reach maximum (255)

 */

 

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

 

// initializing color brightness

float redv = 0.0;

float greenv = 0.0;

float bluev = 0.0;

 

void setup() {

  pinMode(redPin,   OUTPUT);   // sets the pins as output

  pinMode(greenPin, OUTPUT);  

  pinMode(bluePin,  OUTPUT);

  Serial.begin(9600);

  analogWrite(redPin,   0);   // set them all to mid brightness

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

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

  Serial.println("enter color command (e.g. 'r') :"); 

}

 

void loop () {

  // clear the string

  memset(serInString, 0, 100);

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

   

    if(colorCode == 'b' && bluev == 255) Serial.print("Color b' brightness already maximum, cannot ");

    if(colorCode == 'r' && redv == 255) Serial.print("Color r' brightness already maximum, cannot ");

    if(colorCode == 'g' && greenv == 255) Serial.print("Color g' brightness already maximum, cannot ");

   

   

    Serial.print("increase color ");

    Serial.print(colorCode);

    Serial.print("'s brightness by 10% ");

    // Serial.print(colorVal);

    Serial.println();

    serInString[0] = 0;                   // indicates we've used this string

    if(colorCode == 'r' && redv < 255)

    {     

      redv += 25.5;

      analogWrite(redPin, redv);

    }

    else if(colorCode == 'g' && greenv < 255)

    {

      greenv += 25.5;

      analogWrite(greenPin, greenv);

    }

    else if(colorCode == 'b' && bluev < 255)

    {

      bluev += 25.5;

      analogWrite(bluePin, bluev);

    }

   

  }

 

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

  }

}

3. optional assignment

/*

 * Optional assignement

 * display color yellow, purple, and orange when user press 'y', 'p', or 'o' respectively

 */

 

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

 

 

 

void setup() {

  pinMode(redPin,   OUTPUT);   // sets the pins as output

  pinMode(greenPin, OUTPUT);  

  pinMode(bluePin,  OUTPUT);

  Serial.begin(9600);

  analogWrite(redPin,   0);   // set them all to mid brightness

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

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

  Serial.println("enter color command (e.g. 'r') :"); 

}

 

void loop () {

  // clear the string

  memset(serInString, 0, 100);

  //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' || colorCode == 'o' || colorCode == 'y' || colorCode == 'p' ) {

       

   

    Serial.print("Displaying color ");

    Serial.print(colorCode);

 

   

    Serial.println();

    serInString[0] = 0;                   // indicates we've used this string

    if(colorCode == 'r')

    {

      analogWrite(redPin, 255);

      analogWrite(greenPin, 0);

      analogWrite(bluePin, 0);

    }

   

    else if(colorCode == 'g')  

    {

      analogWrite(greenPin, 255);

      analogWrite(bluePin, 0);

      analogWrite(redPin, 0);

    }

   

    else if(colorCode == 'b')

    {

      analogWrite(bluePin, 255);

      analogWrite(greenPin, 0);

      analogWrite(redPin, 0);

    }

   

    if(colorCode == 'o')

    {

      analogWrite(redPin, 200);

      analogWrite(greenPin, 30);

      analogWrite(bluePin, 0);

    }

   

    if(colorCode == 'y')

    {

      analogWrite(redPin, 250);

      analogWrite(greenPin, 250);

      analogWrite(bluePin, 0);

    }

   

    if(colorCode == 'p')

    {

      analogWrite(redPin, 127.5);

      analogWrite(greenPin, 0);

      analogWrite(bluePin, 127.5);

    }

   

   

  }

 

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

  }

}

22.jpg
21.jpg
0
Your rating: None