Random function pause?

Hi,

I am having trouble with my code. I have a project with 6 LED strips with each a IR sensor as a set. I tested all and they work. Now, I want a random 'case' to activate by using a the random function and switch....case.

I need the program to stop when it has chosen a case, until the cased sensor is broken. Now at the end of the loop I placed a delay of 2000ms, after that a new randomnumber comes and the next LED is being lid.

I tried with: while (x == 0) { } where x is given values in the case (still visible in case 1), this made the program to do nothing at all anymore. Can someone suggest me how to make a pause at a case and continue the random generator after that case is over. Thank you in advance!

BTW, the digitalwrite for turning on the LEDs are reversed, they switch relays. Somehow when i started up, all the relays the relayboard i use were being energised.

                             // OUTPUTS NAAR RELAY
#define LED1 2
#define LED2 3
#define LED3 4
#define LED4 5
#define LED5 6
#define LED6 7
#define BUZ 8
                            // INPUTS VAN INFRAROOD SENSOREN
const int SENS1 = A0;
const int SENS2 = A1;
const int SENS3 = A2;
const int SENS4 = A3;
const int SENS5 = A4;
const int SENS6 = A5;

                            //WAARDES VOOR SENSOREN
int waardeSENS1 = 0;
int waardeSENS2 = 0;
int waardeSENS3 = 0;
int waardeSENS4 = 0;
int waardeSENS5 = 0;
int waardeSENS6 = 0;
                            //RANDOM WAARDE
  int x;
//byte randomNR;  
                            //BUZZER VOID
  void buz() {   
  digitalWrite(BUZ, LOW);
  delay(150);
  digitalWrite(BUZ, HIGH);
  delay(250);
  //digitalWrite(BUZ, LOW);
  //delay(250);
  //digitalWrite(BUZ, HIGH);
  //delay(250); 
}

void setup() { 
  Serial.begin(9600);
                                // BENOEMEN OUTPUTS
  pinMode(LED1, OUTPUT); 
  pinMode(LED2, OUTPUT); 
  pinMode(LED3, OUTPUT); 
  pinMode(LED4, OUTPUT); 
  pinMode(LED5, OUTPUT); 
  pinMode(LED6, OUTPUT);
  pinMode(BUZ, OUTPUT);    
                                // BENOEMEN INPUTS
  pinMode(SENS1, INPUT);
  pinMode(SENS2, INPUT);
  pinMode(SENS3, INPUT);
  pinMode(SENS4, INPUT);
  pinMode(SENS5, INPUT);
  pinMode(SENS6, INPUT);
                                //pullup activeren
  digitalWrite(SENS1, HIGH);
  digitalWrite(SENS2, HIGH);
  digitalWrite(SENS3, HIGH);
  digitalWrite(SENS4, HIGH);
  digitalWrite(SENS5, HIGH);
  digitalWrite(SENS6, HIGH);
                                //random, random maken..
  randomSeed(analogRead(0));
}

void loop() {
                                //SHUT OFF relays..  
  digitalWrite(LED1, HIGH);
  digitalWrite(LED2, HIGH);
  digitalWrite(LED3, HIGH);
  digitalWrite(LED4, HIGH);
  digitalWrite(LED5, HIGH);
  digitalWrite(LED6, HIGH);
  digitalWrite(BUZ, HIGH);
                                
                                //RANDOMFUNCTIE
  int randomNR = random(1, 7);
  Serial.println(randomNR);
                                //CODE PAUZEREN TOT 1 CASE IS AFGELOPEN
  //while (x == 0) {            //DOES NOTHING
    }
                                //SWITCH...CASE 
  switch (randomNR){
                                
  case 1:
  x = 0;
  digitalWrite(LED1, LOW);         //TURNS ON RELAY
  if(digitalRead(SENS1)==LOW){ 
  buz();
  digitalWrite(LED1, HIGH);         //TURNS OFF RELAY
  x = 1;
  }
  
  break;
  case 2:
  digitalWrite(LED2, LOW);
  if(digitalRead(SENS2)==LOW){
  buz();
  digitalWrite(LED2, HIGH);
  }
  break;
  
  case 3:
  digitalWrite(LED3, LOW);
  if(digitalRead(SENS3)==LOW){
  buz();
  digitalWrite(LED3, HIGH);
  }
  break;
  
  case 4:
  digitalWrite(LED4, LOW);
  if(digitalRead(SENS4)==LOW){
  buz();
  digitalWrite(LED4, HIGH);
  }
  break;
 
  case 5:
  digitalWrite(LED5, LOW);
  if(digitalRead(SENS5)==LOW){
  buz();
  digitalWrite(LED5, HIGH);
  }
  break;
  
  case 6:
  digitalWrite(LED6, LOW);
  if(digitalRead(SENS6)==LOW){
  buz();
  digitalWrite(LED6, HIGH);
  }
  break;
  
  default:
  digitalWrite(LED6, LOW);
   digitalWrite(LED5, LOW);
    digitalWrite(LED4, LOW);
  delay(500);
  break;
  }
  delay(2000);
                                      //Sensoren uitlezen
  waardeSENS1 = digitalRead(SENS1);
  //Serial.println(waardeSENS1);
  waardeSENS2 = digitalRead(SENS2);
  //Serial.println(waardeSENS2);
  waardeSENS3 = digitalRead(SENS3);
  //Serial.println(waardeSENS3);
  waardeSENS4 = digitalRead(SENS4);
  //Serial.println(waardeSENS4);
  waardeSENS5 = digitalRead(SENS5);
  //Serial.println(waardeSENS5);
  waardeSENS6 = digitalRead(SENS6);
  //Serial.println(waardeSENS6);
  }

The whole idea of loop() is to let it loop() as often as possible. And instead of delaying something, you just don't do anything and continue. That's already how you designed your switch :slight_smile: Only the stuff around it not.

Another option would be to hang it in each case. Instead of

if (digitalRead(SENS2) == LOW) {
//do
while(digitalRead(SENS2));

But ONLY do this if you're not planning to extent the program whatsoever. Because while waiting we can't do anything, nothing, zero. But if we let the loop() run freely and only do the stuff we want to do when we want to we can do loads of other stuff :slight_smile:

I don't understand what you want to do, sorry.

But, does your code compile ? These lines :

  //while (x == 0) {            //DOES NOTHING
    }

The first line is commented out but not the second, which acts as the end of the loop. The rest is out of any function, and this should not compile...

For the rest of your code, you should use arrays and for loops. This would simplify the structure of the code, and even help you get rid of this switch / case.

byte ledPins[6] = {2,3,4,5,6,7};
const int sensors[6] = {A0,A1,A2,A3,A4,A5};  // not sure this works though
int waardeSENS[6] = {0};

In the setup:

Serial.begin(9600);
for (byte i=0; i<6; i++) {
  pinMode(ledPins[i],OUTPUT);
  pinMode(sensors[i], INPUT);
  digitalWrite(sensors[i], HIGH); //pullup activeren
  randomSeed(analogRead(0));
}

In the loop:

  for (byte i=0; i<6; i++) digitalWrite(ledPins[i], HIGH);
  int randomNR = random(1, 7);
  Serial.println(randomNR);
  x = 0;
  digitalWrite(ledPins[randomNR], LOW);         //TURNS ON RELAY
  if(digitalRead(sensors[randomNR])==LOW){ 
    buz();
    digitalWrite(ledPins[randomNR], HIGH);         //TURNS OFF RELAY
    x = 1;
  }
  delay (2000);
  for (byte i=0; i<6; i++) waardeSENS[i] = digitalRead(sensors[i]);

And you should initialize x when you define it :
byte x = 0;you can even define it as a bool.

@lesept is completely right about using arrays. Would reduce the to code to around 40 lines.

My comment:

const int sensors[6] = {A0,A1,A2,A3,A4,A5};  // not sure this works though
//yes works but why not byte?
const byte sensorPins[6] = {A0,A1,A2,A3,A4,A5};
 randomSeed(analogRead(0));

Will NOT be very random is a sensor is connected to A0 :wink:

Thanks for the replies! that shorter code makes it much more clear. Tommorow I can test it out.

I add while(x==0) {} at the end, so hopefully the next 'set', randomnumber, will only start when the requirment of the in use sensorvalue has been met. If this doesnt work I will try to use:

while(digitalRead(sensors[randomNR])==LOW){
//instead of
if(digitalRead(sensors[randomNR])==LOW){

My code looks like this now:

  byte ledPins[6] = {2,3,4,5,6,7};
  int BUZ = 8;

  const int sensors[6] = {A0,A1,A2,A3,A4,A5};  // not sure this works though
  int waardeSENS[6] = {0};

  int x;
  
  void buz() {   
  digitalWrite(BUZ, HIGH);
  delay(1000);
  digitalWrite(BUZ, LOW);
  delay(100);
  }

    
void setup() {
  Serial.begin(9600);
  for (byte i=0; i<6; i++) {
  pinMode(ledPins[i],OUTPUT);
  pinMode(sensors[i], INPUT);
  digitalWrite(sensors[i], HIGH); //pullup activeren
  randomSeed(analogRead(0));
  }
  }

void loop() {
  
  for (byte i=0; i<6; i++) digitalWrite(ledPins[i], HIGH);
  int randomNR = random(1, 7);
  Serial.println(randomNR);
  x = 0;
  digitalWrite(ledPins[randomNR], LOW);         //TURNS ON RELAY
  if(digitalRead(sensors[randomNR])==LOW){ 
    buz();
    digitalWrite(ledPins[randomNR], HIGH);         //TURNS OFF RELAY
    x = 1;
  }
  while(x == 0) {}
  
  for (byte i=0; i<6; i++) waardeSENS[i] = digitalRead(sensors[i]);

}

Juuls:
I add while(x==0) {} at the end, so hopefully the next 'set', randomnumber, will only start when the requirment of the in use sensorvalue has been met. If this doesnt work I will try to use:

That will not work :wink: How is that x going to change? Remember, while x == 0 you are after all stuck in that while-loop :wink:

Aka, or hang it in the place you want to wait (aka, checking for in input). Or don't hang it at all and just do the stuff when you want to and let the loop() run free! :slight_smile:

With the first, don't expect the program to be expandable to do more stuff. That's why the second option is preferred by must of us :slight_smile: