Functions on a string?

Hello!
I'm going to try and explain the situation as simply as possible, as it's a bit confusing :confused: :

The aim is to pick an LED at random to turn on and off again.
I have individual functions to do so on each separate LED.
I'm trying to make it possible to pick one of said functions, out of a list.
The randomly chosen function would then execute.

So I've been trying to make it possible for LED's to be chosen at random to turn on for 1 second, then off again, and for this to repeat. But being a newcomer to the Arduino (and C!), i'm having trouble finding the best way to do this. I was assuming a string of functions would work, but am not even sure if this is a possibility.
Any advice would be much appreciated, thanks in advance! :slight_smile:

I was assuming a string of functions would work, but am not even sure if this is a possibility.

During the compilation process, names are converted to addresses. At run time, picking a function by name is NOT a possibility. You could use a series of if statements, using strcmp(), to determine if the name entered means anything. and, if it does, call the appropriate function.

Or, you could research function pointers.

Hi finnthehuman

Does each of the LED functions do something different with its respective LED when called? Or is the only difference the LED that it operates on?

Regards

Ray

Hackscribble:
Does each of the LED functions do something different with its respective LED when called? Or is the only difference the LED that it operates on?

A little update, the LED's must stay on until some input tells it to turn off. This shouldn't effect the main question though.

But yes, all functions do exactly the same thing. (Just with their own color)

Sounds like you should put the pin numbers for the LEDs in an array and just pick the array index at random. Then a single function would work with any LED.

What tells a particular LED to turn off?

...R

Robin2:
What tells a particular LED to turn off?

This is further in to the project. But to turn the LED off, the user is required to press one of the switches (or buttons?), depending on whether the number of LED's turned on is odd or even.

Again, this is further on, and I would obviously need to find out how to turn on multiple random LED's at once!

Use random for random numbers, use an array to hold the actual pin numbers
indexed by the output of random()?

  int pinnum = ledpins [random (N)] ;
  flash (pinnum) ;   // one function needed

I'm not sure I understand exactly what you're trying to do, but maybe:

#define NUMBEROFLEDS  4
#define LOWPIN        8
#define HIPIN        11

int ledPins[NUMBEROFLEDS] = {LOWPIN, 9, 10, HIPIN};

long index;
long lastIndex; 

void setup() {
  Serial.begin(9600);
  randomSeed(A0); 
}

void loop() {
  
  index = random(LOWPIN, HIPIN + 1); 
  
  // Code to set buttonOn and buttonOff
  
  if (buttonOn == true) {
    turnOn(ledPins[index]);
    lastIndex = index;
  } else {
    if (buttonOff == true) {
      turnOff(ledPins[lastIndex]);
    }      
  }
}