Hi ,
Is millis a default setting in Arduino.?
If so how do i change that to micros if needed.
Thanks
Arduino also has micros() by default.
millis() returns the number of milliseconds that have passed since the last power up or reset
micros() returns the number of microseconds that have passed since the last power up or reset
They are separate functions and can be used in the same way as one another. It is just that the timescales are different
What is it that you are trying to do ?
It's not a setting. It's a function that returns the number of milliseconds that has elapsed since the microcontroller last started (so ms. since power-up). It returns an unsigned long, so it will overflow eventually and restart counting from zero.
Micros() works analogously and returns microseconds since startup.
Thanks for your reply.
In a recent sketch , a pot was mapped , but no unit of time was called for.
The pot was mapped in milliseconds without code instructions , just curious as to how it selected millis and how i would change to micros if needed.
Best regards
By pot, do you mean a potentiometer? How does it relate to time, I wonder? There's a lot of crucial information missing from your brief description so it's impossible to understand what's going on here.
Post the full sketch so that we can see what you are talking about
Basically if the code does something like
if (millis() - previousTime >= requiredPeriod)
{
//do something
}
}
change it to
if (micros() - previousTime >= requiredPeriod)
{
//do something
}
}
Hi UKHeliBob , the sketch is for an LED array and would never require micros.
My interest was to understand why millis function was implemented.
Any help appreciated.
int potPin = A0;
int potValue;
// LED patterns here
unsigned char led_pattern[] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01
};
void setup() {
// set pin 2 to 5 as outputs
for (int i = 2; i <= 5; i++) {
pinMode(i, OUTPUT);
}
}
void loop() {
potValue = analogRead(A0);
int delayValue = map(potValue, 0, 1023, 1, 1000);
DisplayPattern(led_pattern, sizeof(led_pattern));
delay(delayValue);
}
void DisplayPattern(unsigned char *pattern, int num_patterns)
{
static int pattern_num = 0; // keeps count of patterns
unsigned char mask = 1; // for testing each bit in pattern
// LEDs on pin 2 to pin 5
for (int i = 2; i <= 5; i++) {
// check if bit in pattern is set or not and switch LED accordingly
if (pattern[pattern_num] & mask) {
digitalWrite(i, HIGH);
}
else {
digitalWrite(i, LOW);
}
mask <<= 1; // adjust mask for checking next bit in pattern
}
pattern_num++; // move to next pattern for next function call
// keep pattern within limits of pattern array
if (pattern_num >= num_patterns) {
pattern_num = 0;
}
}
millis() was implemented to provide an easy way to count or measure a 1 millisecond interval and the same for micros() for a 1 microsecond (not quite true) interval
Do you perhaps mean how millis() is used for timing ?
If so, then see Using millis() for timing. A beginners guide, Several things at the same time and the BlinkWithoutDelay example in the IDE
/*
Blink without Delay
Turns on and off a light emitting diode (LED) connected to a digital pin,
without using the delay() function. This means that other code can run at the
same time without being interrupted by the LED code.
The circuit:
- Use the onboard LED.
- Note: Most Arduinos have an on-board LED you can control. On the UNO, MEGA
and ZERO it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN
is set to the correct LED pin independent of which board is used.
If you want to know what pin the on-board LED is connected to on your
Arduino model, check the Technical Specs of your board at:
https://www.arduino.cc/en/Main/Products
created 2005
by David A. Mellis
modified 8 Feb 2010
by Paul Stoffregen
by Scott Fitzgerald
modified 11 Nov 2013
modified 9 Jan 2017
by Arturo Guadalupi
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
*/
// constants won't change. Used here to set a pin number:
const int ledPin = LED_BUILTIN;// the number of the LED pin
// Variables will change:
int ledState = LOW; // ledState used to set the LED
// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0; // will store last time LED was updated
// constants won't change:
const long interval = 1000; // interval at which to blink (milliseconds)
void setup() {
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
}
void loop() {
// here is where you'd put code that needs to be running all the time.
// check to see if it's time to blink the LED; that is, if the difference
// between the current time and last time you blinked the LED is bigger than
// the interval at which you want to blink the LED.
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
}
The BWD sketch describes the millis function.
The led array sketch provided , has no mention of millis , yet millis is implemented.
If i wanted a fast binary stream and not flashing lights , i might want micros instead of millis.
That is what i sought to understand.
Thanks for your patience and input.
So, is your question fully answered now ?
No.
Part of my original question was how do i change millis to micros if needed.
The current sketch works fine with millis , but the code (unlike BWD) has no mention of millis , hence the first part of my original question.
Ditch the LED's and now i want a fast binary stream.
How do i change the millis to micros?
Best regards
That makes the potentiometer control the delay from 1 to 1000 through its full range.
delayValue is not a time, it is a number, between 1 and 1000.
It is only because you use it in millis(delayValue) that any delay happens.
Change the map parameter last argument 1000 to the maximum delay in milliseconds you desire.
int delayValue = map(potValue, 0, 1023, 1, 500);
would, for example, make the max delay 500 milliseconds.
This will work, but starts to behave oddly and eventually "break".
int delayValue = map(potValue, 0, 1023, 1, 50);
will have only 50 steps as you rotate the potentiometer, and a maximum of 50 milliseconds when used in the delay().
Since it is integer math, if you need fine control over shorter delays, you'll need, for example, to switch back to
int delayValue = map(potValue, 0, 1023, 1, 1000);
but use it later with
delayMicroseconds(delayValue);
instead of miils();
which, I hope obvsly, will give you fine control over a delay between 1 and 1000 microseconds.
I didn't look at your code, this may not be the best way to go about what you are really trying to do…
Oh, IC, you don't have any code. What are you up to?
HTH
a7
Thanks alto777.
Post #8 has the code.
It' a light array for my grandkids.
It doesn't need the high speed , but i wondered why millis was adopted without mention in the sketch as per Blink without Delay.
I assumed it was a default function ( hence my original question).
You added "delayMicroseconds" and hey presto.
I'm all done here.
Thanks and HAND.
Caio
So it does, sry. ![]()
I'm all done here.
Good luck!
a7