Using delay(100)
will cause the sketch to wait at that line for 100 milliseconds. That is not (quite) the same as DOING something every 100 milliseconds. The difference is that other things take time too, so the time will be 100 milliseconds plus how ever long everything else takes.
The solution used by @Majenko still has that problem in a small way, because a new millis()
value is used to set the new value of start
. In most situations, it will work fine, but it is possible that the either or both of the millis()
results were more than 100 after the previous value of start
. That would happen if code that replaces the comment took more than 100ms some time. The fix for that is very simple. Just change start = millis()
to start += 100
. That will attempt to speed up the following loop, if it was getting behind.
As a side note, it would be good to change the 100 millisecond references to a defined name. Either a #define
or a const
. Since the millis()
function returns an "unsigned long" value, I would use the same data type for a related const
value. That just means that the compiler will not have to (invisibly) convert between different types when doing the math operations.
#define REPORT_CYCLE 100
const unsigned long REPORT_CYCLE = 100; // milliseconds between measurement and reporting cycles
If you are a beginner, you may not have seen static
variables yet. If you know about them, fine, but if not, they can be replaced by global
scope variables.
The @Majenko code uses a fixed count of 100 iterations to do the measurement and reporting. That works fine as long as neither the for 10 seconds or every 1/10 second changes. If either of those change, the count limit would also need to be recalculated and changed. Instead, I would use another defined name for how long to do measurement and reporting cycles, and let the compiler handle how many times that actually is.
Doing an analogWrite()
sets the value (brightness here), which will stay constant until changed again. Since your program only sets a single value, it only needs to be done once. Move that statement to setup()
.
There is no need to do Serial.end()
when the reporting is done. That disables serial communication, but is unneeded, if no more Serial.println()
commands are executed anyway.
To avoid calling millis()
multiple times in the same 'pass' (in each if test), I like to call it once, and save the result.
Taking the @Majenko code, plus your starting sample, and described changes gets
// Intervals in milliseconds
#define REPORT_CYCLE 100
#define REPORT_INTERVAL 10000
unsigned long nextStart, endTime;
int analogInPin = A0;
int LEDreading; // analog reading
int LEDpin = 9; // LED on pin 9
void setup() {
Serial.begin(9600);
analogWrite(LEDpin,255); // LED brightness
nextStart = millis(); // Time for "right now"
endTime = nextStart + REPORT_INTERVAL;
}
void loop() {
unsigned long now = millis();
if (now < endTime) { // still need to do more reporting
if (now >= nextStart) { // time to get and report another reading
nextStart += REPORT_CYCLE; // When the next reading should be done
LEDreading = analogRead(analogInPin);
Serial.println(LEDreading);
}
}
// do other things, both while still reporting, and after reporting is done
}
delay()
, then you can print withoutdelay()
.