Control stepper motor by potentiometr

Hello. I am new on arduino. I thought it is so easy, but it isn't. I am useing Arduino UNO and Adafruit Motor Shield V1. I have a problem by control stepper motor by potentiometr. I want the motor to increase speed as the value on the potentiometer increases. At the moment the motor turns at one speed only when moving the potentiometer. I check many code's and I programed on visuino but neither works just I want.

#include <AFMotor.h>

AF_Stepper stepper1(100, 2);

int analogPin = 0;     // Pin analogowy

int val = 0;           // variable to store the value read
int prepos = 0;
int pos = 0; //0 to 224 .
void setup()
{
 
 stepper1.setSpeed(200);  // Obroty
  Serial.begin(9600);
}

void loop() {
 
  prepos = pos;
  val = analogRead(analogPin);    // read the input pin
 
  pos = map (analogRead(analogPin), 0, 1023,0, 200);
 
  if (pos != prepos){
    int diststep = pos;
    if (diststep > 0) {
   
   stepper1.step(diststep, FORWARD, DOUBLE);
    }

    }
   
  }

Mayby who know how to connect stepper motor and potentiometr in visuino to adafruit motor shield because this connection in the attachment does not work.


Sorry for my english.

Read the forum guidelines to see how to properly post code and some good information on making a good post.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

You can go back and fix your original post by highlighting the code and clicking the </> in the menu bar.

Is that the function that controls speed? Wouldn't you put the required speed to that function to control speed? Like this? Untested.

#include <AFMotor.h>

AF_Stepper stepper1(100, 2);

int analogPin = A0; // Pin analogowy

int val = 0; // variable to store the value read
int prepos = 0;
int pos = 0; //0 to 224
.
void setup()
{
   stepper1.setSpeed(200); // Obroty
   Serial.begin(9600);
}

void loop()
{
   prepos = pos;
   val = analogRead(analogPin); // read the input pin

   //pos = map (analogRead(analogPin), 0, 1023, 0, 200);
   
   // read pot and map to stepper speed
   int spd = map (analogRead(analogPin), 0, 1023, 0, 200);
   stepper1.setSpeed(spd);
   
   if (pos != prepos)
   {
      int diststep = pos;
      if (diststep > 0)
      {
         stepper1.step(diststep, FORWARD, DOUBLE);
      }
   }
}

That is a stepper motor shield. Usually "motor shield" handles DC motors. Bad description of the seller.

The picture You posted is not readable no matter what I try to magnify it.

In a project like this make test code verifying that reading the pot works and You understand it.

The second part would be running the stepper.
I would like You to post a link to the stepper shield datasheet. I didn't find it directly.

Please, also provide schematics showing pin names and power supplies.

I advise You to use serial monitor and Serial.print in the code to tell how the execution is going on. That was my life line being thrown into advanced multi million systems for many years.

New games, new bets. More information, please.

I added video when motor is working on my code.

I corrected.

#include <AFMotor.h>

AF_Stepper stepper1(100, 2);

int analogPin = A0; // Pin analogowy

int val = 0; // variable to store the value read
int prepos = 0;
int pos = 0; //0 to 224
.
void setup()
{
   stepper1.setSpeed(200); // Obroty
   Serial.begin(9600);
}

void loop()
{
   prepos = pos;
   val = analogRead(analogPin); // read the input pin

   //pos = map (analogRead(analogPin), 0, 1023, 0, 200);
   
   // read pot and map to stepper speed
   int spd = map (analogRead(analogPin), 0, 1023, 0, 200);
   stepper1.setSpeed(spd);
   
   if (pos != prepos)
   {
      int diststep = pos;
      if (diststep > 0)
      {
         stepper1.step(diststep, FORWARD, DOUBLE);
      }
   }
}

It does not working.

arduino-motor-shield-v1
It is this board.

I corrected.

Not strange nothing is happening.
First You set prepos = pos. Then, down in the code You test if theyre unequal....

Please post a data sheet or a link to where you got your stepper motor. That ancient motor shield is not an appropriate driver for a modern bipolar stepper motor.

See here for how to use the motor shield library.

There are examples with the library to help to learn to use it.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.