Light tone generator.

Submitted by thomas.may on Tue, 03/05/2013 - 18:31

Building on the potentiometer lab, where I used a millis() approach to measure the interval between write states of the LED which was as an alternative to using delay(), I decided to use this same technique of making the lights blink to influence the reading of the photoresistor, thus creating a square wave oscillation. There is two LED, thus two "oscillators". The analog read of the photoresistor is then passed to the digital output on which the piezo buzzer is on, creating tones based on the oscillations of the LEDs as well as from the ambient light which influence mostly the times when both LED are off.

In Action.

http://youtu.be/0Ry9YHPc57M

Arduino Code:

 

/* Theremin
 * --------

 *

 *

 * Created 24 October 2006

 * copyleft 2006 Tod E. Kurt <tod@todbot.com

 * http://todbot.com/

 */


int potPin = 0;    // select the input pin for the potentiometer

int speakerPin = 7;

const int ledPin0 =  9;

const int ledPin1 =  10;

int val = 0;

int ledState0 = LOW;

int ledState1 = LOW;

long previousMillis0 = 0; 

long previousMillis1 = 1;

long interval0 = 1000;

long interval1 = 1000;

void setup() {

  pinMode(ledPin0, OUTPUT);

  pinMode(ledPin1, OUTPUT); 

  pinMode(speakerPin, OUTPUT); 

  Serial.begin(9600);

  Serial.println("ready");

}


void loop() {

  long interval0 = analogRead(A2);

  long interval1 = analogRead(A1);

  

  unsigned long currentMillis = millis();

  digitalWrite(speakerPin, LOW);

  

  if(currentMillis - previousMillis0 > interval0) {

    // save the last time you blinked the LED 

    previousMillis0 = currentMillis;   


    // if the LED is off turn it on and vice-versa:

    if (ledState0 == LOW)

      ledState0 = HIGH;

    else

      ledState0 = LOW;


    // set the LED with the ledState of the variable:

    digitalWrite(ledPin0, ledState0);

  }

    if(currentMillis - previousMillis1 > interval1) {

    // save the last time you blinked the LED 

    previousMillis1 = currentMillis;   


    // if the LED is off turn it on and vice-versa:

    if (ledState1 == LOW)

      ledState1 = HIGH;

    else

      ledState1 = LOW;


    // set the LED with the ledState of the variable:

    digitalWrite(ledPin1, ledState1);

  }

  

  val = analogRead(potPin);    // read value from the sensor

                   // process the value a little

    digitalWrite(speakerPin, HIGH);

    delayMicroseconds(val);

    digitalWrite(speakerPin, LOW);

    delayMicroseconds(val);


}

 

1262_HW5_0.jpg
0
Your rating: None
Drupal theme by Kiwi Themes.