-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest.ino
180 lines (133 loc) · 3.2 KB
/
test.ino
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Potentiometer
// pin A0
const int val = 0;
// Motor
int motor[] = {5,6}; // 5,6 - pwm
const int lowerVal = 0;
int higherVal;
const float maximumSpeed = 255;
const int minimumSpeed = 70;
int delaySpeed;
// LED
const int led1 = 7; // red
const int led2 = 8; // green
// Interrupt
// for ISR, has to be volatile when is used in interrupt
// volatile int interruptControl;
volatile byte state = LOW;
const int ledInterrupt = 13;
// Declare Function
void toggle();
void reset();
void interrupt();
void motorRun(int A, int B, int T);
void setup() {
// create interrupt at Digital pin 2
attachInterrupt(0, toggle, CHANGE);
attachInterrupt(0, toggle, CHANGE);
pinMode(ledInterrupt, OUTPUT);
// Declare motor output
int counter;
for (counter=0; counter<2; counter++) {
pinMode(motor[counter], OUTPUT);
}
// Declare led as output
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
// for serial print to show
Serial.begin(9600);
}
void loop() {
if (state) {
interrupt();
} else {
int analogVal1 = analogRead(val);
// print
Serial.print("value: ");
Serial.println(analogVal1);
Serial.println("");
// if 0 <= analogVal < 300
// Turn Left / Anti-Clockwise
// red LED
if (analogVal1 >= 0 && analogVal1 <= 300) {
digitalWrite(led1, HIGH);
digitalWrite(led2, LOW);
// Motor Go Anti-Clockwise
// use PWM control the output
// lowerVal = 0
// higherVal (0<=X<=200)
// 0 <= analogVal1 < 300
float higherValA = maximumSpeed - (analogVal1 / 300.0 * maximumSpeed);
higherVal = (int) higherValA;
// A -> 0
// B -> 1
motorRun(0,1,higherVal);
}
// if 723 <= analogVal1 < 1024
// Turn Right / Clockwise
// green LED
else if (analogVal1 >= 724 && analogVal1 <= 1023) {
digitalWrite(led1, LOW);
digitalWrite(led2, HIGH);
// Motor Go Clockwise
// use PWM control the output
// lowerVal = 0
// higherVal (0<=X<=255)
// 723 <= analogVal1 < 1024
int convertedVal = analogVal1 - 724;
float higherValB = (convertedVal / 300.0 * maximumSpeed);
higherVal = (int) higherValB;
motorRun(1,0,higherVal);
}
// if 300 <= analogVal1 <= 722
// Pause
else {
reset();
}
Serial.print("higherVal: ");
Serial.println(higherVal);
}
delay(80);
}
void toggle() {
state = !state;
}
void reset() {
// reset
higherVal = 0;
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
analogWrite(motor[0], lowerVal);
analogWrite(motor[1], lowerVal);
}
void interrupt() {
reset();
digitalWrite(ledInterrupt, HIGH);
delay(150);
digitalWrite(ledInterrupt, LOW);
delay(150);
}
// Motor Run
// A -> 0
// B -> changing
void motorRun(int A, int B, int T)
{
T = (maximumSpeed - higherVal);
int maxS = maximumSpeed - T;
if (maxS <= 60)
{
maxS = 60;
}
float _T = T / 2;
T = (int) _T;
analogWrite(motor[A], lowerVal);
analogWrite(motor[B], maxS);
delay(80);
analogWrite(motor[B], minimumSpeed);
delay(T);
analogWrite(motor[B], maxS);
delay(80);
analogWrite(motor[B], minimumSpeed);
delay(T);
analogWrite(motor[B], maxS);
}