Description:
In this lab we learned how to control a DC motor with the various input devices we had learned about. In this way we explored motion as an output.
For the homework assignment, I created an anthropomorphic figurine out of cardboard, a bottle cap, and a dark-coloured wristband. The figurine also acts as a colour diffuser, for 3 different LEDs (R,B,G) are inside of it and covered in a plastic sheet. The colour can be seen through the figurine's eyes, mouth, and hair.
For the "hair", I used a pipe cleaner twisted into the shape of blades. I attached the pipe cleaner to the DC motor, which was inside the figurine and cannot be seen. The bottle cap served the purpose to stabilize the motor while still allowing light to come out of the figurine's "head".
A potentiometer controlled the speed of rotation of the figurine's "hair", and key commands in the computer control the brightness of each different LED as preferred, in order to make different colour combinations as desired. The user introduces de colour of choice ("r", "b", "g") and then uses the "w" key to increase the chosen LED's brightness, and the key "s" to decrease it (these keys mimic the function of "up" and "down" arrows). The user also has the choice of typing "f", "m", "o" for setting an LED directly to full, middle, or 0 brightness, respectively.
The inspiration for this figurine came from the impending Halloween celebrations, as I thought of something that could be used to decorate the house and that can be played around with (colours and motion).
A demo of how the figurine works can be seen here: https://docs.google.com/file/d/0ByMuJqORzfcSSkV4Zl85cUl5Mk0/edit?usp=sha...
Components:
1- Arduino Uno Microcontroller
1- Breadboard
3- 220 Ω Resistors
3 - Light Emmitting Diodes (LEDs) (Red, Green, Blue)
1- USB Cable
1- MacBook Pro 13" (OSX)
1 - DC Motor
1 - Diode (1N4004)
1 - Transistor (TIP120)
2 - AA batteries
Code:
/*
* Sofia Solar Cafaggi
*/
char serInString[100];
char colorCode;
char Control;
int colorVal;
int RIntensity=0;
int BIntensity=0;
int GIntensity=0;
//Flags
int Red=0;
int Blue=0;
int Green=0;
//LEDs
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
//Motor and Pot
int potPin = 0; // select the input pin for the potentiometer
int motorPin = 8; // select the pin for the Motor
int valspeed = 0;
void setup() {
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
Serial.println("Choose a colour (R,B,G). Then use 'w' or 's' to increase/decrease its brightness, respectively. You can also use 'f', 'm', 'o' to turn the selected LED to full, medium, or zero brightness, respectively. :");
}
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 = Serial.read();
valspeed = analogRead(potPin); // read the value from the sensor, between 0 - 1024
//Serial.println(val);
analogWrite(motorPin, valspeed/4); // analogWrite can be between 0-255
//Red LED Loop
if(colorCode=='r'||colorCode=='R')
{
Serial.println("You've chosen colour Red. Press W to increase intensity, press S to decrease intensity");
Red=1;
Blue=0;
Green=0;
}
if(Red==1)
{
if(colorCode=='w'||colorCode=='W')
{
RIntensity=RIntensity+25;
if(RIntensity > 255)
{RIntensity=0;}
}
if(colorCode=='s'||colorCode=='S')
{
RIntensity=RIntensity-25;
if(RIntensity < 0)
{RIntensity=255;}
}
if(colorCode=='o'||colorCode=='O')
{
RIntensity=0;
}
if(colorCode=='f'||colorCode=='F')
{
RIntensity=255;
}
if(colorCode=='m'||colorCode=='M')
{
RIntensity=127;
}
}
//Blue LED Loop
if(colorCode=='b'||colorCode=='B')
{
Serial.println("You've chosen colour Blue. Press W to increase intensity, press S to decrease intensity");
Red=0;
Blue=1;
Green=0;
}
if(Blue==1)
{
if(colorCode=='w'||colorCode=='W')
{
BIntensity=BIntensity+25;
if(BIntensity > 255)
{BIntensity=0;}
}
if(colorCode=='s'||colorCode=='S')
{
BIntensity=BIntensity-25;
if(BIntensity < 0)
{BIntensity=255;}
}
if(colorCode=='o'||colorCode=='O')
{
BIntensity=0;
}
if(colorCode=='f'||colorCode=='F')
{
BIntensity=255;
}
if(colorCode=='m'||colorCode=='M')
{
BIntensity=127;
}
}
//Green LED Loop
if(colorCode=='g'||colorCode=='G')
{
Serial.println("You've chosen colour Green. Press W to increase intensity, press S to decrease intensity");
Red=0;
Blue=0;
Green=1;
}
if(Green==1)
{
if(colorCode=='w'||colorCode=='W')
{
GIntensity=GIntensity+25;
if(GIntensity > 255)
{GIntensity=0;}
}
if(colorCode=='s'||colorCode=='S' && Green==1)
{
GIntensity=GIntensity-25;
if(GIntensity < 0)
{GIntensity=255;}
}
if(colorCode=='o'||colorCode=='O')
{
GIntensity=0;
}
if(colorCode=='f'||colorCode=='F')
{
GIntensity=255;
}
if(colorCode=='m'||colorCode=='M')
{
GIntensity=127;
}
}
analogWrite(redPin,RIntensity);
analogWrite(bluePin,BIntensity);
analogWrite(greenPin,GIntensity);
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++;
}
}
- Login to post comments