Lab 2

johns's picture

Description

Use the r, g or b key to increase the light level with 20%

Components used

  • 3 Light Emitting Diode (LED)
  • 3 Resistors
  • 1 Ping pong ball

Arduino 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;
int colorVal;
int colorValRed;
int colorValGreen;
int colorValBlue;
 
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);   
  colorValRed = 0;
  colorValGreen = 0;
  colorValBlue = 0;
  Serial.println("enter color command (r, g or b) :");  
}
 
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 ");
    
    serInString[0] = 0;                   // indicates we've used this string
    if(colorCode == 'r'){ 
      colorValRed=colorValRed+51;
      if(colorValRed > 255){
        colorValRed=0;
        Serial.print(colorValRed);
        analogWrite(redPin, colorValRed);
      }else if(colorValRed < 256){
        Serial.print(colorValRed);
        analogWrite(redPin, colorValRed);
      }
    }else if(colorCode == 'g'){
      colorValGreen=colorValGreen+51;
      if(colorValGreen > 255){
        colorValGreen=0;
        Serial.print(colorValGreen);
        analogWrite(greenPin, colorValGreen);
      }else if(colorValGreen < 256){
        Serial.print(colorValGreen);
        analogWrite(greenPin, colorValGreen);
      }
    }else if(colorCode == 'b'){
      colorValBlue=colorValBlue+51;
      if(colorValBlue > 255){
        colorValBlue=0;
        Serial.print(colorValBlue);
        analogWrite(bluePin, colorValBlue);
      }else if(colorValBlue < 256){
        Serial.print(colorValBlue);
        analogWrite(bluePin, colorValBlue);
      }
    } 
    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++;
  }
}
 

Arduino code

Description: Set the lightsetting to one of the different mood settings of the color mood chart (http://www.color-chart.org/mood-ring-color-chart.php)

 

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 colorValRed;
int colorValGreen;
int colorValBlue;
 
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);   
  colorValRed = 0;
  colorValGreen = 0;
  colorValBlue = 0;
  Serial.println("enter one of the following moods: Pleased, relaxed, excited, agressive, unhappy or subdued");  
}
 
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 == 'e' || colorCode == 'a' || colorCode == 'u' || colorCode == 's' || colorCode == 'p') {
   
    if(serInString[0] == 'r' && serInString[1] == 'e' && serInString[2] == 'l' && serInString[3] == 'a' && serInString[4] == 'x' && serInString[5] == 'e' && serInString[6] == 'd'){
 
      analogWrite(redPin, 64);
      analogWrite(greenPin, 255);
      analogWrite(bluePin, 255);
  
    }else if(serInString[0] == 'e' && serInString[1] == 'x' && serInString[2] == 'c' && serInString[3] == 'i' && serInString[4] == 't' && serInString[5] == 'e' && serInString[6] == 'd'){
      
      analogWrite(redPin, 253);
      analogWrite(greenPin, 253);
      analogWrite(bluePin, 52);
      
    }else if(serInString[0] == 'a' && serInString[1] == 'g' && serInString[2] == 'r' && serInString[3] == 'e' && serInString[4] == 's' && serInString[5] == 's' && serInString[6] == 'i' && serInString[7] == 'v' && serInString[8] == 'e'){
      
      analogWrite(redPin, 253);
      analogWrite(greenPin, 47);
      analogWrite(bluePin, 47);
    } 
    else if(serInString[0] == 'u' && serInString[1] == 'n' && serInString[2] == 'h' && serInString[3] == 'a' && serInString[4] == 'p' && serInString[5] == 'p' && serInString[6] == 'y'){
      
      analogWrite(redPin, 255);
      analogWrite(greenPin, 50);
      analogWrite(bluePin, 253);
    
    }else if(serInString[0] == 'p' && serInString[1] == 'l' && serInString[2] == 'e' && serInString[3] == 'a' && serInString[4] == 's' && serInString[5] == 'e' && serInString[6] == 'd'){
    
      analogWrite(redPin, 238);
      analogWrite(greenPin, 213);
      analogWrite(bluePin, 252);
   
    }else if(serInString[0] == 's' && serInString[1] == 'u' && serInString[2] == 'b' && serInString[3] == 'd' && serInString[4] == 'u' && serInString[5] == 'e' && serInString[6] == 'd'){
    
      analogWrite(redPin, 48);
      analogWrite(greenPin, 34);
      analogWrite(bluePin, 251);
    }
 
    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++;
  }
}
IMG_1078.jpg
0
Your rating: None