void setup DDRB PB3 PB4 PORTB GTCCR TSM PLLCSR PCKE PLLE TCCR1 CTC1 CS

  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
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
void setup()
{
DDRB = (1 << PB3) | (1 << PB4);
/*
PORTB = 0;
// GTCCR = TSM;
PLLCSR &= (~(1 << PCKE)) & (~(1 << PLLE));
TCCR1 = (1 << CTC1) | (1 << CS12) | (1 << CS13);
OCR1A = 200;
TIMSK = (1 << OCIE1A);
GTCCR = (1 << PSR1);
TCNT1 = 0;
}
volatile int offCounter1 = 100;
volatile int offCounter2 = 100;
volatile int periodCounter = 0;
ISR(TIM1_COMPA_vect)
{
static int state = 0;
if (state == 0)
{
PORTB |= (1 << PB3) | (1 << PB4);
periodCounter = 0;
OCR1A = 94;
TCNT1 = 0;
state = 1;
}
else if (state == 1)
{
if (offCounter1 < offCounter2)
{
OCR1A = offCounter1;
} else
{
OCR1A = offCounter2;
}
TCNT1 = 0;
state = 2;
} else if (state == 2)
{
if (offCounter1 < offCounter2)
{
PORTB &= ~(1 << PB3);
state = 3;
OCR1A = offCounter1 - offCounter2;
}
else
{
PORTB &= ~(1 << PB4);
OCR1A = offCounter1 - offCounter2;
state = 4;
}
}
else if (state == 3)
{
PORTB &= ~(1 << PB4);
OCR1A =
}
else if (state == 4)
{
PORTB &= ~(1 << PB3);
}
else
{
if (periodCounter >= offCounter2)
{
PORTB &= ~(1 << PB4);
}
if (periodCounter >= offCounter1)
{
PORTB &= ~(1 << PB3);
}
}
}
inline int toCounter(int angle)
{
return map(angle, 0, 180, 94, 282);
}
enum State
{
STATE_RIGHT,
STATE_DOWN,
STATE_LEFT,
STATE_UP
};
volatile int horizontalAngle = 0;
volatile int verticalAngle = 180;
State state = STATE_RIGHT;
int delta = 0;
void loop()
{
offCounter1 = toCounter(horizontalAngle);
offCounter2 = toCounter(verticalAngle);
if (state == STATE_RIGHT)
{
++horizontalAngle;
if (horizontalAngle >= (180 - delta))
{
state = STATE_DOWN;
}
}
else if (state == STATE_LEFT)
{
--horizontalAngle;
if (horizontalAngle <= delta)
{
state = STATE_UP;
}
++delta;
}
else if (state == STATE_UP)
{
++verticalAngle;
if (verticalAngle >= (180 - delta))
{
state = STATE_RIGHT;
}
}
else if (state == STATE_DOWN)
{
--verticalAngle;
if (verticalAngle <= delta)
{
state = STATE_LEFT;
}
}
if (delta >= 90)
{
delta = 0;
}
}