Go Down

Topic: Arduino Motor Shield Stepper Motor control with Joystick (Read 8 times) previous topic - next topic

TheBlommie

Hi Ash

I amended the code accordingly but still no luck. Giving me one-liner codes won't really help me since I don't know arduino coding or any other type of programming to a huge extend.

Here is the code the last time with the changes...

#include <Stepper.h>

const int spr = 200; // motor steps per revolution
const int pwm_cha = 3;
const int pwm_chb = 11;
const int dir_cha = 12;
const int dir_chb = 13;
const int brake_cha = 9;
const int brake_chb = 8;

int Pot1 = A0;

Stepper stepper(spr, dir_cha, dir_chb);

void setup()
{
    pinMode(Pot1, INPUT);
    pinMode(pwm_cha, OUTPUT);
    pinMode(pwm_chb, OUTPUT);
    pinMode(brake_cha, OUTPUT);
    pinMode(brake_chb, OUTPUT);
    digitalWrite(pwm_cha, HIGH);
    digitalWrite(pwm_chb, HIGH);
    digitalWrite(brake_cha, LOW);
    digitalWrite(brake_chb, LOW);
}

void loop()
{
  int PVal=analogRead(Pot1);
  if (PVal >= 507 || PVal <= 517)
  {
   
  }
  if (PVal > 517 )
  {
    int K= map (PVal, 518,1023,0,75); // max is the value that you get from your experiment with the max speed of the stepper before stalling
    stepper.setSpeed(K); //this set the speed of the stepper motor
    stepper.step(spr); //this set the direction and the number of step it would take
  }
  if (PVal < 507 )
  {
    int K= map (PVal, 0,506,0,75); // max is the value that you get from your experiment with the max speed of the stepper before stalling
    stepper.setSpeed(K); //this set the speed of the stepper motor
    stepper.step(-spr); //this set the direction and the number of step it would take
  }
}

For some reason nothing is happening.

But thanks anyway for your help. I'm sure I'll manage somehow somewhere eventually while searching the net. I need a solution quite urgent and can't struggle like this for too long.

Regards

noisyvoid

I have been using your problem to learn about the stepper board.  And the problem you are having is due to the Shield using A0 as current sensing.  Move the Pot to A2 and amend the code accordingly.

Secondly, the mapping are slightly wrong - the last if statement needs to be reversed.

Finally, I have improved the response time when in the slow speed by mapping the number of steps to the speed.

I have put some prints in to look at the values and also there is some servo stuff to show the position of the pot - this can all be removed

regards,
James

Code: [Select]

#include <Stepper.h>
#include <Servo.h>

const int num_step = 50; // motor steps per revolution
const int pwm_cha = 3;
const int pwm_chb = 11;
const int dir_cha = 12;
const int dir_chb = 13;
const int brake_cha = 9;
const int brake_chb = 8;


int Pot1 = 2;
int val;
Servo myservo;  // create servo object to control a servo
Stepper stepper(num_step, dir_cha, dir_chb);

void setup()
{
   Serial.begin(9600);
   pinMode(pwm_cha, OUTPUT);
   pinMode(pwm_chb, OUTPUT);
   pinMode(brake_cha, OUTPUT);
   pinMode(brake_chb, OUTPUT);
   digitalWrite(pwm_cha, HIGH);
   digitalWrite(pwm_chb, HIGH);
   digitalWrite(brake_cha, LOW);
   digitalWrite(brake_chb, LOW);
   
   myservo.attach(2);
}

void loop()
{
 int PVal=analogRead(Pot1);
 if (PVal >= 507 || PVal <= 517)
 { // fill in with the one you need to make it brake
 }
 if (PVal > 517 )
 {
   int K= map (PVal, 518,1023,0,400); /* max is the value that you get from your experiment with the max speed of the stepper before stalling*/
    int S=map (K, 0,400,0,num_step);
    stepper.setSpeed(K); //this set the speed of the stepper motor
    stepper.step(S); //this set the direction and the number of step it would take
    Serial.print("Forward, K=");
    Serial.print(K);
    Serial.print(" PVAL=");
    Serial.println(PVal);    
  }
if (PVal < 507 )
 {
   int K= map (PVal, 0,506,400,0); /* max is the value that you get from your experiment with the max speed of the stepper before stalling*/
    int S=map (K, 0,400,0,num_step);
    stepper.setSpeed(K); //this set the speed of the stepper motor
    stepper.step(-S); //this set the direction and the number of step it would take
    Serial.print("Backward, K=");
    Serial.print(K);
    Serial.print(" PVAL=");
    Serial.println(PVal);    
  }
 
 // This next bit just outputs the pot position to a servo
 val = map(PVal, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180)
 myservo.write(val);                  // sets the servo position according to the scaled value
 delay(15);      
}

TheBlommie

Ash, you are awesome man!! Thanks so much. My headaches are over. You cracked the code and full credit goes to you. I'll post a link on youtube as soon as my project is complete.

But thanks again! I really appreciate it very much!

TheBlommie

Ash, you are awesome man!! Thanks so much. My headaches are over. You cracked the code and full credit goes to you. I'll post a link on youtube as soon as my project is complete.

But thanks again! I really appreciate it very much!

TheBlommie

Ash, you are awesome man!! Thanks so much. My headaches are over. You cracked the code and full credit goes to you. I'll post a link on youtube as soon as my project is complete.

But thanks again! I really appreciate it very much!

Go Up