Lab 2

carlos.sandoval's picture

I originally parametrically designed a diffuser to be laser cutted in acrylic, but I had some problems with the access to the CAD/CAM lab at the CED. I did an alternative diffuser by folding paper.

-Code:

/*
 * Serial RGB LED
 * ---------------
 * Serial commands control the brightness of R,G,B LEDs
 *
 * Created 18 October 2006

 * 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

int redColorVal = 0; // Red Color Value
int greenColorVal = 0; // Green Color Value
int blueColorVal = 0; // Blue Color Value


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 zero brightness
  analogWrite(greenPin, 0);   // set them all to zero brightness
  analogWrite(bluePin,  0);   // set them all to zero brightness
  Serial.println("set the brightness foreach color (every time you type the r,g, or b, color increases 5%) :");  
}

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);
    
    serInString[0] = 0;                   // indicates we've used this string
    if(colorCode == 'r') {
      redColorVal = redColorVal + 13;
      if(redColorVal > 255){
        redColorVal = 0;
      }
      Serial.print("Setting Red Color to ");
      Serial.print(redColorVal);
      analogWrite(redPin, redColorVal);
      
    }else if(colorCode == 'g'){
      greenColorVal = greenColorVal + 13;
      if(greenColorVal > 255){
        greenColorVal = 0;
      }
      Serial.print("Setting Green Color to ");
      Serial.print(greenColorVal);
      analogWrite(greenPin, greenColorVal);
    }
      else if(colorCode == 'b'){
      blueColorVal = blueColorVal + 13;
      if(blueColorVal > 255){
        blueColorVal = 0;
      }
      Serial.print("Setting Green Color to ");
      Serial.print(blueColorVal);
      analogWrite(bluePin, blueColorVal);    
      }
   Serial.println();
  }
       

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

difusser
0
Your rating: None