Go Down

Topic: motor issues (Read 1 time) previous topic - next topic

Robin2

im still figuering out where i would implement the changes on the counts for the microseps i would select
Your code structure is going in the right direction.

It may be worth considering how you want to keep track of steps. It may be easier always to count microsteps and to increment them by (say) 4 for a single step (assuming max of 4x microstepping). Then you could change the value to be added for every step in the function changeMicrosteps()

Whatever you do you should avoid using floating point arithmetic if at all possible - it is very slow and can be imprecise.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

MarkT

I recommend trying out the AccelStepper library and making things easy for yourself...
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

meph328

i will look into it, but still would love to come up with a solution to change the counts based on the microseps choise :)

Robin2

i will look into it, but still would love to come up with a solution to change the counts based on the microseps choise :)
That is not difficult. What have you tried? What happened?

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

meph328

Sorry Robin i was only able to answer today to reply due to work life

here is the code i have at moment

Code: [Select]

#define MotorDir  6
#define MotorStep 7
#define ButaoA 9
#define ButaoB 10
#define ButaoC 5
#define Enable 8
#define MS1 11
#define MS2 12
#define MS3 13

int msDelay = 1;
int ButaoEstadoA = 0;
int ButaoEstadoB = 0;

int count = 0;
int MicroPassos;
int Micro;
const float angulo = 1.8;
int ButaoEstadoC;
int pressC = 1;
int val = 1;
float degree;
int State = 0;

void setup()
{
  Serial.begin(9600);
  pinMode(MotorDir, OUTPUT);
  pinMode(MotorStep, OUTPUT);
  pinMode(ButaoA, INPUT_PULLUP);
  pinMode(ButaoB, INPUT_PULLUP);
  pinMode(ButaoC, INPUT_PULLUP);
  pinMode(Enable, OUTPUT);
  pinMode(MS1, OUTPUT);
  pinMode(MS2, OUTPUT);
  pinMode(MS3, OUTPUT);
  digitalWrite(Enable, HIGH);
}

void loop()

{
  degree = (count * angulo) / Micro ;

  MicroButtons();

  changeMicrosteps();

  MotorButtons();

  CheckMicrosteps();

  SerialPrint();

}

void MicroButtons()
{
  ButaoEstadoA = digitalRead(ButaoA);
  ButaoEstadoB = digitalRead(ButaoB);
  val = digitalRead(ButaoC);
}

void changeMicrosteps() {

  if (pressC == 1)
  {
    FullStep();
  }
  if (pressC == 2)
  {
    HalfStep();
  }
  if (pressC == 3)
  {
    SixteenthStep();
  }
}

void MotorButtons() {

  if (ButaoEstadoA == LOW)
  {
    MotorClockWsise();

    if (count == MicroPassos) {
      count = 0;
      degree = 0;
    }
    count = count + 1;
 
  }
 
 

 if (ButaoEstadoB == LOW)
  {
    MotorClock();

    if (count == 0) {
      count = MicroPassos;
      degree = 360;
    }
    count = count - 1;
  }
   else if (ButaoEstadoA||ButaoEstadoB == LOW)
   
  {
    MotorStop();
  }
   }

void MotorClockWsise()
{
  digitalWrite(Enable, LOW);
  digitalWrite(MotorDir, LOW);
  digitalWrite(MotorStep, HIGH);
  delayMicroseconds(msDelay);
  digitalWrite(MotorStep, LOW);
  delayMicroseconds(msDelay);
}

void MotorClock()
{
  digitalWrite(Enable, LOW);
  digitalWrite(MotorDir, HIGH);
  digitalWrite(MotorStep, HIGH);
  delayMicroseconds(msDelay);
  digitalWrite(MotorStep, LOW);
  delayMicroseconds(msDelay);
}

void MotorStop() {

  digitalWrite(MotorStep, LOW);
  digitalWrite(Enable, HIGH);
}


void CheckMicrosteps()
{
  if (val != ButaoEstadoC)
  {
    if (val == LOW) {
      pressC++;
      if (pressC > 3) {
        pressC = 1;
      }
    }
  }
  ButaoEstadoC = val;
}


void SerialPrint()
{
  Serial.print("    count=");
  Serial.print(count);
  Serial.print("    graus=");
  Serial.println(degree);
  Serial.print("    buttons press=");
  Serial.print(pressC);
}

void FullStep()
{
  MicroPassos = 200;
  Micro = 1;
  digitalWrite(MS1, LOW);
  digitalWrite(MS2, LOW);
  digitalWrite(MS3, LOW);
}
void HalfStep()
{
  MicroPassos = 400;
  Micro = 2;
  digitalWrite(MS1, HIGH);
  digitalWrite(MS2, LOW);
  digitalWrite(MS3, LOW);
}
void SixteenthStep()
{
  MicroPassos = 3200;
  Micro = 16;
  digitalWrite(MS1, HIGH);
  digitalWrite(MS2, HIGH);
  digitalWrite(MS3, HIGH);
}


i have a question that isnt making sense to me on this part of the code: else if (ButaoEstadoA||ButaoEstadoB == LOW)
 Shouldnt that be set has HIGH sense im using arduino internal pullups and they reversed state, and the motor would be off if im not pressing the button?
only works if is set on LOW,could you clarify me why is acting that way


Code: [Select]

void MotorButtons() {

  if (ButaoEstadoA == LOW)
  {
    MotorClockWsise();

    if (count == MicroPassos) {
      count = 0;
      degree = 0;
    }
    count = count + 1;
 
  }
 
 

 if (ButaoEstadoB == LOW)
  {
    MotorClock();

    if (count == 0) {
      count = MicroPassos;
      degree = 360;
    }
    count = count - 1;
  }
   else if (ButaoEstadoA||ButaoEstadoB == LOW)
   
  {
    MotorStop();
  }
   }


about the part of what i was trying to achived when i change the microsteps mode to change the count based on the microsteps i chose, i have tryed this, but doesnt work it will keep doubling and doubling, how could i make it to only double one time and then keep counting 1 by 1?
Code: [Select]

if (pressC == 2)
  {
    HalfStep();
count=count*2;
  }
  if (pressC == 3)
  {
    SixteenthStep();
count=count*16;
  }


but isnt working sense it will keep doubling the count over and over, no idea how can i make

Robin2

If you use INPUT_PULLUP then LOW means that the button has been pressed.

I think you need a separate variable to keep track of the displayed position so that it can be incremented by 16 when doing full steps, 8 when doing half steps and 1 when doing 1/16th steps. Immediately after the line
Code: [Select]
count = count - 1;
you could have a line
Code: [Select]
displayCount += microEquivalent; // assumes microEquivalent has value 16, 8 or 1

I notice in a few places that you have duplicate code. For example the two functions MotorClockWsise() and MotorClock() are identical apart from the line digitalWrite(MotorDir, LOW);

Avoiding duplication reduces the scope for errors and makes debugging easier.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Go Up