View on GitHub

oddgui

Lab 4 Servo and Tone

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.

</param></param></param></embed>

Physcomp Lab4 – Photocell tone from Zeven Rodriguez on Vimeo.

This code is to check the range of the photocells

voidsetup() { Serial.begin(9600); } voidloop() { 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 Hagmanhttp://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 noiseMaker; // instance of the tone libraryvoid**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

</param></param></param></embed>

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 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

</param></param></param></embed>

Physcomp Lab4 - Musical Instrument from Zeven Rodriguez on Vimeo.
*#include * *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.

**

</param></param></param></embed>

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

This code does not use the arduino servo library

*/* Servo control from an analog inputThe minimum (minPulse) and maxiumum (maxPulse) valueswill 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 waslast pulsed. millis() produces an overflow error (i.e. generates a numberthat’s too big to fit in a long variable) after about 5 days. if you’remaking a program that has to run for more than 5 days, you may need toaccount for this.by Tom Igoeadditions by Carlyn Maw & Rob FaludiCreated 28 Jan. 2006Updated 10 Jun. 2008*/int servoPin = 2; // Control pin for servo motorint minPulse = 500; // Minimum servo positionint maxPulse = 2500; // Maximum servo positionint pulse = 0; // Amount to pulse the servolong lastPulse = 0; // the time in milliseconds of the last pulseint refreshTime = 20; // the time needed in between pulsesint analogValue = 0; // the value returned from the analog sensorint analogPin = 0; // the analog pin that the sensor’s onvoidsetup() { pinMode(servoPin, OUTPUT); // Set servo pin as an output pin pulse = minPulse; // Set the motor position value to the minimumSerial.begin(9600); } voidloop() { 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 ondelayMicroseconds(pulse); // Length of the pulse sets the motor positiondigitalWrite(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 <div style=" text-align:center;"></param></param></param></embed></div>Physcomp Lab4 - Using Servo Library from Zeven Rodriguez on Vimeo. */*Servo control from an analog input using the Arduino Servo libraryThis 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 libraryhandles 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 2009by Rory NugentCreated 20 Jan 2009by Tom Igoe*/ #include «span style=”color: #cc6600;”>Servo</span>.h> // include the servo libraryServo servoMotor; // creates an instance of the servo object to control a servoint analogPin = 0; // the analog pin that the sensor is onint analogValue = 0; // the value returned from the analog sensorint servoPin = 2; // Control pin for servo motor, may only be pin 9 or 10voidsetup() { servoMotor.attach(servoPin); // attaches the servo on pin 2 to the servo object } voidloop() { 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 servodelay(15); // waits for the servo to get there } *