Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a0c1eb0

Browse files
committed
Moved some "general" functions to a new Sketch.
1 parent 4ba3cc6 commit a0c1eb0

File tree

2 files changed

+45
-41
lines changed

2 files changed

+45
-41
lines changed

‎Arduino-Says.ino‎

Lines changed: 4 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,17 @@ void playGame() {
7171
int choice;
7272
int onTime = 500, offTime = 300;
7373
int timeDecrease = 25;
74-
bool rightAnswer = true;
74+
bool rightAnswer;
7575

7676
Node x;
7777
List gameSequence = newList();
7878
generateRandomSequence(gameSequence);
7979
while (lives > 0) {
80+
rightAnswer = true;
8081
playSequence(gameSequence, onTime, offTime);
8182
x = getListHead(gameSequence);
8283
while (x != NULL && rightAnswer) {
83-
//waits for input
84+
//Waits for input
8485
choice = -1;
8586
while (choice == -1) {
8687
choice = checkInputArray(buttons, N_LEDS);
@@ -93,6 +94,7 @@ void playGame() {
9394
rightAnswer = false;
9495
}
9596
}
97+
9698
if (rightAnswer) {
9799
score++;
98100
displayRightAnswer(score);
@@ -143,27 +145,6 @@ void playLED(int index, int onTime) {
143145
digitalWrite(leds[index], LOW);
144146
}
145147

146-
//Returns the pin number of an input that was set to HIGH in the inputs array.
147-
//If all were set to LOW, returns -1.
148-
int checkInputArray(int inputs[], int len) {
149-
for (int i = 0; i < len; i++) {
150-
if (digitalRead(inputs[i]) == HIGH) {
151-
return inputs[i];
152-
}
153-
}
154-
return -1;
155-
}
156-
157-
//Returns the index of the pin pinNumber in the inputs array.
158-
int pinToIndex(int inputs[], int len, int pinNumber) {
159-
for (int i = 0; i < len; i++) {
160-
if (inputs[i] == pinNumber) {
161-
return i;
162-
}
163-
}
164-
return -1;
165-
}
166-
167148
//Plays the animation for when the answer is wrong. Turns of one LED.
168149
void displayWrongAnswer(int lives) {
169150
Serial.println("Wrong answer!");
@@ -195,23 +176,5 @@ void displayGameOver(int score) {
195176
gameSetup();
196177
}
197178

198-
//Outputs value to all pins in the outputs array.
199-
void outputArray(int outputs[], int len, int value) {
200-
for (int i = 0; i < len; i++) {
201-
digitalWrite(outputs[i], value);
202-
}
203-
}
204-
205-
//Writes HIGH for each pin in sequence and then LOW (following the order of the array).
206-
//Each steps lasts stepDelay ms.
207-
void outputArraySequence(int sequence[], int len, float loops, int stepDelay) {
208-
for (int i = 0; i < len*loops; i++) {
209-
digitalWrite(sequence[i%len], HIGH);
210-
delay(stepDelay);
211-
digitalWrite(sequence[i%len], LOW);
212-
delay(stepDelay);
213-
}
214-
}
215-
216179
//Nothing here
217180
void loop() {}

‎PeripheralsControl.ino‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/* Useful functions to control electronic devices. */
2+
3+
//Returns the pin number of an input that was set to HIGH in the inputs array.
4+
//If all were set to LOW, returns -1.
5+
int checkInputArray(int inputs[], int len) {
6+
for (int i = 0; i < len; i++) {
7+
if (digitalRead(inputs[i]) == HIGH) {
8+
return inputs[i];
9+
}
10+
}
11+
return -1;
12+
}
13+
14+
//Returns the index of the pin pinNumber in the inputs array.
15+
int pinToIndex(int inputs[], int len, int pinNumber) {
16+
for (int i = 0; i < len; i++) {
17+
if (inputs[i] == pinNumber) {
18+
return i;
19+
}
20+
}
21+
return -1;
22+
}
23+
24+
//Outputs value to all pins in the outputs array.
25+
void outputArray(int outputs[], int len, int value) {
26+
for (int i = 0; i < len; i++) {
27+
digitalWrite(outputs[i], value);
28+
}
29+
}
30+
31+
//Writes HIGH for each pin in sequence and then LOW (following the order of the array).
32+
//Each steps lasts stepDelay ms.
33+
void outputArraySequence(int sequence[], int len, float loops, int stepDelay) {
34+
for (int i = 0; i < len*loops; i++) {
35+
digitalWrite(sequence[i%len], HIGH);
36+
delay(stepDelay);
37+
digitalWrite(sequence[i%len], LOW);
38+
delay(stepDelay);
39+
}
40+
}
41+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /