Pages: [1]   Go Down
Author Topic: Fully Rectified Sine Wave With PWM  (Read 814 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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



* rect.bmp (212.69 KB, 585x124 - viewed 8 times.)
Logged

Quebec
Offline Offline
Jr. Member
**
Karma: 3
Posts: 78
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Quebec
Offline Offline
Jr. Member
**
Karma: 3
Posts: 78
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

With the help of my old ti85:


Code:
  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
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 58
Posts: 4002
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Examples can be found in your IDE.

Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: