Question about Variables [closed]

is there a way to use a previous assigned value to variable to adjust the step motor? what i mean is:

example: Sensor reads 100 degrees so

Adjust the step motor to 100 degrees and hold, then after 10 min

the sensor reads 110 degrees so

Adjust the step motor (110 - 100) by extra 10 degrees

and so on..

I'm really new to this and i'm sorry if that's a stupid question :<

closed as unclear what you're asking by VE7JRO♦, sempaiscuba, per1234, gre_gor, JurajApr 19 '18 at 7:10

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

• Start with example code for driving the stepper motor. Then calculate how many degrees equals one step. Use a variable to save the current Position of the stepper in units of steps. Everytime the Sensor reads a different value, calculate how many steps to take, run the stepper and maintain the variable. – chrisl Apr 18 '18 at 6:58
• I see you are getting close votes on your question. I strongly suggest you do some basic C (or C++) tutorials. Asking a "question about variables" is really nothing to do with motors or sensors. Do some tutorials and then you will understand how to use variables. The Arduino is programmed in C++ if that helps. – Nick Gammon Apr 18 '18 at 7:30

Based on your description, the program needs to know only the new target setting and the current position. That means you need two variables. Algorithmically, it would look as follows (written in C-like pseudo-code):

// initialize global variables (might need float instead of int)
int target_pos = 0;
int current_pos = 0;

void setup() {
// add code to set up pin IO mode, prepare communication with the sensor...
}

// function to read the sensor
int reading = 0;   // local variable to store sensor reading
}

// function to adjust the motor position
// add code to to adjust the stepper to (target_pos-current_pos);
}

void loop() {  // repeat forever
target = ReadSensorFunction()   // call sensor read function
current_pos = target_pos        // update current position
// add code to delay for 10 minutes
}

This code initializes the position as zero. In case it's not zero when the controller is turned on, you'll need additional code in setup() to assign current_pos the correct value based on user input.

Initially you read the value of sensor to some variable, like: