Thursday, May 5, 2011

The Final Project: Our Code

While finding the correct pieces and circuitry proved to be the hardest part of our project, luckily crafting a code proved to be easier. Below is the final code we settled on for our device. It proved to be the easiest way to manipulate the Arduino to allow us to achieve success.


Arduino Program:
int redled = 4;
int IRsensor = 0;
unsigned int A = 440;

double alpha = 0.75;
int period = 20;
double change = 0.0;
int greenPin = 8;
int buzzer = 7;
int clock = 998;
int sec = 0;

void setup()                 
{

pinMode(redled, OUTPUT);
pinMode(greenPin, OUTPUT);

}

void loop()                  
{
  
    static double oldValue = 0;
    static double oldChange = 0;
    int rawValue = analogRead(IRsensor);
    double value = alpha * oldValue + (1 - alpha) * rawValue;
    change = value - oldValue;
    
    digitalWrite(redled, (change < 0.0 && oldChange > 0.0));
  
    oldValue = value;
    oldChange = change;
    delay(period);
   
    Serial.print(rawValue);
    Serial.print(",");
    Serial.println(value);
    }
void loop()                 
{
   delay(clock);
 sec = sec + 1;

 if (sec <= 30)
  {
    digitalWrite(greenPin, HIGH);
  }
 
if (sec == 30)
  {
    tone(buzzer,A);
  }
 
if (sec > 30 && sec < 60)
  {
    noTone(buzzer);
    digitalWrite(greenPin, LOW);
     }

if (sec == 59)
  {
    sec = 0;

No comments:

Post a Comment