Go Down

Topic: How to modify the stepper pins from a PC (Read 652 times) previous topic - next topic

mBlast

Hi,

Here's a problem i can't solve with my poor programming skills. In the Stepper_Onerevolution sketch (from the arduino's example), the line 24 initialize the stepper library, giving the steps per revolution and the four pin numbers. This works fine. But in my sketch, i want to send these 5 datas from a PC, so that i can modify them "on the fly". Here's my sketch :
Code: [Select]
#include <Stepper.h>
const int ORDERMOTOR = 251;
int SerialValue;

int fnMotorGetData() {
  int MotorDef;
  int NumPin1;
  int NumPin2;
  int NumPin3;
  int NumPin4;
  fnWaitForBytes(10);
  MotorDef = Serial.read();
  NumPin1 = Serial.read();
  NumPin2 = Serial.read();
  NumPin3 = Serial.read();
  NumPin4 = Serial.read();
  fnMotorRun(MotorDef, NumPin1, NumPin2, NumPin3, NumPin4);
}

int fnMotorRun(int MotorDef, int PinOne, int PinTwo, int PinThree, int PinFour) {
    Motor.setSpeed(30);
    Motor.step(NumberOfSteps);
}

int fnWaitForBytes(int NbBytes) {
  while (Serial.available() < NbBytes) {
    delay(10);
  }
}

void setup(){
  Serial.begin(9600);
}

void loop(){

  if (Serial.available() >= 0) {
    SerialValue = Serial.read();
    switch (SerialValue) {

    case ORDERMOTOR:
      fnMotorGetData();
      break; 
    }
  }
}


With this one, when i do a check, arduino says that "Motor" was not declared in the good scope. If i add a line at the beginning that looks like
Code: [Select]
Stepper myStepper(stepsPerRevolution, 8,9,10,11); the datas that i send from the pc don't change anything - if another motor is on pin 4,5,6,7 and i send these datas, he doesn't move. Any idea of what i'm doing bad ? that would help a lot. Thanks

PaulS

So, where IS Motor defined?
Code: [Select]
int fnMotorRun(int MotorDef, int PinOne, int PinTwo, int PinThree, int PinFour) {
    Motor.setSpeed(30);
    Motor.step(NumberOfSteps);
}


You have a most unreasonable expectation in this code:
Code: [Select]
int fnMotorGetData() {
  int MotorDef;
  int NumPin1;
  int NumPin2;
  int NumPin3;
  int NumPin4;
  fnWaitForBytes(10);
  MotorDef = Serial.read();
  NumPin1 = Serial.read();
  NumPin2 = Serial.read();
  NumPin3 = Serial.read();
  NumPin4 = Serial.read();
  fnMotorRun(MotorDef, NumPin1, NumPin2, NumPin3, NumPin4);
}

Unless this function is not called unless Serial.available() is tested elsewhere (and known to be 5 or more), you will be reading garbage from the serial port, and will end up with a bunch of pins set to -1.

mBlast

You're right, i forgot to modify that  - the sketch provided here is a simplified version of another one. But still, with 
Code: [Select]
fnWaitForBytes(5); my main problem remain the same.

PaulS

Quote
my main problem remain the same.

Your main problem is that you keep posting snippets. Post all of your code, and clearly define what the problem is.

mBlast

Hum, the first code was complete, wasn't it ?
I found the answer... which is : it is not possible  =( Once the stepper is initialized with this :
Code: [Select]
Stepper myStepper(400, 8,9,10,11);
you can't modify the five required datas because the stepper.h define them as private. I will have to rewrite the stepper.h so that these datas can be modified once myStepper is initialized, but it is not in the scope of my skills for moment.

PaulS

Quote
you can't modify the five required datas because the stepper.h define them as private. I will have to rewrite the stepper.h so that these datas can be modified once myStepper is initialized, but it is not in the scope of my skills for moment.

I guess I fail to see why you want/need to do this, anyway. The pins actually have to have wires physically attached to them. You should not be arbitrarily assigning pins numbers. You should definitely not be hot-swapping motor control wires.

dxw00d

Quote
if another motor is on pin 4,5,6,7 and i send these datas, he doesn't move. Any idea of what i'm doing bad ? that would help a lot. Thanks


Define two motors. One on 4, 5, 6, and 7, and one on 8, 9, 10, 11. Then just send instructions for which motor to use.

mBlast

You're right, and i'm not doing hot rewiring. But as i need to control up to 7 steppers, i use an arduino mega wih many digital I/O. I wanted my PC software to be able to choose the motor pins if i need to change them without doing anything in the arduino sketch - some kind of user-friendly way to wire the motor and fully control them from the PC.

remiss

Keep 7 stepper variables and choose which one you will alter by a sending some command..

Btw, if you're using serial console in arduino: Unless you are typing รป into there you want be getting what you expect.. typing 1 in serial console will result in serial.read showing 49..

mBlast

weel, remiss : that was my idea... but it's not possible : stepper.h do not allow to modify stepper variables (number of steps per turn, pin1 to 4) once the objetc has been initialized. That's why i will need to rewrite stepper.h to do so... but that will be made later, i'm not good enough to modify library for the moment.

remiss

#10
Feb 06, 2012, 06:35 pm Last Edit: Feb 06, 2012, 06:40 pm by remiss Reason: 1

weel, remiss : that was my idea... but it's not possible : stepper.h do not allow to modify stepper variables (number of steps per turn, pin1 to 4) once the objetc has been initialized. That's why i will need to rewrite stepper.h to do so... but that will be made later, i'm not good enough to modify library for the moment.


Initialize several stepper objects then...

At any rate, make it work with one stepper motor first..

edit: Also, my more important point was that if you are using normal serial console through arduino IDE, writing 251 will result in when calling serial.read three times outputting 50, 53, 49 or '2', '5', '1'

kf2qd

Since you are controlling this from your PC and serial port, why worry about tHose settings in the Arduino - just send it the number of steps you want and do the housekeeping in the PC.

You can drive more steppers from the Arduino if you make/get some driver boards that woulds only use 2 control wires - 1 for step and 1 for direction. Depending on how many motors are running at one time you could have multiple motors direction inputs tied together as long as you only drive one of them at a time, or keep track of those that can run the same direction and only run those together. As is, on an Arduino you could drive 6 step & direction type controllers. (And you could program a ATTiny2313 or similar chip as the brain in the controller driving the transistors/bridge connected to the motor.)

Go Up