Random pointless box coding

hey guys I and just getting into this arduino stuff and done a few simple project so far but I am struggling with some of the code as I only done visual basic befor and not C++

What I have got is the pointless box and I want to make it do random stuff each time the switch it flicked what I got so far is as follows

void loop()
{
switchval = digitalRead(sp); // reads the value of the switch

if (val == HIGH) // to catch the switch being flicked
{
randomSeed(millis()) // set random seed
randNum = random(1, 10)

call random fuction depending on the randNum
}

void random1()
{
do some random servo stuff
reset servo
go back to normal code
}

void random2()
{
do some random servo stuff
reset servo
go back to normal code
}
blah blah blah

just don't know how to make and call the fuctions the best way. other that doing "if 1 then call random1 if 2 call .........."
I want to learn how to code properly

please can people help thanks

If/else if or switch.

If there are going to be a great deal of random functions, you can use an array of function pointers.

yes there going to be a few random functions so I would like to use an array of functions but could u give me some example code please on how to do this?

garycox:
yes there going to be a few random functions so I would like to use an array of functions but could u give me some example code please on how to do this?

Google "C++ function pointer array". Plenty of examples already out there.

well I have a look and I cant find or maybe understand,

I don't want to give the function any parameters I just want to run a lump of code then carry on in the void loop with no returns using an array. I not sure if its a function I need so I don't want to tell it anything

I just want to say depending on the random number run the appropriate lump of code then continue

garycox:
well I have a look and I cant find or maybe understand,

Try looking at some of the examples for longer than 14 minutes.

I don't want to give the function any parameters

Then don't. Function pointers don't require arguments or non-void return types.

I not sure if its a function I need so I don't want to tell it anything

If you don't want to use functions with an array of function pointers, then you're going to have to use a switch or if/else if.

sorry you will have to excuse my newbie ness to c++ and I don't really know what I need to do, I will try to look up more functions with an array of function pointers.

Thanks for ur help so far and pointers

Try something like this:

switch (randNum) 
{
    case 1:
      random1();   //Call random1 function when randNum = 1
      break;
    case 2:
      random2();   //Call random2 function when randNum = 2
      break;
    default:
       random10();   //Call random10 function when randNum doesn't match anything else
       // default is optional
}

P.S.
In this application switch/case is a better solution, but also take a look at [u]if-statements[/u]. Beginning programmers usually learn conditional execution with if-statements and if/else statements before learning switch/case, and you should understand both.

oh right ok, that's sound like I good idea, I will get this into my code and report back to see how I get on.

thanks

Function pointers example:

typedef void (* GenericFP)(void); //function pointer prototype to a function which takes no arguments and returns 'void'

GenericFP functionArray[3] = { &random1, &random2, ...}; //create an array of 'GenericFP' function pointers. 

void setup() {
}

void loop(){ 
  boolean val = digitalRead(1);  // reads the value of the switch
  
  if (val == HIGH) {               // to catch the switch being flicked
     randomSeed(millis());          // set random seed
     byte randNum = random(1, 10);
     
     functionArray[randNum](); //call the randNum'th function.
  }
}

void random1(){
  
}

void random2(){
  
}

void random ...

Hey guys cheers for the coding tips and thanks to DVDdoug I got the switch case working in my project. now I want to get the function arrays working, I just trying with the code that tom gave me just trying to see if I can get it to work before I apply it to my project. I got this little test programme.

typedef void (* GenericFP)(void); //function pointer prototype to a function which takes no arguments and returns 'void'

GenericFP functionArray[2] = { &random1, &random2}; //create an array of 'GenericFP' function pointers. 

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

void loop(){ 

     randomSeed(millis());          // set random seed
     byte randNum = random(1, 3);
     Serial.println(randNum);
     
     functionArray[randNum](); //call the randNum'th function.
}

void random1(){
  Serial.println("Random sentence with ONE");
  delay(1000);
}

void random2(){
  Serial.println("Random sentence with TWO");
  delay(1000);    
}

but all I get in to serial monitor is a random number then loads "&" all on one line, and it never seems to call the function. I don't know what I have done wrong can someone help pls?

Someone might want to check on their array indicies.

random(1,3) will return either 1 or 2. For the array you need 0 or 1.

Try this:

random(2); //a random number up to but excluding 2.

That has seem to have done the trick, thanks again.

Now to put it into my project. :stuck_out_tongue: