-
Notifications
You must be signed in to change notification settings - Fork 0
/
shift_monitoring.ino
234 lines (181 loc) · 3.96 KB
/
shift_monitoring.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*************************
* Gear shift indicator + *
* quick shifter *
* By: Omar Gamal *
* *
* 17/4/2016 *
**************************/
//let power-button-resistor-ground and take the output at
//button and resistor mutual pin
//defining constants for shift up/down buttons
#define upButton 11
#define downButton 12
#define relay 10
//defining the 7-segment pins
#define a 7
#define b 8
#define c 2
#define d 3
#define e 4
#define f 5
#define g 6
//6 speed gearbox
#define maxShifts 6
#define minShifts 0
//defining boolean variables to save last button press
boolean lastUpButton = LOW;
boolean lastDownButton = LOW;
//the number of gear we're in
int i=0;
//controls the light of the 7-segment
void light(int number)
{
switch (number)
{
case 0:
digitalWrite(a, HIGH);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g, LOW);
break;
case 1:
digitalWrite(a, LOW);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, LOW);
break;
case 2:
digitalWrite(a, HIGH);
digitalWrite(b, HIGH);
digitalWrite(c, LOW);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, LOW);
digitalWrite(g, HIGH);
break;
case 3:
digitalWrite(a, HIGH);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, HIGH);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, HIGH);
break;
case 4:
digitalWrite(a, LOW);
digitalWrite(b, HIGH);
digitalWrite(c, HIGH);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
break;
case 5:
digitalWrite(a, HIGH);
digitalWrite(b, LOW);
digitalWrite(c, HIGH);
digitalWrite(d, HIGH);
digitalWrite(e, LOW);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
break;
case 6:
digitalWrite(a, HIGH);
digitalWrite(b, LOW);
digitalWrite(c, HIGH);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);
break;
}
}
//a function that shifts up
void upShift()
{
if(digitalRead(upButton) == lastUpButton)
{
if(i<maxShifts)
{
digitalWrite(relay, HIGH);
Serial.println(++i);
light(i);
delay(200);
}
}
while(digitalRead(upButton) == HIGH)
{
//it stays here until you release the button
}
//to prevent shifting more than once
delay(5);
digitalWrite(relay, LOW);
}
//a function that shifts down
void downShift()
{
if(digitalRead(downButton) == lastDownButton)
{
if(i==1)
{
digitalWrite(relay, HIGH);
Serial.println('N');
light(0);
delay(200);
i--;
}
if(i>(minShifts+1))
{
digitalWrite(relay, HIGH);
Serial.println(--i);
light(i);
delay(200);
}
}
while(digitalRead(downButton) == HIGH)
{
//it stays here until you release the button
}
//to prevent shifting more than once
delay(5);
digitalWrite(relay, LOW);
}
void setup()
{
pinMode(upButton, INPUT);
pinMode(downButton, INPUT);
pinMode(relay, OUTPUT);
Serial.begin(9600);
Serial.println('N');
light(0);
pinMode(a,OUTPUT);
pinMode(b,OUTPUT);
pinMode(c,OUTPUT);
pinMode(d,OUTPUT);
pinMode(e,OUTPUT);
pinMode(f,OUTPUT);
pinMode(g,OUTPUT);
}
void loop()
{
lastUpButton = digitalRead(upButton);
lastDownButton = digitalRead(downButton);
//if upshift button is pressed
if(lastUpButton == HIGH)
{
upShift();
}
//if downshift button is pressed
else if(lastDownButton == HIGH)
{
downShift();
}
}