Program that Generates a Random Number and outputs it to a 7 segment display

Hello All,
I am trying to make a sketch that would generate a random number from 0 to 9 and output it to a 7 segment display after each push of a button by the user.

my input pin is A0
my 5v is hooked up to one side of the button then a 10k resistor (per prof instructions)
then i have 1k resistors hooked up to each segment of the display and a ground attached to the common ground.
The display works but the program doe snot read the input from the button.
If anyone could help me out that would be great.
Code below.

//defines read pin

int ReadPin = A0;

//used to output actual numbers on the board

int d[][7] = 
//a, b, c, d, e, f, g
{{1, 1, 1, 1, 1, 1, 0},  // digit 0
{0, 1, 1, 0, 0, 0, 0},  // 1
{1, 1, 0, 1, 1, 0, 1},  // 2
{1, 1, 1, 1, 0, 0, 1},  // 3
{0, 1, 1, 0, 0, 1, 1},  // 4
{1, 0, 1, 1, 0, 1, 1},  // 5
{1, 0, 1, 1, 1, 1, 1},  // 6
{1, 1, 1, 0, 0, 0, 0},  // 7
{1, 1, 1, 1, 1, 1, 1},  // 8
{1, 1, 1, 1, 0, 1, 1}
};

//variable used for the lops that make the segments light up.

int r = 0;

//function to make a random number

int Rando() {
int num = random(9)/random(7,0);

return num;
}

void setup() {
  // put your setup code here, to run once:

//defines ReadPin as input

pinMode(ReadPin, INPUT);

//loop to define the digital pin 2-8 as output

for (int i = 2; i <= 8; i++)
  pinMode(i, OUTPUT);

//to seed rndom gnerator
randomSeed(0);
//test for button click

if (analogRead(ReadPin) == HIGH) {

  //if click do ranom num function and assign value to the int dig
  int dig = Rando();

//light up correct segments of display

  for (int a = 0; a <8; a++)
    {
     digitalWrite(a+2,d[dig][a]);
   } 
  
}

//turns off display if not being used

else {
   for (int e = 2; e <8; e++)
    { 
  digitalWrite(e, LOW);

    }
}
}

//currently commented out as I am messing with other parts

void loop() {
/*  if (digitalRead(ReadPin) == HIGH) {
  Serial.println("Hello");
  int dig = Rando();
  for (int a = 0; a <8; a++)
    {
     digitalWrite(a+2,d[dig][a]);
   } 
  
}
else {
   for (int e = 2; e <8; e++)
    { 
  digitalWrite(e, LOW);

    }
}
*/
//EOF
}

hw4.ino (1.6 KB)

You should read the forum rules before post.

thought i did good what did i mess up?

The code should be post in a proper form.
Edit your first post, select the code and presse the first utton in the toolbar.

The display works but the program doe snot read the input from the button.

if (analogRead(ReadPin) == HIGH)
Try
if (digitalRead(ReadPin))

How is the switch wired?

LarryD:
if (analogRead(ReadPin) == HIGH)
Try
if (digitalRead(ReadPin))

How is the switch wired?

the switch is a single click black 4 pin button with 5v going in one side and a0 is on the other sid reading with the GRND after that

You will need something like this, R = 10K
2015-09-25_18-38-33.jpg
.

i tried that but it still doesn't register a click

Perhaps because of this abomination:

//test for button click

if (analogRead(ReadPin) == HIGH) {

Hint: do a Serial print of analogRead(ReadPin), and of HIGH. Look at the values and be amazed.

Why is that an issue

See post #4 and 8

Also I would use:
const byte ReadPin =14; // pin A0 is D14

You are wanting to read a digital level not an analog level.
Analog levels are from 0 to 1023, not HIGH

Therefore use digitalRead.

Ok thanks guys but still not sure why not working

A quick serial print tells me the program gets to right before the loop but won't enter
If (analogRead(ReadPin))....

htownlax25:
Ok thanks guys but still not sure why not working

Even after it was explained to you twice three times?

I have made all the changes suggested. So yea

Please attach your current sketch.

//defines read pin

const byte ReadPin = 14; //as suggested

//used to output actual numbers on the board

int d[][7] = 
//a, b, c, d, e, f, g
{{1, 1, 1, 1, 1, 1, 0},  // digit 0
 {0, 1, 1, 0, 0, 0, 0},  // 1
 {1, 1, 0, 1, 1, 0, 1},  // 2
 {1, 1, 1, 1, 0, 0, 1},  // 3
 {0, 1, 1, 0, 0, 1, 1},  // 4
 {1, 0, 1, 1, 0, 1, 1},  // 5
 {1, 0, 1, 1, 1, 1, 1},  // 6
 {1, 1, 1, 0, 0, 0, 0},  // 7
 {1, 1, 1, 1, 1, 1, 1},  // 8
 {1, 1, 1, 1, 0, 1, 1}
 };

//variable used for the lops that make the segments light up.

int r = 0;

//function to make a random number

int Rando() {
int num = random(9)/random(7,0);

  return num;
}

void setup() {
    // put your setup code here, to run once:
    //debugging code
    Serial.begin(9600); // serial debugging

//defines ReadPin as input

  pinMode(ReadPin, INPUT);

//loop to define the digital pin 2-8 as output

 for (int i = 2; i <= 8; i++)
    pinMode(i, OUTPUT);

  //to seed rndom gnerator
  randomSeed(0);

//test for button click

Serial.println("got here"); // prints this in the window

  if (digitalRead(ReadPin)) { // change as suggested
Serial.println(ReadPin); //does not get here
Serial.println("Hello"); //or here
    //if click do ranom num function and assign value to the int dig
    int dig = Rando();

//light up correct segments of display

    for (int a = 0; a <8; a++)
      {
       digitalWrite(a+2,d[dig][a]);
     } 
    
 }

//turns off display if not being used

  else {
     for (int e = 2; e <8; e++)
      { 
    digitalWrite(e, LOW);

      }
  }
}

//currently commented out as I am messing with other parts

void loop() {
/*  if (digitalRead(ReadPin) == HIGH) {
    Serial.println("Hello");
    int dig = Rando();
    for (int a = 0; a <8; a++)
      {
       digitalWrite(a+2,d[dig][a]);
     } 
    
 }
  else {
     for (int e = 2; e <8; e++)
      { 
    digitalWrite(e, LOW);

      }
  }
*/
//EOF
}

Well, how can it work with everything useful commented out?

i commented out what i didn't think was working and placed the code in the setup to only do it once per button press