I was reading this page regarding bit manipulation: bit manipulation
Using their approach for bit toggle (bit_flip), here's an easy way to toggle a pin:
#define toggle(b) (digitalWrite(b, !digitalRead(b)))
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
//let's test it:
toggle(LED_BUILTIN); // or whatever pin you want to toggle
delay(200);
}
There's many other bit manipulations on that page to try. Seems to work ok (tried on an Uno)
regards
Russell
J-M-L
October 22, 2022, 11:22am
2
This is actually a poor way from a semantics and macro perspective
that would be a tiny bit better
#define toggle(b) digitalWrite((b), (digitalRead((b)) == HIGH ? LOW : HIGH))
but would fail if you wanted to toggle many pins in a row like this for example
int pin = 3;
while (pin < 9) toggle(pin++); // toggle pin 3 to 8 (won't work actually)
macros should be avoided in favor of inline functions
Note that this is not bit manipulation.
surepic
October 22, 2022, 12:04pm
4
You can call it pin flip or whatever but not bit flip or even bit manipulation. Or byte flip :-)))
ah so much for instilling everyone with awe
1 Like
J-M-L
October 22, 2022, 1:17pm
6
tough to impress crowd here
A native toggle function would be more impressive, some processors have a hardware toggle function associated with GPIOs.
In a flip-flop, is it not the bit that is flipping/flopping in response to a clock pulse?
1.
#define toggle(b) (digitalWrite(b, !digitalRead(b)))
The readable version (for pupil who wants simplification) of the above is:
void setup()
{
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
}
void loop()
{
toggle(LED_BUILTIN);
delay(1000);
}
void toggle(byte b)
{
digitalWrite(b, !digitalRead(b));
}
2.
#define toggle(b) digitalWrite((b), (digitalRead((b)) == HIGH ? LOW : HIGH))
The readable version (for pupil who wants simplification) of the above is:
void setup()
{
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
}
void loop()
{
toggle(LED_BUILTIN);
delay(1000);
}
void toggle(byte b)
{
byte state = (digitalRead(b) == HIGH ? LOW : HIGH); //fits with tertiary operator format
digitalWrite(b, state);
}
Koepel
October 22, 2022, 4:11pm
10
The maker of Wokwi made this blog a while ago: https://blog.wokwi.com/5-ways-to-blink-an-led-with-arduino/ .
Since then, others came up with more ideas. I think there are now 10 ways to blink a led.
If you think that I wrote this post only to mention "Wokwi", then you are right.
1 Like
surepic
October 22, 2022, 5:13pm
11
In a flip flop yes but this is flip without flop
1 Like
b707
October 22, 2022, 7:54pm
12
Sure, atmega328 has such function too.
Fastest way to toogle Arduino UNO pin state.
For example arduino pin12 (PORT B, pin4)
PINB = (1<<4);
PieterP
October 22, 2022, 9:34pm
14
JohnRob:
asm
Error-prone and unnecessary: the compiler is smart enough to generate sbi instructions when you write equivalent C++ code:
2 Likes
b707
October 22, 2022, 9:40pm
15
PieterP:
PINB |= 1<<PB5;
the | operator is superfluous here, since the PIN register is read-only and zero bits do not affect it
PieterP
October 22, 2022, 9:43pm
16
The compiler does differentiate between them, though. Try it out in the link above: |= results in an sbi instruction, = in an immediate load and an out instruction.
b707:
PIN register
Is it "input (PINx)" or "output (PORTx)" Pin-register?
J-M-L
October 23, 2022, 4:55am
18
Interesting I had never tried to see if that would be faster this way even if not necessary i
b707
October 23, 2022, 6:25am
19
it is PIN, really ^)
see the datasheet
J-M-L
October 23, 2022, 6:56am
20
The arduino documentation says PINx is read only but that’s not correct if you go look at the 328P’s spec. It does drive this flipping capability when writing a 1 to the corresponding bit