Switch Case Not Random

Hi all,

Currently in Lockdown in Australia thought I would try to make a project for my students when we get back to school soon. I'm trying to build a reaction button game (press the button that lights up) and I've got it mostly working. I'm using an Arduino Mega connected to 8 LED Arcade buttons. I've set up the circuit and it works! However the buttons are not random they follow the cases so it becomes really easy to learn the pattern. I've tried using the random function however the values always seem to go in order. Any help would be greatly appreciated.

int Button = 0;
int LED = 0;
int LED1 = 2;
int BUT1 = 3;
int LED2 = 4;
int BUT2 = 5;
int LED3 = 6;
int BUT3 = 7;
int LED4 = 8;
int BUT4 = 9;
int LED5 = 26;
int BUT5 = 27;
int LED6 = 24;
int BUT6 = 25;
int LED7 = 28;
int BUT7 = 29;
int LED8 = 30;
int BUT8 = 31;

void setup()
{
  Serial.begin(9600);
  pinMode(LED1, OUTPUT);
  pinMode(BUT1, INPUT);
  pinMode(LED2, OUTPUT);
  pinMode(BUT2, INPUT);
  pinMode(LED3,OUTPUT);
  pinMode(BUT3,INPUT);
  pinMode(LED4,OUTPUT);
  pinMode(BUT4,INPUT);  
  pinMode(LED5,OUTPUT);
  pinMode(BUT5,INPUT);  
  pinMode(LED6,OUTPUT);
  pinMode(BUT6,INPUT);
  pinMode(LED7,OUTPUT);
  pinMode(BUT7,INPUT);  
  pinMode(LED8,OUTPUT);
  pinMode(BUT8,INPUT);
}

void loop() {
LED = random(1,8);

switch (LED) {
  case 1:
    while(LED = 1) {
    digitalWrite(LED1, HIGH);
      Button = digitalRead(BUT1);
    Serial.println("Button 1 ON");
  if (Button == 1) {
    delay(100);
    digitalWrite(LED1, LOW);
    Serial.println("Button 1 HIT");
    Button = 0;
   delay(100); 
   break;
  }} 
 
  case 2:
    while(LED = 2) {
      digitalWrite(LED2, HIGH);
      Button = digitalRead(BUT2);
     Serial.println("Button 2 ON");
  if (Button == 1) {
     delay(100);
    digitalWrite(LED2, LOW);
    Serial.println("Button 2 HIT");
    Button = 0;
     delay(100); 
         break;
  }}
  
  case 3:
    while(LED = 3) {
    digitalWrite(LED3, HIGH);
      Button = digitalRead(BUT3);
          Serial.println("Button 3 ON");
  if (Button == 1) {
     delay(100);
    digitalWrite(LED3, LOW);
    Serial.println("Button 3 HIT");
    Button = 0;
    delay(100);
     break;
  }}
   
  case 4:
    while(LED = 4) {
    digitalWrite(LED4, HIGH);
      Button = digitalRead(BUT4);
    Serial.println("Button 4 ON");
  if (Button == 1) {
    delay(100);
    digitalWrite(LED4, LOW);
    Serial.println("Button 4 HIT");
    Button = 0;
    delay(100);
        break;  
  }}
  
 
    case 5:
    while(LED = 5) {
    digitalWrite(LED5, HIGH);
      Button = digitalRead(BUT5);
    Serial.println("Button 5 ON");
  if (Button == 1) {
    delay(100);
    digitalWrite(LED5, LOW);
    Serial.println("Button 5 HIT");
    Button = 0;
    delay(100);
       break;
  }}
    
    case 6:
    while(LED = 6) {
    digitalWrite(LED6, HIGH);
      Button = digitalRead(BUT6);
    Serial.println("Button 6 ON");
  if (Button == 1) {
    delay(100);
    digitalWrite(LED6, LOW);
    Serial.println("Button 6 HIT");
    Button = 0;
    delay(100);
        break;
  }}

      case 7:
    while(LED = 7) {
    digitalWrite(LED7, HIGH);
      Button = digitalRead(BUT7);
    Serial.println("Button 7 ON");
  if (Button == 1) {
    delay(100);
    digitalWrite(LED7, LOW);
    Serial.println("Button 7 HIT");
    Button = 0;
    delay(100);
        break;
  }}

      case 8:
    while(LED = 8) {
    digitalWrite(LED8, HIGH);
      Button = digitalRead(BUT8);
    Serial.println("Button 8 ON");
  if (Button == 1) {
    delay(100);
    digitalWrite(LED8, LOW);
    Serial.println("Button 8 HIT");
    Button = 0;
    delay(100);
        break;
  }}
  
  default:
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
digitalWrite(LED5, LOW);
digitalWrite(LED6, LOW);
digitalWrite(LED7, LOW);
digitalWrite(LED8, LOW);
delay(200);
}}

Should this be "=="?
Paul

1 Like

Read this discussion.

1 Like

This would be a really good time to learn about state-machines and functions, rewrite the code, and lose the delay() calls.

Oops. In all 8 cases.

1 Like

Hello and good morning to DownUnder
What is your current target for your students:

  1. How random() works?
    or
  2. Write a smart sketch ?

wbr from the Baltic Sea

1 Like

Let’s assume you have used a ==
The break only takes you out of the while loop and not the case statement. You need another one before the next case.

But as others have pointed out you don’t seed the random number generator with a random event so it will always give you the same sequence.
Try a push a button to start and during the wait for the button to be pressed keep a count and use that as the seed number.

1 Like

I hope that statement is a disguise to make us work on your homework.
The code you posted is of a quality that should prohibit you to teach about Arduino.

1 Like

Thanks Paul, this definitely fix it. Such a silly error can't believe I missed it. :slight_smile:

Hey Whandall, luckily I'm not teaching Arduino, it's a personal hobby of mine. I'm just trying to make a few interactive projects for our common room for some student engagement in STEM subjects. I'm a chemistry/physics teacher and I only started coding a year ago.

Thanks Mike, I've added another break and tried the seed random number generator and it's fix the problem. I'm going to add a start button (maybe a buzzer too) with a 7 seg number display to keep count. Thanks for the support I'm still learning all about Arduino!

Hi Paul, I'm not looking to teach students about this yet! I'm still learning myself so I thought by making projects I could learn circuits and code and also get students interested in coding.

So you should investigate more into writing code for a microcontroller,
how to make things work smoothly and non-blocking, the usage of arrays for repetitive data etc.
Investigate state-machines, they are a great tool for any sequential behavior.

You may find this useful

1 Like

I am not an expert programmer, I would how ever suggest that looking into HTML / CSS would be a great general way to encourage your students to learn programming languages. I make this suggestion due to the easy way that a " DOCTYPE html" can be created and shared. Note if the project is going to be Class project, maybe students should comment code that they have edited.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.