Using an array to turn off LEDs in non sequential order

Hey all,

I was wondering if anyone can help.....

I am using a thermocouple to act as an actuator to turn on a series of lights. When i breathe on it, it turns on the LEDs (very nearly) simultaneously, which is desired. When my timer reaches a certain point and the temp falls below a point, the LEDs turn off sequentially (that is, 1 then 2 then 3 then 4.....etc). What i'm trying to figure out is how to make them turn off non-sequentially (That is, turn off LED 2 then 7 then 4 then 1.....etc.) Ideally, this new sequence would be random each time.

I seem to remember seeing a post where someone randomized their array to do this, but i cant find it, and also, Im an art student, not a programmer, so i tend to stumble through even the simplest solutions.

Any help is greatly appreciated! Thank in advance!

#include <SPI.h>
#include "Adafruit_MAX31855.h"

#define MAXDO   3
#define MAXCS   4
#define MAXCLK  5

int pinArray[] = {7, 8, 9, 10, 11, 12, 13};
int count = 0;

//List of random times for each pendulum to run
int randomTimes[] = {random(4600, 8400), random(4600, 8400), random(4600, 8400), random(4600, 8400), random(4600, 8400), random(4600, 8400), random(4600, 8400)};
int timer = 0; //tracks current loop

Adafruit_MAX31855 thermocouple(MAXCLK, MAXCS, MAXDO);
#if defined(ARDUINO_ARCH_SAMD)
// for Zero, output on USB Serial console, remove line below if using programming port to program the Zero!
#define Serial SerialUSB
#endif

void setup() {

  for (count = 0; count < 7; count++) {
    pinMode(pinArray[count], OUTPUT);
  }

#ifndef ESP8266
  while (!Serial);     // will pause Zero, Leonardo, etc until serial console opens
#endif

  Serial.begin(9600);

  Serial.println("MAX31855 test");
  // wait for MAX chip to stabilize
  delay(500);
}

void loop() {




  // basic readout test, just print the current temp
  Serial.print("Internal Temp = ");
  Serial.println(thermocouple.readInternal());
  double c = thermocouple.readCelsius();
  if (isnan(c)) {
    Serial.println("Something wrong with thermocouple!");
  } else {
    Serial.print("C = ");
    Serial.println(c);
  }

  //////////////////////////////////////////
  if (c >= 30 && timer < randomTimes[count]) { //turn pendulums on
    timer = 10000; //increment timer
    for (count = 0; count < 7; count++) {
      //if time set for that pendulum is less than curren time
      digitalWrite(pinArray[count], HIGH); //turn on this pendulum

    }
  }



  if (c < 30 && timer > randomTimes[count]) { //turn pendulums off
        for (count=0;count<7;count++) {
    digitalWrite(pinArray[count], LOW);
    timer = 0; //reset timer
    delay (randomTimes[count]);
  }


  delay (500);
}

zara2355:
Hey all,

I was wondering if anyone can help.....

I am using a thermocouple to act as an actuator to turn on a series of lights. When i breathe on it, it turns on the LEDs

You must have very hot breath! Do they call you "dragon"?

Its in Celcius, so sadly, no, I am not of dragon origin :slight_smile:

try this

the function shuffleArray below takes two parameters

  • the address of an array of integers
  • the number of elements in that array

when you call the function, the array elements will be shuffled in a random order.

the code loops, so you can see that every second a new arrangement is proposed

Hope this helps :slight_smile:

(this is called a Fisher–Yates shuffle)

int myLedArray[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
int myLedArrayNb = sizeof(myLedArray) / sizeof(myLedArray[0]);

void setup() {


  // put your setup code here, to run once:
  Serial.begin(115200);

  Serial.println("----------------------------------");
  Serial.println("--------- BEFORE SHUFFLE ---------");
  Serial.println("----------------------------------");
  Serial.println("");
  Serial.println("");

  for (int i = 0; i < myLedArrayNb; i++) {
    Serial.print(myLedArray[i]);
    Serial.print("  ");
  }
  Serial.println("");



  Serial.println("----------------------------------");
  Serial.println("---------- AFTER SHUFFLE ---------");
  Serial.println("----------------------------------");
  Serial.println("");



}

void shuffleArray(int* array, int nbVal)
{
  int index;

  for (int i = nbVal - 1; i > 0; i--)
  {
    index = random(0, i + 1); // a (long) number betweeen 0 and i
    if (index != i)
    {
      array[index] ^= array[i];
      array[i] ^= array[index];
      array[index] ^= array[i];
    }
  }
}




void loop() {
  // put your main code here, to run repeatedly:

  shuffleArray(myLedArray, myLedArrayNb);
  for (int i = 0; i < myLedArrayNb; i++) {
    Serial.print(myLedArray[i]);
    Serial.print("  ");
  }
  Serial.println("");
  delay(1000);
}