Zeven Rodriguez

Lab 4 Servo and Tone

ITP PHYSCOMP

Comments Closed


Share this post

The breadboard is wired with 2 photocells in series go to pin 0. The photocells give an average range in the resistance. Depending which photocell you choose it pushes the resistance either towards the ends of the range. They make sure that all the voltage does not reach the arduino. The speaker goes to pin 8. It does not go to the Pulse Width Modulation because PWM changes the volume of the speaker.

Physcomp Lab4 – Photocell tone from Zeven Rodriguez on Vimeo.

This code is to check the range of the photocells

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

 void loop() {
   int sensorValue = analogRead(0);
   Serial.println(sensorValue, DEC);
 }

This code uses the 2 photocells to make a thermin
/*
   Theremin
  
  Plays tones based on a sensor reading
  uses Tone library by Brett Hagman
  http://code.google.com/p/arduino-tone/
  
  circuit:
  * photoresistor from +5V to analog in 0
  * photoresistor from analog pin 0 to ground
  * 8-ohm speaker on digital pin 8
  
  created 10 Sep 2009
  by Tom Igoe 
  */

 #include <Tone.h>
 Tone noiseMaker;    // instance of the tone library

 void setup() {
   // start the music:
   noiseMaker.begin(8);
 }

 void loop() {
   // get a sensor reading:
   int sensorReading = analogRead(0);
   // map the results from the sensor reading's range
   // to the desired pitch range:
   int pitch = map(sensorReading, 200, 900, 100, 1000);
   // change the pitch:
   noiseMaker.play(pitch);
 }

This video shows using 3 analog resistors to make a make-shift instrument

Phsycomp Lab4 - Three analog inputs from Zeven Rodriguez on Vimeo.
Here is the code

 /*
  
  circuit:
  * 3 force-sensing resistors from +5V to analog in 0 through 5
  * 3 10K resistors from analog in 0 through 5 to ground
  * 8-ohm speaker on digital pin 8
  
  */

 #include <Tone.h>
 Tone noiseMaker;    // instance of the tone library
 const int threshold = 10;    // minimum reading of the sensors that generates a note

 // notes to play, corresponding to the 3 sensors:
 int notes[] = {
   NOTE_A4, NOTE_B4,NOTE_C4 };

 void setup() {
   // start the music:
   noiseMaker.begin(8);
 }

 void loop() {
   for (int thisSensor = 0; thisSensor < 3; thisSensor++) {
     // get a sensor reading:
     int sensorReading = analogRead(thisSensor);
     // if the sensor is pressed hard enough:
     if (sensorReading > threshold) {
       // play the note corresponding to this sensor:
       noiseMaker.play(notes[thisSensor]);
     }
     else {
       // stop playing:
       noiseMaker.stop();
     }
   }
 }

Finally I made an instrument

Physcomp Lab4 - Musical Instrument from Zeven Rodriguez on Vimeo.
#include <Tone.h>
Tone noiseMaker; // instance of the tone library
int notes[] = {
NOTE_D4, NOTE_E4,NOTE_C4,NOTE_C3 };
int durations[] = {
100, 100, 100, 100, 100};
void setup() {
// start the music:
noiseMaker.begin(8);
Serial.begin(9600);
}
void loop() {
int pot = analogRead(5);
pot = pot/4;
Serial.println(pot);
//depending on were the pot is play series of tones at differnt pitches
if(pot > 204){
for (int thisNote = 0; thisNote < 5; thisNote ++) {
// change the pitch:
noiseMaker.play(notes[0]);
delay(durations[thisNote]);
// stop for the next note:
noiseMaker.stop();
}
}
else if((pot < 204) && (pot > 153)){
for (int thisNote = 0; thisNote < 5; thisNote ++) {
// change the pitch:
noiseMaker.play(notes[1]);
delay(durations[thisNote]);
// stop for the next note:
noiseMaker.stop();
}
// hold before repeating:
}
else if((pot < 153) && (pot > 102)){
for (int thisNote = 0; thisNote < 5; thisNote ++) {
// change the pitch:
noiseMaker.play(notes[2]);
delay(durations[thisNote]);
// stop for the next note:
noiseMaker.stop();
}
// hold before repeating:
}
else if((pot < 102) && (pot > 51)){
for (int thisNote = 0; thisNote < 5; thisNote ++) {
// change the pitch:
noiseMaker.play(notes[3]);
delay(durations[thisNote]);
// stop for the next note:
noiseMaker.stop();
}
// hold before repeating:
}
else if((pot < 51) && (pot >= 0)){
for (int thisNote = 0; thisNote < 5; thisNote ++) {
// change the pitch:
noiseMaker.play(notes[4]);
delay(durations[thisNote]);
// stop for the next note:
noiseMaker.stop();
}
// hold before repeating:
}
}

Now using the servo

The servo is connected directly to 5v rail on the breadboard. The servo goes to pin 2. The force sensor goes to analog in.

Physcomp Lab4 – Using Pulse code from Zeven Rodriguez on Vimeo.

This code does not use the arduino servo library

 /*
  Servo control from an analog input

 The minimum (minPulse) and maxiumum (maxPulse) values
 will be different depending on your specific servo motor.
 Ideally, it should be between 1 and 2 milliseconds, but in practice,
 0.5 - 2.5 milliseconds works well for me.
 Try different values to see what numbers are best for you.

 This program uses the millis() function to keep track of when the servo was
 last pulsed.  millis() produces an overflow error (i.e. generates a number
 that's too big to fit in a long variable) after about 5 days. if you're
 making a program that has to run for more than 5 days, you may need to
 account for this.

 by Tom Igoe
 additions by Carlyn Maw & Rob Faludi
 Created 28 Jan. 2006
 Updated 10 Jun. 2008
 */

 int servoPin = 2;     // Control pin for servo motor
 int minPulse = 500;   // Minimum servo position
 int maxPulse = 2500;  // Maximum servo position
 int pulse = 0;        // Amount to pulse the servo

 long lastPulse = 0;    // the time in milliseconds of the last pulse
 int refreshTime = 20; // the time needed in between pulses

 int analogValue = 0;  // the value returned from the analog sensor
 int analogPin = 0;    // the analog pin that the sensor's on

 void setup() {
  pinMode(servoPin, OUTPUT);  // Set servo pin as an output pin
  pulse = minPulse;           // Set the motor position value to the minimum
  Serial.begin(9600);
 }

 void loop() {
  analogValue = analogRead(analogPin);      // read the analog input
  pulse = map(analogValue,0,1023,minPulse,maxPulse);    // convert the analog value
                                                        // to a range between minPulse
                                                        // and maxPulse.

  // pulse the servo again if rhe refresh time (20 ms) have passed:
  if (millis() - lastPulse >= refreshTime) {
    digitalWrite(servoPin, HIGH);   // Turn the motor on
    delayMicroseconds(pulse);       // Length of the pulse sets the motor position
    digitalWrite(servoPin, LOW);    // Turn the motor off
    lastPulse = millis();           // save the time of the last pulse
  }
 }

This is servo part is using the Servo.h library
Physcomp Lab4 - Using Servo Library from Zeven Rodriguez on Vimeo.
 /*
 Servo control from an analog input using the Arduino Servo library

 This example code uses the Arduino Servo library which comes packaged with the Arduino software.

 In order to make this work, you must include the Servo.h library file, create an instance of the Servo object. 
 attach a pin to the Servo object, and then write an analog value to the Servo object to set its 
 position.

 The difference between using the Servo library and the older method of pulsing a digital pin is that the library
 handles a lot of the work for you. You no longer need to figure out the translation between pulse length and position. 
 You now can simply specify the angle you'd like your servo to be at and it will turn to that position.

 Updated 08 Sep 2009
 by Rory Nugent
 Created 20 Jan 2009
 by Tom Igoe
 */

 #include <Servo.h>      // include the servo library

 Servo servoMotor;       // creates an instance of the servo object to control a servo

 int analogPin = 0;      // the analog pin that the sensor is on
 int analogValue = 0;    // the value returned from the analog sensor

 int servoPin = 2;       // Control pin for servo motor, may only be pin 9 or 10

 void setup() {
   servoMotor.attach(servoPin);  // attaches the servo on pin 2 to the servo object
 }

 void loop()
 {
   analogValue = analogRead(analogPin);                 // read the analog input (value between 0 and 1023)
   analogValue = map(analogValue, 0, 1023, 0, 179);     // map the analog value (0 - 1023) to the angle of the servo (0 - 179)
   servoMotor.write(analogValue);                       // write the new mapped analog value to set the position of the servo
   delay(15);                                           // waits for the servo to get there 
 }