/*
Arduino | general
NemV1.00 — 7:11 PM
Planning to do an interactive quiz game.
And having an led light flashing green for the correct
answer and flashing red for incorrect. Going to have the
score tracked and displayed on an led as well.
*/
const int NUM_BTNS = 2;
const int NUM_DIGITS = 2;
const int NUM_LEDS = 2;
const int BTN_PINS[] = {13, 12};
const int LED_PINS[] = {11, 10};
const int SEG_PINS[] = {7, 8, 6, 3, 4, 9, 2, 5}; // A-G, DP
const int DIGIT_PINS[] = {A0, A1}; // most significant digit first
// segment lookup table, DP, G - A
const byte NUM_SEGS[] = {
0b00111111, /* 0 */
0b00000110, /* 1 */
0b01011011, /* 2 */
0b01001111, /* 3 */
0b01100110, /* 4 */
0b01101101, /* 5 */
0b01111101, /* 6 */
0b00000111, /* 7 */
0b01111111, /* 8 */
0b01101111, /* 9 */
0b00000000 /* blank */
};
const unsigned long DEBOUNCE_TIME = 20;
const unsigned long BLINK_RATE[] = {1600, 500};
int score = 0;
unsigned long prevTime = 0;
unsigned long lastTime[NUM_LEDS];
bool ledState[NUM_LEDS];
void blinkLed() {
for (int ledNum = 0; ledNum < NUM_LEDS; ledNum++) {
if (millis() - lastTime[ledNum] >= BLINK_RATE[ledNum]) {
lastTime[ledNum] = millis();
ledState[ledNum] = ! ledState[ledNum];
//Serial.print("LED #");
//Serial.print(ledNum);
//Serial.println(" changing");
}
digitalWrite(LED_PINS[ledNum], ledState[ledNum]);
}
}
// function returns which button was pressed, or 0 if none
int checkButtons() {
static bool currState[NUM_BTNS];
static bool lastState[NUM_BTNS];
static unsigned long lastTime[NUM_BTNS];
int btnPressed = 0;
for (int i = 0; i < NUM_BTNS; i++) {
currState[i] = digitalRead(BTN_PINS[i]);
unsigned long now = millis();
if (currState[i] != lastState[i] && now - lastTime[i] >= DEBOUNCE_TIME) {
if (!currState[i]) {
//Serial.println("Button pressed!");
btnPressed = i + 1;
} else {
//Serial.println("Button released!");
}
lastTime[i] = now;
lastState[i] = currState[i];
}
}
return btnPressed;
}
void processButton() {
int btnNum = checkButtons();
if (btnNum) {
Serial.print("Button ");
Serial.print(btnNum);
Serial.println(" pressed.");
if (btnNum == 1) {
score++;
} else if (btnNum == 2) {
score--;
}
}
if (score > 99) score = 99;
if (score < -9) score = -9;
}
//void updateDisplay(long value, int dpPos) {
void updateDisplay(long value) {
int maxPowOfTen = NUM_DIGITS - 1;
//int minBlankDigit = dpPos - 1;
int minBlankDigit = 1;
int digitVal = 0;
long operand1 = 1, operand2 = 1;
bool isMinus = value < 0;
value = abs(value);
// separate the digits
//thousands = (value % 10000) / 1000 (value % operand1 / operand2)
//hundreds = (value % 1000) / 100
//tens = (value % 100) / 10
//ones = (value % 10) / 1
for (int digit = 0; digit <= maxPowOfTen; digit++) {
for (int p = maxPowOfTen - digit; p >= 1; p--) {
operand2 *= 10;
}
operand1 = operand2 * 10;
// don't blank digit where the dp and lower are
if (digit < minBlankDigit) {
// lead zero blanking
digitVal = value < operand2 ? 10 : (value % operand1) / operand2;
} else {
// no lead zero blanking
digitVal = (value % operand1) / operand2;
}
writeDigitSegs(digit, digitVal, (minBlankDigit == digit), isMinus);
operand1 = 1; operand2 = 1;
}
}
void writeDigitSegs(int digit, int number, bool dp, bool neg) {
byte digitSegs = NUM_SEGS[number];
//if (dp) digitSegs |= 0x80; // sets DP
for (int segment = 0; segment < 8; segment++) {
digitalWrite(SEG_PINS[segment], bitRead(digitSegs, segment));
}
if (digit == 0 && neg) digitalWrite(SEG_PINS[6], HIGH); // sets minus sign
digitalWrite(DIGIT_PINS[digit], LOW);
digitalWrite(DIGIT_PINS[digit], HIGH);
}
void setup() {
Serial.begin(9600);
for (int i = 0; i < 8; i++) {
pinMode(SEG_PINS[i], OUTPUT);
}
for (int i = 0; i < NUM_DIGITS; i++) {
pinMode(DIGIT_PINS[i], OUTPUT);
}
for (int i = 0; i < NUM_BTNS; i++) {
pinMode(BTN_PINS[i], INPUT_PULLUP);
}
for (int i = 0; i < 2; i++) {
pinMode(LED_PINS[i], OUTPUT);
}
}
void loop() {
blinkLed();
processButton();
updateDisplay(score);
}
Up
Down