Here's an easy bit toggle method to try

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

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.

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 :slight_smile:

1 Like

tough to impress crowd here :slight_smile:

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);
}

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

In a flip flop yes but this is flip without flop :joy:

1 Like

Sure, atmega328 has such function too.
Fastest way to toogle Arduino UNO pin state.

For example arduino pin12 (PORT B, pin4)

PINB = (1<<4);  

OR

asm ("sbi %0, %1 \n": : "I" (_SFR_IO_ADDR(PINB)), "I" (PINB5)); // Toggle LED

1 Like

Error-prone and unnecessary: the compiler is smart enough to generate sbi instructions when you write equivalent C++ code:

2 Likes

the | operator is superfluous here, since the PIN register is read-only and zero bits do not affect it

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.

Is it "input (PINx)" or "output (PORTx)" Pin-register?

Interesting I had never tried to see if that would be faster this way even if not necessary i

it is PIN, really ^)
see the datasheet

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