Tone Library Lab

I used an 8ohm speaker I took from some object, had to extend the leads, soldered them.

Had huge problems importing the tone library. Couldn’t do it the way the lab said to do it– Alex Kaufman helped by having me open the package contents of the Arduino application, and placing the Tone folder in ~Contents/Resources/Java/Hardware/Libraries.

Then I plugged in the Theramin code and got a very faint tone that I could control by alternating the two photosensors.

Changed the resistor and it became louder. I programmed a switch to cycle through an array of sounds, to make a melody loop– the array went back to 0 after it reached the last number in the array.

All my video documentation on the XBee was somehow lost…

Here’s my code:

#include

Tone noiseMaker = Tone();
int noteValue[] = {
NOTE_C4,
NOTE_C4,
NOTE_E4,
NOTE_E4,
NOTE_G4,
NOTE_G4,
NOTE_F4,
NOTE_E4,
NOTE_D4,
NOTE_E5,
NOTE_FS5,
NOTE_G5 };

int whichNote = 0; // which of the 8 notes we’re playing
int lastSwitchState = LOW; // the state of the switch last time thru the loop
long timestamp = 0; // the time the switch went from off to on
long duration = 0; // how long the switch was pressed for

void setup() {
// set the switch I/O pin to be an input:
pinMode(4, INPUT);
// initialize the tone library:
noiseMaker.begin(7);
// initialize serial communication:
Serial.begin(9600);
}

void loop() {
// read the switch:
int switchState = digitalRead(4);

// see if the switch has changed:
if (switchState != lastSwitchState) {
Serial.println(switchState);
if (switchState == HIGH) {
//increment the note position:
Serial.println(“Switch went on!”);
timestamp = millis();

// increment the note value if it’s less than
// the length of the array. Otherwise, set it
// back to 0:
if (whichNote < 12 ) {
whichNote++;
}
else {
whichNote = 0;
}
}
else {
// the switch went from on to off:
duration = millis() – timestamp;
Serial.print(“duration: ” );
Serial.println(duration);
}

// save the current state of the switch for the
// next time through the loop:
lastSwitchState = switchState;
}

// play a note
noiseMaker.play(noteValue[whichNote]);
}

Tags: , ,

Comments are closed.