diffuser and code for I/O

Assignment: Digital I/O with Arduino Boards + Diffuser

Collaborators:

/* 

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

char colorDIR;

 

int RcolorVal = 1;

int GcolorVal = 1;

int BcolorVal = 1;

int colorVal = 1;

 

int dec = 0;

 

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

 

void setup() {

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

  pinMode(greenPin, OUTPUT);   

  pinMode(bluePin,  OUTPUT);

  Serial.begin(9600);

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

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

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

  Serial.println("enter color command (e.g. 'r+' or 'g+' or 'b+' or 'b-' etc....) :");  

}

 

void loop () {

  //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);  //converst ASCII code to INTERGER VALUES

   colorDIR = serInString[1];

   

    if(colorDIR == '+'){

      dec = + 10; 

    }

   

    if(colorDIR == '-'){

      dec = -10;

    }

    

    if(colorCode == 'r'){

     myPin = redPin; 

          RcolorVal = RcolorVal + dec;

     colorVal = RcolorVal;

 

    }

    

    if(colorCode == 'g'){

    myPin = greenPin; 

        GcolorVal = GcolorVal + dec;

    colorVal = GcolorVal;

 

    }

   

    if(colorCode == 'b'){

    myPin = bluePin;

        BcolorVal = BcolorVal + dec; 

    colorVal = BcolorVal;

 

    }

   

 

   

   if(colorVal < 1){

     colorVal = 1;

     }

   

   if(colorVal > 255){

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

    

    analogWrite(myPin, 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++;

  }

}