openobject.org

S3238806 4B's Project

From Physical Programming

For the 4B's project, i wanted to make a device that will notify the parent if the child was at a certain distant away from the parent. Making a toy that transformed into a bag for the child to carry and a bag for the parent to carry the device.


The magnet is held within the toy.


In doing so we used an Arduino with a Magnetic sensor and a piezo buzzer. When the magnet is out of a specified value a set melody will be set off and will keep repeating itself until the magnet is within range again.


A Copy of the Code (with comments and descriptions)


Media:4B's Project Amy Chan.zip



A Preview of the code is shown below:


  1. include "pitches.h"

int melody[] = {

 NOTE_G4, NOTE_A4, NOTE_B4, NOTE_G4, NOTE_A4, NOTE_B4, NOTE_G4, NOTE_G4, NOTE_G4,NOTE_G4,
 NOTE_A4, NOTE_A4, NOTE_A4, NOTE_A4, NOTE_G4, NOTE_A4, NOTE_B4};


int sensorPin = 0; int trigVal = 100; int meanVal; int speakerPin = 8; int timeDelay = 10; / int noteDurations[] = {

 4, 4, 2, 4, 4, 2, 8, 8, 8, 8, 8, 8, 8, 8, 4, 4, 2 };

void setup() {

 meanVal = analogRead(sensorPin); 
 
 Serial.begin(9600); 
 Serial.println(meanVal);  
 delay(100); 

}

void loop() {

 int currentVal = analogRead(sensorPin);
 int diff = abs(meanVal - currentVal); 
                                       
 Serial.println(diff);   
 
 if(diff > trigVal) {  
   for (int thisNote = 0; thisNote < 8; thisNote++) {


     int noteDuration = 1000/noteDurations[thisNote];
     tone(speakerPin, melody[thisNote],noteDuration);


     int pauseBetweenNotes = noteDuration * 2;
     delay(timeDelay);
   }
 }

}


Thank you for all the help Scott! =D