Arduino & C++
Author |
Message |
Harblkipz

|
Posted: Sun May 31, 2009 5:22 pm Post subject: Arduino & C++ |
|
|
Hey Compsci, I have an Arduino set and I want to build a mini optical light Theremin. I have a buzzer, light sensor, and the main board. The coding for the Arduino though is C++ (Hence why I'm here). I know nothing about C++ coding, all I know is Turing, I have a mixed up code that doesn't work, I was wondering if anyone could possibly fix the bugs in it. I got the code from to examples from this site: http://web.media.mit.edu/~leah/LilyPad/index.html
Here's the code: | int ledPin = 13; // LED is connected to digital pin 13
int sensorPin = 0; // light sensor is connected to analog pin 0
int sensorValue; // variable to store the value coming from the sensor
int speakerPin = 9; // speaker connected to digital pin 9
void setup()
{
pinMode(ledPin, OUTPUT); // sets the ledPin to be an output
Serial.begin(9600); //initialize the serial port
digitalWrite(ledPin, HIGH); // turn the LED on
pinMode(speakerPin, OUTPUT); // sets the speakerPin to be an output
}
void loop() // run over and over again
{
sensorValue = analogRead(sensorPin); // read the value from the sensor
Serial.println(sensorValue); // send that value to the computer
delay(1000); // delay for 1/10 of a second
}
void beep (unsigned char speakerPin, int frequencyInHertz, long timeInMilliseconds) // the sound producing function
{
int x;
long delayAmount = (long)(1000000/frequencyInHertz);
long loopTime = (long)((timeInMilliseconds*1000)/(delayAmount*2));
for (x=0;x<loopTime;x++)
{
digitalWrite(speakerPin,HIGH);
delayMicroseconds(delayAmount);
digitalWrite(speakerPin,LOW);
delayMicroseconds(delayAmount);
}
}
void scale ()
{
digitalWrite(ledPin,HIGH); //turn on the LED
beep(speakerPin,sensorValue,500); //C: play the note C (C7 from the chart linked to above) for 500ms
digitalWrite(ledPin,LOW); //turn off the LED
}
|
What I want it to do is, when the light sensor turns its reading into a number, have the buzzer play the note corresponding to that number. I don't think its overly complicated but I could be wrong. I offer all the bits and karma I can give, can someone please help?
Thanks in advance! |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
|
|