Control Direction Step Motor with Potentiometer

I’m an beginner in Arduino, but I have been developing an Arduino code for one Solar Tracker. So, I have tried some programmes for the speed and direction control of a stepper motor. I managed the speed control with a function Map using a potentiometer, but I don’t get to control the direction using this device.
Below, I’ll leave my code!

// Defines pins numbers
const int stepPin = 7;
const int dirPin = 8;
const int Passosporvolta = 200;
const int Mode0 = 2;
const int Mode1 = 1;
const int Mode2 = 3;

int customDelay,customDelay1,customDelayMapped,customDirection,customDirectionMapped,newCustom ;// Defines variables
int dirMotor = 0;

void setup() {
// Sets the two pins as Outputs
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
pinMode(Mode0,OUTPUT);
pinMode(Mode1,OUTPUT);
pinMode(Mode2,OUTPUT);
digitalWrite(Mode0, LOW);
digitalWrite(Mode1, HIGH);
digitalWrite(Mode2, LOW);
Serial.begin(9600);
}
void loop() {

customDelayMapped = speedUp();
customDirectionMapped = directionUp (customDelayMapped);

digitalWrite(dirPin, customDirectionMapped);
digitalWrite(stepPin, HIGH);
delayMicroseconds(customDelayMapped);
digitalWrite(stepPin, LOW);
delayMicroseconds(customDelayMapped);
}

// Function for reading the Potentiometer
int speedUp() {
int customDelay = analogRead(A0); // Reads the potentiometer
if (customDelay < 512)
{
int newCustom = map(customDelay,0,511,-8000,200);
}
else
{
int newCustom = map(customDelay, 512, 1023, 200,8000);
}
// Convrests the read values of the potentiometer from 0 to 1023 into desireded delay values (300 to 4000)
return newCustom;
}

int directionUp (int customDelay1) {
if (customDelay1 < 0)
{
return LOW;
}
else
{
return HIGH;
}
}

Do not cross-post. Other thread removed.

If you want to make a potentiometer work as centre-off then this is how to do it

int potVal = analogRead(potPin); // range 0-1023
potVal = potVal - 512;  // range -512 to + 511
char direction = 'F';
if (potVal < 0) {
  direction = 'R';
}
potVal = abs(potVal); // treat all values as positive
// map potVal to your desired range

…R

You need to split your analog read down the middle,

(A0 - 512)

this will give you readings from -512 to + 512 (or so). Map the values of 0 - 512 to your desired range, 300 - 4000 and use the sign bit for direction.

This is pretty common. You end up with a pot, when centred (i.e. A0 = 512) the motor will be stationary. As you rotate the pot CCW so the motor begins to spin CCW and CW for the other direction. The more you turn the pot, the faster the motor spins.

Remember that the stepper library, whatever you're using, is only looking for a non-zero step value to control the speed and a zero or one to determine direction.

Thank you, friends! I could understand how to get it! ;)

If anyone can share more ideas, I won't refuse heheheeh

luciocr: If anyone can share more ideas, I won't refuse heheheeh

I don't understand. Have you got it working the way you want based on the info you have already been given?

If you need more help then post the latest version of your program.

...R

Hi, Welcome to the forum.

Please read the first post in any forum entitled how to use this forum. http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code. It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom.. :)