-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsketch_sep22a.ino
More file actions
49 lines (42 loc) · 922 Bytes
/
sketch_sep22a.ino
File metadata and controls
49 lines (42 loc) · 922 Bytes
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
int pwm_pin = 7;
int period_us = 100;
int duty_percent = 0;
void set_period(int period) {
period_us = period;
}
void set_duty(int duty) {
if (duty < 0) duty = 0;
if (duty > 100) duty = 100;
duty_percent = duty;
}
void pwm_output() {
int high_time = (period_us * duty_percent) / 100;
int low_time = period_us - high_time;
if (high_time > 0) {
digitalWrite(pwm_pin, HIGH);
delayMicroseconds(high_time);
}
if (low_time > 0) {
digitalWrite(pwm_pin, LOW);
delayMicroseconds(low_time);
}
}
void setup() {
pinMode(pwm_pin, OUTPUT);
}
void loop() {
for (int duty = 0; duty <= 100; duty++) {
set_duty(duty);
unsigned long start = millis();
while (millis() - start < 5) {
pwm_output();
}
}
for (int duty = 100; duty >= 0; duty--) {
set_duty(duty);
unsigned long start = millis();
while (millis() - start < 5) {
pwm_output();
}
}
}