theramin synth project, programming question(anolog offset)

built it, it works, except there are 4 optical slide keys - only one works… I believe I need to change some variables, as my input rang from the photo resistors is way off. I’m pretty much a dummy with some of this, what does anolog offset mean? I have included attachments for sketch & schematic. here is an example of the part i don’t understand, although they might not even be the right variables to adjust… analog1 = map(constrain(analogRead(1), analog1offset, 900), analog1offset, 900, 255, 0); why so many #'s in one line? the instructions for the project can be found here: http://blog.wingedvictorydesign.com/2009/06/20/arduino-thereminsynth-final-walkthrough/all/1/ I’m desperate to get this thing going good - have $300 invested! please help if you can, would be greatly appreciated! ( the decay key is the on that works good - others are: volume, waveform, wah). this is the coolest project ever… I’ve made a few others, but nothing like this!

sketch.txt (10.3 KB)

Schematic-Rev0.pdf (111 KB)

int analog1 = 255; //volume
byte analog2 = 255; //waveform select

Great names. Not.

built it, it works, except there are 4 optical slide keys - only one works

What pins are they connected to?

I believe I need to change some variables, as my input rang from the photo resistors is way off.

Maybe. I have no idea what this means, as I have no idea what a photo resistor is. The only thing that comes to mind is someone who is camera shy, but that hardly seems to be something that an “input rang” could be obtained from.

here is an example of the part i don’t understand, although they might not even be the right variables to adjust… analog1 = map(constrain(analogRead(1), analog1offset, 900), analog1offset, 900, 255, 0); why so many #'s in one line?

Why did you write the code that way? Break it down into meaningful separate statements.

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

the function keys are connected to anologe pins 0 thru 3 (see attached schematic). I didn’t write the code, I got the entire project from the URL included in the post. a photo resistor changes value(ohms) with the amount of light hitting it.
thank you for the reply - I need all the help I can get… I’ve written easy codes before, but this ones way over my head as far as me having written it lol. That’s why I’m so desperate for assistance! I’ve gone over the schematic 50 times & am sure everything is right,& it does work wonderfully other than those 3 keys (i’m a newbey to Arduino, but have been doing electronic work for 40 years…)

one more thing: rang is a typo - sorry. it should be range.

I sincerely hope for assistance from someone knowledgeable enough to know what a photoresistor is instead of someone giving me a hard time that didn't even read the whole post... someone please come to my rescue!!

built it, it works, except there are 4 optical slide keys - only one works.

But, you didn't way which one. You didn't say what happens when the light hitting the others changes. You are not making it easy to help you.

the delay works good: the other 3 do nothing, but if i remove the slide key with an LED at the end and shine a flashlight directly on the photoresistor, it does start to work; that is why I deduced that the variables need adjustment - the output range is too low. there are several other variables, so i'm not even sure which one needs changing... although the ones I mentioned seem the most obvious. could you tell me what analog offset is? it must be a valid command, because the compiler did not reject it. the arduino uses 1024 steps of 49 mv each to go from a rang from 0 to 5v. the output from my photoresistors is from 2.556 to 3.994 v coming from sensor: removing the key with LED & shining a bright flashlight at the sensor drops the lower end to .571v,(which begins to work, so the bottom end must need to be near 0) so apparently my range is too high. question is, which direction should i change the variables? if i knew which variables to change, it wouldn't take long with experimentation to get it right. ( the actual output is measured in mv, not ohms, because there is an op amp feeding the photoresistors in series with the Arduino, so the ohms fluctuation alters the voltage output). I'm pretty good at this stuff, so if someone could figure out what variable(s) need changing, I could figure it out. my apologizes for the poor notes on the programming, but it was written by someone else! I am also going to make each of the 4 key LED's independently variable for brightness, so when I get close I'll be able to fine tune the system. the ultrasonic ping device works fine - I get a full 8 octaves of C, from bass to soprano. it's actually quite a lot of fun to play with. I've tried to include everything I could think of, but if you have more questions, just ask - I check regularly. thank you for your cooperation.

the delay works good: the other 3 do nothing

We're making progress. Slowly. Which pin is the delay connected to?

but if i remove the slide key with an LED at the end and shine a flashlight directly on the photoresistor, it does start to work

You seem to be very familiar with the hardware and music side of the device. I am not. You seem to be barely aware of the programming side. That is my area of expertise. If you could phrase your answers in terms of "the thing connected to pin n", I will have an easier time following.

So, what is a slide key, and why do you need to remove it to shine a flashlight on the photoresistor? Where should the light on the photoresistor be coming from?

that is why I deduced that the variables need adjustment - the output range is too low.

It could be that the input range is too large, instead. If the actual voltage that the pin sees varies from 0.1V to 0.25V, and you tell the map function that the range is from 0 to 500, that does not correlate well to the actual range of values seen.

could you tell me what analog offset is? it must be a valid command, because the compiler did not reject it

analogNoffset is not a command/function. It is the name of a variable that stores a value.

question is, which direction should i change the variables? if i knew which variables to change, it wouldn't take long with experimentation to get it right. ( the actual output is measured in mv, not ohms, because there is an op amp feeding the photoresistors in series with the Arduino, so the ohms fluctuation alters the voltage output).

You are reading the values of the various pins in setup(). Printing the values that you read to the serial monitor would give you an idea of the initial values.

On each pass through loop(), you read, constrain, and map all in one statement. Break those into three statements, and print out the values read from the analog pins, the constrained value, and the mapped value, for each pin.

It should become fairly obvious what the problem is, quickly.

first off, I'd really like to thank you for working with me on this! you seem very knowledgeable on the programming. I'll try & address your questions one by one: - delay is analog 4 in the sketch, connected to analog pin 3 on the Arduino - your right, the programming is where I get lost... the slide key is a hollow tube with a photoresistor at the bottom; there is another tube fitting inside the first with a green LED at the bottom end & the pink disks on top(see pic)... there is a compression spring in the larger tube, so that you push the key downward bringing the LED closer to the photoresistor - it returns to the all the way up position when released... a kind of potentiometer. the 4 keys are arranged in a pattern to fit your 4 fingers, the further each key travels down, the more effect you get(like i say, the delay key works fine). the ultrasonic ping unit is on the side of the enclosure - you move your other hand in & out from it (up to 3 feet) to get the different tones, bass tones being furthest out. the ping unit sends a modulated square wave to the Arduino to produce the variation in pitch. I pulled the inner tube with LED out & used a bright flashlight to see if more light would make it work, it does.. that's why I feel the variables need some adjustment. - your next point I understand - the range is either to large or small, or simply needs to move up or don between 0 & 5v. that's why i thought it might be possible to adjust the variables to accomplish this. if you could help me figure out which variables to change, I could simply experiment with it. another possibility would be getting photoresistors with a different rating or range... - your last point will take me some time; i'll work on it & get back to you. this should all give you a better idea of what I have here. meanwhile, if you could examine the sketch & determine which variable(s) might need adjustment, it would be a great help... you obviously know the programming end inside out! thank you so much for coming to my aid, I think this can work out somehow! Daniel

your last point will take me some time; i’ll work on it & get back to you. this should all give you a better idea of what I have here. meanwhile, if you could examine the sketch & determine which variable(s) might need adjustment, it would be a great help

Back in Reply #1, I showed you how to separate the read, constrain, and map statement into three separate statements.

Add statements like these

Serial.print("Measured value: ");
Serial.println(measuredValue);

after each variable is valued. Once we can see whether there is a hardware issue (the measured value doesn’t seem to have the right range of values) or a software issue (the value is not constrained, mapped, or used correctly), we’ll have a much better idea how to proceed.

If it is a software issue, the constrain statements may need adjusting. The map statements may need adjusting. The way that the measured, constrained, and mapped values are used to produce the effect may need to be changed (I have no idea whether that logic is correct, or not). Or, the way that the resulting values are output may need to be changed (again, I have no idea about that part of the program).

But, lets get some data so we know what we are dealing with before we worry about what needs to be changed.

Good morning Paul... It's 7:00 AM here(Minnesota) & I just read your post. I will begin work on the coding & serial print(I've done that before). the person's blog where I found this, his unit did all work, so I assume the program does work if I had the correct parameters on my hardware... so in the end, one of 2 things will happen: either we can change the coding to fit my hardware, or I will have to obtain new photoresistors with the proper range/parameters. sounds logical to me, anyway... I'm very lucky to have come across someone who knows what they're doing with the coding(you)! what's frustrating is on his schematic, everything is clearly marked with values except the photoresistors... & just like most components, they come in a multitude of different ratings & ranges! I have other things to do today also, but i'll get the serial print posted later & we can see where we're actually at. thanks again for all your help; with any luck I may start understanding the coding end of this & be better equipped in the future. Daniel

good evening Paul... I really am realizing what a dummy I am when it comes to programming. I made the changes you put in response #1 & it would not compile - got many error messages. then, I decided to leave that part the way it was & do the serial print thing, I had to declare it in the scope... it compiled fine, but when I uploaded it it gave me the message "Com port 5 already in use, quit all programs using that port" not sure what to make of it - there are no other programs using it that I'm aware of. I'm at my wit's end here, but refuse to give up. I know I've asked a lot of you already, but is there some way you could make the changes for me & post it or email to me? Email is konold1@hotmail.com I really appreciate what you've done for me & would be ecstatic if you could do that! or if you have any other ideas or can direct me how to do it, thats fine also - you'r the boss here!

I made the changes you put in response #1 & it would not compile - got many error messages.

Let’s see the modified code and the error messages.

hi... i just read this late at night, will post in morning. thanks for hanging in there with me!

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 **************************/
analog1 = map(measuredValue=constrain(measuredValue=analogRead(1), analog1offset, 900), analog1offset, 900, 255, 0);
analog2 = map(measuredValue=constrain(measuredValue=analogRead(2), analog2offset, 900), analog2offset, 900, 255, 0);
analog3 = map(measuredValue=constrain(measuredValue=analogRead(3), analog3offset, 900), analog3offset, 900, 0, 500);
analog4 = map(measuredValue=constrain(measuredValue=analogRead(4), analog4offset, 900), 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)

Paul, I have posted the changes I made to the loop. I also had to declare measureValue at the beginning... I tried the changes from response # 1, worked with it a long time - it just wont work the way this guy has the programming set up(it probably could if I knew what I was doing). there were 26 error messages when trying to compile - I must be a dummy with the programming, but couldn't get it to work! the way it is now, it compiles & uploads fine & works the same as before... BUT, the serial monitor shows nothing. I suspect I made a simple mistake there. if you have time, could you look & see why i'm getting no serial print?(I've done serial print with other sketches successfully).I tried to copy the 26 error messages for you, but the sketch template does not allow copying from the error section. Please help! I'm going crazy here & wasting a lot of your time! Daniel

Got a multimeter? It may help to get the actual values on the analog pins and compare to the one that works, just to make sure its not a hardware problem

hello Paul S, you still there?

you still there?

I am. I made a reasonable suggestion as to how to break that one complex line of code into 3 lines that could be debugged. You ignored that advice and mangled up some ridiculous code. I thought, therefore, that you were done listening to me.

I was not done listening to you - I tried the suggestion you made & got 24 error messages I couldn't figure out... I really tried to get the serial print working to no avail. I have calculated the correct values for the LDR's(photoresistors) & have ordered them. this should take care of my problem. I am sincerely sorry if I offended you - I'm just horrible at programming lol. if you could make any further suggestions, I would really appreciate it... you know, everyone has to start somewhere, I'm sorry I'm so bad at programming. Sincerely, Daniel

P.S. you did give me a lot of insight on programming & I would like to continue.