Saving 2 Pot values for later use

Hi, I'm trying to create a app to read 2 Pot values. There will be 3 buttons. I'd like to hit a button, save the values-then move the pots, save the values-then utilize that data later...But I can't seem to make it work. Button 1 will save the 2 pot values to start1, start2. Button 2 will save save the pot values to start2, end2. Then Button 3 currently just displays the data. Also, besides the point, I can't figure out how to make it stop displaying data. Would a function be useful here? I tried that, but had much bad luck...

So far, all I can get out are the same pot values on start1, start2 as end1, end2. What the??? I must be missing something basic. I'm a newbie, so please bear with me. Any thoughts here?

int potPin1 = 0;
int potPin2 = 1;
int potVal1 = 0;
int potVal2 = 0;
int panVal = 0;
int tiltVal = 0;
int startPin = 11; // start pin
int endPin = 12; // end pin
int runPin = 8; // run pin

void setup()
{
Serial.begin(9600);
pinMode(startPin, INPUT); // declare pushbutton as input
pinMode(endPin, INPUT); // declare pushbutton as input
pinMode(runPin, INPUT); // declare pushbutton as input
}

void loop()
{
// Control servos with Pots
potVal1 = analogRead(potPin1);
potVal2 = analogRead(potPin2);

// Read button 1
int startVal;
int endVal;
int start1 = 0;
int start2 = 0;
startVal = digitalRead(startPin); // read input value
if (startVal == HIGH) { // check if the input is HIGH
start1 = potVal1; // Set start point for pan
start2 = potVal2; // Set start point for pan
}

// Read button 2
int end1=0;
int end2=0;
endVal = digitalRead(endPin); // read input value
if (endVal == HIGH) { // check if the input is LOW
end1 = potVal1; // Set end point for pan
end2 = potVal2; // Set end point for tilt
}

// Read 3rd button and display results
int runVal = 0;
runVal = digitalRead(runPin); // read input value
if (runVal == HIGH) { // check if the input is HIGH

Serial.println("start1 = ");
Serial.println(start1, DEC);

Serial.println("start2 = ");
Serial.println(start2, DEC);

Serial.println("end1 = ");
Serial.println(end1, DEC);

Serial.println("end2 = ");
Serial.println(end2, DEC);
}

}

Hi, I'm trying to create a app to read 2 Pot values. There will be 3 buttons. I'd like to hit a button, save the values-then move the pots, save the values-then utilize that data later...But I can't seem to make it work. Button 1 will save the 2 pot values to start1, start2. Button 2 will save save the pot values to start2, end2. Then Button 3 currently just displays the data. Also, besides the point, I can't figure out how to make it stop displaying data. Would a function be useful here? I tried that, but had much bad luck...

So far, all I can get out are the same pot values on start1, start2 as end1, end2. What the??? I must be missing something basic. I'm a newbie, so please bear with me. Any thoughts here?

int potPin1 = 0;
int potPin2 = 1;
int potVal1 = 0;
int potVal2 = 0;
int panVal = 0;
int tiltVal = 0;
int startPin = 11; // start pin
int endPin = 12; // end pin
int runPin = 8; // run pin

void setup()
{
Serial.begin(9600);
pinMode(startPin, INPUT); // declare pushbutton as input
pinMode(endPin, INPUT); // declare pushbutton as input
pinMode(runPin, INPUT); // declare pushbutton as input
}

void loop()
{
// Control servos with Pots
potVal1 = analogRead(potPin1);
potVal2 = analogRead(potPin2);

// Read button 1
int startVal;
int endVal;
int start1 = 0;
int start2 = 0;
startVal = digitalRead(startPin); // read input value
if (startVal == HIGH) { // check if the input is HIGH
start1 = potVal1; // Set start point for pan
start2 = potVal2; // Set start point for pan
}

Here you save the pot values to start1 and 2. There're a couple of things here that could be better. Why not name your variables something meaningful like pan_start & (presumably) tilt_start? That may alleviate some confusion. You've also fallen into the trap of documenting "what" instead of "why" - the comments // Set start point for pan are superfluous, and wrong, unless both your pots measure pan.

// Read button 2
int end1=0;
int end2=0;
endVal = digitalRead(endPin); // read input value
if (endVal == HIGH) { // check if the input is LOW

Another comment that disagrees with the code. Which is correct?

Your larger problem is that startX and endX are local to the loop block. That means if both buttons are pressed you'll read identical values into each, and if not, you'll just see zeroes for the readings for the button that wasn't being pressed. Move the 'int start/endX' declarations outside the main loop will mean the values are preserved between calls to loop, and you should see different results.

This is almost certainly also the reason for the continued display of data you mention.

OK I tried to fix up the code, and altered it for use of controlling the pan/tilt start and end of 2 servos. The problem I'm still having is the startpan, endpan and starttilt, endtilt are always the same. I've tried so many ways of doing this I'm starting to lose my mind.

My ultimate goal here would be to:
Using 2 buttons, set the start and end points for the servos. Then with the 3rd button, run a move between the two points, basically creating a very simple motion control rig.

//Example code for using ServoTimeTimer1 library
// hardware control of up to two servos, on Arduino pins 9 & 10
// with additional code to set start/end points for more servo manipulation

#include <ServoTimeTimer1.h>

ServoTimeTimer1 servo1;
ServoTimeTimer1 servo2;

int potPin1 = 0;
int potPin2 = 1;
int potVal1 = 0; // raw pot1 value, used for pan
int potVal2 = 0; // raw pot2 value, used for tilt
int startPin = 11; // start pin
int endPin = 12; // end pin
int runPin = 8; // run pin

int panVal; // use for actual pan value after calculation of potVal1
int tiltVal; // use for actual tilt value after calculation of potVal2

int startVal = 0;
int endVal = 0;
int runVal = 0;

int startpan;
int starttilt;
int endpan;
int endtilt;

void setup()
{
Serial.begin(9600);
servo1.attach(9);
servo2.attach(10);
pinMode(startPin, INPUT); // declare pushbutton as input
pinMode(endPin, INPUT); // declare pushbutton as input
pinMode(runPin, INPUT); // declare pushbutton as input
// servo1.write(1200); // send pan servo to about mid-range
// servo2.write(750); // send tilt servo to about mid-range
}

void loop()
{
// Control servos with Pots
potVal1 = analogRead(potPin1);
potVal2 = analogRead(potPin2);
//Here we convert a 0-1023 range to a 500-2546 range
//2546 is clipped to the maximum value of 2500 internally
panVal = ((potVal12)+500);
tiltVal = ((potVal2
2)+500);
servo1.write(panVal);
servo2.write(tiltVal);
readbuttons();
}

void readbuttons(){
// Read Start button
startVal = digitalRead(startPin); // read input value
if (startVal == HIGH) { // check if the input is HIGH
startpan = panVal; // Set start point for pan
starttilt = tiltVal; // Set start point for tilt
}

// Read End button
endVal = digitalRead(endPin); // read input value
if (endVal == HIGH) { // check if the input is HIGH
endpan = panVal; // Set end point for pan
endtilt = tiltVal; // Set end point for tilt
}

// Read RUN button
runVal = digitalRead(runPin); // read input value
if (runVal == LOW) { // check if the input is LOW
runfunction();
}
}

int runfunction(){
Serial.println("");
Serial.println("Run");

Serial.println("start pan = ");
Serial.println(startpan, DEC);

Serial.println("start tilt = ");
Serial.println(starttilt, DEC);

Serial.println("end pan = ");
Serial.println(endpan, DEC);

Serial.println("end tilt = ");
Serial.println(endtilt, DEC);
}

Essentially what your algorithm boils down to is:

Read values for pots.
Convert values to servo-friendly ranges.
If button1 is down (input is HIGH), store values in startX variables
If button2 is down (input is HIGH), store values in endX variables.
If button3 is up (input is LOW), display startX and endX variables (or do something else with them).

Simple enough, which means either there's a logic error in the implementation (and at first glance it looks ok), or one of your assumptions is wrong. What is the physical process you are using (e.g. press button 1 and hold it down while adjusting the pots, then press button 2)? Are you sure the buttons generate a HIGH when pressed? From your description, the startX and endX values should only be identical when both buttons are pressed - does this indeed correspond to both button inputs being HIGH?

Reduce the code to the smallest set that will still exhibit the problem. I would isolate a single pot and button, and make a sketch that does nothing but output the values of the pot and the button repeatedly so you can check that these items function in isolation. Then gradually add more complexity. If you follow this approach you should rapidly arrive at a solution.

Essentially what your algorithm boils down to is:

Read values for pots.
Convert values to servo-friendly ranges.
If button1 is down (input is HIGH), store values in startX variables
If button2 is down (input is HIGH), store values in endX variables.
If button3 is up (input is LOW), display startX and endX variables (or do something else with them)

Yes, that is all correct. For some reason, I have to use the LOW input for button3-maybe it's my switch. I don't know-but it works this way.

Simple enough, which means either there's a logic error in the implementation (and at first glance it looks ok), or one of your assumptions is wrong. What is the physical process you are using (e.g. press button 1 and hold it down while adjusting the pots, then press button 2)? Are you sure the buttons generate a HIGH when pressed? From your description, the startX and endX values should only be identical when both buttons are pressed - does this indeed correspond to both button inputs being HIGH?

I have tested using an LED to light when I hit the buttons, so I'm sure all buttons work properly as expected. The startx, endx values should only be identical when they are both pressed one after the other, before the servos are moved. But if I set my startx position on the servo, hit button 1 to save them, then move the servos to the endx position, hit button 2 to save them, all should be good right? I hit button 3 to print them and the startx and endx are the same. I can't figure out what I'm doing wrong here.

Reduce the code to the smallest set that will still exhibit the problem. I would isolate a single pot and button, and make a sketch that does nothing but output the values of the pot and the button repeatedly so you can check that these items function in isolation. Then gradually add more complexity. If you follow this approach you should rapidly arrive at a solution.

*Believe me, I've done it about 10 times now over and over. I'm at a total loss here. I can't understand why it doesn't work. *

What happens if you (ignoring pot2/ servo2 entirely) print out the start and end values whenever any button is pushed? Do these values change (assuming you adjust the position between button pushes)?

I just figured it out! For whatever stupid reason-I'm sure it will hit me soon enough-that if I changed the button values to check for LOW instead of high, it works perfectly now. Geeeeeez. All that time for something so dumb.
Thanks guys for all your help!

void readbuttons(){
// Read Start button
startVal = digitalRead(startPin); // read input value
if (startVal == LOW) { // check if the input is LOW
startpan = panVal; // Set start point for pan
// starttilt = tiltVal; // Set start point for tilt
}

// Read End button
endVal = digitalRead(endPin); // read input value
if (endVal == LOW) { // check if the input is LOW
endpan = panVal; // Set end point for pan
// endtilt = tiltVal; // Set end point for tilt
}