Arduino speed control

Go To Last Post
20 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hi wondering if anyone can help
i am working on a camera rail project
i am using a nema 17 /arduino uno/ TB6600 driver /2 hall effects senors /potentiometer

the code i have spliced together works but i need the potentiometer to start from a off position then accelerate when i turn the pot
my problem is once i connect the power the motor starts turning
any help here much appreciated

int driverPUL = 7;    // PUL- pin
int driverDIR = 6;    // DIR- pin
int spd = A0;     // Potentiometer
int hall_sensor_a = 2;
int hall_sensor_b = 3;

// Variables

int pd = 200;       // Pulse Delay period
boolean setdir = LOW; // Set Direction

// Interrupt Handler

void revmotor (){

  setdir = !setdir;
 
}

int stepCount = 0;  

void setup() {

  pinMode (driverPUL, OUTPUT);
  pinMode (driverDIR, OUTPUT);
  attachInterrupt(digitalPinToInterrupt(hall_sensor_a), revmotor, FALLING);
  attachInterrupt(digitalPinToInterrupt(hall_sensor_b), revmotor, FALLING);
 
}

void loop() {
pd = map((analogRead(spd)),0,1023,0,1023);
    digitalWrite(driverDIR,setdir);
    digitalWrite(driverPUL,HIGH);
    delayMicroseconds(pd);
    digitalWrite(driverPUL,LOW);
    delayMicroseconds(pd);
}

 

Last Edited: Sat. Apr 25, 2020 - 02:16 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have no idea what "PUL" stands for, but what I would do is in your SETUP loop, deactivate teh motor driver immediately.

 

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

PUL (pulse)

why would you deactivate the driver ?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

mike2237 wrote:
why would you deactivate the driver ?

 

You answered your own question:

mike2237 wrote:
my problem is once i connect the power the motor starts turning

 

You disable the driver UNTIL your code completes it's setups and other inits.  THEN you re-enable the driver.

 

JIm

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yeah i set up all the pins /verify the code/ upload the code then hooked up the driver  standard practise right

Maybe i did not explain clearer what i am trying to achieve with  this code   as is the motor  runs at a set speed when powered up

my question was how run the motor from a stopped position

 

This section in the code pd = map((analogRead(spd)),0,1023,0,1023); if i increase the last 1023 number i can get the motor to run slower but not from a stopped position

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

mike2237 wrote:

if i increase the last 1023 number i can get the motor to run slower but not from a stopped position

 

You need a special case to check to see if your ADC value is at maximum and not do anything in that situation.

 

if (pd != 1023) {
    do your motor stuff here
} else {
    do nothing
}

 

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

Last Edited: Sat. Apr 25, 2020 - 03:31 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Only toggle the output if pd < 1023.
If ( pd < 1023)
{
// toggle output
}

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for the help fellas

but truth is i ain't got a clue what you mean

cheers anyway

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

what is this supposed to do?

     digitalWrite(driverPUL,HIGH);
     delayMicroseconds(pd);
     digitalWrite(driverPUL,LOW);
     delayMicroseconds(pd);

Where are your comments?

The motor will run at half speed (on 50%).    Are you trying to control torque?   This certainly isn't speed control.

 

So far your replies have been rather uninformative.

 

why would somebody have any idea from this commentary????

my problem is once i connect the power the motor starts turning
any help here much appreciated

 

my problem is whenever I open the metal door which is rusty 

any help here much appreciated

 

 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Last Edited: Sat. Apr 25, 2020 - 04:48 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

>> the code i have spliced together works but i need the potentiometer to start from a off position then accelerate when i turn the pot
>> my problem is once i connect the power the motor starts turning

 

I think you're confusing a potentiometer with a rotary encoder.

 

I would imagine your pot is configured as a voltage divider, so unless it is physically turned to the zero position, it will yield an analog value >0. And your code uses whatever value it reads to control the motor.

 

If you must use a pot, try what model aircraft throttles have done for decades. The throttle must be physically returned to the zero position before any value >0 is used to control the motor. Otherwise you risk switching on the transmitter with the throttle wide open, and the ensuing disaster. This is avoid exactly what you're seeing here.

 

Just create a global boolean 'safe to operate flag' variable that is initialised to false. Only set the flag to true once you have read a zero from the pot. Only operate the motor if the flag is true.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
void loop() {
pd = map((analogRead(spd)),0,1023,0,1023);
if (dp < 1020)
    {
    digitalWrite(driverDIR,setdir);
    digitalWrite(driverPUL,HIGH);
    delayMicroseconds(pd);
    digitalWrite(driverPUL,LOW);
    delayMicroseconds(pd);
    }

The logic is simple - if the pot value is near 1023, don't run the motor.

 

You speak of acceleration - there's no means in your code to do that. What is it you want to achieve?

 

If the pot is at a non-zero point when the circuit is powered, you want to accelerate the motor? If so, at what acceleration - there's a little bit of math involved here. The code is going to get a lot more complex.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What i am trying to do is move a camera along on a linear rail for stop motion animation i wanted to add 2 hall sensors at both ends to send the mounted camera back and forth on the rail  i wanted to control the spead with a potentiometer

but i needed the rail not to move until i turned the potentiometer most of the codes i have found have a starting position as soon as power has been sent to the motor

 

I know practically zero about arduino programing so i was using codes i have found trying to splice them together with zero knowledge hence the muddled codes

 

I have 2 codes this one stepper_speedControl has what i need for the potentiometer control

 

/*
 Stepper Motor Control - speed control

 This program drives a unipolar or bipolar stepper motor.
 The motor is attached to digital pins 8 - 11 of the Arduino.
 A potentiometer is connected to analog input 0.

 The motor will rotate in a clockwise direction. The higher the potentiometer value,
 the faster the motor speed. Because setSpeed() sets the delay between steps,
 you may notice the motor is less responsive to changes in the sensor value at
 low speeds.

 Created 30 Nov. 2009
 Modified 28 Oct 2010
 by Tom Igoe

 */

#include <Stepper.h>

const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
// for your motor

// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);

int stepCount = 0;  // number of steps the motor has taken

void setup() {
  // nothing to do inside the setup
}

void loop() {
  // read the sensor value:
  int sensorReading = analogRead(A0);
  // map it to a range from 0 to 100:
  int motorSpeed = map(sensorReading, 0, 1023, 0, 100);
  // set the motor speed:
  if (motorSpeed > 0) {
    myStepper.setSpeed(motorSpeed);
    // step 1/100 of a revolution:
    myStepper.step(stepsPerRevolution / 100);
  }
}

 

And this code has what i need for the Hall effects

 

/*
  Stepper Motor Limit Switch Demo
  stepper-limit-demo.ino
  Uses Hall Effect sensors as limit switches
  Uses A4988 Stepper Motor Driver module
 
  DroneBot Workshop 2019
  https://dronebotworkshop.com
*/

// Define connections
#define HALL_SENSOR_A      2
#define HALL_SENSOR_B      3
#define DIR      10
#define STEP      11

// Variables
int pd = 4000;       // Pulse Delay period
boolean setdir = LOW; // Set Direction

// Interrupt Handlers

void limit_a (){

  // Reverse motor
  setdir = !setdir;
 
}

void limit_b (){

  // Reverse motor
  setdir = !setdir;
 
}

void setup() {
 
  // Setup the stepper controller pins as Outputs
  pinMode(DIR,OUTPUT);
  pinMode(STEP,OUTPUT);
 
  // Setup the Hall Effect switches as Inputs
  pinMode(HALL_SENSOR_A, INPUT);
  pinMode(HALL_SENSOR_B, INPUT);
 
  // Attach interrupt pins to handlers
  attachInterrupt(digitalPinToInterrupt(HALL_SENSOR_A), limit_a, FALLING);
  attachInterrupt(digitalPinToInterrupt(HALL_SENSOR_B), limit_b, FALLING);
   
}

void loop() {
 
    digitalWrite(DIR,setdir);
    digitalWrite(STEP,HIGH);
    delayMicroseconds(pd);
    digitalWrite(STEP,LOW);
    delayMicroseconds(pd);
 
}

 

trying to combine them results in lots of errors

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

How many problems do you want to solve at once?
Concentrate on your original problem - i gave you a solution. Did that work?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sorry I didn't realize you gave me solution but yeah the pot is 100k

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You are trying to integrate too many new (to you) concepts at once. Try to break down the problem into parts that you can experiment with and learn from. Then start integrating them together. e.g. there are many examples included with the Arduino IDE. Start with the basics and work up.

 

You can't just copy and paste 3rd party code without understanding what it does. (And there's a minor bug in the second example: the variable shared with the interrupt routine should be declared volatile).

 

Did you understand my explanation in reply #10 ? That will address the issue of the motor moving at startup if the pot is not set to zero.

 

If you need acceleration, I would use the excellent AccelStepper library. But that's not a magic solution to your project; you still need to understand how it works. https://www.airspayce.com/mikem/...

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you you are correct it needs to be done in bit by bit to get a understanding of what is what

cheers

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

When you turn it all the way down & stop, how do you want to restart when the knob is turned back up?

a) Pickup from where it left off?

b) Move quickly to the left limit & start there?

 

You could have a timer so if it would choose a or b, depending upon how long it was stopped.

Or perhaps add a "home" button

Note that for repeatability (if needed) it might be better to have a "go" button, to ramp up to the knob setting.

You could have it "record" your varying the knob up/down (say 10 times a sec) & play that back, if that is of interest.

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Last Edited: Sun. Apr 26, 2020 - 07:07 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Lets back up quite a bit for a few posts and get everyone on teh same track.

 

@mike2237:

Use this community to your advantage, by doing the following:

 

Can you post a few pictures of your camera setup on this rail gizmo? 

Can you post a link to the actual rail/actuator?

CAn you post a link to the Driver board you are using?

 

From there, we can go fetch the information we seek and advise properly, rather than frustrate teh heck outta everyone guessing what to do based on your replies.  At the same time WE can ask more intuitive questions instead of shots in teh dark.

 

Cheers,

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

This is the rails i have mounted onto MDF the camera is mounted onto a radius platform with bearings for different angles i will use pins as stoppers
The radius cauls i will put a magnet on either side and position the hall sensors where ever i need them i would like to do that manually so would not need
a homing position for the motor
The center line position is where the belt drive will be fixed
I have yet to setsup still waiting on bearings and other bits

The potentiometer settings i am trying to get  i can only descibe as like volume dial on a radio you turn it hard to the left and there is no sound turn it  to the right it gets louder
with the acceleration inbetween off & on

driver board
https://wiki.dfrobot.com/TB6600_...

I am going to do what was suggested learn how to write the code and do it 1 step at a time

stay safe people

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Mike,

 

Whilst sleeping, I pondered your problem. My suggestion is to look at the GRBL project (or similar ones). GRBL is mainly for CNC machines, but what you want to do is not far removed from a 3D printer. GRBL knows about limit switches, acceleration, speed and distance.

Then it is up to you to use G code to tell it what to do. I'm thinking this path is going to be much more fruitful to you.

 

You can ask me more questions about this, but my experience with GRBL is minimal - I've played briefly with it. It does have a very active forum so specific questions can be directed there. I'd be surprised if you don't find a solution.

 

https://github.com/grbl/grbl/wiki

 

The About GRBL says this:

 

Grbl is a free, open source, high performance software for controlling the motion of machines that move, that make things, or that make things move, and will run on a straight Arduino. 

 

Sort of sounds like what you want methinks.

Last Edited: Sun. Apr 26, 2020 - 10:56 PM