-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimerOne_test
More file actions
33 lines (30 loc) · 1.56 KB
/
Copy pathTimerOne_test
File metadata and controls
33 lines (30 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <TimerOne.h>
float frequency=200; //sets the frequency
float dutyCycle=50.0; //sets the duty cycle
float us=(1/frequency)*1000000; //creates variable for microseconds per pulse
int sequence[]={100,50,50,50,40}; //sequence for length of pulses followed by length of breaks
void setup()
{
Serial.begin(9600);
pinMode(9,OUTPUT);
Timer1.initialize(us); //initializes the frequency of the wave
int n=(sizeof(sequence)/sizeof(sequence[0])); //creates variable for the size of the input sequence
Timer1.pwm(9,0);//initializes the timer with a duty cycle of 0 (off)
for(int i=0;i<n;i=i+1){
Serial.println(i);
if(i%2==0){//sets so if sequence is on an even index there will pulses, and if on an odd index there will be a break
Timer1.setPwmDuty(9,floor((dutyCycle/100)*1023));//Sets the duty cycle of the timer to be the duty cycle the user input
delay(((int)floor(sequence[i]*us))/1000); //delays for the number of pulses in sequence multiplied by the microseconds per pulse, then divided into ms and us
delayMicroseconds(((int)floor(sequence[i]*us))%1000); //us portion of the delay, delayMicrosecond is only accurate to 16383 us, so easier to have an accurate ms delay and add on the us delay
}
else{
Timer1.setPwmDuty(9,0);//sets a break
delay(((int)floor(sequence[i]*us))/1000);//same timing as before
delayMicroseconds(((int)floor(sequence[i]*us))%1000);
}
}
Timer1.stop();//stops the timer once the sequence has run once, may add a parameter for repeating the sequence a certain number of times later
}
void loop()
{
}