-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhomework3.ino
More file actions
202 lines (177 loc) · 4.84 KB
/
homework3.ino
File metadata and controls
202 lines (177 loc) · 4.84 KB
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
// declare all the joystick pins
const int pinSW = 2; // digital pin connected to switch output
const int pinX = A0; // A0 - analog pin connected to X output
const int pinY = A1; // A1 - analog pin connected to Y output
// declare all the segments pins
const int pinA = 4;
const int pinB = 5;
const int pinC = 6;
const int pinD = 7;
const int pinE = 8;
const int pinF = 9;
const int pinG = 10;
const int pinDP = 11;
const int numPossibleMoves = 4;
const int segSize = 8;// modify if you have common anode
const bool commonAnode = false;
const unsigned int debounceDelay = 50;
const unsigned int blinkDelay = 700;
const int longPressTime = 3000;
byte state = false;
int index = 0;
int currentState = 1;
int xValue = 0;
int yValue = 0;
int currentSegment = 7;
int lastSegment = 7;
bool joyMovedX = false;
bool joyMovedY = false;
int minThreshold = 400;
int maxThreshold = 600;
byte lastButtonValue = LOW;
byte buttonValue = LOW;
byte buttonState = LOW;
byte lastButtonState = LOW;
unsigned long lastDebounceTime = 0;
unsigned long lastBlinkTime = 0;
unsigned long pressedTime = 0;
unsigned long releasedTime = 0;
unsigned long pressDuration = 0;
byte blinkState = LOW;
bool longPress = false;
bool shortPress = false;
int displayMapping[segSize][numPossibleMoves] = {
//up, down, left, right
{0, 6, 5, 1}, //a
{0, 6, 5, 1}, //b
{6, 3, 4, 7}, //c
{6, 3, 4, 2}, //d
{6, 3, 4, 2}, //e
{0, 6, 5, 1}, //f
{0, 3, 6, 1}, //g
{7, 7, 2, 7} //dp
};
int segments[segSize] = {
pinA, pinB, pinC, pinD, pinE, pinF, pinG, pinDP
};
byte segmentStates[segSize] = {
0, 0, 0, 0, 0, 0, 0, 0
};
byte dpState = LOW; //decimal point
void setup() {
Serial.begin(9600);
// initialize all the pins
for (int i = 0; i < segSize; i++) {
pinMode(segments[i], OUTPUT);
}
pinMode(pinSW, INPUT_PULLUP);
if (commonAnode == true) {
state = !state;
}
}
void loop() {
longPress = false;
shortPress = false;
//read the button with debounce
buttonState = digitalRead(pinSW);
if(buttonState != lastButtonState){
lastDebounceTime = millis();
lastButtonState = buttonState;
}
if(millis() - lastDebounceTime> debounceDelay){
buttonValue = !buttonState;
}
//check button value and detect long press
if(buttonValue == 1 && lastButtonValue == 0){
pressedTime = millis();
}
else if(buttonValue == 0 && lastButtonValue == 1){
releasedTime = millis();
pressDuration = releasedTime - pressedTime;
if(pressDuration >= longPressTime)
{
//long press detected
longPress = true;
}
else{
//short press detected
shortPress = true;
//currentState = 2;
}
}
//state 1
if(currentState == 1)
{
//check joystick movement
xValue = analogRead(pinX);
yValue = analogRead(pinY);
if(xValue >= minThreshold && xValue <= maxThreshold){
joyMovedX = false;
}
else if(xValue > maxThreshold && joyMovedX == false){
lastSegment = currentSegment;
currentSegment = displayMapping[currentSegment][3];
joyMovedX = true;
}
else if(xValue < minThreshold && joyMovedX == false){
lastSegment = currentSegment;
currentSegment = displayMapping[currentSegment][2];
joyMovedX = true;
}
if(yValue >= minThreshold && yValue <= maxThreshold && joyMovedX == false){
joyMovedY = false;
}
else if(yValue > maxThreshold && joyMovedY == false && joyMovedX == false){
lastSegment = currentSegment;
currentSegment = displayMapping[currentSegment][1];
joyMovedY = true;
}
else if(yValue < minThreshold && joyMovedY == false && joyMovedX == false){
lastSegment = currentSegment;
currentSegment = displayMapping[currentSegment][0];
joyMovedY = true;
}
if(longPress){
currentSegment = 7;
for (int i = 0; i < segSize; i++){
segmentStates[i] = 0;
}
}
if(shortPress){
currentState = 2;
}
//display current configuration
for (int i = 0; i < segSize; i++){
digitalWrite(segments[i], segmentStates[i]);
}
blink(currentSegment);
}
//state2
else if(currentState == 2){
//check joystick movement on X axis
xValue = analogRead(pinX);
if(xValue >= minThreshold && xValue <= maxThreshold){
joyMovedX = false;
}
else if((xValue > maxThreshold || xValue < minThreshold) && joyMovedX == false){
joyMovedX = true;
segmentStates[currentSegment] = !segmentStates[currentSegment];
}
//display current configuration
for (int i = 0; i < segSize; i++){
digitalWrite(segments[i], segmentStates[i]);
}
//check button value
if(shortPress){
currentState = 1;
}
}
lastButtonValue = buttonValue;
}
void blink(int segment){
if(millis() - lastBlinkTime >= blinkDelay){
lastBlinkTime = millis();
blinkState = !blinkState;
}
digitalWrite(segments[segment], blinkState);
}