Teensy 3.5 problem with incromenting a number

Status
Not open for further replies.

JDMDyno

New member
Hi All

I have a problem, hope I can explain it well enough

I have a sketch that increments from a start number to an end number at set interval of time and a variable increase per interval (400/sec or 200/sec etc)

so you could enter 3000 to 8000 at 400/sec rate the interval is 10 milliseconds, it should take 12.5 seconds to get from 3000 to 8000 so I did the code below which works ok but when I put this into my main code it will only increment one time and stays at 3004 like it is not counting the currentMillis

this is a snippet of the serial monitor output, it takes it approx. 400 millis to start the count so is 12.5 sec long as it should be

count 7980
currentMillis 12925
interval1 10
Srate2 4
trgtRPM 7984
count 7984
currentMillis 12935
interval1 10
Srate2 4
trgtRPM 7988
count 7988
currentMillis 12945
interval1 10
Srate2 4
trgtRPM 7992
count 7992
currentMillis 12955
interval1 10
Srate2 4
trgtRPM 7996
count 7996
currentMillis 12965
interval1 10
Srate2 4
trgtRPM 8000
count 8000




Code:
int rat = 0;
long previousMillis1 = 0;
unsigned long interval1 = 10;
int Srate1 =0;
int Srate2 =0;
int trgtRPM;

void setup()
{
  rat=3000;
  Srate1 = 400;
  Srate2 = (Srate1 / 100);
  
}

void loop()
{
  RPMSweep();
} 
 void RPMSweep(){
  unsigned long currentMillis = millis();
  
  
  if(currentMillis - previousMillis1 >= interval1) {

    // Serial.print("previousMillis1 ");
    // Serial.println(previousMillis1);
    previousMillis1 = currentMillis;
    
       
    
    if (rat <8000) {
      rat = rat += (Srate2) ;
    
      trgtRPM = rat;
      
       Serial.print("currentMillis ");
      Serial.println(currentMillis);
      Serial.print("interval1 ");
      Serial.println(interval1);
      Serial.print("Srate2 ");
      Serial.println(Srate2);
       Serial.print("trgtRPM ");
      Serial.println(trgtRPM);
      Serial.print("count ");
       Serial.println(rat);
    }
  }
 }


what could stop it counting?? I have implemented it in the same way in my main code

if I use "while(rat <8000)" instead of "if (rat <8000)" in my code it does count but not at the interval of 10 but much faster about 2 seconds and currentMillis stays at 400

like this
currentMillis 400
interval1 10
Srate2 4
trgtRPM 7992
count 7992
currentMillis 400
interval1 10
Srate2 4
trgtRPM 7996
count 7996
currentMillis 400
interval1 10
Srate2 4
trgtRPM 8000
count 8000

if I move unsigned long currentMillis = millis(); like this below I get the same affect as I am getting in my main code like it is not using unsigned long currentMillis = millis(); in void RPMSweep()

Code:
int rat = 0;
long previousMillis1 = 0;
unsigned long interval1 = 10;
int Srate1 =0;
int Srate2 =0;
int trgtRPM;
unsigned long currentMillis = millis();

void setup()
{
  rat=3000;
  Srate1 = 200;
  Srate2 = (Srate1 / 100);
  
}

void loop()
{
  RPMSweep();
} 
 void RPMSweep(){
  
  
  
  if(currentMillis - previousMillis1 >= interval1) {
 
    // Serial.print("previousMillis1 ");
    // Serial.println(previousMillis1);
    previousMillis1 = currentMillis;
    
       
    
    if (rat <8000) {
      rat = rat += (Srate2) ;
    
      trgtRPM = rat;
      
       Serial.print("currentMillis ");
      Serial.println(currentMillis);
      Serial.print("interval1 ");
      Serial.println(interval1);
      Serial.print("Srate2 ");
      Serial.println(Srate2);
       Serial.print("trgtRPM ");
      Serial.println(trgtRPM);
      Serial.print("count ");
       Serial.println(rat);
    }
  }
 }

and displays this in the serial monitor
currentMillis 400
interval1 10
Srate2 2
trgtRPM 3002
count 3002

hope this makes sense
any help would be good

Thanks Johnathan
 
I'm not sure i understand your question. Your first sketch produces the lines you list before the sketch in the post. And i presume that first sketch is doing what you want -- yes?

Your 2nd sketch only print's once (you might add while(!Serial); in setup() in order to see that output
Code:
previousMillis1 0
currentMillis 300
interval1 10
Srate2 2
trgtRPM 3002
count 3002
that 2nd sketch is spinning in loop()/RPMSweep because you never update currentMillis, so
currentMillis - previousMillis1 stays 0 after once through loop().
what is the question?
 
Last edited:
yes the first sketch I added works on its own as it should but when put into my main sketch i.e lots more stuff going on it has the same effect as the second sketch I posted.
so what would make the first sketch behave like the second sketch just by adding the first sketch to a bigger sketch?

all the parts of the first sketch are declared as it is in the first sketch, the second sketch was just to show how it was behaving,

so I guess my question is what would make the first sketch never update currentMillis?

I could post my full code but it would take some working out as you wouldn't have all the bits to test it
 
Without a posted sketch to demonstrate the trouble - that could be run by anyone interested - it requires guessing what is done in the missing code.

Given the first posted sketch works as desired - then if you built/enabled added code around that it should be done in a way that doesn't break it.
 
Guessing:
You disabled interrupts and didn't enable them again.
You overwrote the bounds of an array.
You used a pointer that is pointing somewhere other than where you think it points.
A function returns a pointer to an autoclass data structure/variable.
You have some interrupt functions that use too much time and your program is interrupt bound and the main loop never runs.

Your example has some variable types defined as unsigned and one as signed, but I guess that will only fail after about 25 days. But mismatched variable types will sometimes cause a program to misbehave.
 
Status
Not open for further replies.
Back
Top