Skip to main content
12 votes

Simultaneous button reading?

Instead of using the high level digitalRead() function access the low level port registers. See this documentation. A port register is one byte and each bit represents one of the digital inputs on ...
Craig's user avatar
  • 2,120
11 votes
Accepted

Simultaneous button reading?

There is a very easy solution to your problem, and it's called "interrupts". The processes inside loop method are executed synchronously, so always one read will be done before second one, and always ...
Filip Franik's user avatar
  • 1,302
8 votes
Accepted

Arduino Object Oriented Programming

"Arduino" is just plain old C++ with (in some cases) bits removed and some helper functions and classes. For the smaller (lower powered AVR for example) there is no full STL in the C++ ...
Majenko's user avatar
  • 106k
6 votes
Accepted

Why are the Do While loops not stopping?

change >= and <= to > and < respectively. You want it to stop at 0, but your do-while will do another round, since 0>=0 is true. So your code only stops at -1 and 256. When ...
Gerben's user avatar
  • 11.3k
6 votes
Accepted

Why is this extremely simple code not working?

The problem is: for (int x = 0; x > 10; x++) { That can be rewritten, in pseudo code, as: X is zero While X is greater than 10: Increment X You see the problem? X is zero. It's not greater than ...
Majenko's user avatar
  • 106k
6 votes

How do I make another action repeat inside a loop?

By using delay() it blocks the rest of the code. The programme needs to keep track of two buttons and update the lightshow at appropriate times, therefore a non-blocking style of coding is needed ...
tim's user avatar
  • 699
5 votes

Any advice on how to repeat the song 6 times instead of infinitely?

Assuming, the program can stop after playing 6 times, put another for loop in the loop function: void loop() { for (int nTimes = 0; nTimes < 6; nTimes++) { // Existing loop code } ...
Michel Keijzers's user avatar
4 votes
Accepted

What's wrong with infinite loops inside loop()?

First, I will give you some unsolicited advise about your coding style. Instead of writing: PORTB &= B11111110; ... PORTB |= B00000001; you could write: PORTB &= ~_BV(PB0); ... PORTB |= _BV(...
Edgar Bonet's user avatar
  • 44.3k
4 votes
Accepted

If/Else Statements on Arduino not executing

Serial.read() takes the first byte out of the serial buffer. Once it's executed, this byte is gone, so the Serial.read() in the next if statement will fetch the next byte (if available). What you can ...
ex-punctis's user avatar
4 votes
Accepted

problem with decrement in a loop

The problem is that you add 15, and than if it's >= 240, you decrease 15, so it will not change (I wouldn't consider it haywire though). You can better use a direction sign. Below is only the ...
Michel Keijzers's user avatar
4 votes

How do I pause a loop just once?

The following code will pause (Give some delay) when it runs for the first time and also when you press the button. void loop() { static int status = true; if(status){ //Pause delay(15); ...
HARSH MITTAL's user avatar
4 votes

Circular buffer implementation and Serial.print()

You create a variable that can point to a circular buffer (*cb) but at no point do you actually point it at a circular buffer. Either you need to create a new circular buffer or don't create a pointer,...
Majenko's user avatar
  • 106k
4 votes
Accepted

for loop crashes program

Funny, I just wrote an answer about the problem, that you have here, about 45min ago. And the problem is, that you are trying to modify a string literal. That is undefined behavior in C++. When you ...
chrisl's user avatar
  • 16.5k
3 votes

Simultaneous button reading?

I think you want after a random time, a 'Start' LED to be lit, and whoever presses his/her button first wins. There are some problems in your sketch: It seems even when a button is pressed to early, ...
Michel Keijzers's user avatar
3 votes

for() loop doesn't seem to break?

Consider the sequence of values of "fadeIn". It starts at 8 and in incremented by 16. So: 8, 24, ... 248, 264, ... . It is never exactly 255. So you never break out of your "for" loop. There is ...
st2000's user avatar
  • 7,470
3 votes
Accepted

Correct way of having loop end when required?

As pointed out by Ignacio Vazquez-Abrams and jsotola, you need to use a state machine, like the one from the Blink Without Delay Arduino tutorial. Yours would be slightly more complicated because you ...
Edgar Bonet's user avatar
  • 44.3k
3 votes

put_index = (put_index + 1) % 100;

The % is the modulo operator in C/C++, which basically means: Divide the value to the left of the % by the value to the right of it Discard the quotient and keep only the remainder. For instance, 10 ...
R Zach's user avatar
  • 131
3 votes

Arduino Switching Between Loops IR Remote

delay(0) apparently[*] acts like delay(2^32) or about 10^10 milliseconds. That may not be what you had in mind ... :) Try reducing the delay to 1 or use delayMicroseconds() to get something shorter. ...
JRobert's user avatar
  • 15.3k
3 votes

Simultaneous button reading?

I was not sure about posting this as an answer, since it is basically some more considerations on the three existing answers (for reference, they are Michel Keijzers's one - using digitalRead -, Craig'...
frarugi87's user avatar
  • 2,721
3 votes

Running in loop or once according to a slide switch position

Let's assume the switch is on pin 9, that the pinMode() has already been set, and that the switch reads HIGH for loop operation. #define MODE_SWITCH 9 #define DO_LOOP HIGH // This loop will ...
JRobert's user avatar
  • 15.3k
3 votes

Writing elegant applications for Arduino

On a multiprocess system, like your typical desktop, you usually have way more processes than logical CPUs. When programming such systems you therefore have to avoid burning more CPU cycles than ...
Edgar Bonet's user avatar
  • 44.3k
3 votes
Accepted

Writing elegant applications for Arduino

A microcontroller's execution path is a tight loop whether you like it or not. If you take a CPU and connect it to empty memory the CPU will execute each empty cell in turn and loop back to the ...
Majenko's user avatar
  • 106k
3 votes

Arduino Object Oriented Programming

Have you programmed OOP before? For alot of projects you can get away with not knowing it. I'll also point out that if you are just a beginner, I don't recommend starting off with OOP. Stick to ...
Techmo's user avatar
  • 31
3 votes
Accepted

Run computation only on request over serial

On a Nano you can #include <avr/sleep.h>, then simply call sleep_mode() from loop(). This will put the device to sleep, by default in IDLE mode, until the next interrupt fires. The Nano will ...
Edgar Bonet's user avatar
  • 44.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
  • 44.3k
3 votes
Accepted

How do I make another action repeat inside a loop?

You have written your question beautifully. In your case, according to your logic, the button has to be kept pressed, so that the LEDs are lit in the order you desire. The single loop() function is ...
ArduinoFan's user avatar
  • 1,079
3 votes

Is there a need to put a delay in the main loop

There is no absolute need for any Arduino program to use delay() The blink without delay sketch that is included in the standard sketch examples is the canonical illustration of this. In most cases ...
RedGrittyBrick's user avatar
3 votes

'for loop' seems to change the execution of pow()

This seems like a case of a confused compiler. More specifically, a confused optimizer. On the first occurrence of pow(10, places), the compiler noticed that places must be equal to 5, and so replaced ...
Edgar Bonet's user avatar
  • 44.3k
2 votes
Accepted

How to stop sending the last received char of UART echo function?

There are many, many problems with this code. The most fundamental flaw is that it uses linear buffers, where you should be using circular buffers instead. I'll come to this later. Now, if we look at ...
Edgar Bonet's user avatar
  • 44.3k
2 votes
Accepted

Should I use a loop instead of an If statement?

What you are lacking is the else concept: If a touch pad is pressed, then Turn the relay on else Turn the relay off Rather than turn the relay off every iteration, you only turn it off if the ...
Majenko's user avatar
  • 106k

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