my code doesn't do waht i think it ought to..

I have a potentiometer strip with an open wiper. I want to have it so that when the wiper is depressed and moved it reports a relative value rather than an absolute one. In this case the scale is bipolar with the ‘centre’ at a value of 8192.

My code doesn’t do it…
a) why?
b) what’s a good way to do it?

Thanks!

#include <MIDI.h>



const int pitchpin = A1;

// variables:
int sensorValue = 0;         // the sensor value
int sensorMin = 1023;        // minimum sensor value
int sensorMax = 0;           // maximum sensor value
int midval = 0;
int prevmidval = 0;
int THRESHOLD = 2;
int pitchbendval = 0;
int pitchdif = 0 ;
int prevpitch = 8192;
int pitchthresh = 0;
int pitchsend = 8192;

void setup()
 
  {
  Serial.begin(1200
  );
}
 

void loop()

while pitchbendval >100 ;
{

  pitchbendval = analogRead(pitchpin) ;
  pitchbendval = map(pitchbendval, 0, 1023, 0, 16383);
 
  pitchdif = (pitchbendval - prevpitch);
  pitchsend = (pitchsend + pitchdif) ;
 
 
  prevpitch = pitchbendval;
 

  Serial.println(pitchsend, DEC);
}

I want to have it so that when the wiper is depressed and moved it reports a relative value rather than an absolute one.

Relative to what?

Relative to the point at which it's first depressed - eg. I touch the pot at 8" and wipe it to 5" - i want it to report the same series of values as if I touched it at 4" and moved it to 1" - does that make sense?

Relative to the point at which it's first depressed - eg. I touch the pot at 8" and wipe it to 5" - i want it to report the same series of values as if I touched it at 4" and moved it to 1"

You will need to keep track of the initial/previous reading, then. It's not hard. You just need to determine when a new reading starts.

Do you have a link to the datasheet for this "Potentiometer strip with an open wiper".

Why wouldn't it? It the part is truly linear, say it was 1000 ohm, 10" and you had 5V at one end and ground at the other. Every 1" would be 1/2V difference. Then the voltage at 8" would be 4V, and at 5" it would be 2.5V. A difference of 1.5.

Then 4" to 1" would be 2V and 0.5V, a difference of 1.5.

0" 0 1" .5 2" 1 3" 1.5 4" 2 5" 2.5 6" 3 7" 3.5 8" 4 9" 4.5 10" 5

with an open wiper

A pot with an open wiper (until pressed?) is going to an electrical challenge to deal with. When it's not pressed it will present a 'floating input' to the analog input pin. Your sketch doesn't seem to know if there is a valid time to read the analog input, that is how does it know it the pot wiper is 'active' Vc not pressed?

Possibly a pull-down or pull-up resistor will give your channel a 'default' value that you can ignore in your sketch?

Lefty

Your code has some syntax errors.

{ must always be balanced with a }, and while-s and functions are regularly never followed by ;

//0 scope void myFunciton() { //+1 scope while (condition) { //+2 scope ///your code } //+2-1 scope } //+1-1 scope //0 scope

Heres that Datasheet.

http://www.google.com/url?sa=t&source=web&cd=1&ved=0CBcQFjAA&url=http%3A%2F%2Fwww.sparkfun.com%2Fdatasheets%2FSensors%2FFlex%2FSoftPot-Datasheet.pdf&rct=j&q=softpot%20datasheet&ei=hxtHTaOuJcH-8Abf2_2GAg&usg=AFQjCNGTrWW3rJLCaSclwzZhQl3jXMRZqQ&sig2=ueQhPdEA97H925aPGjN_lA&cad=rja

You will need to keep track of the initial/previous reading, then. It's not hard. You just need to determine when a new reading starts.

  • precisely - that's what my code is trying to do - but it isn't working properly

Possibly a pull-down or pull-up resistor will give your channel a 'default' value that you can ignore in your sketch?

-Yes, I have a pull down giving me a 0 to ignore - that is my next coding challenge. I need it to read the input, and when it sees >0 to jump to a new loop - how do i structure that? is it with the 'if' command? or can I use the 'while' that i attempted here?

Thanks

Ok - I’ve solved one problem, but still have my main one.

My code now has a loop that just operates when the wiper is depressed.

I want it to return a series of relative values depending on the distance it is ‘wiped’ from the start point. It doesn’t. What is wrong with my code? How do I do this properly.

Your help is eagerly anticipated!

#include <MIDI.h> 



const int pitchpin = A1;

// variables:
int sensorValue = 0;         // the sensor value
int sensorMin = 1023;        // minimum sensor value
int sensorMax = 0;           // maximum sensor value
int midval = 0;
int prevmidval = 0;
int THRESHOLD = 2;
int pitchbendval = 0;
int pitchdif = 0 ;
int prevsend = 0 ;
int prevpitch = 500;
int pitchthresh = 0;
int pitchsend = 500;

void setup() 
  
  {
  Serial.begin(1200
  );
}
  

void loop() {



pitchbendval = analogRead(pitchpin) ;
pitchsend=500 ;
prevpitch=500 ;
pitchdif=0;
prevsend = 0 ;
while (pitchbendval > 2) {

  pitchbendval = analogRead(pitchpin) ;
  
  
  pitchdif = (pitchbendval - prevpitch);
  pitchsend = (prevsend - pitchdif) ;
  prevsend = pitchsend ;



 
//  
//  Serial.println(pitchdif, DEC);
//      Serial.println(prevpitch, DEC);
//  Serial.println(pitchbendval, DEC);
//    Serial.println(pitchsend, DEC);
//      Serial.println (9999, DEC) ;

 prevpitch = pitchbendval;
  Serial.println(pitchsend, DEC);
}
    
}
pitchbendval = analogRead(pitchpin) ;    // this doesn't achieve anything
pitchsend=500 ;
prevpitch=500 ;
pitchdif=0;
prevsend = 0 ;
while (pitchbendval > 2) {

  pitchbendval = analogRead(pitchpin) ;

In your code you have no recollection of the original value of the the pin, as it is overwritten each time through the loop.