Greetings! I am pretty new to Arduino and am taking on my first project. I took a mechatronics course in college so I do have limited experience with microcontrollers and programming, but I am still very inexperienced.
I have a much larger goal in mind (baseball scoreboard), but I thought I’d take it slow and start with some basics.
I am hoping to find some help in creating a basic counter connected to a 7 segment display. Start at zero, each time the button is pressed, add 1 to the display. For simplicity’s sake, it would probably be easiest to reset the counter to 0 after it shows 9 and the button is pressed again. (However, as I get further, I will be using cascading counters, I believe. For example, imagine I am counting outs in an inning, starting with 0. The button is pressed once, the display shows 1 out. Pressed again, the display shows 2 outs. Pressed one more time, it reset the outs to zero and advances the inning.)
I understand some of the basic flow-chart nature of programming, I just really struggle with the syntax.
I have been looking through Arduino tutorials and replicated the following push button program:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
} else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}
I was also able to replicate the following display/counter:
int num_array[10][7] = { { 1,1,1,1,1,1,0 }, // 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,0,0,1,1 }}; // 9
//function header
void Num_Write(int);
void setup()
{
// set pin modes
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
}
void loop()
{
//counter loop
for (int counter = 10; counter > 0; --counter)
{
delay(1000);
Num_Write(counter-1);
}
delay(3000);
}
// this functions writes values to the sev seg pins
void Num_Write(int number)
{
int pin= 2;
for (int j=0; j < 7; j++) {
digitalWrite(pin, num_array[number][j]);
pin++;
}
}
Could someone please help me work through a simple pushbutton counter? I think the array could be helpful with outputting to the display. I feel like I should start from the code directly above, but instead of simply starting at 9 and counting down every second, I would need to include a line where the pushbutton pin is read, and if it is high it advances the counter by one.
Thanks for any help!!