Pages: 1 2 [3]   Go Down
Author Topic: theramin synth project, programming question(anolog offset)  (Read 2237 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

#
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 = (analog2*sine + (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(analog3buffer*i);
    /* 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)
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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)...
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 617
Posts: 49463
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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...
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

#
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 = (analog2*sine + (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(analog3buffer*i);
    /* 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)

Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 617
Posts: 49463
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 27
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 617
Posts: 49463
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

Pages: 1 2 [3]   Go Up
Jump to: