Thursday, March 15, 2012

#include "pitches.h"

long result = 0;
int melody[] = {
  NOTE_C4, NOTE_D4,NOTE_E4, NOTE_D4};
int noteDurations[] = {
  8, 8, 8, 8};
int dark = 0; 
int pushbutton = 0;
int buttonState;
int buttonPin = 12;
int LEDpin = 7;
int LEDon = 0;

void setup()
{
  Serial.begin(9600);
  Serial.println("start");
}


long RCtime(int sensPin){
  long result = 0;
  pinMode(sensPin, OUTPUT); // make pin OUTPUT
  digitalWrite(sensPin, HIGH); // make pin HIGH to discharge capacitor - study the schematic
  delay(1); // wait a ms to make sure cap is discharged
  pinMode(sensPin, INPUT); // turn pin into an input and time till pin goes low
  digitalWrite(sensPin, LOW); // turn pullups off - or it won't work
  while(digitalRead(sensPin)){ // wait for pin to go low
    result++;
  }
  return result; // report results
}



void loop()
{



  Serial.println(analogRead(0));
  while (pushbutton == 0){
    if (analogRead(0)>900) {
      Serial.println("dark");
      dark = 1;

      while(dark == 1){
        for (int thisNote = 0; thisNote < 4; thisNote++) {
          int noteDuration = 1000/noteDurations[thisNote];
          tone(8, melody[thisNote],noteDuration);
          int pauseBetweenNotes = noteDuration * 1.30;
          delay(pauseBetweenNotes);
          noTone(4);
        }
        //  Serial.println("for");
        pinMode(buttonPin,INPUT);
        buttonState = digitalRead(buttonPin);
        Serial.println(buttonState);
        if (buttonState == 0){
          Serial.println("button is pressed");
          pinMode(LEDpin, OUTPUT);
          digitalWrite(LEDpin, LOW);
          dark = 0;
        }
      }
    }

  }
}



No comments:

Post a Comment