Analog Output

Arduino's analog output produces a series of voltage pulses at regular intervals and varying the width of the pulses, called pulse width modulation(PWM).

Servo Motor

Output_ServoMotor.mp4

In this video, an FSR is connected as an analog input sensor, controlling the servo's rotation angle. The important skill is mapping the sensor's input to the appropriate output range.

Servo motor also needs 20 milliseconds delay to run smoothly.

int angle = map(sensor, 0, 1023, 0, 179);

//delay(20);//servo expect delay 20 millisec to run smoothly rather than jitter
//instead write delay, pulse the servo every 20 millisec
if(millis()%20 <2) {
  servoMotor.write(angle);
 }

Speaker

tone(pin, frequency, timeDuration) is the core to control a speaker instead of analogWrite().

Play Tones

Output_Tone1.mp4

Output_Tone2.mp4

In these two videos, FSR's input map to the output frequency. Different time pulse also generates different sound effects.

Play it Loud

Output_Tone3.mp4

Connect a TIP120 transistor to amplifier the speaker.

Play Melody

Output_Tone4.mp4

Use array to play a melody that contains a series of notes.

for (int a = 0; a < 8; a++) {
    int noteDuration = 1000 / noteDurations[a];
    tone(2, melody[a], noteDuration);
    delay(noteDuration + 30);
  }

Musical Instrument

Output_Tone5.mp4

In the above video, FSRs function as a keyboard that triggers the discrete notes. Three FSR forms a simple keyboard that can play three notes. I need to put delay()in the program to make my speaker work, otherwise, the output sound seems be impacted by previous output. delay(5) can make the speaker play the right note, delay(10) make it works smoothly, but delay(20) would feel noticeable pulse gaps that bring the somewhat electronic feeling.