Random sensor order

Hi,

I'm new to arduino and have a question for a school project.

We want to make a puzzle box. We need to activate an X number of sensors in a set order and if done right, the box will open.

The thing is, we want the order to be random. For example, if you have sensors 1-5 then a 4 order would be 1- 3 - 3 - 5. We used a randomseed(A0) in void setup, so that every time you start, you get a new order to complete the puzzle.

Right now the problem is writing the part that links the random number order to the sensors. Is there a (preferably rather easy) way to link sensor A0 to 1? A0=1 doesn't work, but that's the kind of thing I'm hoping to find as a solution.

Looking forward to an answer, and thanks in advance.

Sincerely, Ilias

That's not a puzzle. It's an annoying thing. There's no solution. No way of saying "I've solved it and I can solve it again."

You want the solution to allow the same sensor to occur multiple times? Then just pick randomly until you have the required number. If you don't allow it (like selecting cards from a pack) then you need a different algorithm to record what was selected and what remains.

If your ‘sensors’ are ON/OFF values on input pins you can use a list of input pin numbers at associate a number with a sensor. Then an array of those numbers would be a sequence of sensors.

const int SENSOR_PINS[] = {A0, 2, 3, A3, 5, 7}
const int SENSOR_PIN_COUNT = sizeof SENSOR_PINS / sizeof SENSOR_PINS[0];

const int SEQUENCE_COUNT = 4;
int Sequence[SEQUENCE_COUNT];

void setup() {
    for (int i=0; i < SEQUENCE_COUNT;  i++) {
        Sequence[i] = random(SENSOR_PIN_COUNT);
    }


 int SequenceStep = 0
void loop() {
    int sensorActivated;
    // Check all the inputs until one is found activated
    do {
        sensorActivated = GetSensor();
    } while (sensorActivated == -1);  


    if (sensorActivated == Sequence[SequenceStep]) {
        SequenceStep++;
        if (SequenceStep == SEQUENCE_COUNT) {
             // OPEN THE BOX
        }
    }  else {  // Bad move!
        SequenceStep = 0;  // Start over
    }

    // Wait for all sensors to be released
    do {
        sensorActivated = GetSensor();
    } while (sensorActivated != -1); 
}

Add randomSeed(analogRead(A0)); at the begin of the setup() of johnwasser.

Or store the last used seed in EEPROM, and increment and write it back in setup().