using Savestate

Hi everyone,

I am currently trying to get my head around a project but I need some help understanding some (basic) stuff.

I want to control 4 actuators (3 servo's and 1 DC motor). Basically I want them all to go to position X when the button is pressed, and back to position Y once the button is pressed again. the problem being is that I have just begon with using the arduino Uno (finished most of the first book that comes with it), but I can't for the life of me find a description on how to use a savestate or save position.

It might be that I'm looking at this the wrong way but that's why I'm asking here.

Thanks in forward.

I can't for the life of me find a description on how to use a savestate

Me neither.

AWOL: Me neither.

Would I need to use another function then ? I've been looking into this for a few days now, and being a beginner doesn't help haha.

I (and probably we) have not the slightest idea want you're going in about.

Ok, I'll try to explain it a bit better then.

I want to make a project with 3 Servo motors and one single DC motor (linear Actuator). I want to have all the motors go to a set position once the button is pressed, and go back to another position (the starting position) once the button is pressed again.

Button press 1 = set motor to X angle Button press 2 = set motor to Y angle.

My problem how ever is, I have no idea on how I can save it to the Arduino to have them switch between 2 set points.

how I can save it to the Arduino to have them switch between 2 set points.

Can you explain that sentence, please? What do you mean by "save"?

AWOL:
Can you explain that sentence, please?
What do you mean by “save”?

With save I mean the positions that get stored.

I want the motors to start at position Y
Then if the button get’s pressed I want them to go to position X
after the button get’s pressed again I want them to revert back to position Y

Again I’m just a beginner and I have no idea on how I can best explain this.

I'm unclear (even though I read this thread twice) whether you want to save your last positions even through cycling power or what.

Or, you simply want to go to posX then to posY with the 2 button presses?

You basically said the same thing three times without making anything clearer. Sorry, unfortunately, semantics are a big thing when describing some process the program should carry out. It all needs to mean something significant.

Dan95: I'm unclear (even though I read this thread twice) whether you want to save your last positions even through cycling power or what.

Or, you simply want to go to posX then to posY with the 2 button presses?

You basically said the same thing three times without making anything clearer. Sorry, unfortunately, semantics are a big thing when describing some process the program should carry out. It all needs to mean something significant.

I'm sorry, as I said I'm a beginner so I don't yet know how to properly express myself.

I want to press a button. After that button is pressed, the 3 servo's should go from position X to position Y. The dc motor at that point should turn on and go from X to Y.

After this is done, it should then do nothing.

then if I press the button again the 3 servo's should go from position Y to position X. and the dc motor should then reverse back to position Y as well.

Thanks for helping me guys, this is all very overwhelming to a beginner.

At what time are the positions X and Y determined? If they are part of your program ("sketch") then they are set at the time that you compile the program and download it to the Arduino.

If they are determined every time the Arduino is powered up, then regular variables are OK.

If you want to be able to change X and Y without plugging in the USB and programming it again, BUT you want it to remember them when the Arduino is powered off, then use the EEPROM feature to save the values of X and Y. Find one of the EEPROM examples and look at how that works.

MorganS: At what time are the positions X and Y determined? If they are part of your program ("sketch") then they are set at the time that you compile the program and download it to the Arduino.

If they are determined every time the Arduino is powered up, then regular variables are OK.

If you want to be able to change X and Y without plugging in the USB and programming it again, BUT you want it to remember them when the Arduino is powered off, then use the EEPROM feature to save the values of X and Y. Find one of the EEPROM examples and look at how that works.

I want to determine the X and Y in the sketch, nothing to difficult.

I just want to be able to switch them by pressing a button nothing more.

the project will be a stand alone thing, arduino and battery will be put into an object. Then if a button is pressed it must go from one position to the other and then stop, if pressed again go to the other position.

the problem however is I don't know how to encode different "states" as in defining position X and Y. and how to make them switch from it with a button press.

Now I am 100% sure that this sketch doesn’t do exactly what you want. Your specification is too vague for me to know exactly what is in your head, but it should give you some ideas…

#include <Servo.h>
int X = 20; //degrees - the initial servo position
int Y = 120; //degrees - the servo position when the button is held down
const int inputButtonPin = 2; //put the button between pin 2 and ground
const int numServos = 3; //we allow up to 3 servos in this sketch
Servo myServos[numServos]; //create an array of servos so we don't have to copy-paste lots of "myservo1"
void setup() {
  //each servo in the array must be connected to a separate output pin
  myServos[0].attach(3);
  myServos[1].attach(4);
  myServos[2].attach(5);
  pinMode(inputButtonPin, INPUT_PULLUP);
}

void loop() {
  int outputValue;
  if(digitalRead(inputButtonPin)) {
    //the button is held down
    outputValue = Y;
  } else {
    //the button is released
    outputValue = X;
  }

  //now we've read all the inputs and decided what position to send the servos to, do the outputs
  for(int i=0; i<numServos; i++) {
    myServos[i].write(outputValue);
  }
}

MorganS:
Now I am 100% sure that this sketch doesn’t do exactly what you want. Your specification is too vague for me to know exactly what is in your head, but it should give you some ideas…

#include <Servo.h>

int X = 20; //degrees - the initial servo position
int Y = 120; //degrees - the servo position when the button is held down
const int inputButtonPin = 2; //put the button between pin 2 and ground
const int numServos = 3; //we allow up to 3 servos in this sketch
Servo myServos[numServos]; //create an array of servos so we don’t have to copy-paste lots of “myservo1”
void setup() {
 //each servo in the array must be connected to a separate output pin
 myServos[0].attach(3);
 myServos[1].attach(4);
 myServos[2].attach(5);
 pinMode(inputButtonPin, INPUT_PULLUP);
}

void loop() {
 int outputValue;
 if(digitalRead(inputButtonPin)) {
   //the button is held down
   outputValue = Y;
 } else {
   //the button is released
   outputValue = X;
 }

//now we’ve read all the inputs and decided what position to send the servos to, do the outputs
 for(int i=0; i<numServos; i++) {
   myServos[i].write(outputValue);
 }
}

yes this is Very close to what I meant, Thank you so much for this.
the only difference I meant was, instead of “Holding” the button down, you press it down for it to go to one of the presets.
Basicly if I click the button it switches it output value.
for example see image: http://images.excheap.com/E27-1x3W-RGB-LED-High-Power-Searchlights62673.JPG

if I click on “R” the RGB value changes to “Red”
if I click on “B” it changes to “blue” and will stay there until I press “R” again.

that is what I want to achieve with the servo’s and DC.
go to one position until it has been given another input (without the need of holding said button)

Again thank you so much!

Then you need to do two things:

  1. Remember which state you are in (X or Y)

  2. Identify the moment that the button becomes pressed and ignore multiple presses within a millisecond, which is called "debouncing."

MorganS: Then you need to do two things:

  1. Remember which state you are in (X or Y)

  2. Identify the moment that the button becomes pressed and ignore multiple presses within a millisecond, which is called "debouncing."

and those are the exactly the problems I am having. I read somewere that defining either state can be saved. but I a just a beginner, I've been doing as many tutorials (and reading the basic books) as I could, but to do the 1'st I can't find anywere.

The debounce I had already bookmarked (https://www.arduino.cc/en/Tutorial/Debounce#toc5)