Skip to content

Commit f034dfc

Browse files
committed
Dog with blue eyes
1 parent 054ebf0 commit f034dfc

File tree

2 files changed

+166
-0
lines changed

2 files changed

+166
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
Arduino Uno - Timer1 - 2x Blink
3+
v. 1.0
4+
Copyright (C) 2021 Robert Ulbricht
5+
https://www.arduinoslovakia.eu
6+
7+
IDE: 1.8.15
8+
https://www.arduinoslovakia.eu/2021/8/pes-s-modrymi-ocami?lang=en
9+
10+
This program is free software: you can redistribute it and/or modify
11+
it under the terms of the GNU General Public License as published by
12+
the Free Software Foundation, either version 3 of the License, or
13+
(at your option) any later version.
14+
15+
This program is distributed in the hope that it will be useful,
16+
but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
GNU General Public License for more details.
19+
20+
You should have received a copy of the GNU General Public License
21+
along with this program. If not, see <http://www.gnu.org/licenses/>.
22+
*/
23+
24+
void setupTimer2() {
25+
noInterrupts();
26+
// Clear registers
27+
TCCR2A = 0;
28+
TCCR2B = 0;
29+
TCNT2 = 0;
30+
31+
// 200.32051282051282 Hz (16000000/((77+1)*1024))
32+
OCR2A = 77;
33+
// CTC
34+
TCCR2A |= (1 << WGM21);
35+
// Prescaler 1024
36+
TCCR2B |= (1 << CS22) | (1 << CS21) | (1 << CS20);
37+
// Output Compare Match A Interrupt Enable
38+
TIMSK2 |= (1 << OCIE2A);
39+
interrupts();
40+
}
41+
42+
void setup() {
43+
pinMode(9, OUTPUT);
44+
pinMode(10, OUTPUT);
45+
setupTimer2();
46+
}
47+
48+
class pwmLed {
49+
int led;
50+
int pwm;
51+
int direction;
52+
53+
public:
54+
pwmLed(int _led, int _pwm = 0, int _direction = 1)
55+
: led(_led), pwm(_pwm), direction(_direction)
56+
{}
57+
58+
void Next() {
59+
pwm += direction;
60+
analogWrite(led, pwm);
61+
if (pwm == 255)
62+
direction = -1;
63+
if (pwm == 0)
64+
direction = 1;
65+
}
66+
};
67+
68+
// synchronous eyes
69+
pwmLed pwm1(9, 0, 1);
70+
pwmLed pwm2(10, 0, 1);
71+
72+
ISR(TIMER2_COMPA_vect) {
73+
static int divider = 0;
74+
if (divider == 0) {
75+
pwm1.Next();
76+
pwm2.Next();
77+
}
78+
divider++;
79+
divider %= 1;
80+
}
81+
82+
void loop() {
83+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
Arduino Uno - Timer1 - 2x Blink
3+
v. 1.0
4+
Copyright (C) 2021 Robert Ulbricht
5+
https://www.arduinoslovakia.eu
6+
7+
IDE: 1.8.15
8+
https://www.arduinoslovakia.eu/2021/8/pes-s-modrymi-ocami?lang=en
9+
10+
This program is free software: you can redistribute it and/or modify
11+
it under the terms of the GNU General Public License as published by
12+
the Free Software Foundation, either version 3 of the License, or
13+
(at your option) any later version.
14+
15+
This program is distributed in the hope that it will be useful,
16+
but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
GNU General Public License for more details.
19+
20+
You should have received a copy of the GNU General Public License
21+
along with this program. If not, see <http://www.gnu.org/licenses/>.
22+
*/
23+
24+
void setupTimer2() {
25+
noInterrupts();
26+
// Clear registers
27+
TCCR2A = 0;
28+
TCCR2B = 0;
29+
TCNT2 = 0;
30+
31+
// 200.32051282051282 Hz (16000000/((77+1)*1024))
32+
OCR2A = 77;
33+
// CTC
34+
TCCR2A |= (1 << WGM21);
35+
// Prescaler 1024
36+
TCCR2B |= (1 << CS22) | (1 << CS21) | (1 << CS20);
37+
// Output Compare Match A Interrupt Enable
38+
TIMSK2 |= (1 << OCIE2A);
39+
interrupts();
40+
}
41+
42+
void setup() {
43+
pinMode(9, OUTPUT);
44+
pinMode(10, OUTPUT);
45+
setupTimer2();
46+
}
47+
48+
class pwmLed {
49+
int led;
50+
int pwm;
51+
int direction;
52+
53+
public:
54+
pwmLed(int _led, int _pwm = 0, int _direction = 1)
55+
: led(_led), pwm(_pwm), direction(_direction)
56+
{}
57+
58+
void Next() {
59+
pwm += direction;
60+
analogWrite(led, pwm);
61+
if (pwm == 255)
62+
direction = -1;
63+
if (pwm == 0)
64+
direction = 1;
65+
}
66+
};
67+
68+
// eyes with phase shift
69+
pwmLed pwm1(9, 0, 1);
70+
pwmLed pwm2(10, 50, 1);
71+
72+
ISR(TIMER2_COMPA_vect) {
73+
static int divider = 0;
74+
if (divider == 0) {
75+
pwm1.Next();
76+
pwm2.Next();
77+
}
78+
divider++;
79+
divider %= 1;
80+
}
81+
82+
void loop() {
83+
}

0 commit comments

Comments
 (0)