theramin synth project, programming question(anolog offset)

This:

int measuredValue = analogRead(1);
int constrainedValue = constrain(measuredValue, analog1Offset, 900);
analog1 = map(constrainedValue, analog1offset, 900, 255, 0);

and this:

analog1 = map(measuredValue=constrain(measuredValue=analogRead(1), analog1offset, 900), analog1offset, 900, 255, 0);

are completely different.

The first allows you to see what is measured, what is constrained and what is mapped. The second does not.

Since your problem is that you do not where the problems in your hardware/software are, it is important to be able to verify that the hardware is producing the right results at the first hardware/software interface, not 27 steps later.

I tried the suggestion you made & got 24 error messages I couldn't figure out.

And, I suggested that you post the code that causes the errors, and the errors, and we'd explain what was wrong with your implementation of my suggestions, or what was wrong with my suggestions in the first place.

I did try the codes as you suggested, that's where i got all the error codes... then I tried modifying his - that's what you saw. I'll try it your way again & type out all the error codes(for some reason it won't let me copy from the error section). I'll post the errors when done! thank you

void loop()
{
heartrate–;
analogWrite(HEARTPIN, heartrate);
/* increments the ‘heartbeat’ LED on pin 11 downward. Every 256 times through the main loop

  • it will overflow to (underflow?) to 255. */

periodold = (period + periodold) / 2;
//average new values with old ones for better stability.
period = getfrequency();
//jump over to the getfrequency tab to measure the ultrasound distance and convert it to an appropriate range.

/************************** Read the analog photoresistor inputs **************************/
int measuredValue = analogRead(1);
int constrainedValue = constrain(measuredValue, analog1Offset, 900);
analog1 = map(constrainedValue, analog1offset, 900, 255, 0);
int measuredValue = analogRead(2);
int constrainedValue = constrain(measuredValue, analog2Offset, 900);
analog2 = map(constrainedValue, analog2offset, 900, 255, 0);
int measuredValue = analogRead(3);
int constrainedValue = constrain(measuredValue, analog3Offset, 900);
analog3 = map(constrainedValue, analog3offset, 900, 0, 500);
int measuredValue = analogRead(4);
int constrainedValue = constrain(measuredValue, analog4Offset, 900);
analog4 = map(constrainedValue, analog4offset, 900, 255, 230);
/
take the analog inputs and scale them to a byte datatype. Note that there’s some dead space at the top

  • and bottom of the sensor values-- so that the sensor stays at “off” or goes to “100%” at the end of travel. */

/******************************* Apply volume and effects **********************************/
if (abs(periodold - period) > 25) {
decay = 255;
}
else {
decay = ((decay * analog4) / 255);
}
//if a new tone is produced, reset decay to full, otherwise increment it downward.
analog3buffer = float(analog3) / 200;
//convert sensor 3 to a floating point number in the range of pi/16 to pi/4

for (byte i = 0; i < SAMPLESIZE ; i++) {
playbufferelement = (analog2sine _+ (255-analog2)overtones) / 255;_
_ /* waveform selection-- as analog2 ranges from 0-255 it changes the tone from_
_ * a pure sine wave to a more complex wavetype. /
playbufferelement = playbufferelement
cos(analog3bufferi);
/
Wah effect-- creates a variable comb filter. Note that as cos(0) = 1, when the input_

_ * from the sensor is 0, the waveform is unchanged /
playbufferelement = playbufferelement * analog1 * decay / 65025;_

_
//volume and decay. _
_ playbuffer = playbufferelement + 128;
//bias the values back to 0-255 for PWM output.
Serial.print("Measured value: ");
Serial.println(measuredValue);
}*_

}//end loop)

done1.cpp: In function 'void loop'()': done1:87: error: 'analogOffset' was not declared in this scope done1:89: error: redeclaration of 'int measuredValue' done1:86: error: 'int measuredValue' previously declared here done1:90: error: redeclaration of 'int conatrainedValue' done1:87: error: 'int constrainedValue' previously declared here done1:90: error: 'analog2Offset' was not declared in this scope done1:92: error: redeclaration of 'int measuredValue' done1:86: error: 'int measuredValue" previously declared here done1:93: error: redeclaration of "int constrainedValue' done1:87: error: 'int constrainedValue' previously declared here done1:93: error: 'analog3Offset' was not declared in this scope done1:95: error: redeclaration of 'int measuredValue' done1:86: error: 'int measuredValue" previously declared here done1:96: error: redeclaration of "int constrainedValue' done1:97: error: 'int constrainedValue' previously declared here done1:96: error: 'analog4Offset' was not declared in this scope

those are all the error codes from the loop before it - maybe now we can get somewhere! also, I don't know what I did wrong, but serial print is not working either... thanks, Daniel

done1:89: error: redeclaration of 'int measuredValue'

This one is pretty obvious. You can re-use a variable, but you can't declare a new one with the same name as an existing one, in the same scope.

Remove all the int statements here:

  int measuredValue = analogRead(2);
  int constrainedValue = constrain(measuredValue, analog2Offset, 900);
  int measuredValue = analogRead(3);
  int constrainedValue = constrain(measuredValue, analog3Offset, 900);
  int measuredValue = analogRead(4);
  int constrainedValue = constrain(measuredValue, analog4Offset, 900);

void loop()
{
heartrate–;
int analog1Offset;
analogWrite(HEARTPIN, heartrate);
/* increments the ‘heartbeat’ LED on pin 11 downward. Every 256 times through the main loop

  • it will overflow to (underflow?) to 255. */

periodold = (period + periodold) / 2;
//average new values with old ones for better stability.
period = getfrequency();
//jump over to the getfrequency tab to measure the ultrasound distance and convert it to an appropriate range.

/************************** Read the analog photoresistor inputs **************************/
int measuredValue = analogRead(1);
int constrainedValue = constrain(measuredValue, analog1Offset, 900);
analog1 = map(constrainedValue, analog1offset, 900, 255, 0);
analog2 = map(constrainedValue, analog2offset, 900, 255, 0);
analog3 = map(constrainedValue, analog3offset, 900, 0, 500);
analog4 = map(constrainedValue, analog4offset, 900, 255, 230);
/
take the analog inputs and scale them to a byte datatype. Note that there’s some dead space at the top

  • and bottom of the sensor values-- so that the sensor stays at “off” or goes to “100%” at the end of travel. */

/******************************* Apply volume and effects **********************************/
if (abs(periodold - period) > 25) {
decay = 255;
}
else {
decay = ((decay * analog4) / 255);
}
//if a new tone is produced, reset decay to full, otherwise increment it downward.
analog3buffer = float(analog3) / 200;
//convert sensor 3 to a floating point number in the range of pi/16 to pi/4

for (byte i = 0; i < SAMPLESIZE ; i++) {
playbufferelement = (analog2sine _+ (255-analog2)overtones) / 255;_
_ /* waveform selection-- as analog2 ranges from 0-255 it changes the tone from_
_ * a pure sine wave to a more complex wavetype. /
playbufferelement = playbufferelement
cos(analog3bufferi);
/
Wah effect-- creates a variable comb filter. Note that as cos(0) = 1, when the input_

_ * from the sensor is 0, the waveform is unchanged /
playbufferelement = playbufferelement * analog1 * decay / 65025;_

_
//volume and decay. _
_ playbuffer = playbufferelement + 128;
//bias the values back to 0-255 for PWM output.
Serial.print("Measured value: ");
Serial.println(measuredValue);
}
}//end loop)*_

"Get rid of the int keyword" and "Get rid of the whole statement" are not the same thing!!!!

PS. It's time you started posting code properly. Use the # icon before posting code, so the forum software stops mangling your code.

thank you! I made the changes & had to declare analog1Offset in scope & it compiled - awesome! I simply misunderstood your instructions before... i'll load it & try to print it.

ok, just read your comment - will redo

void loop()
{
heartrate–;
analogWrite(HEARTPIN, heartrate);
/* increments the ‘heartbeat’ LED on pin 11 downward. Every 256 times through the main loop

  • it will overflow to (underflow?) to 255. */

periodold = (period + periodold) / 2;
//average new values with old ones for better stability.
period = getfrequency();
//jump over to the getfrequency tab to measure the ultrasound distance and convert it to an appropriate range.

/************************** Read the analog photoresistor inputs **************************/
int measuredValue = analogRead(1);
int constrainedValue = constrain(measuredValue, analog1Offset, 900);
analog1 = map(constrainedValue, analog1offset, 900, 255, 0);
measuredValue = analogRead(2);
constrainedValue = constrain(measuredValue, analog2Offset, 900);
analog2 = map(constrainedValue, analog2offset, 900, 255, 0);
measuredValue = analogRead(3);
constrainedValue = constrain(measuredValue, analog3Offset, 900);
analog3 = map(constrainedValue, analog3offset, 900, 0, 500);
measuredValue = analogRead(4);
constrainedValue = constrain(measuredValue, analog4Offset, 900);
analog4 = map(constrainedValue, analog4offset, 900, 255, 230);
/
take the analog inputs and scale them to a byte datatype. Note that there’s some dead space at the top

  • and bottom of the sensor values-- so that the sensor stays at “off” or goes to “100%” at the end of travel. */

/******************************* Apply volume and effects **********************************/
if (abs(periodold - period) > 25) {
decay = 255;
}
else {
decay = ((decay * analog4) / 255);
}
//if a new tone is produced, reset decay to full, otherwise increment it downward.
analog3buffer = float(analog3) / 200;
//convert sensor 3 to a floating point number in the range of pi/16 to pi/4

for (byte i = 0; i < SAMPLESIZE ; i++) {
playbufferelement = (analog2sine _+ (255-analog2)overtones) / 255;_
_ /* waveform selection-- as analog2 ranges from 0-255 it changes the tone from_
_ * a pure sine wave to a more complex wavetype. /
playbufferelement = playbufferelement
cos(analog3bufferi);
/
Wah effect-- creates a variable comb filter. Note that as cos(0) = 1, when the input_

_ * from the sensor is 0, the waveform is unchanged /
playbufferelement = playbufferelement * analog1 * decay / 65025;_

_
//volume and decay. _
_ playbuffer = playbufferelement + 128;
//bias the values back to 0-255 for PWM output.
Serial.print("Measured value: ");
Serial.println(measuredValue);
}
}//end loop)*_

now analog1Offset not declared in scope now analog2Offset not declared in scope now analog3Offset not declared in scope now analog4Offset not declared in scope

where do I declare it(I know I sound like an idiot, but I think I'm almost there)...

where do I declare it(I know I sound like an idiot, but I think I'm almost there)...

You have global variables with similar names, except lower case o for offset. Perhaps that is what you (we) should be using...

HOO-RAY! I figured it out on my own(with your help). Compiles & loads now... but serial print still won't work. Almost there! What am I doing wrong with the serial print?

void loop()
{
heartrate–;
int analog1Offset;
int analog2Offset;
int analog3Offset;
int analog4Offset;
analogWrite(HEARTPIN, heartrate);
/* increments the ‘heartbeat’ LED on pin 11 downward. Every 256 times through the main loop

  • it will overflow to (underflow?) to 255. */

periodold = (period + periodold) / 2;
//average new values with old ones for better stability.
period = getfrequency();
//jump over to the getfrequency tab to measure the ultrasound distance and convert it to an appropriate range.

/************************** Read the analog photoresistor inputs **************************/
int measuredValue = analogRead(1);
int constrainedValue = constrain(measuredValue, analog1Offset, 900);
analog1 = map(constrainedValue, analog1offset, 900, 255, 0);
measuredValue = analogRead(2);
constrainedValue = constrain(measuredValue, analog2Offset, 900);
analog2 = map(constrainedValue, analog2offset, 900, 255, 0);
measuredValue = analogRead(3);
constrainedValue = constrain(measuredValue, analog3Offset, 900);
analog3 = map(constrainedValue, analog3offset, 900, 0, 500);
measuredValue = analogRead(4);
constrainedValue = constrain(measuredValue, analog4Offset, 900);
analog4 = map(constrainedValue, analog4offset, 900, 255, 230);
/
take the analog inputs and scale them to a byte datatype. Note that there’s some dead space at the top

  • and bottom of the sensor values-- so that the sensor stays at “off” or goes to “100%” at the end of travel. */

/******************************* Apply volume and effects **********************************/
if (abs(periodold - period) > 25) {
decay = 255;
}
else {
decay = ((decay * analog4) / 255);
}
//if a new tone is produced, reset decay to full, otherwise increment it downward.
analog3buffer = float(analog3) / 200;
//convert sensor 3 to a floating point number in the range of pi/16 to pi/4

for (byte i = 0; i < SAMPLESIZE ; i++) {
playbufferelement = (analog2sine _+ (255-analog2)overtones) / 255;_
_ /* waveform selection-- as analog2 ranges from 0-255 it changes the tone from_
_ * a pure sine wave to a more complex wavetype. /
playbufferelement = playbufferelement
cos(analog3bufferi);
/
Wah effect-- creates a variable comb filter. Note that as cos(0) = 1, when the input_

_ * from the sensor is 0, the waveform is unchanged /
playbufferelement = playbufferelement * analog1 * decay / 65025;_

_
//volume and decay. _
_ playbuffer = playbufferelement + 128;
//bias the values back to 0-255 for PWM output.
Serial.print("Measured value: ");
Serial.println(measuredValue);
}
}//end loop)*_

Almost there! What am I doing wrong with the serial print?

Read back a few replies, where I asked you to start posting code properly. Fix your last post.

Read back even more, where I suggested that the reason for separating the complex lines into three simpler lines was so that you could debug that code. Reading 4 pins, putting the reading into the same variable, and at the end of the code printing that one value doesn't tell you anything about whether the other three devices are working correctly.

To Paul S, i want to thank you for your help - you really do know how to help people. Instead of just giving me the answers i wanted, you made me figure it out on my own with hints... I've got a good handle on programming now! I finally figured it all out, bought the LDR's with the right ranges & it works awesomely! I could never thank you enough for what you've taught me... i was frustrated at first when you refused to give me direct answers, but I now know why! thanks for being my mentor & putting up with me Daniel

thanks for being my mentor & putting up with me

I'm very happy that you were able to get it to work. Thank you for the followup.