arduino initialisation

Not certain if this is the correct section of the forum, but I'll ask here anyway. If it ought to be elsewhere please advise.
I'm driving a stepper motor to control an IC engine throttle. The engine supplies the elec power for the arduino and i want the throttle in a closed position for startup, the choke setting for the throttle is sufficient for engine start. The arduino however sends signals to the stepper whilst it is (presumably) initialising and drives the throttle to random positions.
When the engine fails to start which it will with the throttle in other than the closed position, the elec supply to the arduino also stops and we're back to square one. I thought to try a servo instead of the stepper motor, but the same issue arises although the servo is more readily set to the "closed' position.

Is there a way to get around this without having to have an independent elec supply for the arduino?
thanks,
mike

Can You draw a system description of this and show us? A wiring diagram would also be helpful.

Post your code, too. Or a shortened version that shows the unwanted motion on start up.

This code does the same thing, ie a random move before the control loop takes effect. I have shown the servo version but the stepper motor has the same startup issue.

/* Sweep
 by BARRAGAN <http://barraganstudio.com>
 This example code is in the public domain.

 modified 8 Nov 2013
 by Scott Fitzgerald
 http://www.arduino.cc/en/Tutorial/Sweep
*/

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position

void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
//myservo.write(5);
delay(1000);
}

void loop() {
  for (pos = 5; pos <= 175; pos += 1) { // goes from 5 degrees to 175 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  for (pos = 175; pos >= 5; pos -= 1) { // goes from 175 degrees to 5 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
}

I don’t think its a code issue as such, more an issue with what the arduino is doing with its outputs whilst its powering up.
As far as a cct diagram goes, think of this;
the engine drives a generator. The generator supplies power to the arduino. It can power the arduino even whilst starting up as it generates sufficient voltage and current even at cranking (hand cranking that is) speeds. The stepper motor is connected to the arduino and mechanically to the throttle all the time.
I could insert a time delay in the arduino power cct, but the throttle needs to be closed and the arduino needs to do that.

mikep32:
This code does the same thing,

Your Original Post says that you are using a stepper motor but the code in Reply #3 is for a servo - a very different thing.

...R

Conventionally in RC planes boats etc servos are used for controlling throttles and are a simple proven solution

Would it make any difference if pin 9 has a pull down resistor, and there is a digitalWrite(9,0) followed by a "pinMode(9, OUTPUT)"?

hammy:
Conventionally in RC planes boats etc servos are used for controlling throttles and are a simple proven solution

I know. But is the OP actually using a servo or a stepper?

And a model plane or boat is not normally used for long periods.

...R

Robin2:
Your Original Post says that you are using a stepper motor but the code in Reply #3 is for a servo - a very different thing.

...R

yes, i'll probably use a stepper motor because the servo is a modellers servo and probably not good for long periods of use. I expect it would not survive long. I posted the servo code simply because both the servo and stepper exhibit the same random movement while the arduino is powering up.

The initial conditions required by the engine are a closed throttle for engine start. As the throttle could have been left in any position during shutdown, i need the throttle to be driven to the closed position.
The servo, as a positioning device, would have been good for that which is why i looked into it and found the same initialisation problem existed. The code was easier to post.

Railroader:
Would it make any difference if pin 9 has a pull down resistor, and there is a digitalWrite(9,0) followed by a "pinMode(9, OUTPUT)"?

thank you for that suggestion. I recall seeing some discussion around that topic, something to do with "charging the buffer" or something like that (??), but had forgotten the details, I will certainly give that a try.

mikep32:
I will certainly give that a try.

I’ve tried, no improvement. I’ll attach the code with the extra lines. Perhaps I’ve put it in the wrong place?
I did notice that the “random” movement seems to be a position jump to about the 90 degree point and takes place just after myservo.attach(9).

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position

void setup() {
  digitalWrite(9,0);
  pinMode(9,OUTPUT);
 Serial.begin(57600);
 Serial.println("about to attach servo");
 delay(2000); 
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
Serial.println("servo attached");
delay(2000);
}

void loop() {
  for (pos = 5; pos <= 175; pos += 1) { // goes from 5 degrees to 175 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
  for (pos = 175; pos >= 5; pos -= 1) { // goes from 175 degrees to 5 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
}

mikep32:
I posted the servo code simply because both the servo and stepper exhibit the same random movement while the arduino is powering up.

The reasons and the solution are likely to be very different so please post the stepper motor code that is causing you a problem.

Studying a parrot when it is the dog that is ill is just a waste of time.

...R

mikep32:
I’ve tried, no improvement. I’ll attach the code with the extra lines. Perhaps I’ve put it in the wrong place?
I did notice that the “random” movement seems to be a position jump to about the 90 degree point and takes place just after myservo.attach(9).

Yes, that is what I had in mind, but it didn't help, I understand. 

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position

void setup() {
  digitalWrite(9,0);
  pinMode(9,OUTPUT);
Serial.begin(57600);
Serial.println(“about to attach servo”);
delay(2000);
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
Serial.println(“servo attached”);
delay(2000);
}

void loop() {
  for (pos = 5; pos <= 175; pos += 1) { // goes from 5 degrees to 175 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable ‘pos’
    delay(15);                      // waits 15ms for the servo to reach the position
  }
  for (pos = 175; pos >= 5; pos -= 1) { // goes from 175 degrees to 5 degrees
    myservo.write(pos);              // tell servo to go to position in variable ‘pos’
    delay(15);                      // waits 15ms for the servo to reach the position
  }
}

If you want the servo to start at 0 degrees, do a servo.write(0) before the servo.attach.

groundFungus:
If you want the servo to start at 0 degrees, do a servo.write(0) before the servo.attach.

That helped a lot. The servo returned to its zero without much delay, and only a minor movement as the arduino powered on. Good enough i would think.
Thanks

With a stepper motor, you would need to drive the stepper to the closed position on startup, preferably with a switch mounted so the arduino can detect when that position has been reached, although not needed if there is a hard stop at that location.

Have not dealt with any mechanics on modern engines, but back in the days of carburetors there was a spring to return the throttle to the closed position, also served as a safety device to return the engine to idle if the throttle linkage broke.

I’m having a good deal of trouble with the forum/my pc/my browser /my fingers or who knows!! If i have already posted this reply please forgive me. My reply disappeared when i tried to preview it and i don’t sewem to have access to my profile to see if it was saved as a draft.

Thanks David, my code includes an essential “close throttle” in setup(). A spring is not possible and was not part of the original gear.

The randomness of my stepper operation turned out to be simply bad connections in the many low quality plugin wires in my test setup. The servo problem was a valid problem solved thanks to “fungus”.

i still have an issue with initialisation though. The engine is a pull start. Enough elec energy is produced on each pull to begin the arduino initialising, but the code needs to get to the ‘closethrottle’ in setup() to close the throttle to allow engine to start.
A larger capacitor in the power supply might help, but of course will take more energy to charge.
I count 6 seconds from initial power-on to get to the closethrottle in setup(). Is there any way to reduce that time?
I enclose my code with all junk esp that before the setup() deleted.

//====pin3 will be the  speed pulse input and trigger interrupt 1
volatile unsigned long revstart = 0;  // start of cycle
volatile unsigned long revend = 0;//start of next cycle
volatile float deltat; //time between revstart and revend, ie period of cycle
float freq = 0.0;
float freqerror = 0.0; //difference between setpeed and actual speed
float freqerrorabs = 0.0; //absolute value of freqerror
const byte ecopin = 7;
byte ecopinstate;
const byte stepPin = 2; //make a step; must be pin2 if cnc shield is used
const byte mso1 = 8; //set combinatn of mo1,2,3 for fractional steps
const byte mso2 = 9; //ditto
const byte mso3 = 10; //ditto
const byte dirPin = 5; //set this to 1 (high) or 0 (low) for cw or ccw, must be pin5 if cnc shield is used
int n = 0;
int stepPinstate = 0;

//*********THIS SECTION TO ADJUST PARAMETERS*****************

int throttlestart = 4800;

int triggerpt = 1; //number of interrupts before updating speed

float setspeed = 60.0; //desired engine speed in Hz

float ecoSpeed = 50.0;

float dBand = 0.50; //dzone is the acceptable setspeed deadzone eg 60Hz +- dzone

int wait = 100; //TIME BETWEEN HIGH AND LO TO STEPPER, ALSO USED FOR TUNING

int throttleholdoff = 2000;

int notstarted = 25; //speed below which engine considered to not be running
//=================================
float  checkspeed()
{
  noInterrupts();
  freq = 1000000 / (deltat);
  interrupts();
  ecopinstate = digitalRead(ecopin);
  if (ecopinstate == LOW) {
    setspeed = ecoSpeed; //reduce setspeed from nominal to ecospeed hz for economy mode
  }
  else setspeed = 60;

  freqerror = freq - setspeed;
  freqerrorabs = abs(freqerror);

}

//=========================
void checktISR() {
  revstart = revend;// set revstart to previous revend micros
  revend = micros(); //set revend to current micros
  deltat = revend - revstart;
  //Serial.println(deltat/1000);
}

//==========================
int tuneup() {

  if (freqerrorabs > 10) {
    wait = 300;
  }
  else if (freqerrorabs > 5) {
    wait = 400;
  }
  else if (freqerrorabs > 3) {
    wait = 600;
  }
  else
    wait = 900;

}

//===========================
int moveThrottle() {

  
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(wait); //delay some micros

    digitalWrite(stepPin, LOW);
    delayMicroseconds(wait);
    
}

//================================
void setup() {
  //pinMode(11,INPUT_PULLUP);//USED IF LIVE TUNING ADJUSTMENT IS IN PLAY
  //pinMode(A0,INPUT);     //DITTO
  Serial.begin(9600);
  Serial.println("10august-uses-wait-engine-spd-cntrl");
  delay(2000);
  pinMode(ecopin, INPUT_PULLUP);
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode (mso1 , OUTPUT); //all high sets 1/16th steps
  pinMode (mso2 , OUTPUT);
  pinMode (mso3 , OUTPUT);
  digitalWrite(mso1, HIGH);
  digitalWrite(mso2, HIGH);
  digitalWrite(mso3, HIGH);

  // close throttle for startup
  digitalWrite(dirPin, LOW); // Enables the stepper-motor to close the throttle
  for (int n = 0; n < throttlestart; n++) //n determines the angle to move vs step size
  {
    digitalWrite(stepPin, HIGH);
    //stepPinstate = digitalRead(stepPin);
    // Serial.print(stepPinstate);
    delayMicroseconds(wait); //allow stepper to make step
    digitalWrite(stepPin, LOW);
    delayMicroseconds(wait);
      }
      
  delay (throttleholdoff); //wait millis to let engine startup speed settle
  Serial.print("end of 'close throttle for startup'");
  attachInterrupt(digitalPinToInterrupt(3), checktISR, RISING); //rise on pin 3 triggers checktisr
  
}

//==========
void loop()
{
  checkspeed();
  if (((freqerrorabs) > dBand) && (freq > notstarted)) {
      
    tuneup();
    
    if (freqerror > ( dBand)) {
    digitalWrite(dirPin, LOW); //LOW = close throttle
    moveThrottle();

    }
    else if (freqerror < (-dBand) && (freq > notstarted)) {
    digitalWrite(dirPin, HIGH); //HIGH= open throttle
    moveThrottle();
    }

  }

}

Thanks for any help.

Well I’m a goose! :fearful: I just noticed a 2 sec delay and a serial.print at the start of setup() . Removing those reduces the power-on to closethrottle by, surprise, about 2 secs. I’d still like it to be lower, so any ideas please?

Instead of trying to make it an instant ON device.

Energize the arduino.
Let run through it's routine of exercising the pins.
Once it has settled.
Then run the sketch

That way it does not matter if we are controlling a stepper or a servo

At the end of setup()
Set everything in the place for start
When control passes to loop()
Start.

Not much scope for change. Serial isn't going to help since you don't really print much. Is there a reason to run at such a low baud rate though?

You have a delay at the end, can you reduce it?

Also, you have 4800 steps to make, can you reduce one or both of the delays in that loop?