Go Down

Topic: Signal timing (Read 77 times) previous topic - next topic

mattman4347

I'm working on a project with stepper motors. I have them turn every 20 seconds. It takes 1 second for them to complete that. I'm wondering if the one second will accumulate over time, or if it starts counting again as soon as the signal to turn is sent. I will post the code below but I'm not sure if it will help.

Thanks- Matt



#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_StepperMotor *myMotor = AFMS.getStepper(100, 2);
void setup() {
  AFMS.begin();
  myMotor->setSpeed(1);
}
void loop() {
  myMotor->step(1, FORWARD, MICROSTEP);
  delay(20000);
  myMotor->step(1, FORWARD, MICROSTEP);
}

free-bee

#1
Aug 15, 2015, 04:01 am Last Edit: Aug 15, 2015, 04:06 am by free-bee
I know nothing about the motor shields. Here's what I would do. Get a stopwatch and start it when the motors stop moving. Stop the watch when the motors stop moving the second time. You don't need to be perfect on your timing, just well enough to know if there's an extra second in the execution.

As for if that extra second will accumulate over time depends on the application. If you need the motors to turn in sync with something else, one second is more than enough to notice. After one minute, the motors will be 3 seconds late to update. If that's the case, just change "delay(20000)" to "delay(19000)". But this is just if the controller has to take the extra second delay. You could try this (which should work either way):
Code: [Select]
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_StepperMotor *myMotor = AFMS.getStepper(100, 2);
unsigned long Now;
void setup() {
  AFMS.begin();
  myMotor->setSpeed(1);
}
void loop() {
Now=millis();
  myMotor->step(1, FORWARD, MICROSTEP);
  while(millis()-Now <= 20000){
  //do nothing
}
  myMotor->step(1, FORWARD, MICROSTEP); //is this supposed to be here?
}

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy