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)