 # Count numbers from an array with a button press

Hello!
So I am using a 7pin LED to show a series of numbers.
As a smaller part of a larger project I am trying to figure out how to have these numbers count down during a button press but when the button is released the display blinks the current digit.

This is one of my attempts at the coding. As you can see when the button is pressed it is trapped inside of a for loop that proceeds with the counting, but it does not detect the button in this loop.

Hopefully somebody can help or nudge me in the proper direction.

Thank you.

Attached is the code I have.

``````/*
*  Switch and LED program
*/

int pinA = 13;
int pinB = 12;
int pinC = 11;
int pinD = 10;
int pinE = 9;
int pinF = 8;
int pinG = 7;
int pinDP = 6;
int switchpin = 5;
int val;

// Fill missing rows
int digits = {{0,0,0,0,0,0,1},
{1,0,0,1,1,1,1},
{0,0,1,0,0,1,0},
{0,0,0,0,1,1,0},
{1,0,0,1,1,0,0},
{0,1,0,0,1,0,0},
{0,1,0,0,0,0,0},
{0,0,0,1,1,1,1},
{0,0,0,0,0,0,0},
{0,0,0,1,1,0,0},
};

void setup() {
Serial.begin(9600);
// Set all pins as output
pinMode(pinA, OUTPUT);
pinMode(pinB, OUTPUT);
pinMode(pinC, OUTPUT);
pinMode(pinD, OUTPUT);
pinMode(pinE, OUTPUT);
pinMode(pinF, OUTPUT);
pinMode(pinG, OUTPUT);
pinMode(pinDP, OUTPUT);
pinMode(switchpin, INPUT);
// Set all pins to high to turn off all segments
digitalWrite(pinA, HIGH);
digitalWrite(pinB, HIGH);
digitalWrite(pinC, HIGH);
digitalWrite(pinD, HIGH);
digitalWrite(pinE, HIGH);
digitalWrite(pinF, HIGH);
digitalWrite(pinG, HIGH);
digitalWrite(pinDP, HIGH);
}

void loop(){
int blank = {{1,1,1,1,1,1,1}}; //create a blink in the LED
int i;

if (val== HIGH);{

for (i=0; i<9 ; i++) {  // fill for loop limit
showDigit(digits[i]);   // fill array index with student number
delay(1000);             // fill time delay
delay(1000);
}
if (val == LOW) { //check if button is pressed
showDigit(digits[i]);   // fill array index with student number
delay(1000);             // fill time delay
delay(1000);
}

}
}

// This function uses an array of 7 1's and 0's to turn LEDs ON/OFF
void showDigit(int signals[]) {
// Set all pins levels & fill missing parts
digitalWrite(pinA, signals);
digitalWrite(pinB, signals);
digitalWrite(pinC, signals);
digitalWrite(pinD, signals);
digitalWrite(pinE, signals);
digitalWrite(pinF, signals);
digitalWrite(pinG, signals);
}
``````

NumberbuttonTEST.ino (2.12 KB)

`````` if (val== HIGH);{
``````

An if statement will run conditionally the next statement after it. That can be a single line of code or is can be a block of code between { and }. In your case it is the single empty line of code denoted by the ; there. So this says, if (val == HIGH) then do nothing. The block behind that is run unconditionally and is not under the control of the if statement.

Moral: if statements don't get semicolons.

If you want to check the button inside the for loop, then write a line with digitalWrite to check it in there and an if statement to sue break to end the loop if you want.

Personally, I wouldn't use a for loop at all. The loop function is already looping. So you can just let it increment the variable one time, show it if needed, and loop back around to check the button.

To start with, you could save a lot of memory by switching to an array of bytes for your digits. Not a huge deal now but it's good to get into the habit before you start working on larger programs. You've got an array with 70 int's in it, that's 140 bytes of memory. You could do the same thing with an array of 10 bytes, which is of course 10 bytes of memory.

``````const byte myByteArray = {  B00111111, //Digit_0
B00000110, //Digit_1
B01011011, //Digit_2
B01001111, //Digit_3
B01100110, //Digit_4
B01101101, //Digit_5
B01111101, //Digit_6
B00000111, //Digit_7
B01111111, //Digit_8
B01101111 }; //Digit_9
``````

The B in front of the group of 1s & 0s denotes that you're writing a binary number. You don't specifically need to write them as binary numbers but it can help you visualize which LED Segments are On/Off. Note: the binary number reads from right to left. Here's what LED segments they'll control:

`````` .GFEDCBA
B00110011
``````

To work with the invidual bits familiarize yourself with the arduino functions: bitRead(x, n); bitSet(x, n); bitClear(x, n);

For now all you'll need is bitRead(variableToRead, bitToRead); Here's how you would turn on/off the pins for what I'm referring to as Digit_0 in myByteArray above:

Note: My 7 segment LED display needs pins set high to turn things on. If yours is different then just flip all the 1s & 0s I typed for all the values in myByteArray

``````digitalWrite(pinA, bitRead(myByteArray, 0) );
``````pinMode(switchpin, INPUT_PULLUP);