Some Help saving and recalling servo commands with push-buttons?

Hey guys - im using a modified 'knob' sketch to control 4 sail winch servos independently, while attached to amplifier tone controls, Its all working perfectly - thanks to the help of some of the community members !
I need to be able to store and recall the settings when the device is in use, but there is no need to permanently store a setting, i just want to be able to do it while the device is turned on.
So i have two push buttons, one for 'save' and one for 'recall' . i have NO IDEA how to do this, my tutor seemed to think that i could;

1.create 4 new variables

2.press button A to copy the analog values which are received by the arduino via potentiometer input (one for each corresponding servo)

  1. Press the other button (B) to get the values back.

My questions are:

So would the buttons be plugged into digital or analogue input?

What code functions are used for temporarily storing 4 analogue values (each between 0-1023) ?

How would i recall these values and send them to the servos, ordering them to move to the saved position?

my current code is below.

PLEASE don't refrain from commenting as any advice is taken on board!
thanks, Si

#include <Servo.h> 
 
Servo myservo1;  // create servo object to control a servo 
Servo myservo2; 
Servo myservo3;
Servo myservo4;

int potpin = 0;  // analog pin used to connect the potentiometer
int val1;    // variable to read the value from the analog pin 
int potpin2 = 1;  // analog pin used to connect the potentiometer
int val2;    // variable to read the value from the analog pin 
int potpin3 = 2; // analog pin used to connect the potentiometer
int val3;    // variable to read the value from the analog pin 
int potpin4 = 3; // analog pin used to connect the potentiometer
int val4;    // variable to read the value from the analog pin 
 


void setup() 
{ 
  myservo1.attach(9);  // attaches the servo on pin 9 to the servo object 
  
  myservo2.attach(10);  // attaches the servo on pin 10 to the servo object 
  
  myservo3.attach(5);  // attaches the servo on pin 11 to the servo object 
  
  myservo4.attach(6); // attaches the servo on pin 12 to the servo object 
  


}

void loop() 
{ 
  val1 = analogRead(potpin);            // reads the value of the potentiometer (value between 0 and 1023) 
  val1 = map(val1, 0, 1023, 1500, 2425);     // scale it to use it with the servo (value between 0 and 180) 
  myservo1.writeMicroseconds(val1);                  // sets the servo position according to the scaled value 
                             // waits for the servo to get there 
  val2 = analogRead(potpin2);            // reads the value of the potentiometer (value between 0 and 1023) 
  val2 = map(val2, 0, 1023, 1500, 2425);     // scale it to use it with the servo (value between 0 and 180) 
  myservo2.writeMicroseconds(val2);                  // sets the servo position according to the scaled value 
  
  
  val3 = analogRead(potpin3);            // reads the value of the potentiometer (value between 0 and 1023) 
  val3 = map(val3, 0, 1023, 1500, 2425);     // scale it to use it with the servo (value between 0 and 180) 
  myservo3.writeMicroseconds(val3);                  // sets the servo position according to the scaled value 

  val4 = analogRead(potpin4);            // reads the value of the potentiometer (value between 0 and 1023) 
  val4 = map(val4, 0, 1023, 1500, 2425);     // scale it to use it with the servo (value between 0 and 180) 
  myservo4.writeMicroseconds(val4);                  // sets the servo position according to the scaled value 
  delay(15); 
}

So would the buttons be plugged into digital or analogue input?

Are they analog devices (some range of output voltages/values) or digital devices (on/off)? Seems like this should be pretty easy to answer. If not, start over at the beginning.

What code functions are used for temporarily storing 4 analogue values (each between 0-1023) ?

The assignment operator (=). Of course, you need to define some place to store those values. If they are to make any sense, though, you need to store when a change occurred, or how long to stay at a particular setting.

How would i recall these values

That would depend on how you saved them.

and send them to the servos, ordering them to move to the saved position?

The same way you move the servos in the first place.

Is there some reason you are not using the simpler write() function, setting the servo to a specific angle?

A couple of issues with your requirements:

What happens after you press button B? Does the system stay at the 'B' setting forever? It might make more sense to be able to push 'B' a second time to return control to the potentiometers. Look at some button debounce examples if you decide to do this.

What does pressing 'B' do if you haven't pressed 'A'?

Also, consider giving your potentiometer and val variables names that reflect the tone control they're activating - just to make it easier to read/debug. Use of arrays would be another place to improve.

PaulS:

So would the buttons be plugged into digital or analogue input?

Are they analog devices (some range of output voltages/values) or digital devices (on/off)? Seems like this should be pretty easy to answer. If not, start over at the beginning.

What code functions are used for temporarily storing 4 analogue values (each between 0-1023) ?

The assignment operator (=). Of course, you need to define some place to store those values. If they are to make any sense, though, you need to store when a change occurred, or how long to stay at a particular setting.

How would i recall these values

That would depend on how you saved them.

and send them to the servos, ordering them to move to the saved position?

The same way you move the servos in the first place.

Is there some reason you are not using the simpler write() function, setting the servo to a specific angle?

the reason for using writemicroseconds is it allows larger angles which the winch servos can accommodate as they aren't limited to 180degree rotation!
I guess the buttons could be used in both an analogue or digital setting, they are the kind which are only 'on' when held down, they do not have a latch.

The assignment operator (=). Of course, you need to define some place to store those values. If they are to make any sense, though, you need to store when a change occurred, or how long to stay at a particular setting.

could you explain to me how i might implement this, an example perhaps? i only have 2 days to find a solution!

thanks alot
si

wildbill:
A couple of issues with your requirements:

What happens after you press button B? Does the system stay at the 'B' setting forever? It might make more sense to be able to push 'B' a second time to return control to the potentiometers. Look at some button debounce examples if you decide to do this.

id like it to just be an instant movement with control returning maybe a second after they are commanded to move, where could i find such examples?

What does pressing 'B' do if you haven't pressed 'A'?

ideally, nothing, or return the servos to 0degees.

Also, consider giving your potentiometer and val variables names that reflect the tone control they're activating - just to make it easier to read/debug. Use of arrays would be another place to improve.

Im planning to do so, I don't understand what you mean by 'array' sorry for my ignorance im a bit of a noob.

thanks for your replies everyone, its all good, if you were me what would you do?

could you explain to me how i might implement this...i only have 2 days to find a solution!

First, you need to define what saving and recalling servo commands means.

Every time loop is executed, you issue a servo move command, based on the current potentiometer reading. If you were to try to save all the data that is generated, and replay it, you'd quickly run out of memory.

You need to actually issue a move command only when there is a (significant) change in potentiometer reading. That, then, requires knowing (and saving) WHEN the change took place, so that the replay can keep the setting the same for the same period of time.

This won't be a two day implementation, unless you are really good. If you were, you wouldn't need to be asking these basic questions. Not that I mean to belittle your skills, but two days is not enough time to make this change.

PaulS:

could you explain to me how i might implement this...i only have 2 days to find a solution!

First, you need to define what saving and recalling servo commands means.

Every time loop is executed, you issue a servo move command, based on the current potentiometer reading. If you were to try to save all the data that is generated, and replay it, you'd quickly run out of memory.

You need to actually issue a move command only when there is a (significant) change in potentiometer reading. That, then, requires knowing (and saving) WHEN the change took place, so that the replay can keep the setting the same for the same period of time.

This won't be a two day implementation, unless you are really good. If you were, you wouldn't need to be asking these basic questions. Not that I mean to belittle your skills, but two days is not enough time to make this change.

Here is a video of the project i made a few days ago - http://www.youtube.com/watch?v=cHw7ucIRMNU
Im not sure i'm explaining it very well, either that or i don't really understand these problems you suggest i might encounter. Just to be sure you know what im getting at, I don't want to save a movement or an action/sweep, just a value (or 4 values) at chosen instance of time, say i move all my pots to the halfway position, i want to be able to save that value (512?), then move the pots to random positions, then with the click of a button make them return to that previously saved half way point.

I think i see what your saying, as the pot reading and the servo are linked together all the time - which means you have to tell it to ignore current input?

Ill admit im not too hot on all of this, but im sure i can do it, i kind of have to...!
my tutor seems to think it is just a few simple lines of code that need adding.
If im barking up the wrong tree please inform me...ahah
Thanks
si

Replaying servo motion and resetting the servos to where they were at some specific point in time are two different things.

If you want to store the servo positions when switch A is pressed, and put them back there when switch B is pressed, that is easy to do. You need to define what will return control to the potentiometers, then we can help you with the code.

If the servos are to go to some saved position while switch B is pressed, that's one thing. If they are to go there when switch B is pressed, and resume moving to the pot positions when switch B is pressed again, that's another thing. If you add another switch to return to servo control based on pot input, that's another thing again.

Clear up this ambiguity, and the rest is easy.

PaulS:
Replaying servo motion and resetting the servos to where they were at some specific point in time are two different things.

If you want to store the servo positions when switch A is pressed, and put them back there when switch B is pressed, that is easy to do. You need to define what will return control to the potentiometers, then we can help you with the code.

If the servos are to go to some saved position while switch B is pressed, that's one thing. If they are to go there when switch B is pressed, and resume moving to the pot positions when switch B is pressed again, that's another thing. If you add another switch to return to servo control based on pot input, that's another thing again.

Clear up this ambiguity, and the rest is easy.

Thanks very much for the help so far it's much appreciated!
I defiantly do not want to replay motion.

That is exactly what I want to do!

Id like to Tell the servos to move to a specific position WHEN B is pressed, and then to Resume Control To The Pots WHEN button B is pressed for a second time. Failing that i could add a third button, but which out of these which is the more simple task?

Also i don't wish to permanently store the positions when button A is pressed, each time it is pressed the integer can be overwritten if i didn't make that clear.

Thanks
si

So, you have a state machine. The number of times that switches A and B have been pressed defines the state.

When the A count is greater than 1 and the B count is odd, read data from the pots and set the servos, and check the status of switch A.
Otherwise, use the data saved when the A switch is pressed.

Some code to get you started:

#include "Bounce.h" // Use the http://arduino.cc/playground/Code/Bounce library

Bounce A(pinA, 10); // Substitute valid value for pinA
Bounce B(pinB, 10); // Substitute valid value for pinB

int countA = 0;
int countB = 0;

const int servoCount = ??; // however many servos you have
int servoPos[servoCount]; // Array to hold servo positions

void loop()
{
  A.update();
  B.update();

  if(A.read() == HIGH) // Make this LOW is using internal pullups
  {
    // Save the current servo positions in servoPos array
  }

  if(B.read() == HIGH && countA > 0) // Make this LOW is using internal pullups
  {
     countB++;
  }

  if(countB % 2 == 1 && countA > 0)
  {
     // Set servos to saved positions...
  }
  else
  {
     // Set servos to pot-controlled positions...
  }
}