Stepper following POT - bit noisy

This is my first Post:

This project was originally to learn about how to wire up the stepper motor driver.

Hardware:

Once I got the basics of speed control, changed the code to get the motor to follow the POT, (full sweep of the pot, translates to 1 rotation of the stepper motor).
Long story short I spent a couple of days trying all sorts of methods to stop the motor vibrating ( this only happens when the pot is in the middle of range of motion).

I used some some timing code, although timing microseconds worked in a trial sketch as soon as I implemented it within the main program the Serial.println() function was so slow and timings were no use at all. Im using a arduino UNO R3 have been programing it for about 3 days I am completely new to this.

I attempted to use arrays to store sensor and Average results as a buffer.
int array[10]; basically made the program non responsive. When I made the array size 5 or less the program functioned but didnt help with the noise. I dont really understand how array of size [10] broke the machine. I was not even utilizing it in the code. Was just in declared variables.

I have 2 main questions:
Given the current hardware im using how would you filter the POT while still maintaining some responsiveness?

Secondly:
There seems to be hundreds of options regarding hardware. If I wanted to do software based filtering more maths and algorithms focused while having some real world feedback. What would you recomend?

// Stepper Follows Pot - Arduino Uno R3

//Hardware Pins
const int analogInPin = A0;  // Analog input pin that the potentiometer is attached to
const int analogOutPin = 9; // Analog output pin that the LED is attached to
const int stepPin = 3; 
const int dirPin = 5;   

//State Variables
int sensorValue1 = 0; // 0-1023 potentiometer 
int sensorValue2 = 0; // 2 vars to calc change
int sensorChange = 0; // 
int pulsePerRev = 20000; // Pulses Per Revolution 40_000
int pulseCount = 0; // Absolute position within 1 revolution
int pulseControlCount = 0; //Change during control step postive OR negative
int counter = 0;
int i = 0;
int j = 0;
int k = 0;
unsigned int stepDelay = 3; // Controls Velocity of Motor

void setup() 
{
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  digitalWrite(stepPin, HIGH);
  pulseCount = 0;
  
}

void loop() 
{
  j = j + 1;
  k = k + 1;
  
  //Update State:
  if(j= 5)
  {
    sensorValue2 = analogRead(analogInPin); // Raw sensor value.
    sensorChange = sensorValue2 - sensorValue1;
    sensorValue1 = sensorValue2;
    j = 0;
  }
 
  if(sensorValue1 == 0)
  {
    pulseCount = 0;
  }
 
  //Decision Step: what will we do with latests state information?
  pulseControlCount = abs(map(sensorChange, 0, 511, 0, pulsePerRev));
 
  //Do Step: Control stepper motor
  if(k= 1000)
  {
    delayMicroseconds(10);
    if(sensorChange > 0) 
    {
      digitalWrite(dirPin, LOW);
      for(i=1; i<pulseControlCount; i++)
      {
        digitalWrite(stepPin, HIGH);
        delayMicroseconds(stepDelay);
        digitalWrite(stepPin, LOW);
        delayMicroseconds(stepDelay);
      }
      pulseCount = pulseCount + pulseControlCount;  
    }
  
    if(sensorChange <=0)
    {
      digitalWrite(dirPin, HIGH);
      for (i=1; i<pulseControlCount; i++)
      {
        digitalWrite(stepPin, HIGH);
        delayMicroseconds(stepDelay);
        digitalWrite(stepPin, LOW);
        delayMicroseconds(stepDelay);
      }
      pulseCount = pulseCount - pulseControlCount; 
    }
    k = 0;
  }
    
} 


Thanks for you help. There were so many versions of the code I ended up with what you see. I cleared out as much variables and junk. I ended up with what you see here.

Here are a error :
wrong if(j= 5)
right if(j== 5)

again
wrong if(k= 1000)
right if(k== 1000)

1 Like

unsigned int stepDelay = 3; // Controls Velocity of Motor

for(i=1; i<pulseControlCount; i++)
{
digitalWrite(stepPin, HIGH);
delayMicroseconds(stepDelay);
digitalWrite(stepPin, LOW);
delayMicroseconds(stepDelay);
}

166667 steps per second? Which Arduino?

It's an ardunino UNO R3, sorry I am getting into a mess, because I don't really understand the limits of the hardware.

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