dimanche 13 décembre 2020

Arduino Pick a random word from array every time sketch is ran

I have some code:

char *fruits[]={"Lettuce", "Tomato", "Pineapple", "Apple"}; //This is the array
long fruit1; 
void setup(){
 Serial.begin(9600);
 randomSeed(500);
 fruit1 = random(sizeof(fruits)/sizeof(char*));
 Serial.println(fruits[fruit1]);//Prints random in serial
}

This code picks a random fruit the first time an arduino sketch is ran, but every other time I run it, it uses the same random fruit it picked the first time the sketch is ran. I want it to pick a random fruit from the array every time the sketch is ran, meaning everytime I turn it on and off I want to see a different and random fruit from the last. Sorry if I'm doing something wrong.

Code source: https://forum.arduino.cc/index.php?topic=45653.0




Aucun commentaire:

Enregistrer un commentaire