Digital IO and diffuser

Assignment: Digital I/O with Arduino Boards + Diffuser

Collaborators:

Assignment: Digital I/O with Arduino Boards + Diffuser
Collaborators:

Assignment: Digital I/O with Arduino Boards + Diffuser
Collaborators:

Description

We used Pulse modulation technique to fake analog input to variably control LED blinking.

My pingpong and paper diffuser installations are attached.

Components Used

  • Light Emitting Diode (LED) -3
  • Resistor -3
  • wires
  • pingpong ball
  • paper

Arduino Code

 

/*

* Gopal Vaswani

*/

 

//include support for manipulating strings.

//for a useful string comparison function, see the bottom of this file... stringsEqual()

#include <stdio.h>

#include <string.h>

 

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 redValue = 5;

int greenValue = 5;

int blueValue = 5;

 

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 or rrrrrrrrbbbb') :");

}

 

void loop () {

//read the serial port and create a string out of what you read

readSerialString(serInString, 100);

 

 

 

//UNCOMMENT ONE OF THE FOLLOWING COMMANDS, OR NOTHING WILL HAPPEN WHEN YOU

//RUN THE PROGRAM...

 

//Uncomment the following line to read commands of the form 'r245' or 'b3'

processNumericalCommands(serInString);

 

//Uncomment the following line to read commands of the form 'rrrb'

processRepeatKeyCommands(serInString, 100);

 

//Or write your own function...

//YOUR_FUNCTION_HERE(serInString);

 

//Erase anything left in the serial string, preparing it for the

//next loop

resetSerialString(serInString, 100);

 

delay(100);  // wait a bit, for serial data

}

 

void resetSerialString (char *strArray, int length) {

for (int i = 0; i < length; i++) {

strArray[i] = '\0';

}

}

 

//read a string from the serial and store it in an array

//you must supply the array variable

void readSerialString (char *strArray, int maxLength) {

int i = 0;

 

if(!Serial.available()) {

return;

}

while (Serial.available() && i < maxLength) {

strArray[i] = Serial.read();

i++;

}

}

 

//go through the string, and increase the red value for each 'r',

//the green value for each 'g', and the blue value for each 'b'.

//For example "rrrg" increases red by 30 and green by 10.

void processRepeatKeyCommands(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];

 

//check for each case

switch(colorCode){

case 'r':

redValue = (redValue + 10) % 255;

analogWrite(redPin, redValue);

Serial.print("setting color r to ");

Serial.println(redValue);

break;

case 'g':

greenValue = (greenValue + 10) % 255;

analogWrite(greenPin, blueValue);

Serial.print("setting color r to ");

Serial.println(greenValue);

break;

case 'b':

blueValue = (blueValue + 10) % 255;

analogWrite(bluePin, blueValue);

Serial.print("setting color r to ");

Serial.println(blueValue);

break;

}

i++;

}

}

 

//change the value of the red, green, or blue LED according to the command received.

//for example, r240 sets the red LED to the value 240 (out of 255)

void processNumericalCommands(char *strArray) {

//read in the first character in the string

colorCode = serInString[0];

 

//if the first character is r (red), g (green) or b (blue), do the following...

if( colorCode == 'r' || colorCode == 'g' || colorCode == 'b' ) {

//convert the string to an integer

//(start at the second character, or the beginning of the string '+1')

colorVal = atoi(serInString+1);

Serial.print("setting color ");

Serial.print(colorCode);

Serial.print(" to ");

Serial.print(colorVal);

Serial.println();

 

if(colorCode == 'r')

analogWrite(redPin, colorVal);

else if(colorCode == 'g')

analogWrite(greenPin, colorVal);

else if(colorCode == 'b')

analogWrite(bluePin, colorVal);

}

}

 

 

 

//compare two strings to see if they are equal

//compares the first 'numCharacters' characters of string1 and string2 to

//see if they are the same

//

//E.g. stringsEqual("hello","hello",5) => true

//     stringsEqual("hello","helaabbnn",3) => true

//     stringsEqual("hello","helaa",5) => false

boolean stringsEqual(char *string1, char *string2, int numCharacters) {

if (strncmp(string1, string2, numCharacters) == 0) {

return true;

} else {

return false;

}

}