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
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()
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 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