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 ...
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 ...
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++ ...
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 ...
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 ...
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 ...
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
}
...
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(...
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 ...
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 ...
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);
...
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,...
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 ...
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, ...
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 ...
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
...
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 ...
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. ...
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'...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
loop × 124arduino-uno × 32
programming × 18
led × 13
serial × 10
button × 10
arduino-ide × 8
c++ × 8
esp8266 × 7
arduino-mega × 5
interrupt × 5
array × 5
neopixel × 5
motor × 4
esp32 × 4
nodemcu × 4
servo × 4
pwm × 4
code-review × 4
input × 4
relay × 3
sketch × 3
ir × 3
stepper × 3
variables × 3