Ping pong diffuser

Submitted by soren svejstrup on Mon, 02/11/2013 - 12:14

Description

This program lets you increment the different colors by 15 percent. Inputs are 'r', 'g' and 'b'. 

The syntax is a bit different from what I have coded before. Biggest problem for me right now is to understand the differences and to link it with the physical Arduino components.

Components used

  • Breadboard
  • Arduino Uno
  • red, green and blue LED
  • 3 220 ohm resisters
  • 4 ground cables
  • 3 power cables
  • USB cable
  • Ping pong ball
  • Cotton

 

Code

 

//The following code was modified from the provided serial_led_rgb to provide an alternate means of input control to Arduino (e.g., "rrgbbgr" could be used);
/* 
* Serial RGB LED
* ---------------
* Serial commands control the brightness of R,G,B LEDs
 
*
* Command structure is "", 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 
* http://todbot.com/
*
* Modified by Soren Svejstrup 11 Feb
*/
 
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 bluePin = 9; // Red LED, connected to digital pin 9
int redPin = 10; // Green LED, connected to digital pin 10
int greenPin = 11; // Blue LED, connected to digital pin 11
 
int blueValue = 127;
int redValue = 127;
int greenValue = 127;
 
void setup() {
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(greenPin, OUTPUT); 
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
analogWrite(redPin, redValue); // set them all to mid brightness
analogWrite(greenPin, greenValue); // set them all to mid brightness
analogWrite(bluePin, blueValue); // 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("You typed ");
Serial.print(serInString);
Serial.print(". ");
 
processSerialCommands(serInString, 100);
}
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++;
}
}
 
void processSerialCommands(char *strArray, int maxLength) {
int i = 0;
 
while (i < maxLength && strArray[i] != '\0') {
//Read in the character at position i in the string
colorCode = serInString[i];
 
//If the character is r (red)...
if (colorCode == 'r') {
//Increase the current red value by 10, and if you reach 255 go back to 0
redValue = (redValue + 15) % 255;
analogWrite(redPin, redValue);
Serial.print("setting color r to ");
Serial.println(redValue);
 
//If the character is g (green)...
} else if (colorCode == 'g') {
greenValue = (greenValue + 15) % 255;
analogWrite(greenPin, greenValue);
Serial.print("setting color g to ");
Serial.println(greenValue);
 
//If the character is b (blue)...
} else if (colorCode == 'b') {
blueValue = (blueValue + 15) % 255;
analogWrite(bluePin, blueValue);
Serial.print("setting color b to ");
Serial.println(blueValue);
}
 
i++;
}
}
Assignment 2
0
Your rating: None
Drupal theme by Kiwi Themes.