Telescope control timing problem

Hi,

I posted this code when I had a do..while loop problem, which is now solved.

Now the real enigma...

The defaul timing loop for my stepper driver should give 21.3 Hz (this gives correct rate for telescope to track stars), but I've found in practice you have to be able to trim this rate slightly. So I've set up some control with a pot on analog pin A0 to trim +/- 10% when the pot is away from its mid point (with +/- 500 'deadzone').

I have checked the pot output and the maths:
//trimming control
trim = analogRead(A0);
if (trim<400){ra = raset-((raset0.1)((-0.0025trim)+1)); //raset-((raset0.1)((-0.0025trim)+1))
}
if (trim>600){ra = raset+((raset0.1)((0.002364trim)-1.4151)); //raset+((raset0.1)((0.0023585trim)-1.4151))
}
if (trim<=600 && trim >=400){ra=raset;}

The timer pulse should give between 19 and 23 Hz with a default of 21.4 Hz

What I get is: 70Hz default !!why??!!
And when pot read = 0, 103Hz; and when pot read = 1023, 371Hz !!

This is crazy. Is there something obviously wrong with my timing loops and the delayMicroseconds ?? Why do I get 70Hz when 1/0.046900 = 21.3 ??? How can the loop run faster than the programmed delay?

Thanks for any help.

Theo

P.S. I know the control program still has problems, but my concern here is why the timing is off.

Here's the whole thing:

//scope

int trim = 0; //1024 = 5v
long ra = 46895;
long raset =46895;
float acc = 1;
float dacc = 1;
int pulselength = 5;
float accrate = 0.05;
int npin = 4;
int spin = 5;
int epin = 6;
int wpin = 7;
//int gnpin = 4;
//int gspin = 5;
//int gepin = 6;
//int gwpin = 7;
int rastep = 8;
int radir = 9;
int decstep = 10;
int decdir = 11;
int raenable = 12;
int decenable = 13;

//tracking rate = 21.317hz = 0.0469 sec
// with 5us pulse 1x RA = 46895us

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

//N
//pinMode(0,INPUT); //will be used for guide inputs when not on serial
//S
//pinMode(1,INPUT);
//E
//pinMode(2,INPUT);
//W
//pinMode(3,INPUT);

// guide N
pinMode(4,INPUT);
//S
pinMode(5,INPUT);
//E
pinMode(6,INPUT);
//W
pinMode(7,INPUT);

//step RA
pinMode(8,OUTPUT);
//dir RA
pinMode(9,OUTPUT);

//step dec
pinMode(10,OUTPUT);
//dir dec
pinMode(11,OUTPUT);

//enable ra
pinMode(12,OUTPUT);
//enable dec
pinMode(13,OUTPUT);

digitalWrite(radir, HIGH); //set fwd on RA
digitalWrite(raenable, HIGH); //enable ra
digitalWrite(decenable, HIGH); //enable dec

}

//timing loop
void loop() {

//Serial.print(trim);
//Serial.println();
//Serial.print("  ;  ");
//Serial.print(ra);
//Serial.print("  ;  ");
//Serial.print(acc);
//Serial.print("  ;  ");
//Serial.print(1/(ra/acc * 1E-06));
//Serial.print(digitalRead(wpin));
//Serial.print(digitalRead(epin));
//Serial.print(digitalRead(spin));
//Serial.print(digitalRead(npin));
 // Serial.println();

debug print

//trimming control
 trim = analogRead(A0);
 if (trim<400){ra = raset-((raset*0.1)*((-0.0025*trim)+1)); //raset-((raset*0.1)*((-0.0025*trim)+1))
 }

 if (trim>600){ra = raset+((raset*0.1)*((0.002364*trim)-1.4151)); //raset+((raset*0.1)*((0.0023585*trim)-1.4151))
 }
 
 if (trim<=600 && trim >=400){ra=raset;}

  while (digitalRead(wpin)==HIGH)
   {

read pot on pin A0 and trim 'ra' value +/- 10%

 // RA slew w
      acc = acc + accrate;
      if (acc > 500) {acc = 500;}
     digitalWrite(radir, HIGH);
     digitalWrite(rastep, HIGH);   
     delayMicroseconds(pulselength);
     digitalWrite(rastep, LOW);  
     delayMicroseconds(ra / acc);
   }
    
     
  while (digitalRead(epin)==HIGH)
    {
      // RA slew e
      acc = acc + accrate;
      if (acc > 500) {acc = 500;}
     digitalWrite(radir, LOW); //reverse direction
     digitalWrite(rastep, HIGH);   
     delayMicroseconds(pulselength);
     digitalWrite(rastep, LOW);  
     delayMicroseconds(ra / acc);
    }
    
   while (digitalRead(npin)==HIGH)
   {
   //dec slew n
   dacc = dacc + accrate;
   if (dacc > 500) {dacc = 500;}
     digitalWrite(decdir, HIGH);
     digitalWrite(decstep, HIGH);   
     delayMicroseconds(pulselength);
     digitalWrite(decstep, LOW);  
     delayMicroseconds(ra / dacc);
     }
   
  while (digitalRead(spin)==HIGH)
  {
   //dec slew s
   dacc = dacc + accrate;
   if (dacc > 500) {dacc = 500;}
     digitalWrite(decdir, LOW);
     digitalWrite(decstep, HIGH);   
     delayMicroseconds(pulselength);
     digitalWrite(decstep, LOW);  
     delayMicroseconds(ra / dacc);
     }

if telescope slew buttons pressed then move scope with accelerating speed up to 500x 'raset'

//dec pulse if dacc>1
    if (dacc > 1) {dacc=dacc-(dacc/100); 
      
    digital Write(decstep, HIGH);   
    delayMicroseconds(pulselength);
    digitalWrite(decstep, LOW);  
    delayMicroseconds(ra / dacc); //range of dacc=1 to 500
    }
    else
      {dacc = 1;
          if (acc > 1) //bring down slew rate if nothing pressed
    {acc = acc -(acc/100); }
    else
    {acc=1; }

if buttons not pressed, decelerate to normal speed

    // RA pulse when no other inputs
    digitalWrite(rastep, HIGH);   
    delayMicroseconds(pulselength);
    digitalWrite(rastep, LOW);  
    delayMicroseconds(ra / acc); //range of acc=1 to 500
      }//end ra else

}   //end loop

normal speed stepping when nothing pressed, should be 21.3 hz but actually gives 70hz?

It would be polite to post the code using code tags (use the # icon on the editor's toolbar), and add some meaningful comments, and remove the meaningless ones.

ok, thanks, didn't know how to label code- done it now, hope it makes it clearer. The real problem is the very last bit - the timing loop... Why does it give 70hz (measured with a frequency meter on pin8/gnd)?

Any ideas?

Theo

Maybe you should try to reduce your sketch to one that produces just your required 21.3 Hz and build from that.

My experience is that if I can't see the cause of the problem somewhere, I usually look the wrong way :slight_smile:

Another thing would be to make the things that should never change when your sketch runs, like pin numbers, into "const"s.

it is also polite to remove code that you have commented-out.

OK, I hope this is polite.

here's the simple loop:

void loop() {

digitalWrite(rastep, HIGH);
delayMicroseconds(5);
digitalWrite(rastep, LOW);
delayMicroseconds(46895);

} //end loop

I've checked this loop on its own and it gives 70Hz, when it should give 21.3Hz.

Could it be noise in the pulse confusing my frequency meter? With my 555 circuit that I'm trying to replace with the arduino (to give acceleration etc.) it reads 21.3Hz, so the frequency meter appears to be accurate on that circuit. I don't have an osciloscope to check how clean the pulse signal is though.

Thanks,

Theo

How about

delayMicroseconds(46895L);

?

What does the 'L' mean? I tried it anyway and got the same result. Still 70Hz.

or try this:

void loop() 
{
  digitalWrite(rastep, HIGH);
  delayMicroseconds(5);
  digitalWrite(rastep, LOW); 
  delay(46);  
  delayMicroseconds(895);
}

The L stand for Long ...

OK, you have it...

delayMicroseconds is not accurate for large numbers and freaks out. I'm now using delay(val/1000) instead and it is working perfectly.

Thanks for the help!

Theo