Lotto code

Arduino and 16x2 LCD Keypad Shield


// include the library code:
	#include <LiquidCrystal.h>;
	#include "DFR_Key.h"

	#define RELAY_ON 0
	#define RELAY_OFF 1 
	#define NOSORT 0
	#define SORT 1 
	#define LIGHT 10 

	int LOTTONUMBERS=7;
	unsigned long countrand=0;
	unsigned long time, timenow,saved_time=0;

	int randNumber[10],rN;
	DFR_Key keypad;

	int localKey = 0;
	String keyString = "";

	// initialize the library with the numbers of the interface pins
	LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

	//--------------------------------------
	// Check that a number is unique
	int CheckNumber(int rN,int arr)
	{
	  int i; 
	  if (rN==0) return(0);  
	  if (arr==1) return(1);  
	  for (i=1; i<arr; i++) {
	    if  (randNumber[i]==rN) return(0); 
	  }
	  return(1); 
	}

	//--------------------------------------
	// Sort the numbers, are 6,7,8 therefore a simple sort algoritms is fine
	int SortNumbers()
	{
	  int c=0, i, tmpi;

	  do{
	    c=0;
	    for (i=0; i<LOTTONUMBERS; i++){
	      if (randNumber[i] > randNumber[i+1]) {
	        tmpi = randNumber[i];
	        randNumber[i] = randNumber[i+1];
	        randNumber[i+1] = tmpi;
	        c++;
	      }
	    }
	  } 
	  while (c > 0);
	}

	//--------------------------------------
	// Setup initial function
	void setup() {

	  int i;
	  unsigned long time;
	  // set up the LCD's number of columns and rows: 
	  // Print a message to the LCD.
	  //Serial.begin(9600);
	  pinMode(LIGHT, OUTPUT);
	  lcd.begin(16, 2);
	  digitalWrite(LIGHT, HIGH);
	  lcd.setCursor(0,0);
	  lcd.print("Numbers=");
	  lcd.print(LOTTONUMBERS);
	  keypad.setRate(10);
	  do {
	    localKey = keypad.getKey(); 
	    if (localKey == UP_KEY) { 
	      LOTTONUMBERS++; 
	      localKey=0; 
	    }
	    if (localKey == DOWN_KEY) { 
	      LOTTONUMBERS--; 
	      localKey=0; 
	    }
	    if (LOTTONUMBERS < 6) LOTTONUMBERS=6;
	    if (LOTTONUMBERS > 8) LOTTONUMBERS=8;
	    lcd.setCursor(0,0);
	    lcd.print("Numbers=");
	    lcd.print(LOTTONUMBERS);
	    delay(250);
	  } 
	  while (localKey != SELECT_KEY);  


	  lcd.setCursor(0,0);
	  lcd.print("Randomizing...");

	  time = millis();
	  (int)random(time);
	  keypad.setRate(10);
	  do {
	    localKey = keypad.getKey(); 
	    time = millis();
	    (int)random(time);
	    countrand++;
	  } 
	  while (localKey != DOWN_KEY);
	  time = millis();
	  (int)random(time);
	  countrand++;
	  lcd.setCursor(0,0);
	  lcd.print("               ");
	  delay(250);
	  saved_time = millis()/1000;
	}

	//--------------------------------------
	// The Lotto Core
	void lotto(int sort) {
	  int i=1;
	  countrand++;
	  lcd.setCursor(0,0);
	  lcd.print("Lotto: ");

	  do {
	    rN = (int)random(49)+1;
	    if (CheckNumber(rN,i)) 
	    { 
	      randNumber[i]=rN;
	      i++;
	    } 
	  } 
	  while (i <= LOTTONUMBERS);  
	  if (sort == SORT) SortNumbers();

	  if (randNumber[1]<10) lcd.print(" ");
	  lcd.print(randNumber[1]);
	  lcd.print("-");
	  if (randNumber[2]<10) lcd.print(" ");
	  lcd.print(randNumber[2]);
	  lcd.print("-");
	  if (randNumber[3]<10) lcd.print(" ");
	  lcd.print(randNumber[3]);
	  lcd.print(" ");
	  lcd.setCursor(0,1);
	  for (i=4; i<=LOTTONUMBERS; i++) {
	    if (randNumber[i]<10) lcd.print(" ");
	    lcd.print(randNumber[i]);
	    if (i<LOTTONUMBERS) lcd.print("-");
	  }
	}

	//--------------------------------------
	// The Core LOOP of the Arduino
	void loop() {
	  int c=0;


	  lotto(SORT);
	  localKey = keypad.getKey();

	  if (localKey == DOWN_KEY) {
	    digitalWrite(LIGHT, HIGH);
	    delay(250);
	    do {
	      localKey = keypad.getKey();
	      time = millis();
	      (int)random(time);
	      countrand++;
	    } 
	    while (localKey != UP_KEY);
	    saved_time = millis()/1000;
	  }
	  if (localKey == RIGHT_KEY) {
	    digitalWrite(LIGHT, HIGH);
	    saved_time = timenow;
	  }
	  timenow = millis()/1000;
	  if ((timenow - saved_time) > 10) { 
	    digitalWrite(LIGHT, LOW); 
	  }

	  c++;
	}