0

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 :<

2
  • 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, 2018 at 6:58
  • 2
    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, 2018 at 7:30

2 Answers 2

2

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 ReadSensorFunc(void) {
    int reading = 0;   // local variable to store sensor reading
    reading = ...;     // add code to read sensor
    return reading;
}

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

void loop() {  // repeat forever
    target = ReadSensorFunction()   // call sensor read function
    AdjustStepMotorFunc()           // call position adjustment 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.

1

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

previous = readSensor();

Every ten minutes you have to call a function ( logic using timer interrupt / RTC /millis() ) which reads the current value from sensor and compares it with previous value to get the offset. Then you pass it to the function which adjusts motor speed. Update 'previous' variable

offset = readSensor()-previous;
previous = readSensor();
adjustSpeed(offset);

Not the answer you're looking for? Browse other questions tagged or ask your own question.