Flip-flop 2 pins to drive h-bridge using tone code?

Trying to control h-bridge module and was wondering if this was the best way to flip-flop 2 pins at some frequency?

// Pins connected to h-bridge module
int pin1 = 5;
int pin2 = 6;

void setup() 
{
    pinMode(pin1,OUTPUT);    
    pinMode(pin2,OUTPUT);    
}

void loop() 
{
    tone(pin1,1000);
    digitalWrite(pin2, !digitalRead(pin1));
}

Is there no way to make 2 pins opposite of each other without having to read the other pin on each loop??

having something like this for example would be cool if I could somehow offset the two pins:

void loop() 
{
    tone(pin1,1000);
    tone(pin2,1000);  // But pin2 offset by 180 degrees??
}

You're essentially using a sandbox toy to perform an earthworks excavation, tone(). It was not designed for, and will not perform such a demanding task.

To gain control of PWM on these MCU's you have to write low level code to control the hardware timers.

You could use a hardware timer to generate the frequency, and there are complementary output pins to provide the alternate phase you need.

No software required once the timer is configured,

Sorry I'm a newbee, are you saying Arduino/esp8266 can't control a simple h-bridge module like BTS7960? Or that "tone" code can not?

Is this better?

void loop() 
{
    digitalWrite(pin1, high);
    digitalWrite(pin2, low);
    delayMicroseconds(1000);

    digitalWrite(pin1, low);
    digitalWrite(pin2, high);
    delayMicroseconds(1000);
}

It's better, if and only if, the resulting timing is acceptable for your (unstated) application.

Do you need dead time? What frequency, frequency accuracy, and pulse resolution do you require?

There is :slight_smile:
See the BlinkWithoutDelay() and add the second pin as inverse of the first.

Sure. The ToneAC library does that.

Actually, I think that's a really clever idea. And it would probably work OK, if loop (and everything else you add to it, eventually), digitalRead(), and digitalWrite() were infinitely fast.
Unfortunately, those are relatively slow, so this would result in significant time periods where both sides of the H-bridge were on at the same time, which is highly undesirable.

The timers may provide complementary output modes; many newer microcontrollers have at least one time specifically aimed at "motor control" with all sorts of fancy features (complementary outputs, dead times, etc.) But I don't think that any of the Arduino libraries support those features, and I'm not sure whether they're present on the Uno AVR.

Use a hardware inverter to generate the second output?

Im trying to use this to mess around with resonate frequencies of different piezo buzzers and bigger piezo transducers...

This code is pretty much all I need in the sketch... I may add a POT so can physically change the frequency and have it outputted on the serial so thats all for the code probably

I don't need it to be crazy accurate, once am at a close enough frequency I can always double check the exact frequency with an external oscilloscope

PS I'm hoping to go up to 40kHz

If you are only "messing around" why do you need complementary outputs? One will energize a piezo...

This could be done on an ESP32 ... 1 Hz resolution, 2 complimentary, synchronized square wave outputs (doubles the volume), optional dead time insertion. Could add a toneAC function ... already using an invert function as option for servo control to simplify interface hardware.

Wokwi_badge ESP32_3-Phase 40kHz ESP32 3 Phase PWM Outputs (40kHz, 10-bit)

Next revision of ESP32-ESP32S2-AnalogWrite ... ugh -wish I had more time available!