Instructables

Step 2: Honor the CODE

Picture of Honor the CODE
IMG_3961.jpg
Here's the code and a pseudo-schematic for the Arduino.  If you don't know what this stuff means, there are alot of self-starter web sights for Arduino.  I recommend Arduino.cc and Lady Ada.  Don't let all this technical garble stop you from playing with it.

CODE: (copy and paste this into your Arduino sketch.  It's not cheating)

/*This code will use a 'tunable' light sensor (photo-resistor) to do two things
when the light level is BELOW a certain threshold: Turn on some flashing LEDs,
and sound an alarm!!!

This code was compiled using source code from http://www.arduino.cc/en/Tutorial/Melody
(2005) D. Cuartielles for K3, and http://ardx.org/CODE09.

The COPY and PASTE functions were written sometime long ago by a really
lazy person...who I personally love.*/

//PhotoResistor Pin
int lightPin = 0;
//LED Pin
int ledPin = 9;
int ledPin2 =11;
int ledPin3 =13;
//Soundpin
int soundPin= 3;
//number of notes
int length = 7;
char notes[]= "cacacaa";
int beats[] = {1,1,1,1,1,1,1};
int tempo = 300;

void playTone(int tone, int duration) {
  for (long i = 0; i < duration * 1000L; i += tone * 2) {
    digitalWrite(soundPin, HIGH);
    delayMicroseconds(tone);
    digitalWrite(soundPin, LOW);
    delayMicroseconds(tone);
  }
}

void playNote(char note, int duration) {
  char names[] = { 'c', 'a' };
  int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014 };

  // play the tone corresponding to the note name
  for (int i = 0; i < 8; i++) {
    if (names[i] == note) {
      playTone(tones[i], duration);
    }
  }
}


void setup()
{
  pinMode(ledPin, OUTPUT);
 pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT); //sets the led pin to output
  pinMode(soundPin, OUTPUT);
}

void loop()
{
 int threshold=500;       /*<---- THIS IS THE KEY FEATURE ! ! ! */

    while (analogRead(lightPin)>threshold)

    {
    for (int i = 0; i < length; i++) {
    if (notes[i] == ' ') {
      delay(beats[i] * tempo/3); // rest
    } else {
      playNote(notes[i], beats[i] * tempo*1.3);
    // pause between notes
    delay(tempo/2); }

     if (analogRead(lightPin)>threshold)
 {
  digitalWrite(ledPin,HIGH);
  digitalWrite(ledPin2,HIGH);
  digitalWrite(ledPin3,HIGH);
  delay (20);
  digitalWrite(ledPin,LOW);
  digitalWrite(ledPin2,LOW);
  digitalWrite(ledPin3,LOW);
  delay(20);
  digitalWrite(ledPin,HIGH);
  digitalWrite(ledPin2,HIGH);
  digitalWrite(ledPin3,HIGH);
  delay (20);
  digitalWrite(ledPin,LOW);
  digitalWrite(ledPin2,LOW);
  digitalWrite(ledPin3,LOW);
  delay(20);
  digitalWrite(ledPin,HIGH);
  digitalWrite(ledPin2,HIGH);
  digitalWrite(ledPin3,HIGH);
  delay (20);
  digitalWrite(ledPin,LOW);
  digitalWrite(ledPin2,LOW);
  digitalWrite(ledPin3 ,LOW);
  delay(20);

 }else{

  digitalWrite(ledPin, LOW); 
 }
    } 
  }
}


:END CODE

The trick in this code is to use the "threshold" to tune the light sensor for the local conditions.  If you get everything else to work on your work bench, remember to anticipate adjusting the threshold in ANY other lighting condition.

Also, the second image show's a MESS of wires and the BREAD BOARD.  We used the BREAD BOARD because it's a quick way to build a circuit AND , because we could use the TACTILE SWITCH to turn on/off the signals from the ARDUINO when the dishwasher door was closed/open.  In other words, we didn't have to write some code for the switch to enter the system.  The switch just connected power from the ARDUINO to the BREAD BOARD when the door closed.  It'll make more sense in a minute.