/*
Wokwi | questions
Your Imaginary Friend!
OP — 4/26/24 at 12:37 PM
So I grabbed a code from youtube that makes the seven segment
display goes 0-9, It uses the left button but I also want the
right button to randomize a number on the seven segment display
I don't know how to, so can someone help me add a code to make
the right button randomize a number on the seven segment display?
https://wokwi.com/projects/396246697158175745
*/
const int COUNT_RATE = 1000;
const int PAUSE_PIN = A1;
const int RANDOM_PIN = A0;
const int GRN_LED_PIN = 4;
const int RED_LED_PIN = 3;
const int BLU_LED_PIN = 2;
const int SEG_PINS[] = {6, 5, 7, 10, 9, 11, 12, 8};
const bool DIGIT_BITS[10][8] = {
/* A B C D E F G DP */
{1, 1, 1, 1, 1, 1, 0, 0}, // 0
{0, 1, 1, 0, 0, 0, 0, 0}, // 1
{1, 1, 0, 1, 1, 0, 1, 0}, // 2
{1, 1, 1, 1, 0, 0, 1, 0}, // 3
{0, 1, 1, 0, 0, 1, 1, 0}, // 4
{1, 0, 1, 1, 0, 1, 1, 0}, // 5
{1, 0, 1, 1, 1, 1, 1, 0}, // 6
{1, 1, 1, 0, 0, 0, 0, 0}, // 7
{1, 1, 1, 1, 1, 1, 1, 0}, // 8
{1, 1, 1, 1, 0, 1, 1, 0}, // 9
};
int oldPauseState = HIGH;
int oldRandomState = HIGH;
unsigned long prevCountMillis = 0;
bool checkPauseButton() {
static bool pauseMode = false;
int state = digitalRead(PAUSE_PIN);
if (state != oldPauseState) {
oldPauseState = state;
if (state == LOW) {
//Serial.println("Pause pressed");
pauseMode = !pauseMode;
Serial.print("Pause mode: ");
Serial.println(pauseMode ? "true" : "false");
} else {
//Serial.println("Pause released");
}
delay(20); // debounce
}
return pauseMode;
}
bool checkRandomButton() {
static bool randomMode = false;
int state = digitalRead(RANDOM_PIN);
if (state != oldRandomState) {
oldRandomState = state;
if (state == LOW) {
//Serial.println("Random pressed");
randomMode = !randomMode;
Serial.print("Random mode: ");
Serial.println(randomMode ? "true" : "false");
} else {
//Serial.println("Random released");
}
delay(20); // debounce
}
return randomMode;
}
void updateDisplay(int number) {
for (int segment = 0; segment < 8; segment++) {
digitalWrite(SEG_PINS[segment], DIGIT_BITS[number][segment]);
}
}
void setup() {
Serial.begin(115200);
for (int i = 0; i <= 8; i++) {
pinMode(SEG_PINS[i], OUTPUT);
}
pinMode(GRN_LED_PIN, OUTPUT);
pinMode(RED_LED_PIN, OUTPUT);
pinMode(BLU_LED_PIN, OUTPUT);
pinMode(PAUSE_PIN, INPUT_PULLUP);
pinMode(RANDOM_PIN, INPUT_PULLUP);
randomSeed(analogRead(random(0, 1024)));
}
void loop() {
static bool hasRun = false;
static int oldCount = 0;
static int count = 0;
bool isPaused = checkPauseButton();
bool isRandom = checkRandomButton();
digitalWrite(GRN_LED_PIN, (!isPaused && !isRandom));
digitalWrite(RED_LED_PIN, isPaused);
digitalWrite(BLU_LED_PIN, isRandom);
if (isRandom && !hasRun) {
count = random(0, 10);
hasRun = true;
} else if (!isRandom) hasRun = false;
if (!isPaused && !isRandom) {
if (millis() - prevCountMillis >= COUNT_RATE) {
prevCountMillis = millis();
count++;
if (count >= 10) count = 0;
}
}
if (count != oldCount) {
oldCount = count;
Serial.print("Number: ");
Serial.println(count);
}
updateDisplay(count);
}
Run
Pause
Random