Lab 2 : Heart Beat Diffuser

Submitted by fchasen on Tue, 02/12/2013 - 22:08

 

Description

In honor of Valentines day, I made my diffuser code beat like a heart.
Typing "love" makes it beat faster and
typing "sleep" makes is beat slower.

The diffuser was made from a unwrapped gum holder, stuffed with cotton.
The mesh skirt at the bottom helped to cause the colors to reflect and blend better.
I first tried to use tin foil before it was obvious what a bad idea that was.

 

Components Used

  •  3 LED (Red, Green, Blue)
  • Arduino Uno
  • Breadboard
  • 3 220-ohm Resistor
  • Plastic gum can
  • rubber band
  • white mesh
  • cotton fluffy stuff

Code

 

/* 
 * Heart Beat
 * Author: Fred Chasen
 */
 
//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;
 
//Adjusted for my pin setup
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 11
 
int redValue = 0;
int greenValue = 0;
int blueValue = 0;
 
int pulseCounter = 150;
int dir = 1;
int beat = 10;
 
void setup() {
  pinMode(redPin,   OUTPUT);   // sets the pins as output
  pinMode(greenPin, OUTPUT);   
  pinMode(bluePin,  OUTPUT);
  Serial.begin(9600);
}
 
void loop () {
  //read the serial port and create a string out of what you read
  readSerialString(serInString, 100);
 
  pump(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++;
  }
}
 
void pump(char *strArray) {
   int i = 0;
   
   if(stringsEqual(strArray, "love", 4)) {
     beat += 10;
     Serial.print("love ");
     Serial.print(beat); 
     Serial.print("\n"); 
     
   }
   
   if(stringsEqual(strArray, "sleep", 5)) {
     beat = beat * .5;
     
     Serial.print("sleep ");
     Serial.print(beat); 
     Serial.print("\n");
   }
   
   
   redValue = pulseCounter;
   greenValue = 25.5 - pulseCounter * .1;
   blueValue = 12.75 - pulseCounter * .05;
   analogWrite(redPin, redValue);
   analogWrite(greenPin, greenValue);
   analogWrite(bluePin, blueValue);
   
   pulseCounter = pulseCounter + beat * dir;
   
   if(pulseCounter >= 255){
     dir = dir * -1;
     pulseCounter = 255;
   }
   
   if(pulseCounter < 150){
     dir = dir * -1;
     pulseCounter = 150;
   }
   
}
 
 
 
//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;
  }
}
 
Diffuser
0
Your rating: None
Drupal theme by Kiwi Themes.