Stepper capabilities

I have read much of what is one here on the use of a stepper motor with an arduino board, but I am still not altogether sure whether it can allow me to achieve my purpose "reasonably easily". Before I get stuck in I wondered whether in principle I could achieve the following:

  1. I'd like to be able to rotate a stepper using external software, which will also be recording several over instruments, by a given total number of steps.

  2. The stepper should do it's thing, count each step or n steps I set and spit out the total counts to a register that my program, when required, can read. The stepper will keep working till the total number of steps are made and the register will of course just continue updating if it is isn't queried.

  3. Would prefer if the number in the register is a number relative to some value I initialise it to, this is not critical at this stage, but would be handy.

  4. If I change my mind and decide that the rotation I set is heading me towards disaster, I would like to be able to stop/interrupt the on going rotation.

The questions relating to the points above:

  1. How quickly can I communicate and get information like a simple integer number of steps from the board? 10 times/sec, 1 time/second etc?

  2. I read somewhere that every time you connect to board it runs your script from the start, this would mess up my initialisation, why is this setup that way and is there a way around this default?

I have already ordered a board but trying read as much and understand as much as possible before getting it, so don't have any code or definite questions yet but would appreciate answers to these points to get a sense of the scope available to me.

Cheers!

  1. How quickly can I communicate and get information like a simple integer number of steps from the board? 10 times/sec, 1 time/second etc?

More like hundreds of times a second.

  1. I read somewhere that every time you connect to board it runs your script from the start, this would mess up my initialisation, why is this setup that way and is there a way around this default?

that is the way it works, you have to write your code to suite.

Thanks for your response.

  1. I read somewhere that every time you connect to board it runs your script from the start, this would mess up my initialisation, why is this setup that way and is there a way around this default?

that is the way it works, you have to write your code to suite.

So I see that I would have to initialise then do small step take value, read other instruments, and repeat. This, if you say I can communicate 100s of times per second, shouldn't be a problem. Would it be possible to keep a running count in non-volatile memory, is such a thing available?

It is just that if my external programme falls over then it might be very difficult for me to know the position of the stepper, I can't see or read it off anyway physically when I would have it running and that would be very problematic.

Cheers

Ardenuf:
Thanks for your response.

  1. I read somewhere that every time you connect to board it runs your script from the start, this would mess up my initialisation, why is this setup that way and is there a way around this default?

that is the way it works, you have to write your code to suite.

So I see that I would have to initialise then do small step take value, read other instruments, and repeat. This, if you say I can communicate 100s of times per second, shouldn't be a problem. Would it be possible to keep a running count in non-volatile memory, is such a thing available?

It is just that if my external programme falls over then it might be very difficult for me to know the position of the stepper, I can't see or read it off anyway physically when I would have it running and that would be very problematic.

Cheers

I found the answer to my question: http://arduino.cc/playground/Main/InterfacingWithHardware#Storage

You can't just keep a count and hope they will remain in step, you need to calibrate it every time the power is applied.
Remember you have a limit on the number of times you can write to an EEPROM location, it might be 100,000 times but if you are writing at the rate you are sending steps it will fail soon.

You need to reconsider what it is you are doing, you don't do stepping motors like you are suggesting, you need some form of feedback or absolute position indicator.

I have been away from this for a while, but I am back now!

I have now got the stepper working and I think that my original ideaof just counting will work because I will be running it way under the stall torque and hence there shouldn't be a problem with skipped steps. Also i will have something reading the displacement that I will be able to back calibrate with as a double check. In principle of course I could read this with the UNO and check each time I step but that will be the next iteration.

So my question goes back to my original post. If I wanted to change the direction, switch the motor on and off, read the number of counts etc, could I do this through the same cable as I propgrammed the board with?

Right now I have the whole thing doing what I want, in terms of control, with external switches to do interrupts. I'd like to use a computer programme to do this instead and wondered if i would have to use the RS232 serial port?

Many thanks!

I'd like to use a computer programme to do this instead and wondered if i would have to use the RS232 serial port?

Yes the USB lead looks to a PC like an RS232 port. You can fire commands down that and get the arduino to interpret them, do what you ask and fire answers back. It is quite a standard thing to do.

Thanks for that. I have now got this working like so where the stepper is interrupted and the direction switched on the toggle of a switch.

What I want to ask is why when I restart serial monitor it always goes from the start of the count. If I start the serial monitor when i first uploaded the programme the count number starts at 0 and if I start the serial monitor after running for 1 minte it starts at 0, doesn't the programme just keep counting? I don't understand why the count is reset.

int motorPin1 = 9;
int motorPin2 = 10;
int motorPin3 = 11;
int motorPin4 = 12; 
int interruptpin = 0;  // Interrupt 0 is on DIGITAL PIN 2!
volatile int state = LOW;  // The input state toggle
int delayTime = 3;  // 3 msecs seems to be as fast you can go and not have it
                    //stick when you change direction
int count =0;
void setup() {
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(motorPin3, OUTPUT);
  pinMode(motorPin4, OUTPUT);
  Serial.begin(9600); 
  //Attach the interrupt to the input pin and monitor for ANY Change
  attachInterrupt(interruptpin, stateChange, CHANGE);
 
}

void loop() {
  
  if (state){
  digitalWrite(motorPin1, HIGH);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, HIGH);
  digitalWrite(motorPin4, LOW);
  delay(delayTime);
  digitalWrite(motorPin1, HIGH);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, HIGH);
  delay(delayTime);
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, HIGH);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, HIGH);
  delay(delayTime);
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, HIGH);
  digitalWrite(motorPin3, HIGH);
  digitalWrite(motorPin4, LOW);
  delay(delayTime);
  count = count + 1;
  Serial.println(count);
  delay(1000); 
}
 
 else
  if (!state) {
    
   digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, HIGH);
  digitalWrite(motorPin3, HIGH);
  digitalWrite(motorPin4, LOW); 
    delay(delayTime);
  digitalWrite(motorPin1, LOW);
  digitalWrite(motorPin2, HIGH);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, HIGH); 
  delay(delayTime);
  digitalWrite(motorPin1, HIGH);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, LOW);
  digitalWrite(motorPin4, HIGH);
   delay(delayTime);
  digitalWrite(motorPin1, HIGH);
  digitalWrite(motorPin2, LOW);
  digitalWrite(motorPin3, HIGH);
  digitalWrite(motorPin4, LOW);
  delay(delayTime);
  count = count - 1;
  Serial.println(count);
  delay(1000);
  }
  
  
}

void stateChange()
{
  static unsigned long last_interrupt_time = 0;
  unsigned long interrupt_time = millis();
  // If interrupts come faster than 50ms, assume it's a bounce and ignore
  if (interrupt_time - last_interrupt_time > 50)
  {
    state = !state;
  }
  last_interrupt_time = interrupt_time;
}

Ardenuf:
What I want to ask is why when I restart serial monitor it always goes from the start of the count. If I start the serial monitor when i first uploaded the programme the count number starts at 0 and if I start the serial monitor after running for 1 minte it starts at 0, doesn't the programme just keep counting? I don't understand why the count is reset.

There is some variation, but starting the serial monitor will restart an Arduino. I think this is always true for a UNO when connecting to the USB, from any OS.

Thanks!

I thought so but assume there must be a way to do this without reseting the counter? How could I get the count without reseting it? using a different serial connection and reading with a separate computer programme? That's what I eventually plan to do but wanted to do it with the USB connection.

It is something that is node bynthe drivers in the PC. You could get a driver that does not reset the the arduino by toggling the DTR line but I don't know of a varent. The other way is to disconnect the auto reset on the arduino, there is a small track you can cur. But this means you have to use manual reset when uploading sketches