Week 5
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
const int LED[] = {11, 10, 9, 8, 7, 6, 5, 4};
|
||||
const int BUTTON[] = {A0, A1, A2, A3};
|
||||
|
||||
int clk = 0; // 定義每個模式中的計數器
|
||||
unsigned int clk = 0; // the counter in every mode
|
||||
|
||||
void setup() {
|
||||
for (int i = 0; i < 8; i++) {
|
||||
@@ -15,23 +15,29 @@ void setup() {
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Function: call_interval
|
||||
* Call a function per DELAY
|
||||
*
|
||||
* f: the function being called
|
||||
*
|
||||
* Side effect: increase clk per DELAY
|
||||
*/
|
||||
void call_interval( void (*f)(unsigned int) ) {
|
||||
static unsigned long checkpoint = 0;
|
||||
if (millis() - checkpoint > DELAY) {
|
||||
for (int i = 0; i < 8; i++) {
|
||||
digitalWrite(LED[7 - i], !((value >> i) % 2));
|
||||
}
|
||||
(*f)(clk);
|
||||
clk += 1;
|
||||
checkpoint = millis();
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Function: get_mode
|
||||
* Return the current mode from buttons input
|
||||
*
|
||||
* Side effects: reset the clk on button pressed
|
||||
*/
|
||||
Function: get_mode
|
||||
Return the current mode from buttons input
|
||||
|
||||
Side effects: reset the clk on button pressed
|
||||
*/
|
||||
unsigned int get_mode() {
|
||||
static int last = 0;
|
||||
for (int i = 0; i < 4; i++) {
|
||||
@@ -45,20 +51,12 @@ unsigned int get_mode() {
|
||||
}
|
||||
|
||||
/*
|
||||
* Function: binary
|
||||
* Display the LED as binary of clk
|
||||
*
|
||||
* increase clk per DELAY
|
||||
* Side effect: increase clk per DELAY
|
||||
*/
|
||||
Function: binary
|
||||
Display the LED as binary of clk
|
||||
*/
|
||||
void binary(unsigned int value) {
|
||||
static unsigned long checkpoint = 0;
|
||||
if (millis() - checkpoint > DELAY) {
|
||||
for (int i = 0; i < 8; i++) {
|
||||
digitalWrite(LED[7 - i], !((value >> i) % 2));
|
||||
}
|
||||
clk += 1;
|
||||
checkpoint = millis();
|
||||
for (int i = 0; i < 8; i++) {
|
||||
digitalWrite(LED[7 - i], !((value >> i) % 2));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -67,16 +65,12 @@ void gray_code(unsigned int value) {
|
||||
}
|
||||
|
||||
/*
|
||||
* Function: random_popup
|
||||
* Randomly blink LED
|
||||
*
|
||||
* Randomly select LED. Each LED will turn on for 3 clk
|
||||
* Side effect: increase clk per DELAY
|
||||
*/
|
||||
void random_popup() {
|
||||
static unsigned long checkpoint = 0;
|
||||
static int hp[] = {0, 0, 0, 0, 0, 0, 0, 0};
|
||||
|
||||
Function: random_popup
|
||||
Randomly select LED. Each LED will be turned on for 3 clk
|
||||
*/
|
||||
void random_popup(unsigned int clk) {
|
||||
static int hp[8];
|
||||
|
||||
// Reset hp on mode changing
|
||||
if (clk == 0) {
|
||||
for (int i = 0; i < 8; i++) {
|
||||
@@ -84,26 +78,20 @@ void random_popup() {
|
||||
}
|
||||
}
|
||||
|
||||
if (millis() - checkpoint > DELAY) {
|
||||
for (int i = 0; i < 8; i++) {
|
||||
|
||||
// Decrease hp
|
||||
if (hp[i] > 0) {
|
||||
hp[i] -= 1;
|
||||
}
|
||||
digitalWrite(LED[i], !(hp[i]));
|
||||
for (int i = 0; i < 8; i++) {
|
||||
// Decrease hp
|
||||
if (hp[i] > 0) {
|
||||
hp[i] -= 1;
|
||||
}
|
||||
|
||||
// randomly select LED which is not turned on
|
||||
int choice;
|
||||
do {
|
||||
choice = random(8);
|
||||
} while (hp[choice] > 0);
|
||||
hp[choice] = 4;
|
||||
|
||||
checkpoint = millis();
|
||||
clk++;
|
||||
digitalWrite(LED[i], !(hp[i]));
|
||||
}
|
||||
|
||||
// randomly select LED which is not turned on
|
||||
int choice;
|
||||
do {
|
||||
choice = random(8);
|
||||
} while (hp[choice] > 0);
|
||||
hp[choice] = 4;
|
||||
}
|
||||
|
||||
void clear_all() {
|
||||
@@ -115,13 +103,13 @@ void clear_all() {
|
||||
void loop() {
|
||||
const int mode = get_mode();
|
||||
if (mode == 1) {
|
||||
binary(clk);
|
||||
call_interval(binary);
|
||||
}
|
||||
if (mode == 2) {
|
||||
gray_code(clk);
|
||||
call_interval(gray_code);
|
||||
}
|
||||
if (mode == 3) {
|
||||
random_popup();
|
||||
call_interval(random_popup);
|
||||
}
|
||||
if (mode == 4) {
|
||||
clear_all();
|
||||
|
||||
Reference in New Issue
Block a user