Nombre aléatoire

Pour programmer la génération d’un nombre aléatoire, il est possible d’utiliser l’algorithme suivant : lire le nombre stocké en mémoire, générer un nombre basé sur ce nombre, stocker le nombre produit.

Ce qui donne le sketch suivant ;

 //-- Write and read EEPROM Value  
 #include <EEPROM.h>  
 void setup()  
 { int addr = 0;  
  byte nVal, nRnd;  
  Serial.begin(9600);  
  //-- read the value of the adress 0 of the EEPROM  
  nVal = EEPROM.read(addr);  
  Serial.print("Memoire = ");  
  Serial.print(nVal, DEC);  
  Serial.println();  
  //-- generate random number  
  randomSeed(nVal);  
  nRnd = random(0, 254);  
  Serial.println("Random = ");  
  Serial.println(nRnd);  
  //-- write at address 0 the value in the EEPROM  
  EEPROM.write(addr, nRnd);  
  Serial.println("Fin");  
 }  
 void loop()  
 {  
 }  
Articles récents
Commentaires récents
fatima dans Bienvenue !
AdminDroid dans Bienvenue !
fatima dans Bienvenue !
Archives
Catégories
%d blogueurs aiment cette page :