Slowly stopping of oscillation

Hi
I' m a beginner and its my very first project so i might have many mistakes.
My project is producing an oscillator for welding. I want to control system with 3 potantiometer.

It will be like this: Automatic Welding Oscillator Weaver PLC Motorized Linear Type MIG Machine 220v - YouTube

  1. pot in A4 connection, controls oscillation range
  2. pot in A2 connection, controls oscillation speed
  3. pot in A0 connection, controls delay time between oscillations

My questions are;

  1. I can' t control speed. What can i add to control speed with A2 connection?
  2. When it changes rotation, it stops and moves back suddenly. Probably it causes over heating of driver.
    So may i stop and force again slowly? To do this how may i modify codes?
  3. For this operation i use nema 23 with tb6560 and powering 24V 6,5A supply. Driver is heating too much. TB6560 switces are in this position;
    S1 0 S2 0 S3 0 S4 0 S5 0 S6 0
    To reduce overheating, maybe i have to adjust it in better set up?

Code below;

// defines pins numbers
const int stepPin = 5;
const int dirPin = 2;
const int enPin = 8;
void setup() {

// Sets the two pins as Outputs
pinMode(stepPin,OUTPUT);
pinMode(dirPin,OUTPUT);
pinMode(A0, INPUT);
pinMode(A2, INPUT);
pinMode(A4, INPUT);
pinMode(enPin,OUTPUT);
digitalWrite(enPin,LOW);

}
void loop() {

int range = analogRead (A4);
int speed= analogRead (A2);
int wait= analogRead (A0);

digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction

for(int x = 0; x < range; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin,LOW);
delayMicroseconds(500);
}
{ delay(wait);

{ digitalWrite(dirPin,LOW); //Changes the rotations direction

for(int x = 0; x < range; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(500);
digitalWrite(stepPin,LOW);
delayMicroseconds(500);
}
{ delay(wait);
}

}}}

You have useless curly braces in your code. Get rid of them.

Put no more than ONE } on a line.

You never use speed, so why do you bother reading that pin?

Please don't double post.

What values do you get from:

int range = analogRead (A4);
int speed= analogRead (A2);
int wait= analogRead (A0);

You should begin debugging your code yourself by Serial.print() the return values, so you can see what is being returned.

How do you have those pins wired to the pots?

Paul

Checkout the AccelStepper library - you'll almost certainly need speed ramping for the stepper, so
use a library that supports it like AccelStepper.

PaulS:
You have useless curly braces in your code. Get rid of them.

Put no more than ONE } on a line.

You never use speed, so why do you bother reading that pin?

You re soo right. I m newbie as i said I ll make it better with your helps.

About speed, i will write thats why i add a read line but i dont know hot to do. its like "fill in the blanks please" type need...

Paul_KD7HB:
Please don't double post.

What values do you get from:

int range = analogRead (A4);
int speed= analogRead (A2);
int wait= analogRead (A0);

You should begin debugging your code yourself by Serial.print() the return values, so you can see what is being returned.

How do you have those pins wired to the pots?

Paul

A0 A2 and A4 has 10k potantiometer wires. I will add a 2x16 led for next step but ofc as you said for checking whats going on in program, i will add serial print.
Sorry for double, first was on wrong topic but i couldt move.

  • @markT
    I will check it tomarrow. About that, in arduino can we use more than one library in same program?

About that, in arduino can we use more than one library in same program?

Yes.

azir:
Hi
I' m a beginner and its my very first project so i might have many mistakes.
My project is producing an oscillator for welding. I want to control system with 3 potantiometer.

It will be like this: https://www.youtube.com/watch?v=VYz2Bxtgy-0

  1. pot in A4 connection, controls oscillation range
  2. pot in A2 connection, controls oscillation speed
  3. pot in A0 connection, controls delay time between oscillations

My questions are;

  1. I can' t control speed. What can i add to control speed with A2 connection?
 void loop() {

int range = analogRead (A4);
  int speed= analogRead (A2);
  int wait= analogRead (A0);

digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction
  for(int x = 0; x < range; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(500);
  }
  delay(wait);

digitalWrite(dirPin,LOW); //Changes the rotations direction
 
  for(int x = 0; x < range; x++)
{
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(500);
    digitalWrite(stepPin,LOW);
    delayMicroseconds(500);
  }
  delay(wait);
}

Looks like you should use the value 'speed' in the second 'delayMicroseconds(500);" in each loop. Right now you delay 500 microseconds twice in each iteration. That's 1 millisecond of delay so a little slower than 1000 steps per second. To get 1000 to 5096 microseconds delay between steps (1000 to 200 steps per second) you could use this:

 for(int x = 0; x < range; x++)
{
   digitalWrite(stepPin,HIGH);
   delayMicroseconds(500);
   digitalWrite(stepPin,LOW);
   delayMicroseconds(500 + (4 * speed));
 }