Digital IO With Arduino Boards
Albert Hodo. Tangible User Interface. Fall 2022
Prompt: Design a good diffuser for your RGB LEDs (e.g. ping pong ball, Styrofoam, etc); Then connect 3 leds to an arduino and use Pulse Width Modulation to fake analog behaviour. Use the serial monitor to control the RGB values with multiple key presses. For example, pressing ‘r’ 5 times will set the brightness to 50% (or brightness = 127) and pressing ‘r’ 10 times will set it to 100% (or brightness = 255).
Components Used
- Post-it notes (Paper)
- plastic frosted countainer
- 3 LEDs
- 3 Resisters
- 1 Breadboard
- 1 Arduino Uno
- Jump wires
Process: I created the first diffuser out of post-it notes inspired by the Japanese art of Origami. I followed the tutorial which can be found on YouTube here: https://www.youtube.com/watch?v=pFTfj2hkpts. I tested this with the arduino setup and realised the light wasnt bright enough because the paper was more opaque than I anticipated after the folding process. I then decided to use a frosted plastic as the diffuser. One way to allow the paper gain its translucent material is to soak it in oil which i didn't have access to, given the time constraint.
Concerning the Arduino Setup, i connected the wires to the resisters and the LEDs and made the code automate the fading process using the PWM. After which i used the Serial Monitor to send data to the code to change the values of the LEDs manually.
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;
float colorVal;
int count=0;int redPin = 11; // Red LED, connected to digital pin 9
int greenPin = 9; // Green LED, connected to digital pin 10
int bluePin = 10; // Blue LED, connected to digital pin 11void setup() {
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
analogWrite(redPin, 127); // set them all to mid brightness
analogWrite(greenPin, 127); // set them all to mid brightness
analogWrite(bluePin, 127); // set them all to mid brightness
Serial.println("enter color command (e.g. 'r43') :");
}void loop () {
// count=0;
// 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' ) {
int tempVal=countNumberOf(colorCode);
Serial.print("temp debug 1: tempVal is: ");
Serial.println(tempVal);
if(tempVal<=10){
colorVal=tempVal*25.5;
}else{
colorVal=255;
}
Serial.print("value is:");
Serial.println(colorVal);
if(colorCode == 'r')
analogWrite(redPin, colorVal);
else if(colorCode == 'g')
analogWrite(greenPin, colorVal);
else if(colorCode == 'b')
analogWrite(bluePin, colorVal);
colorVal=0;
tempVal=0; //...not really needed
}
delay(1000); // 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++;
}
}int countNumberOf(char item){
int tempCounter=0;
for(int i=0; i<100;i++){
if(serInString[i]==item){
// tempCounter++;
count++;
Serial.print("the temp counter is: ");
Serial.println(count);
return count;
}
}
}
Demo