Go Down

Topic: Fully Rectified Sine Wave With PWM (Read 1 time) previous topic - next topic

QAMember

I need to create a sine wave function through the PWM pins where the sine wave is pointy at the crests and 2 times wide at the bottom. I'm guessing a function which emulates the full rectification of a sine wave would be the way to go. Here is some of the current code I am using:

void loop() {
  float a = 128.0;
  float b = 128.0;
  float c = 28000.0;
  float d = 2.0;
  float e = 18.3;    //offset
{
  int value = a + b * sin((millis()/c) * d * PI );
  analogWrite(2,value);
}


semicolo

If you want to rectify a sinus you have to use abs(sin...)
in your case -abs since you want to invert it.

semicolo

With the help of my old ti85:


Code: [Select]

  float a = 255.0;
  float b = 255.0;
  float c = 28000.0;
  float d = 2.0;

void loop() {
{
  int value = a - b * abs(sin(d*PI*millis()/c));
  analogWrite(2,value);
}


You'll get a full intervall each 14s

GoForSmoke


I need to create a sine wave function through the PWM pins where the sine wave is pointy at the crests and 2 times wide at the bottom. I'm guessing a function which emulates the full rectification of a sine wave would be the way to go. Here is some of the current code I am using:

void loop() {
  float a = 128.0;
  float b = 128.0;
  float c = 28000.0;
  float d = 2.0;
  float e = 18.3;    //offset
{
  int value = a + b * sin((millis()/c) * d * PI );
  analogWrite(2,value);
}




You could put a tiny capacitor across the pin and ground. That would slant leading and trailing edges.

PWM is about changing the duty cycle.
Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

QAMember

Semicolo, thank you, very much, for the code.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy