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.