Skip to main content
17 votes
Accepted

_delay_ms() is much slower than expected (by a factor of 6) on TinyAVR 0/1 (ATTiny1604)

Your toolchain is not broken. In the ATTiny804/1604 datasheet, page 77, it shows the CPU clock is divided from the 20/16 MHz oscillator by a prescaler (the ATTiny1614 datasheet shows the same clock ...
比尔盖子's user avatar
12 votes
Accepted

delay(time); vs if(millis()-previous>time); and drift

There's one important thing that you need to remember when working with time on an Arudino of any form: Every operation takes time. Your foo() function will take an amount of time. What that time is,...
Majenko's user avatar
  • 105k
8 votes
Accepted

Use timer0 without affecting millis() and micros()

So I was wondering if I could attach an ISR to timer0 without affecting the above Arduino functions, Yes. A few ways, depending on your level of comfort: You can declare the stock Arduino Timer0 OVF ...
dannyf's user avatar
  • 2,770
5 votes
Accepted

How do I run 4 LEDs sequentially based off of a push button input?

To be honest, I don't understand exactly the problems, but I try to give some guideline and hope you can use the idea to fix your problems. What you need is a so called 'state machine'. I think (...
Michel Keijzers's user avatar
4 votes
Accepted

How to randomize the delay in Arduino Stepper code?

In setup(), add randomSeed(analogRead(0)); This reads the value of an analog input pin, which if not connected, will float to relatively random values between 0 and 1023. This "seeds" the random ...
jose can u c's user avatar
  • 6,974
4 votes

Issue using micros() instead of millis()

First off, when dealing with a time system that wraps around, don't compare two times. Compare the differences. Second, cache your function call results if you are going to use it more than once. ...
David G.'s user avatar
  • 161
4 votes
Accepted

How can I SPI "Latch" (LE/CS/SS) for a specific number of SCLK cycles?

I've achieve this by "bit-banging" via a modified shiftOut() of ArduinoCore-avr/cores/arduino/wiring_shift.c, essentially allowing for two data lines on the same clocking: void keyedShiftOut(...
George Kerwood's user avatar
3 votes

Ensuring specific frequency at specific time?

As mentioned in comments and other answers, using timer 1 for hardware-timed bit toggling is likely to produce better results than code using delays based on calls like delayMicroseconds(), primarily ...
James Waldby - jwpat7's user avatar
3 votes

Ensuring specific frequency at specific time?

Generating a linear frequency ramp with accurate timings is not an easy task. I am providing here only a partial answer, where I first go through the math of the problem, then give some ideas for the ...
Edgar Bonet's user avatar
  • 43.5k
3 votes
Accepted

Syncing/taking external clock for SPI on Arduino Due

For your external source to be an SPI bus master, it has to be the only one generating the clock signal. Yet, calling SPI.transfer() also generates a clock signal from the Arduino; both devices are ...
SoreDakeNoKoto's user avatar
3 votes
Accepted

Is the shiftOut code depending on the (low) speed of an Arduino?

Some time ago, I timed digitalWrite() and direct port write commands by looping over 10,000 executions of each and timing them with millis(). I made both tests on a 16MHz Atmega 328p at 16MHz. For ...
JRobert's user avatar
  • 15.3k
3 votes

How fast does the Arduino execute code?

The Arduino Nano Every does not have analog outputs. The function analogWrite() uses PWM as an ersatz of analog output. As soon as the function returns, you know that the timer registers controlling ...
Edgar Bonet's user avatar
  • 43.5k
3 votes
Accepted

Arduino Timer Drift Issue

Any fee-running clock will drift. Worse yet: its drift rate will not be constant. This is a fact of life, and there is nothing you can do about it. There is no software solution, although your ...
Edgar Bonet's user avatar
  • 43.5k
3 votes

I need a 10KHZ PWM signal from pin 3 and 11 using timer 2

You can't do it with Timer 2 without external hardware. This code will generate a 10 kHz signal on pin 3, however it uses both of the timer's registers to do that: OCR2A is used to set the period (ie....
Nick Gammon's user avatar
  • 38.4k
2 votes

Force compiler to execute instructions sequentially?

You can use the optimize function attribute to change the compiler optimization level for an individual function. That's the closest you'll get. optimize The optimize attribute is used to ...
Majenko's user avatar
  • 105k
2 votes
Accepted

Arduino not recognizing when a timer reaches 0

As per comment and Majenko's answer you are current creating local variables called secLeft and secLeft2 inside the 'if block. These take precedence over the ones you have defined globally. Removing ...
Peter M's user avatar
  • 148
2 votes

Use timer0 without affecting millis() and micros()

I think the first solution suggested by dannyf's answer is wrong as ISR in AVR GCC can't be defined as weak function (why). I think the best solution is to add handler (AKA hook) functions using ...
Yahya Tawil's user avatar
2 votes
Accepted

Can I get some help with a metronome code on my arduino uno?

As I wrote in my comment, you don't actually set the value of interval to the new value, when the encoder position changes. So the blinking interval does not change. You need to use the formula to set ...
chrisl's user avatar
  • 16.4k
2 votes
Accepted

When GPS can't see satellites, how can I continue to tick off a smooth-ish time?

What you want, namely something to “smooth the received (GPS) time”, is called a GPS disciplined oscillator. Obviously, you are not after the high accuracy of commercial GPSDOs, but the working ...
Edgar Bonet's user avatar
  • 43.5k
2 votes

Switched from 3ft to 10ft connection wires. My setup doesnt run correctly now. Timing issues?

The pins of logic chips and processors are not line drivers and will not operate properly when used as such. Sometimes you may get lucky but that generally would be intermittent in operation. They ...
Gil's user avatar
  • 1,843
2 votes

Switched from 3ft to 10ft connection wires. My setup doesnt run correctly now. Timing issues?

A better solution would be to get a Pololu Wixel and companion shield for your Mega, and then put the Mega near the object. You can then program the Mega wirelessly and completely avoid problems with ...
starship15's user avatar
2 votes
Accepted

delayMIcroseconds and micros execution time not explainable

The Arduino core uses hardware Timer0 with its ISR (Interrupt Service Routine) for timekeeping. While delayMicroseconds() directly uses the value of the hardware timer, delay() and millis() are ...
chrisl's user avatar
  • 16.4k
2 votes

I need a 10KHZ PWM signal from pin 3 and 11 using timer 2

As explained in Nick Gammon's answer, you cannot really generate two PWM signals at 10 kHz from Timer 2: the register OCR2A will be used to set the frequency, and you won't have independent control of ...
Edgar Bonet's user avatar
  • 43.5k
1 vote
Accepted

Does DmxSerial stops interrupts?

Yes, it does disable interrupts when sending a byte: #if defined(__AVR__) void dmxSendByte(volatile uint8_t value) { uint8_t bitCount, delCount; __asm__ volatile ( "cli\n" ...
Majenko's user avatar
  • 105k
1 vote

Arduino timer to turn a serve at a certain time

in setup you are calling pinMode(b, INPUT); pinMode(up, INPUT); pinMode(down, INPUT); but b, up and down all equal 0 so this is what you are doing is : pinMode(0, INPUT) pinMode(0, INPUT) pinMode(0,...
esoterik's user avatar
  • 628
1 vote

Strange discrete errors when reading HC-SR04 sonar sensor

I think it's from the HC-SR04... it uses echos and it seems your bad readings are some kind of echo reflections. What you should do is either remove the bad readings, or take the average/most common ...
Michel Keijzers's user avatar
1 vote

delay(time); vs if(millis()-previous>time); and drift

Because you reset the timer after the operation. static unsigned long PrevTime=millis(); unsigned long t = millis(); if (t - PrevTime > time) { foo(); PrevTime = t; }
Juraj's user avatar
  • 18.1k
1 vote

Use timer0 without affecting millis() and micros()

So I was wondering if I could attach an ISR to timer0 without affecting the above Arduino functions ... No, because those functions use an already-attached interrupt. it doesn't need to be very ...
Nick Gammon's user avatar
  • 38.4k
1 vote

Use timer0 without affecting millis() and micros()

An interrupt can only have one interrupt service routine. The only way you can do something else using an interrupt that is already handled elsewhere would be to modify that existing interrupt handler ...
Majenko's user avatar
  • 105k
1 vote

Arduino not recognizing when a timer reaches 0

int secLeft=(secLeft2--); int secLeft2=(secLeft); You are making 2 new variables there instead of using the existing ones.
Majenko's user avatar
  • 105k

Only top scored, non community-wiki answers of a minimum length are eligible