big noob here.. just i have a class for my engineering program working with arduino and this forum has helped me through it so far. our goal for my current project is to modify project 06 in the arduino project book so one light sensor controls pitch and a second light sensor controls duration of the tone. im having a lot of trouble trying to get this code to work.. ill post what i have written anything helps at this point im totally hooped.
int sensorValue;
// variable to calibrate low value
int sensorLow = 1023;
// variable to calibrate high value
int sensorHigh = 0;
int sensorValue2;
// variable to calibrate low value
int sensorLow2 = 1023;
// variable to calibrate high value
int sensorHigh2 = 0;
// LED pin
const int ledPin = 13;
void setup() {
// Make the LED pin an output and turn it on
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);
// calibrate for the first five seconds after program runs
while (millis() < 5000) {
// save the maximum sensor value
sensorValue = analogRead(A0);
if (sensorValue > sensorHigh) {
sensorHigh = sensorValue;
}
// save the minimum sensor value
if (sensorValue < sensorLow) {
sensorLow = sensorValue;
}
// save the maximum sensor value
sensorValue2 = analogRead(A1);
if (sensorValue2 > sensorHigh) {
sensorHigh = sensorValue2;
}
// save the minimum sensor value
if (sensorValue < sensorLow) {
sensorLow = sensorValue;
}
}
//turn the LED off, signaling the end of the calibration
digitalWrite(ledPin, LOW);
}
void loop() {
//read the input from A0 and store it in a variable
sensorValue=analogRead(A0);
// map the sensor values to a wide range of pitches
int pitch=map(sensorValue, sensorLow, sensorHigh, 100, 1000);
}
//read the input from A1 and store it in a variable
sensorValue2=analogRead(A1);
// map the sensor values to a wide range of pitches
int tone=map(sensorValue2, sensorLow2, sensorHigh2, 1, 20);
// play the tone for 20 ms on pin 8
;
Errors, like double curly braces and extra semicolons. are harder to find in messy code.
I cleaned things up for you, so you can continue finishing the loop yourself.
Leo..
int sensorValue1, sensorValue2;
int sensorLow1 = 1023, sensorLow2 = 1023; // variable to reset low value
int sensorHigh1 = 0, sensorHigh2 = 0;; // variable to reset high value
const byte ledPin = 13; // LED pin
void setup() {
pinMode(ledPin, OUTPUT); // Make the LED pin an output and turn it on
digitalWrite(ledPin, HIGH);
while (millis() < 5000) { // calibrate for the first five seconds after program runs
sensorValue1 = analogRead(A0);
if (sensorValue1 > sensorHigh1) sensorHigh1 = sensorValue1; // save the maximum sensor value
if (sensorValue1 < sensorLow1) sensorLow1 = sensorValue1; // save the minimum sensor value
sensorValue2 = analogRead(A1);
if (sensorValue2 > sensorHigh2) sensorHigh2 = sensorValue2;
if (sensorValue2 < sensorLow2) sensorLow2 = sensorValue2;
digitalWrite(ledPin, LOW); //turn the LED off, signaling the end of the calibration
}
}
void loop() {
sensorValue1 = analogRead(A0); //read the input from A0 and store it in a variable
int pitch = map(sensorValue1, sensorLow1, sensorHigh1, 100, 1000); // map the sensor values to a wide range of pitches
sensorValue2 = analogRead(A1); //read the input from A1 and store it in a variable
int tone = map(sensorValue2, sensorLow2, sensorHigh2, 1, 20); // map the sensor values to a wide range of pitches
// play the tone for 20 ms on pin 8
}
#define NOTE_B0 31
#define NOTE_DS8 4978
int sensorValue;
int sensorLow = 1023; // variable to calibrate low value
int sensorHigh = 0; // variable to calibrate high value
//
int sensorValue2;
int sensorLow2 = 1023; // variable to calibrate low value
int sensorHigh2 = 0; // variable to calibrate high value
// LED pin
const byte ledPin = 13;
const byte pinTone = 8;
void setup()
{
// Make the LED pin an output and turn it on
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);
//calibrate for the first five seconds after program runs
while (millis() < 5000)
{
// save the maximum sensor value
sensorValue = analogRead(A0);
if (sensorValue > sensorHigh)
{
sensorHigh = sensorValue;
}
// save the minimum sensor value
if (sensorValue < sensorLow)
{
sensorLow = sensorValue;
}//if
// save the maximum sensor value
sensorValue2 = analogRead(A1);
if (sensorValue2 > sensorHigh )
{
sensorHigh = sensorValue2;
}
// save the minimum sensor value
if (sensorValue < sensorLow)
{
sensorLow = sensorValue;
}//if
}//while
//turn the LED off, signaling the end of the calibration
digitalWrite(ledPin, LOW);
}//setup
// state names
#define READ_SENSORS 0
#define PLAY_NOTE 1
//
void loop()
{
static byte
state = READ_SENSORS;
static unsigned long
tDur;
unsigned long
ulLength,
tNow;
tNow = millis();
switch( state )
{
case READ_SENSORS:
ulLength = map( analogRead( A1 ), sensorLow2, sensorHigh2, 50, 500 );
tone( pinTone, map( analogRead( A0 ), sensorLow, sensorHigh, NOTE_B0, NOTE_DS8 ) );
tDur = tNow;
state = PLAY_NOTE;
break;
case PLAY_NOTE:
if( (tNow - tDur) >= ulLength )
{
noTone( pinTone );
state = READ_SENSORS;
}//if
break;
}//switch
}//loop