Skip to content

Instantly share code, notes, and snippets.

@ggazzo
Created January 30, 2017 20:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ggazzo/8bf56cdbebc30d07a4dcfe51be25fc21 to your computer and use it in GitHub Desktop.
Save ggazzo/8bf56cdbebc30d07a4dcfe51be25fc21 to your computer and use it in GitHub Desktop.
#include "TimerOne.h"
#define PI 3.14159
#define SIZE 50
const float increment = PI / SIZE;
int dregreePhase = 360;
int delayDegree = 90;
float radiansPhase, radiansAux;
float calcRadians(int degree) {
return (float) degree * (PI / 180);
}
unsigned int counter = 0;
void setFrequency(int freq) {
Timer1.initialize(1000000 / (freq * SIZE));
}
void interruption(){
static uint8_t pwm = 0x00;
static bool up = true;
float x = counter * increment;
OCR2A = (128 + 127 *sin(radiansPhase + x));
OCR2B = (128 + 127 *sin(radiansAux + x));
counter = (counter + 1) % (SIZE * 4);
}
void setup() {
Timer1.attachInterrupt(interruption);
radiansPhase = calcRadians(dregreePhase);
radiansAux = calcRadians(dregreePhase - delayDegree);
DDRB = (1 << PB3);
DDRB |= (1 << PB5);
DDRD = (1 << PD3);
setFrequency(1);
TCCR2A = 1 << COM2A1 | 1 << COM2B1 | 1 << WGM21 | 1 << WGM20;
TCCR2B = 1 << CS20;
sei();
}//end setup
void loop () {
static int f = 1;
delay(1000);
setFrequency(f++);
f = f > 59? 1: f;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment