invert pwm signal

void invertAnalogWrite(int pin, float value) {

  analogWrite(pin, value);
  TCCR1A = TCCR1A & ~B00110000; //switch off output B
  TCCR1A |= B00110000;  //switch on the B output with inverted output
}



void loop() {

  for (t = 0; t < 10; t = t + 0.0001) {

    Uref = sin(2 * PI * 50 * t);

    x = Uref * (1023 / 5);

    analogWrite(11, x);

    invertAnalogWrite(10, x);
    
//***********************************************//
    
    notUref = -1 * sin(2 * PI * 50 * t);

    y = notUref * (1023 / 5);
   
    analogWrite(3, y);

    invertAnalogWrite(9,y);



  }

Hello, i want do invert this pwm signal, i invert the Uref perfecty,but notUref, I can’t … and i don’t know why… Can help me please?

Didn't I see a similar question recently?

How do you think that a PWM signal looks like (generated by analogWrite), and how its inverse should look like? How should an Uref be involved?

Simple external circuitry can provide the true inverse of a PWM signal, using an transistor or integrated inverting gate.

DrDiettrich:
Didn't I see a similar question recently?

How do you think that a PWM signal looks like (generated by analogWrite), and how its inverse should look like? How should an Uref be involved?

Simple external circuitry can provide the true inverse of a PWM signal, using an transistor or integrated inverting gate.

ok, that right but are some kind of code to invert the signal without a eletronic circuit?

The PWM signals are created in hardware, not easily accessible to application code. But you can use two PWM output pins, to output the true and inverse pulses by according analogWrite() calls.

analogWrite(10, 255 - x);

my problem is in notUref, analogWrite(10, 255-y) don’t result …

DrDiettrich:
The PWM signals are created in hardware, not easily accessible to application code. But you can use two PWM output pins, to output the true and inverse pulses by according analogWrite() calls.

I create my pwm signal are created in arduino, i want to use two output pwm pins for Uref and NotUref, and another two for inverse Uref and inverse notUref...

Subtracting from 255 works every time...

255-0 = 255
255-255 = 0

255-10 = 245
255-245 = 10

255-127 = 128
255-128 = 127


I haven't completely analyzed your code, but it could be a couple of things...

You can't write a negative value to the PWM output. (A complete sine wave must be biased, usually centered around 127 or 128.)

analogWrite() doesn't take a float (although it might automatically convert it to a byte.)

You can't do bitwise operations on a float. (You can, but it doesn't make sense.)

DVDdoug:
Subtracting from 255 works every time...

255-0 = 255
255-255 = 0

255-10 = 245
255-245 = 10

255-127 = 128
255-128 = 127


I haven't completely analyzed your code, but it could be a couple of things...

You can't write a negative value to the PWM output. (A complete sine wave must be biased, usually centered around 127 or 128.)

analogWrite() doesn't take a float (although it might automatically convert it to a byte.)

You can't do bitwise operations on a float. (You can, but it doesn't make sense.)

My objetive is do a sin wave and make the corresponding pwm , after this make a invert of pwm . Ca you help me? what you change in my code?

ricperes:
after this make a invert of pwm

This is going round in circles.

What do you mean by "invert of pwm".

Can you make a pencil drawing showing PWM and inverted PWM and post a photo of it ?

...R

That’s it… i want to use a sin wave to generate the first pwm and after do the second…

ex pwm.png

ricperes:
That's it...

What "that" are you referring to?

From your diagram it looks like you want one PWM signal to be the mirror image of the other.

I suspect the simplest way to do that is to use an external device (a logic inverter or a transistor) to create the mirror signal.

I guess if you want to do it entirely within the Arduino you could arrange for the PWM output to trigger an interrupt and use the ISR to output opposing values on two I/O pins.

...R