Lab 1: Tones

Play tones

I added a Serial.begin in the setup code in order to print the outputs. I then saw that while my FSR was recording the force I was pressing down on it. When I confirmed that, I went to go look at my map() code. I realized that from the FSR, my FSR range was incorrect. It was originally 200, 900, but I saw that its lower range was 0. Roughly 900 was correct. When I adjusted the map() range to be 0 on the lower end instead of 200, the speaker emitted sound when the FSR was pressed.

IMG_0976_480p_rotated_withAudio.mov

Then I rewrote the code to output just a tone but through a transisistor to see if I could get a louder sound, like this:

void setup() {
  Serial.begin(9600);
}
 
void loop() {
  // get a sensor reading:
  //int sensorReading = analogRead(A0);
  // map the results from the sensor reading's range
  // to the desired pitch range:
  //float frequency = map(sensorReading, 200, 900, 100, 1000);
  // change the pitch, play for 10 ms:
  tone(8, 600, 10);
  delay(10);
}

With the transistor in, I could in fact get a louder sound:

img_0977_rotatedRight_480p.mov

Here you can see a song playing from new code written in the Arduino code:

img_0978_rotatedRight_withAudio.mov

On this last one, I worked with Adnan because we didn't have enough FSRs to work independently. We wired up three FSRs to output three different notes into the speaker:

img_0981_480p_rotated.mov

I don't know why it's not playing the video itself but only the audio. In any case, what you are seeing is three FSRs and one speaker connected to breadboard with an Arduino on it. We are pressing the FSRs in a sequence to as to make a beat.

Here is the code that we used:

#include "pitches.h"
 
const int threshold = 500;      // minimum reading of the sensors that generates a note
const int speakerPin = 8;      // pin number for the speaker
const int noteDuration = 20;   // play notes for 20 ms
 
// notes to play, corresponding to the 3 sensors:
int notes[] = {
NOTE_A4, NOTE_B4,NOTE_C3 };
 
void setup() {
  Serial.begin(9600);       // initialize serial communications
}
 
void loop() {
  for (int thisSensor = 0; thisSensor < 3; thisSensor++) {
    // get a sensor reading:
    int sensorReading = analogRead(thisSensor);
    Serial.println(analogRead(A0));
    // if the sensor is pressed hard enough:
    if (sensorReading > threshold) {
      // play the note corresponding to this sensor:
      tone(speakerPin, notes[thisSensor], noteDuration);
    }
  }
}