I posted this topic here before. At that time I didn't even know the right questions to ask. I hope I am now better prepared to receive your help.
I have a needle valve which has a non linear output as a function of stem rotation.
I want to connect the valve stem to the stepper motor and control the motor with an Arduino Uno, 10 turn (10K) potentiometer/A4988 I'm using a "27:1 geared stepper motor. The valve stem rotation corresponds to a linear output for the first 6 rotations. That is, if the 1st rotation induces a flow of x, then the next 5 rotations produce a flow of 2x,3x, 4x, 5x, 6x. The 7th rotation increases the flow by 2x and, the 8th rotation increases the flow by 3x.
I can linearize the valve output in time by modifying the speed of the motor. So the first 6 rotations will be at time t/rotation, the 7th is at 1/2t and the 8th is at 1/3t.
I can do this by changing the stepdelay map such that the 1st 6 rotations are at map(val, 0, 1023, 1, 500) then the 7th is at map(val, 0, 1023, 1, 1000) and the 8th at map(val, 0, 1023, 1, 1500). .
Ultimately, I want to have it go through this protocol then wait 20 sec and reset to the origin. (8 revolutions back). Im hoping to avoid using an encoder but that decision will be determined by performance.
Currently, the code I cobbled together allows me to set a speed via the potentiometer rotate the motor 6 rotations in one direction then reverse direction for 6 rotations. I have the analogread occurring every 3000 steps (about 5370 steps per rev) using a countdown function.(Thanks Cattledog)
Any attempt ive made at cut, paste, modify the existing code hasn't worked.
If you could help me figure out how to get the motor to do the 6 rotations at a speed set by the potentiometer followed by the 1 rotation at half speed I would greatly appreciate it. Hopefully I can figure out the last (1/3 speed) with that info.
Here is the code to date:
#include <Stepper.h>
int steppin = 4;
int dirpin = 3;
int stepdelay;
int Distance = 0; //How far we've traveled
int AnalogReadCountDown = 3000;
int NumSteps = 5370.2479; //how many steps does the stepper take to make a rotation (the actual motor, not factoring in "micro" steps)
int FullRotation = NumSteps ;
void setup()
{
pinMode (steppin, OUTPUT);
pinMode (dirpin, OUTPUT);
digitalWrite(4, LOW);
digitalWrite(3, LOW);
Serial.begin(9600);
//int val = analogRead(A0);
//stepdelay = map(val, 0,1023,1,500);
}
void loop ()
{
if(AnalogReadCountDown == 0){
int val= analogRead(A0);
stepdelay = map(val, 0,1023,1,500);
Serial.println(stepdelay);
AnalogReadCountDown = 3000;}
digitalWrite(steppin, HIGH);
delayMicroseconds(stepdelay);
digitalWrite(steppin, LOW);
delayMicroseconds(stepdelay);
Distance = Distance + 1; // record this step
AnalogReadCountDown = AnalogReadCountDown - 1;
if (Distance == FullRotation * 6) { //as soon as distance = "FullRotation", that means we have made a full revolution!
if (digitalRead(3) == LOW){
digitalWrite(3, HIGH); //this is a "toggle" to reverse whichever way its going to the other direction!
}
else{
digitalWrite(3, LOW);
}
Distance = 0; //reset our Distance counter!
}
}
Im obviously brand new to coding. If someone could give me a clue as to how to get the motor to do a 7th rotation at 1/2 the original speed set by the pot (corresponding to the stepdelay = map(val, 0, 1023, 1, 1000); I would be eternally grateful.