Go Down

Topic: Makezine Arduino theremin code (Read 2898 times) previous topic - next topic

This was a cool project to make, but the author didn't not have the code attached to his post, so I made my own code for his design
              Webpage-  http://makezine.com/projects/arduino-theremin/


Code: [Select]

#include <NewPing.h>
#include <Servo.h>
#define PING_PIN  2  // Arduino pin tied to both trigger and echo pins on the ultrasonic sensor.
#define MAX_DISTANCE 300 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(PING_PIN, PING_PIN, MAX_DISTANCE); // NewPing setup of pin and maximum distance.

Servo myservo;  // create servo object to control a servo

int val;    // variable to read the value from the analog pin

int prPin = 0; // Pin where the photo resistor is connected to

int prReading; // The analog reading from the photoresistor

int buzzerPin = 3; // Connect Buzzer to Pin 4

long buzzerFreq; // The frequency to buzz the buzzer

// You can experiment with these values:
long BUZZ_FREQ_MAX = 2500; // Maximum frequency for the buzzer

long PR_MAX = 1023; // Maximum value for the photoresistor



void setup() {
  Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
pinMode(buzzerPin, OUTPUT); // set a pin for buzzer output
  myservo.attach(10);  // attaches the servo on pin 10 to the servo object
}

void loop() {
     prReading = analogRead(prPin); // Values 0-1023

    buzzerFreq = (prReading * BUZZ_FREQ_MAX) / PR_MAX;

    buzz(buzzerPin, buzzerFreq, 10);
   
  delay(30);                     // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
  Serial.print("Ping: ");
  Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance and print result (0 = outside set distance range, no ping echo)
  Serial.println("cm");
unsigned int cm = uS / US_ROUNDTRIP_CM;

   
   if(cm >= 45) {            // You can change values to suite
     val= 115;
   } 
   if(cm <= 39) {
     val= 170;
   } 
   if(cm <= 37) {
     val= 170;
   } 
   if(cm <= 33) {
     val= 170;
   } 
   if(cm <= 29) {
     val= 170;
   } 
   if(cm <= 27) {
     val= 170;
   }
  if( cm <= 25) {
    val = 170;
  } 
   if(cm <= 24) {
     val= 165;
   }
   if(cm <= 22) {
     val= 160;
   } 
   if(cm <= 20) {
     val= 155;
   } 
   if(cm <= 18) {
     val= 150;
   } 
   if(cm <= 16) {
     val= 145;
   }
    if(cm <= 14) {
     val= 140;
   } 
   if(cm <= 12) {
     val= 135;
   } 
   if(cm <= 10) {
     val= 130;
   } 
   if(cm <= 8) {
     val= 125;
   } 
   if(cm <= 6) {
     val= 120;
   } 
   if(cm <= 4) {
     val= 115;
   }
   
  myservo.write(val);                  // sets the servo position according to the scaled value
  delay(50);                         // waits for the servo to get there
}

void buzz(int targetPin, long frequency, long length) {

    long delayValue = 1000000/frequency/2;

    long numCycles = frequency * length/ 1000;

    for (long i=0; i < numCycles; i++){

        digitalWrite(targetPin,HIGH);

        delayMicroseconds(delayValue);

        digitalWrite(targetPin,LOW);

        delayMicroseconds(delayValue);

    }}

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy