I created a program to show a random number on a 7 segment display. It shows a random number in the serial monitor but always shows the same segments 'a c d and g'. anyone know hopw to fix this without changing the base of the coding its for and exercise so i need to have most of the code like I wrote it.
int num_array[11][7] = {
{ 1, 1, 1, 1, 1, 0, 1 }, // 0
{ 0, 1, 1, 0, 0, 0, 0 }, // 1
{ 1, 1, 0, 1, 1, 1, 0 }, // 2
{ 1, 1, 1, 1, 0, 1, 0 }, // 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, 0, 0, 1, 1 }, // 9
{ 0, 0, 0, 0, 0, 0, 0 }
}; //OFF
int buttonState = 0;
const int button = 12;
int randVal;
bool pressVal;
void num_Write(int);
void setup() {
Serial.begin(9600);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(button, INPUT);
}
void loop() {
buttonState = digitalRead(button);
if (buttonState == LOW) {
turnOff();
pressVal = false;
}
else
{
if (pressVal == false)
{
int pin = 2;
for (int i = 0; i < 7 ; i++)
{
digitalWrite(pin++, num_array[11][i]);
}
randVal = rand() % 10;
pressVal = true;
}
Serial.println(randVal);
if (randVal == 0)
{
void zero();
}
else if (randVal == 1)
{
void one();
}
else if (randVal == 2)
{
void two();
}
else if (randVal == 3)
{
void three();
}
else if (randVal == 4)
{
void four();
}
else if (randVal == 5)
{
void five();
}
else if (randVal == 6)
{
void six();
}
else if (randVal == 7)
{
void seven();
}
else if (randVal == 8)
{
void eight();
}
else
{
void nine();
}
}
}
void zero()
{
int pin = 2;
for (int j = 0; j < 7; j++) {
digitalWrite(pin++, num_array[0][j]);
}
delay(250);
}
void one()
{
int pin = 2;
for (int j = 0; j < 7; j++) {
digitalWrite(pin++, num_array[1][j]);
}
delay(250);
}
void two()
{
int pin = 2;
for (int j = 0; j < 7; j++) {
digitalWrite(pin++, num_array[2][j]);
}
delay(250);
}
void three()
{
int pin = 2;
for (int j = 0; j < 7; j++) {
digitalWrite(pin++, num_array[3][j]);
}
delay(250);
}
void four()
{
int pin = 2;
for (int j = 0; j < 7; j++) {
digitalWrite(pin++, num_array[4][j]);
}
delay(250);
}
void five()
{
int pin = 2;
for (int j = 0; j < 7; j++) {
digitalWrite(pin++, num_array[5][j]);
}
delay(250);
}
void six()
{
int pin = 2;
for (int j = 0; j < 7; j++) {
digitalWrite(pin++, num_array[6][j]);
}
delay(250);
}
void seven()
{
int pin = 2;
for (int j = 0; j < 7; j++) {
digitalWrite(pin++, num_array[7][j]);
}
delay(250);
}
void eight()
{
int pin = 2;
for (int j = 0; j < 7; j++) {
digitalWrite(pin++, num_array[8][j]);
}
delay(250);
}
void nine()
{
int pin = 2;
for (int j = 0; j < 7; j++) {
digitalWrite(pin++, num_array[9][j]);
}
delay(250);
}
void turnOff()
{
int pin = 2;
for (int j = 0; j < 7; j++) {
digitalWrite(pin++, num_array[10][j]);
}
delay(250);
}
1 Answer 1
Note, this is not an answer to your question, but I like to show some programming improvements, making your sketch much shorter and easier to maintain.
Instead of having all functions one
, two
, etcetera, use a parameter to pass the number. So you get this: (I couldn't test it as I don't have an Arduino IDE at hand). Also you can use a for
loop in the loop
function to set the output pins.
int num_array[11][7] = {
{ 1, 1, 1, 1, 1, 0, 1 }, // 0
{ 0, 1, 1, 0, 0, 0, 0 }, // 1
{ 1, 1, 0, 1, 1, 1, 0 }, // 2
{ 1, 1, 1, 1, 0, 1, 0 }, // 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, 0, 0, 1, 1 }, // 9
{ 0, 0, 0, 0, 0, 0, 0 }
}; //OFF
int buttonState = 0;
const int button = 12;
int randVal;
bool pressVal;
void num_Write(int);
void digit(int value);
void setup() {
Serial.begin(9600);
for (int pin = 2; pin <= 8; pin++)
{
pinMode(pin, OUTPUT);
}
pinMode(button, INPUT);
}
void loop() {
buttonState = digitalRead(button);
if (buttonState == LOW) {
turnOff();
pressVal = false;
}
else
{
if (pressVal == false)
{
int pin = 2;
for (int i = 0; i < 7 ; i++)
{
digitalWrite(pin++, num_array[11][i]);
}
randVal = rand() % 10;
pressVal = true;
}
Serial.println(randVal);
if ((randVal >= 0) && (randVal <= 9))
{
digit(randVal);
}
}
}
void digit(int value)
{
int pin = 2;
for (int j = 0; j < 7; j++) {
digitalWrite(pin++, num_array[value][j]);
}
delay(250);
}
void turnOff()
{
int pin = 2;
for (int j = 0; j < 7; j++) {
digitalWrite(pin++, num_array[10][j]);
}
delay(250);
}
You can also remove turnOff
if you treat it like digit 10 but that is not so clear (or put the implementation of digit
to another function and call it from digit
and turnoff
.
To solve your problem, print the values of each pin to the serial monitor. If they are correct, there is a problem in your circuit. If they are incorrect, there is a problem in your sketch. In the latter case, add more print statements until you found the exact problem.
Update
- Fixed void forward declaration to function call (see comment of chrisl)
If the digit
function is never called, it means:
((randVal >= 0) && (randVal <= 9))
is false, however, that is not possible since it is initialized to 0 and with the rand function it gets always values between 0 to 9.- pressVal is always TRUE.
So check the value of pressVal, also add (lots of) print
statements, especiallya after each if
and else
so you know exactly the flow of your sketch. You will see it's very easy to find the error. If you pinpoint the problem to a digitalRead
that is not the expected value, THAN there is high likely a problem with your circuit (a button that is never going to be LOW or HIGH). In that case, you probably forgot a pullup/pulldown resistor.
-
Tnx for the feedback, will try to implement this seems alot neater than my coding. any idea how it would come that the wrong segments light up?Bavo– Bavo2019年11月14日 13:48:34 +00:00Commented Nov 14, 2019 at 13:48
-
Not sure, I would follow the tip at the end, add more print statements to be sure that the sketch is ok, and if not, check the circuit.Michel Keijzers– Michel Keijzers2019年11月14日 13:52:30 +00:00Commented Nov 14, 2019 at 13:52
-
So i tried finding the source of my problem and it never goes to this part of the code
void digit(int value) { int pin = 2; for (int j = 0; j < 7; j++) { digitalWrite(pin++, num_array[value][j]); } delay(250); }
Bavo– Bavo2019年11月14日 14:02:35 +00:00Commented Nov 14, 2019 at 14:02 -
2Did you remove the void in
void digit(randVal);
inside the loop function? Because that is wrong, since you don't want a forward declaration of a function there.chrisl– chrisl2019年11月14日 14:11:56 +00:00Commented Nov 14, 2019 at 14:11 -
@chrisl I should have removed it (which I did now), probably you refer to that. And you're right, copy/paste error (having no Arduino IDE at hand). Thanks for the notification.Michel Keijzers– Michel Keijzers2019年11月14日 14:20:23 +00:00Commented Nov 14, 2019 at 14:20
digitalWrite(pin++, num_array[11][i]);
cannot work. The first dimension ofnum_array
has only 11 elements. The element with the index 11 would be the 12th. So this is reading some data outside of the array.