In this lab we explored the Pulse width modulation which imitates analog behaviour even when a digital input is given. We also used the serial communication which enabled us to send an input through our keyboards to control the LEDs. I used Red, Green and Blue LEDs for this lab. The circuit was created as per the diagram provided in class. In order to create the fading lights, the example code was uploaded to the microcontroller. After this another example to provide the serial input was uploaded which enabled the brightness of the light to be controlled by user input . Input was providedin the form <color,brightness value>. e.g. r20.
Materials:
1 breadboard, 1 Arduino, 3 LED(red,green,blue), 3 220ohms resistors, connecting wires, candle stand (diffusor)
Diffusor:
After having created 3 fading LEDs I used a diffusor whch was hollow candle stand. It blended the light coming from 3 LEDs and produced some interesting colors.
Serial Input
I then built upon this code to create my own, which would allow the user to press a particular color key multiple times where each time the key is pressed, the brightness of the LED would increase by 10%. The brightess of the key would go upto the maximum limit 255 and after that a message will be displayed stating Max brightness reached. I also display on the serial port the brightness value each time the key is presssed. The user can provide the input iin one go and control brightness of all 3 LEDs at the same time. e.g. rrrrr would increase the brightness of Red LED by 50%. rrgggbb would increase the brightness of Red LED by 20%, Green LED by 30%, blue LED by 20%.
I also added by own twist to the serial input. When the user types "clear" on the serial monitor, the brightness of all LED's is turned to 0. When the user types "disco" then the LED's blink one after the other creating a disco effect.
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 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
// Initializing colors
int redColorVal = 0;
int greenColorVal = 0;
int blueColorVal = 0;
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 0 brightness
analogWrite(greenPin, 0); // set them all to 0 brightness
analogWrite(bluePin, 0); // set them all to 0 brightness
Serial.println("Enter the command clear to switch off light");
Serial.println("Enter the command disco to see disco lights");
Serial.println("Enter the command as a combination of rgb(rrgrggggggrrrr) to illuminate lights");
}
void loop () {
// clear the string
memset(serInString, 0, 100);
//read the serial port and create a string out of what you read
readSerialString(serInString);
if (strcmp(serInString, "clear") == 0) //check if user input is clear
{
analogWrite(redPin, 0); // set them all to 0 brightness
analogWrite(greenPin, 0); // set them all to 0 brightness
analogWrite(bluePin, 0); // set them all to 0 brightness
memset(serInString, 0, 100);
}
if (strcmp(serInString, "disco") == 0) //Check if user input is disco
{
for (int i =0 ; i< 10 ; i++)
{
digitalWrite(redPin, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(greenPin, LOW); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(bluePin, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(redPin, LOW); // turn the LED off by making the voltage LOW
delay(1000);
digitalWrite(greenPin, HIGH); // turn the LED off by making the voltage LOW
delay(1000);
digitalWrite(bluePin, LOW); // turn the LED off by making the voltage LOW
delay(1000);
Serial.print(i);
}
memset(serInString, 0, 100);
}
for(int i = 0; i <= sizeof(serInString); i++)
{
colorCode = serInString[i];
if( colorCode == 'r')
{
if(redColorVal+25.5 >= 255)
{
Serial.print(" Red reached max brightness");
Serial.println();
redColorVal = 255;
}
else
{
redColorVal = redColorVal + 25.5;
}
analogWrite(redPin, redColorVal);
Serial.print("Setting Red LED to ");
Serial.print(redColorVal);
Serial.println();
}
if( colorCode == 'g')
{
if(greenColorVal+25.5 >= 255)
{
Serial.print("Green reached max brightness, starting all over again ");
Serial.println();
greenColorVal = 255;
}
else
{
greenColorVal = greenColorVal + 25.5;
}
analogWrite(greenPin, greenColorVal);
Serial.print("Setting Green LED to ");
Serial.print(greenColorVal);
Serial.println();
}
if( colorCode == 'b')
{
if(blueColorVal+25.5 >= 255)
{
Serial.print("Blue beached max brightness, starting all over again ");
Serial.println();
blueColorVal = 255;
}
else
{
blueColorVal = blueColorVal + 25.5;
}
analogWrite(bluePin, blueColorVal);
Serial.print("Setting Red LED to ");
Serial.print(blueColorVal);
Serial.println();
}
}
delay(500);
}
//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++;
}
}
- Login to post comments