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[10][7] = {{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][7] = {{1,1,1,1,1,1,1}}; //create a blink in the LED
int i;
val = digitalRead(switchpin);
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
showDigit(blank[0]); //blink
delay(1000);
}
if (val == LOW) { //check if button is pressed
showDigit(digits[i]); // fill array index with student number
delay(1000); // fill time delay
showDigit(blank[0]); //blink
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[0]);
digitalWrite(pinB, signals[1]);
digitalWrite(pinC, signals[2]);
digitalWrite(pinD, signals[3]);
digitalWrite(pinE, signals[4]);
digitalWrite(pinF, signals[5]);
digitalWrite(pinG, signals[6]);
}
NumberbuttonTEST.ino (2.12 KB)