Code Compilation for ‘Ob’

EXPERIMENTAL INTERACTION // DN1010 // DISOBEDIENT OBJECTS // CODE

Here is a list of the longer codes used in Micro-Project 4 - Disobedient Objects! 

We advise you to read this side by side with the original post and look our for 'SEE [CODE A]' to refer back here!

[CODE 1]

Here is the code for the chime by Arduino Forum user Linkaan: https://forum.arduino.cc/index.php?topic=393731.0  

unsigned int C6 = 1047;
unsigned int Ab5 = 831;
unsigned int Bb5 = 932;
unsigned int G5 = 784;
unsigned int F5 = 698;
unsigned int E5 = 659;
unsigned int Eb5 = 622;
unsigned int D5 = 587;
unsigned int Db5 = 554;
unsigned int C5 = 523;
unsigned int B4 = 494;
unsigned int Bb4 = 466;
unsigned int nA4 = 440;
unsigned int Ab4 = 415;
unsigned int G4 = 392;
unsigned int Gb4 = 370;
unsigned int F4 = 349;
unsigned int E4 = 330;
unsigned int Eb4 = 311;
unsigned int D4 = 294;
unsigned int Db4 = 277;
unsigned int C4 = 262;
unsigned int B3 = 247;
unsigned int Bb3 = 233;
unsigned int nA3 = 220;
unsigned int GS3 = 208;
unsigned int G3 = 196;
unsigned int Gb3 = 185;
unsigned int F3 = 175;
unsigned int E3 = 165;

#define speakerPin 11

void setup() { 
  pinMode(speakerPin, OUTPUT);
  buzz(speakerPin,G3,166);
  buzz(speakerPin,C4,166);
  buzz(speakerPin,E4,166);
  buzz(speakerPin,G4,166);
  buzz(speakerPin,C5,166);
  buzz(speakerPin,E5,166);
  buzz(speakerPin,G5,500);
  buzz(speakerPin,E5,500);
  buzz(speakerPin,E3,166);
  buzz(speakerPin,C4,166);
  buzz(speakerPin,Eb4,166);
  buzz(speakerPin,Ab4,166);
  buzz(speakerPin,C5,166);
  buzz(speakerPin,Eb5,166);
  buzz(speakerPin,Ab5,500);
  buzz(speakerPin,Eb5,500);
  buzz(speakerPin,Bb3,166);
  buzz(speakerPin,D4,166);
  buzz(speakerPin,F4,166);
  buzz(speakerPin,Bb4,166);
  buzz(speakerPin,D5,166);
  buzz(speakerPin,F5,166);
  buzz(speakerPin,Bb5,500);
  delay(50);
  buzz(speakerPin,Bb5,166);
  delay(50);
  buzz(speakerPin,Bb5,166);
  delay(50);
  buzz(speakerPin,Bb5,166);
  buzz(speakerPin,C6,2000);
}

void loop() {
  // nope!
}

void buzz(int targetPin, long frequency, long length) {
  long delayValue = 1000000 / frequency / 2; // calculate the delay value between transitions
  //// 1 second's worth of microseconds, divided by the frequency, then split in half since
  //// there are two phases to each cycle
  long numCycles = frequency * length / 1000; // calculate the number of cycles for proper timing
  //// multiply frequency, which is really cycles per second, by the number of seconds to
  //// get the total number of cycles to produce
  for (long i = 0; i < numCycles; i++) { // for the calculated length of time...
    digitalWrite(targetPin, HIGH); // write the buzzer pin high to push out the diaphram
    delayMicroseconds(delayValue); // wait for the calculated delay value
    digitalWrite(targetPin, LOW); // write the buzzer pin low to pull back the diaphram
    delayMicroseconds(delayValue); // wait again or the calculated delay value
  } 
}
[CODE 2]
const int analogPin = A0;
int speakerPin = 11;
int threshold = 700;

unsigned int C6 = 1047;
unsigned int Ab5 = 831;
unsigned int Bb5 = 932;
unsigned int G5 = 784;
unsigned int F5 = 698;
unsigned int E5 = 659;
unsigned int Eb5 = 622;
unsigned int D5 = 587;
unsigned int Db5 = 554;
unsigned int C5 = 523;
unsigned int B4 = 494;
unsigned int Bb4 = 466;
unsigned int nA4 = 440;
unsigned int Ab4 = 415;
unsigned int G4 = 392;
unsigned int Gb4 = 370;
unsigned int F4 = 349;
unsigned int E4 = 330;
unsigned int Eb4 = 311;
unsigned int D4 = 294;
unsigned int Db4 = 277;
unsigned int C4 = 262;
unsigned int B3 = 247;
unsigned int Bb3 = 233;
unsigned int nA3 = 220;
unsigned int GS3 = 208;
unsigned int G3 = 196;
unsigned int Gb3 = 185;
unsigned int F3 = 175;
unsigned int E3 = 165;


void setup() {
  pinMode(analogPin, INPUT);
  pinMode(speakerPin, OUTPUT);
}

void loop() {
  Serial.begin(9600);
  int analogValue = analogRead(analogPin);
  if (analogValue < threshold) {
    pinMode(speakerPin, OUTPUT);
    buzz(speakerPin, G3, 166);
    buzz(speakerPin, C4, 166);
    buzz(speakerPin, E4, 166);
    buzz(speakerPin, G4, 166);
    buzz(speakerPin, C5, 166);
    buzz(speakerPin, E5, 166);
    buzz(speakerPin, G5, 500);
    buzz(speakerPin, E5, 500);
    buzz(speakerPin, E3, 166);
    buzz(speakerPin, C4, 166);
    buzz(speakerPin, Eb4, 166);
    buzz(speakerPin, Ab4, 166);
    buzz(speakerPin, C5, 166);
    buzz(speakerPin, Eb5, 166);
    buzz(speakerPin, Ab5, 500);
    buzz(speakerPin, Eb5, 500);
    buzz(speakerPin, Bb3, 166);
    buzz(speakerPin, D4, 166);
    buzz(speakerPin, F4, 166);
    buzz(speakerPin, Bb4, 166);
    buzz(speakerPin, D5, 166);
    buzz(speakerPin, F5, 166);
    buzz(speakerPin, Bb5, 500);
    delay(50);
    buzz(speakerPin, Bb5, 166);
    delay(50);
    buzz(speakerPin, Bb5, 166);
    delay(50);
    buzz(speakerPin, Bb5, 166);
    buzz(speakerPin, C6, 2000);;
  } else {}
}

void buzz(int targetPin, long frequency, long length) {
  long delayValue = 1000000 / frequency / 2; // calculate the delay value between transitions
  //// 1 second's worth of microseconds, divided by the frequency, then split in half since
  //// there are two phases to each cycle
  long numCycles = frequency * length / 1000; // calculate the number of cycles for proper timing
  //// multiply frequency, which is really cycles per second, by the number of seconds to
  //// get the total number of cycles to produce
  for (long i = 0; i < numCycles; i++) { // for the calculated length of time...
    digitalWrite(targetPin, HIGH); // write the buzzer pin high to push out the diaphram
    delayMicroseconds(delayValue); // wait for the calculated delay value
    digitalWrite(targetPin, LOW); // write the buzzer pin low to pull back the diaphram
    delayMicroseconds(delayValue); // wait again or the calculated delay value
  }
}
[CODE 3]
const int analogPin = A0;
const int threshold = 700;

int run;
int buttonPin = 12;


unsigned int C6 = 1047;
unsigned int Ab5 = 831;
unsigned int Bb5 = 932;
unsigned int G5 = 784;
unsigned int F5 = 698;
unsigned int E5 = 659;
unsigned int Eb5 = 622;
unsigned int D5 = 587;
unsigned int Db5 = 554;
unsigned int C5 = 523;
unsigned int B4 = 494;
unsigned int Bb4 = 466;
unsigned int nA4 = 440;
unsigned int Ab4 = 415;
unsigned int G4 = 392;
unsigned int Gb4 = 370;
unsigned int F4 = 349;
unsigned int E4 = 330;
unsigned int Eb4 = 311;
unsigned int D4 = 294;
unsigned int Db4 = 277;
unsigned int C4 = 262;
unsigned int B3 = 247;
unsigned int Bb3 = 233;
unsigned int nA3 = 220;
unsigned int GS3 = 208;
unsigned int G3 = 196;
unsigned int Gb3 = 185;
unsigned int F3 = 175;
unsigned int E3 = 165;

#define speakerPin 2


void setup() {
Serial.begin(9600);

}

void loop() {

int analogValue = analogRead(analogPin);
Serial.println(analogValue);
delay(1);
int switchState = 0;
switchState = digitalRead(buttonPin);

if (switchState == HIGH)
{
if (analogValue > threshold)
{

}
else {

pinMode(speakerPin, OUTPUT);
buzz(speakerPin, G3, 166);
buzz(speakerPin, C4, 166);
buzz(speakerPin, E4, 166);
buzz(speakerPin, G4, 166);
buzz(speakerPin, C5, 166);
buzz(speakerPin, E5, 166);
buzz(speakerPin, G5, 500);
buzz(speakerPin, E5, 500);
buzz(speakerPin, E3, 166);
buzz(speakerPin, C4, 166);
buzz(speakerPin, Eb4, 166);
buzz(speakerPin, Ab4, 166);
buzz(speakerPin, C5, 166);
buzz(speakerPin, Eb5, 166);
buzz(speakerPin, Ab5, 500);
buzz(speakerPin, Eb5, 500);
buzz(speakerPin, Bb3, 166);
buzz(speakerPin, D4, 166);
buzz(speakerPin, F4, 166);
buzz(speakerPin, Bb4, 166);
buzz(speakerPin, D5, 166);
buzz(speakerPin, F5, 166);
buzz(speakerPin, Bb5, 500);
delay(50);
buzz(speakerPin, Bb5, 166);
delay(50);
buzz(speakerPin, Bb5, 166);
delay(50);
buzz(speakerPin, Bb5, 166);
buzz(speakerPin, C6, 2000);
}
}
if (switchState == LOW)
{

}
}

void buzz(int targetPin, long frequency, long length) {
long delayValue = 1000000 / frequency / 2; // calculate the delay value between transitions
//// 1 second's worth of microseconds, divided by the frequency, then split in half since
//// there are two phases to each cycle
long numCycles = frequency * length / 1000; // calculate the number of cycles for proper timing
//// multiply frequency, which is really cycles per second, by the number of seconds to
//// get the total number of cycles to produce
for (long i = 0; i < numCycles; i++) { // for the calculated length of time...
digitalWrite(targetPin, HIGH); // write the buzzer pin high to push out the diaphram
delayMicroseconds(delayValue); // wait for the calculated delay value
digitalWrite(targetPin, LOW); // write the buzzer pin low to pull back the diaphram
delayMicroseconds(delayValue); // wait again or the calculated delay value
}
}
[CODE 4]
const int analogPin = A0;
int switchState = 0;
int buttonPin = 12;
int speakerPin = 5;
int threshold = 600;

unsigned int C6 = 1047;
unsigned int Ab5 = 831;
unsigned int Bb5 = 932;
unsigned int G5 = 784;
unsigned int F5 = 698;
unsigned int E5 = 659;
unsigned int Eb5 = 622;
unsigned int D5 = 587;
unsigned int Db5 = 554;
unsigned int C5 = 523;
unsigned int B4 = 494;
unsigned int Bb4 = 466;
unsigned int nA4 = 440;
unsigned int Ab4 = 415;
unsigned int G4 = 392;
unsigned int Gb4 = 370;
unsigned int F4 = 349;
unsigned int E4 = 330;
unsigned int Eb4 = 311;
unsigned int D4 = 294;
unsigned int Db4 = 277;
unsigned int C4 = 262;
unsigned int B3 = 247;
unsigned int Bb3 = 233;
unsigned int nA3 = 220;
unsigned int GS3 = 208;
unsigned int G3 = 196;
unsigned int Gb3 = 185;
unsigned int F3 = 175;
unsigned int E3 = 165;


void setup() {
 pinMode(buttonPin, INPUT_PULLUP);
 pinMode(speakerPin, OUTPUT);
 while (digitalRead(buttonPin) == LOW) {}
}

void loop() {
 Serial.begin(9600);
 int analogValue = analogRead(analogPin);
 pinMode(buttonPin, INPUT);
 if (analogValue < threshold) {
  pinMode(speakerPin, OUTPUT);
  buzz(speakerPin, G3, 166);
  buzz(speakerPin, C4, 166);
  buzz(speakerPin, E4, 166);
  buzz(speakerPin, G4, 166);
  buzz(speakerPin, C5, 166);
  buzz(speakerPin, E5, 166);
  buzz(speakerPin, G5, 500);
  buzz(speakerPin, E5, 500);
  buzz(speakerPin, E3, 166);
  buzz(speakerPin, C4, 166);
  buzz(speakerPin, Eb4, 166);
  buzz(speakerPin, Ab4, 166);
  buzz(speakerPin, C5, 166);
  buzz(speakerPin, Eb5, 166);
  buzz(speakerPin, Ab5, 500);
  buzz(speakerPin, Eb5, 500);
  buzz(speakerPin, Bb3, 166);
  buzz(speakerPin, D4, 166);
  buzz(speakerPin, F4, 166);
  buzz(speakerPin, Bb4, 166);
  buzz(speakerPin, D5, 166);
  buzz(speakerPin, F5, 166);
  buzz(speakerPin, Bb5, 500);
  delay(50);
  buzz(speakerPin, Bb5, 166);
  delay(50);
  buzz(speakerPin, Bb5, 166);
  delay(50);
  buzz(speakerPin, Bb5, 166);
  buzz(speakerPin, C6, 2000);;
  } else {}
}

void buzz(int targetPin, long frequency, long length) {
 long delayValue = 1000000 / frequency / 2; // calculate the delay value  between transitions
 //// 1 second's worth of microseconds, divided by the frequency, then split in half since
//// there are two phases to each cycle
 long numCycles = frequency * length / 1000; // calculate the number of  cycles for proper timing
 //// multiply frequency, which is really cycles per second, by the number of seconds to
 //// get the total number of cycles to produce
 for (long i = 0; i < numCycles; i++) { // for the calculated length of  time...
 digitalWrite(targetPin, HIGH); // write the buzzer pin high to push out the diaphram
 delayMicroseconds(delayValue); // wait for the calculated delay value
 digitalWrite(targetPin, LOW); // write the buzzer pin low to pull back the diaphram
 delayMicroseconds(delayValue); // wait again or the calculated delay value
 }
} 

[CODE 5]
buzz(speakerPin, G3, 166);
buzz(speakerPin, C4, 166);
buzz(speakerPin, E4, 166);
buzz(speakerPin, G4, 166);
buzz(speakerPin, C5, 166);
buzz(speakerPin, E5, 166);
buzz(speakerPin, G5, 500);
buzz(speakerPin, E5, 500);
buzz(speakerPin, E3, 166);
buzz(speakerPin, C4, 166);
buzz(speakerPin, Eb4, 166);
buzz(speakerPin, Ab4, 166);
buzz(speakerPin, C5, 166);
buzz(speakerPin, Eb5, 166);
buzz(speakerPin, Ab5, 500);
buzz(speakerPin, Eb5, 500);
buzz(speakerPin, Bb3, 166);
buzz(speakerPin, D4, 166);
buzz(speakerPin, F4, 166);
buzz(speakerPin, Bb4, 166);
buzz(speakerPin, D5, 166);
buzz(speakerPin, F5, 166);
buzz(speakerPin, Bb5, 500);
delay(50);
buzz(speakerPin, Bb5, 166);
delay(50);
buzz(speakerPin, Bb5, 166);
delay(50);
buzz(speakerPin, Bb5, 166);
buzz(speakerPin, C6, 2000);

//repeat if reset button is not pressed!

  buzz(speakerPin, G3, 166);
buzz(speakerPin, C4, 166);
buzz(speakerPin, E4, 166);
buzz(speakerPin, G4, 166);
buzz(speakerPin, C5, 166);
buzz(speakerPin, E5, 166);
buzz(speakerPin, G5, 500);
buzz(speakerPin, E5, 500);
buzz(speakerPin, E3, 166);
buzz(speakerPin, C4, 166);
buzz(speakerPin, Eb4, 166);
buzz(speakerPin, Ab4, 166);
buzz(speakerPin, C5, 166);
buzz(speakerPin, Eb5, 166);
buzz(speakerPin, Ab5, 500);
buzz(speakerPin, Eb5, 500);
buzz(speakerPin, Bb3, 166);
buzz(speakerPin, D4, 166);
buzz(speakerPin, F4, 166);
buzz(speakerPin, Bb4, 166);
buzz(speakerPin, D5, 166);
buzz(speakerPin, F5, 166);
buzz(speakerPin, Bb5, 500);
delay(50);
buzz(speakerPin, Bb5, 166);
delay(50);
buzz(speakerPin, Bb5, 166);
delay(50);
buzz(speakerPin, Bb5, 166);
buzz(speakerPin, C6, 2000);


//move on to sirens!

// Whoop up

for (int hz = 440; hz < 1000; hz++) {
tone(speakerPin, hz, 50);
delay(5);
}
noTone(speakerPin);

// Whoop down

for (int hz = 1000; hz > 440; hz--) {
tone(speakerPin, hz, 50);
delay(5);
}
noTone(speakerPin);

//repeat if reset button is not pressed!

// Whoop up

for (int hz = 440; hz < 1000; hz++) {
tone(speakerPin, hz, 50);
delay(5);
}

noTone(speakerPin);

// Whoop down

for (int hz = 1000; hz > 440; hz--) {
tone(speakerPin, hz, 50);
delay(5);
}

noTone(speakerPin);

//repeat if reset button is not pressed!

// Whoop up

for (int hz = 440; hz < 1000; hz++) {
tone(speakerPin, hz, 50);
delay(5);
}

noTone(speakerPin);

// Whoop down

for (int hz = 1000; hz > 440; hz--) {
tone(speakerPin, hz, 50);
delay(5);
}

noTone(speakerPin);

//repeat if reset button is not pressed!

// Whoop up

for (int hz = 440; hz < 1000; hz++) {
tone(speakerPin, hz, 50);
delay(5);
}

noTone(speakerPin);

// Whoop down

for (int hz = 1000; hz > 440; hz--) {
tone(speakerPin, hz, 50);
delay(5);
}

noTone(speakerPin);

//final scream UNTIL YOU PRESS STOP

buzz(speakerPin, C6, 200000);

[CODE 6]
const int analogPin = A0;
int switchState = 0;
int buttonPin = 12;
int speakerPin = 5;
int threshold = 800;

unsigned int C6 = 1047;
unsigned int Ab5 = 831;
unsigned int Bb5 = 932;
unsigned int G5 = 784;
unsigned int F5 = 698;
unsigned int E5 = 659;
unsigned int Eb5 = 622;
unsigned int D5 = 587;
unsigned int Db5 = 554;
unsigned int C5 = 523;
unsigned int B4 = 494;
unsigned int Bb4 = 466;
unsigned int nA4 = 440;
unsigned int Ab4 = 415;
unsigned int G4 = 392;
unsigned int Gb4 = 370;
unsigned int F4 = 349;
unsigned int E4 = 330;
unsigned int Eb4 = 311;
unsigned int D4 = 294;
unsigned int Db4 = 277;
unsigned int C4 = 262;
unsigned int B3 = 247;
unsigned int Bb3 = 233;
unsigned int nA3 = 220;
unsigned int GS3 = 208;
unsigned int G3 = 196;
unsigned int Gb3 = 185;
unsigned int F3 = 175;
unsigned int E3 = 165;


void setup() {
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(speakerPin, OUTPUT);
  while (digitalRead(buttonPin) == LOW) {}
}

void loop() {
  Serial.begin(9600);
  int analogValue = analogRead(analogPin);
  pinMode(buttonPin, INPUT);
  if (analogValue < threshold) {
    pinMode(speakerPin, OUTPUT);
    buzz(speakerPin, G3, 166);
    buzz(speakerPin, C4, 166);
    buzz(speakerPin, E4, 166);
    buzz(speakerPin, G4, 166);
    buzz(speakerPin, C5, 166);
    buzz(speakerPin, E5, 166);
    buzz(speakerPin, G5, 500);
    buzz(speakerPin, E5, 500);
    buzz(speakerPin, E3, 166);
    buzz(speakerPin, C4, 166);
    buzz(speakerPin, Eb4, 166);
    buzz(speakerPin, Ab4, 166);
    buzz(speakerPin, C5, 166);
    buzz(speakerPin, Eb5, 166);
    buzz(speakerPin, Ab5, 500);
    buzz(speakerPin, Eb5, 500);
    buzz(speakerPin, Bb3, 166);
    buzz(speakerPin, D4, 166);
    buzz(speakerPin, F4, 166);
    buzz(speakerPin, Bb4, 166);
    buzz(speakerPin, D5, 166);
    buzz(speakerPin, F5, 166);
    buzz(speakerPin, Bb5, 500);
    delay(50);
    buzz(speakerPin, Bb5, 166);
    delay(50);
    buzz(speakerPin, Bb5, 166);
    delay(50);
    buzz(speakerPin, Bb5, 166);
    buzz(speakerPin, C6, 2000);
    //repeat
    buzz(speakerPin, G3, 166);
    buzz(speakerPin, C4, 166);
    buzz(speakerPin, E4, 166);
    buzz(speakerPin, G4, 166);
    buzz(speakerPin, C5, 166);
    buzz(speakerPin, E5, 166);
    buzz(speakerPin, G5, 500);
    buzz(speakerPin, E5, 500);
    buzz(speakerPin, E3, 166);
    buzz(speakerPin, C4, 166);
    buzz(speakerPin, Eb4, 166);
    buzz(speakerPin, Ab4, 166);
    buzz(speakerPin, C5, 166);
    buzz(speakerPin, Eb5, 166);
    buzz(speakerPin, Ab5, 500);
    buzz(speakerPin, Eb5, 500);
    buzz(speakerPin, Bb3, 166);
    buzz(speakerPin, D4, 166);
    buzz(speakerPin, F4, 166);
    buzz(speakerPin, Bb4, 166);
    buzz(speakerPin, D5, 166);
    buzz(speakerPin, F5, 166);
    buzz(speakerPin, Bb5, 500);
    delay(50);
    buzz(speakerPin, Bb5, 166);
    delay(50);
    buzz(speakerPin, Bb5, 166);
    delay(50);
    buzz(speakerPin, Bb5, 166);
    buzz(speakerPin, C6, 2000);


    // Whoop up
    for (int hz = 440; hz < 1000; hz++) {
      tone(speakerPin, hz, 50);
      delay(5);
    }
    noTone(speakerPin);

    // Whoop down
    for (int hz = 1000; hz > 440; hz--) {
      tone(speakerPin, hz, 50);
      delay(5);
    }
    noTone(speakerPin);


    // Whoop up
    for (int hz = 440; hz < 1000; hz++) {
      tone(speakerPin, hz, 50);
      delay(5);
    }
    noTone(speakerPin);

    // Whoop down
    for (int hz = 1000; hz > 440; hz--) {
      tone(speakerPin, hz, 50);
      delay(5);
    }
    noTone(speakerPin);
    // Whoop up
    for (int hz = 440; hz < 1000; hz++) {
      tone(speakerPin, hz, 50);
      delay(5);
    }
    noTone(speakerPin);

    // Whoop down
    for (int hz = 1000; hz > 440; hz--) {
      tone(speakerPin, hz, 50);
      delay(5);
    }
    noTone(speakerPin);

    // Whoop up
    for (int hz = 440; hz < 1000; hz++) {
      tone(speakerPin, hz, 50);
      delay(5);
    }
    noTone(speakerPin);

    // Whoop down
    for (int hz = 1000; hz > 440; hz--) {
      tone(speakerPin, hz, 50);
      delay(5);
    }
    noTone(speakerPin);

    buzz(speakerPin, C6, 200000);
}}

void buzz(int targetPin, long frequency, long length) {
  long delayValue = 1000000 / frequency / 2; // calculate the delay value between transitions
  //// 1 second's worth of microseconds, divided by the frequency, then split in half since
  //// there are two phases to each cycle
  long numCycles = frequency * length / 1000; // calculate the number of cycles for proper timing
  //// multiply frequency, which is really cycles per second, by the number of seconds to
  //// get the total number of cycles to produce
  for (long i = 0; i < numCycles; i++) { // for the calculated length of time...
    digitalWrite(targetPin, HIGH); // write the buzzer pin high to push out the diaphram
    delayMicroseconds(delayValue); // wait for the calculated delay value
    digitalWrite(targetPin, LOW); // write the buzzer pin low to pull back the diaphram
    delayMicroseconds(delayValue); // wait again or the calculated delay value
  }
}

[CODE 7]
#include "pitches.h"

int sensorPin = A6; // Light Sensor connected to A6
int sensorValue = 0; // variable to store the value coming from the sensor
int TestLED = A3; // LED connected to analog pin A3
int buzzer = 7; // Piezo Buzzer connected to Pin 7

//Auxiliary variables
int i = 0;

/*
Array with all the notes, duration, and pause between notes.
First position is note, second is duration, third is pause.
*/
int music[18][3] =
{
  {NOTE_F4, 200, 200},
  {NOTE_F4, 200, 200},
  {NOTE_AS4, 1200, 1200},
  {NOTE_F5, 600, 600},
  {NOTE_DS5, 200, 200},
  {NOTE_D5, 200, 200},
  {NOTE_C5, 200, 200},
  {NOTE_AS5, 1200, 1200},
  {NOTE_F5, 600, 600},
  {NOTE_DS5, 200, 200},
  {NOTE_D5, 200, 200},
  {NOTE_C5, 200, 200},
  {NOTE_AS5, 1200, 1200},
  {NOTE_F5, 600, 600},
  {NOTE_DS5, 200, 200},
  {NOTE_D5, 200, 200},
  {NOTE_DS5, 200, 200},
  {NOTE_C5, 900, 1200}
};

//Function to help us test if sensor conditions are met
boolean isLightDetected(int sensorValue)
{
  return sensorValue < 10000 && sensorValue >= 100;
}

void setup() // run once, when the sketch starts
{
  Serial.begin(9600); // initialize the serial port
  pinMode(A6, INPUT); // sets analog pin A6 to be an input
  pinMode(TestLED, OUTPUT); // sets analog pin 3 to be an output
}

void loop() // run over and over again
{
  sensorValue = analogRead(sensorPin); // read the value from the sensor
  Serial.println(sensorValue); // send that value to the computer
  
  if(isLightDetected(sensorValue)) //Light Detected
  {
    digitalWrite(TestLED, HIGH); //turns on led connected to pin 3
    
    //Initialize counter
    i = 0;
    //Now just play through (iterate) the array
    while(isLightDetected(sensorValue) && i < 18) //While light is detected AND i is below the total number of notes (18)
    {
      //Play a note
      tone(buzzer, music[i][0], music[i][1]);
      delay(music[i][2]);
      
      //Increment counter
      i++;
      
      //Read latest sensor data
      sensorValue = analogRead(sensorPin);
    }
    
    noTone(7);
  }
  else //No Light Detected
  {
    digitalWrite(TestLED, LOW);
  }
}
[CODE 8] : final code
const int analogPin = A0;
int analogValue = 0;
int switchState = 0;
int buttonPin = 12;
int speakerPin = 5;
int threshold = 600; //to be changed depending on lighting
int i;

unsigned int C6 = 1047;
unsigned int Ab5 = 831;
unsigned int Bb5 = 932;
unsigned int G5 = 784;
unsigned int F5 = 698;
unsigned int E5 = 659;
unsigned int Eb5 = 622;
unsigned int D5 = 587;
unsigned int Db5 = 554;
unsigned int C5 = 523;
unsigned int B4 = 494;
unsigned int Bb4 = 466;
unsigned int nA4 = 440;
unsigned int Ab4 = 415;
unsigned int G4 = 392;
unsigned int Gb4 = 370;
unsigned int F4 = 349;
unsigned int E4 = 330;
unsigned int Eb4 = 311;
unsigned int D4 = 294;
unsigned int Db4 = 277;
unsigned int C4 = 262;
unsigned int B3 = 247;
unsigned int Bb3 = 233;
unsigned int nA3 = 220;
unsigned int GS3 = 208;
unsigned int G3 = 196;
unsigned int Gb3 = 185;
unsigned int F3 = 175;
unsigned int E3 = 165;

//chime array:
int chime[60][3] =
{ {speakerPin, G3, 166},
{speakerPin, C4, 166},
{speakerPin, E4, 166},
{speakerPin, G4, 166},
{speakerPin, C5, 166},
{speakerPin, E5, 166},
{speakerPin, G5, 500},
{speakerPin, E5, 500},
{speakerPin, E3, 166},
{speakerPin, C4, 166},
{speakerPin, Eb4, 166},
{speakerPin, Ab4, 166},
{speakerPin, C5, 166},
{speakerPin, Eb5, 166},
{speakerPin, Ab5, 500},
{speakerPin, Eb5, 500},
{speakerPin, Bb3, 166},
{speakerPin, D4, 166},
{speakerPin, F4, 166},
{speakerPin, Bb4, 166},
{speakerPin, D5, 166},
{speakerPin, F5, 166},
{speakerPin, Bb5, 500},
{speakerPin, 0, 50},
{speakerPin, Bb5, 166},
{speakerPin, 0, 50},
{speakerPin, Bb5, 166},
{speakerPin, 0, 50},
{speakerPin, Bb5, 166},
{speakerPin, C6, 2000},
{speakerPin, G3, 166},
{speakerPin, C4, 166},
{speakerPin, E4, 166},
{speakerPin, G4, 166},
{speakerPin, C5, 166},
{speakerPin, E5, 166},
{speakerPin, G5, 500},
{speakerPin, E5, 500},
{speakerPin, E3, 166},
{speakerPin, C4, 166},
{speakerPin, Eb4, 166},
{speakerPin, Ab4, 166},
{speakerPin, C5, 166},
{speakerPin, Eb5, 166},
{speakerPin, Ab5, 500},
{speakerPin, Eb5, 500},
{speakerPin, Bb3, 166},
{speakerPin, D4, 166},
{speakerPin, F4, 166},
{speakerPin, Bb4, 166},
{speakerPin, D5, 166},
{speakerPin, F5, 166},
{speakerPin, Bb5, 500},
{speakerPin, 0, 50},
{speakerPin, Bb5, 166},
{speakerPin, 0, 50},
{speakerPin, Bb5, 166},
{speakerPin, 0, 50},
{speakerPin, Bb5, 166},
{speakerPin, C6, 2000},

};


void setup() {
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(speakerPin, OUTPUT);
  while (digitalRead(buttonPin) == LOW) {} 
//loop only starts when on button is pressed
}

void loop() {
  Serial.begin(9600);
  int analogValue = analogRead(analogPin);
  pinMode(buttonPin, INPUT);
  if (analogValue < threshold) {
  i = 0;
  while (analogValue < threshold)
  {
  if (i < 60) {
  buzz(speakerPin, chime[i][1], chime[i][2]);
  i++;
  analogValue = analogRead(analogPin);
  }
  if (i > 59) { //siren whoop up
  for (int hz = 440; hz < 1000 && analogValue < threshold; hz++)
{
  tone(speakerPin, hz, 50);
  delay(5);
  analogValue = analogRead(analogPin);
  }

  noTone(speakerPin);
  //siren whoop down
  for (int hz = 1000; hz > 440 && analogValue < threshold; hz--)
  {
  tone(speakerPin, hz, 50);
  delay(5);
  analogValue = analogRead(analogPin);
  }
  noTone(speakerPin);
  }
  }

  noTone(speakerPin);
  }
}

void buzz(int targetPin, long frequency, long length) {
long delayValue = 1000000 / frequency / 2; // calculate the delay value between transitions
//// 1 second's worth of microseconds, divided by the frequency, then split in half since
//// there are two phases to each cycle
long numCycles = frequency * length / 1000; // calculate the number of cycles for proper timing
//// multiply frequency, which is really cycles per second, by the number of seconds to
//// get the total number of cycles to produce
for (long i = 0; i < numCycles; i++) { // for the calculated length of time...
digitalWrite(targetPin, HIGH); // write the buzzer pin high to push out the diaphram
delayMicroseconds(delayValue); // wait for the calculated delay value
digitalWrite(targetPin, LOW); // write the buzzer pin low to pull back the diaphram
delayMicroseconds(delayValue); // wait again or the calculated delay value
}
}

Leave a Reply