Random program output don't work

Hi i found on this forum a program to select random outputs that i want to use to create a reaction game.
The program works very well but when i instal a relay board some of the relays don't work. I have tested the relay board with a simple program and it works perfect. It looks like the outputs aren't declared as output.

//https://forum.arduino.cc/t/random-output-selection-only-once/394681/9//

const int stickCount = 8;
boolean selected[stickCount];  // true => stick has been selected
boolean inSequence = false;    // true => button has been pressed and sequences are running

int pushButton = 3;   // choose the input pin (for a pushbutton)
int val = 0;     // variable for reading the pin status

int catch_a_stick = 0;
unsigned long a = 4000UL; //tthis sets the time before the next stick will fall
unsigned long b = 800UL;
int stick1 = 5; //this makes stick 1 fall
int stick2 = 6; //this makes stick 2 fall
int stick3 = 7; //this makes stick 3 fall
int stick4 = 8; //this makes stick 4 fall
int stick5 = 9; //this makes stick 5 fall
int stick6 = 10; //this makes stick 6 fall
int stick7 = 11; //this makes stick 7 fall
int stick8 = 12; //this makes stick 8 fall

void setup() { //this sets the output pins


  Serial.begin(9600);
  randomSeed(analogRead(0));
  Serial.println("Ready ...");

  // set all selected to false
  clearSelected();


  pinMode(pushButton, INPUT_PULLUP);    // declare pushbutton as input

  pinMode(stick1,OUTPUT);  // declare to be an output
  pinMode(stick2,OUTPUT);  // declare to be an output
  pinMode(stick3,OUTPUT);  // declare to be an output
  pinMode(stick4,OUTPUT);  // declare to be an output
  pinMode(stick5,OUTPUT);  // declare to be an output
  pinMode(stick6,OUTPUT);  // declare to be an output
  pinMode(stick7,OUTPUT);  // declare to be an output
  pinMode(stick8,OUTPUT);  // declare to be an output
}

// call before slecting random sequence
void clearSelected()
{
  for (int i = 0; i < stickCount; i++)
  {
    selected[i] = false;
  }
  Serial.println("Sticks cleared.");
}

void loop()  {

  int catch_a_stick;
  int rnd;

  // if button is pressed and not running the sequence
  if ( (inSequence == false) && (digitalRead(pushButton) == LOW) )
  {
    inSequence = true;    // set flag
    clearSelected();      // mark all sticks as available

  }

  // if we are running the sequence
  if ( inSequence == true )
  {

    // if there are sticks that are not chosen
    if ( sticksChosen() < stickCount )
    {
      while ( true )
      {
        // choose a possible stick
        rnd = random(stickCount);

        // if it has not been selected
        if ( selected[rnd] == false )
        {
          catch_a_stick = rnd;            // set as chosen
          selected[rnd] = true;           // mark that it has been selected
          Serial.print("Stick:  "); Serial.println(catch_a_stick);
          break;                          // done looking
        } // if

      } // while

      // run selected sequence
      Serial.println("  Begin Delay ");
      delay(b);
      Serial.println("   End Delay ");
      switch (catch_a_stick) {
        case 0: //if catch_a_stick equals 0 then the stick1 will be released, stick 1 will fall
          analogWrite(stick1, HIGH);
          Serial.println("  Begin Delay 2");
          delay(a);
          Serial.println("   End Delay 2");
          analogWrite(stick1, LOW);
          break;
        case 1: //if catch_a_stick equals 1 then the stick2 will be released, stick 2 will fall
          digitalWrite(stick2, HIGH);
          Serial.println("  Begin Delay 2");
          delay(a);
          Serial.println("   End Delay 2");
          digitalWrite(stick2, LOW);
          break;
        case 2: //if catch_a_stick equals 2 then the stick3 will be released, stick 3 will fall
          digitalWrite(stick3, HIGH);
          Serial.println("  Begin Delay 2");
          delay(a);
          Serial.println("   End Delay 2");
          digitalWrite(stick3, LOW);
          break;
        case 3: //if catch_a_stick equals 3 then the stick4 will be released, stick 4 will fall
          digitalWrite(stick4, HIGH);
          Serial.println("  Begin Delay 2");
          delay(a);
          Serial.println("   End Delay 2");
          digitalWrite(stick4, LOW);
          break;
        case 4: //if catch_a_stick equals 4 then the stick5 will be released, stick 5 will fall
          analogWrite(stick5, HIGH);
          Serial.println("  Begin Delay 2");
          delay(a);
          Serial.println("   End Delay 2");
          analogWrite(stick5, LOW);
          break;
        case 5: //if catch_a_stick equals 5 then the stick6 will be released, stick 6 will fall
          digitalWrite(stick6, HIGH);
          Serial.println("  Begin Delay 2");
          delay(a);
          Serial.println("   End Delay 2");
          digitalWrite(stick6, LOW);
          break;
        case 6: //if catch_a_stick equals 6 then the stick7 will be released, stick 7 will fall
          digitalWrite(stick7, HIGH);
          delay(a);
          digitalWrite(stick7, LOW);
          break;
        case 7: //if catch_a_stick equals 7 then the stick8 will be released, stick 8 will fall
          digitalWrite(stick8, HIGH);
          Serial.println("  Begin Delay 2");
          delay(a);
          Serial.println("   End Delay 2");
          digitalWrite(stick8, LOW);
          break;

        default:
          break;
      }

    } else {

      // all completed, clear flag
      inSequence = false;

    } // else


  } // if



}


// return # of sticks that have been chosen
//
int sticksChosen()
{
  int retVal = 0;

  // look at all sticks
  for (int i = 0; i < stickCount; i++)
  {
    // increment count if this one has been selected
    if ( selected[i] == true ) retVal++;

  } // for

  return retVal;

}

Read about functions and arrays.

Have i done, this program works perfect when i use leds. But when i use the relay it looks like for some outputs i don't have enough power to activate them. It looks the same like you don't declare the outputs well.

Post a link to the specs of the relays you are using.

Have you ever measured how much current the relay is pulling?

Relays need quite some current. you should not power the relays from the arduino 5V pin.
This 5V pin can not supply enough current.

best regards Stefan

1 Like

This is an error:

Make an array like this.

Before:

int stick1 = 5; //this makes stick 1 fall
int stick2 = 6; //this makes stick 2 fall
int stick3 = 7; //this makes stick 3 fall
int stick4 = 8; //this makes stick 4 fall
int stick5 = 9; //this makes stick 5 fall
int stick6 = 10; //this makes stick 6 fall
int stick7 = 11; //this makes stick 7 fall
int stick8 = 12; //this makes stick 8 fall

After:

const byte stick[stickCount] = {
  5, //this makes stick 1 fall
  6, //this makes stick 2 fall
  7, //this makes stick 3 fall
  8, //this makes stick 4 fall
  9, //this makes stick 5 fall
  10, //this makes stick 6 fall
  11, //this makes stick 7 fall
  12 //this makes stick 8 fall
}

After this you will not need the switch-case statement, you can simply have

          digitalWrite(stick[catch_a_stick], HIGH);
          Serial.println("  Begin Delay 2");
          delay(a);
          Serial.println("   End Delay 2");
          digitalWrite(stick[catch_a_stick], LOW);

These changes will not fix the problem, that may be a power supply problem as @StefanL38 suggested. LEDs normally draw 10~20mA each. Relays modules can draw 60~80mA each. The Arduino's 5V pin can supply perhaps 450mA.

But these changes will improve your code and make it much shorter.

I´m using a seeduino relay shield. I only use 1 relay at a time. When i test it with a simple program all 4 relays are working. When i test it with the random program only 2 relays are working. The other 2 only the led is lighting up a little bit.

you haven't provided a link to exact that relay-shield that you are using.
if it is this seeeduino-relay-shield

the description says
Interface via digital I/O pins 4,5,6, and 7

this means you should provide a link to a datasheet of exact that relay-shield that you are using

best regards Stefan

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