I started by assigning the Arduino pins connected to each component: the 7-segment display, IR sensor, switch, and buzzer. I also created a variable to store the current number (which works like a score or counter).
array where each row represents a digit (0 to 9), and each column represents one of the 7 segments. This way, I can easily display any number by looking it up in the array.
Set up each pin as input or output.
Used INPUT_PULLUP for the button
Displayed 0 on the 7-segment at startup .Started the serial monitor for debugging.
used a nested if condition inside the loop() function.
checked if the switch is pressed using:
if (switchState == LOW)
If that’s true, I then check the IR sensor with:
if (state == LOW)
So this means the second condition only runs if the first one is already true.
I did this to make sure the counter only increases when both the switch is pressed and the IR sensor detects something.
It helped me control the logic better and avoid wrong or repeated counts.
code :
int segPins[]={11,10,7,8,9,12,13};
int dpPin =6;
int irPin=3;
int buzzerPin=5;
int switchPin=4;
int count =0;
byte digits[10][7]={
{1,1,1,1,1,1,0},
{0,1,1,0,0,0,0},
{1,1,0,1,1,0,1},
{1,1,1,1,0,0,1},
{0,1,1,0,0,1,1},
{1,0,1,1,0,1,1},
{1,0,1,1,1,1,1},
{1,1,1,0,0,0,0},
{1,1,1,1,1,1,1},
{1,1,1,1,0,1,1},
};
void setup(){
pinMode(irPin,INPUT);
pinMode(buzzerPin,OUTPUT);
pinMode(switchPin,INPUT_PULLUP);
for(int i=0;i<7;i++){
pinMode(segPins[i],OUTPUT);
}
pinMode(dpPin,OUTPUT);
digitalWrite(dpPin,LOW);
displayDigit(0);
Serial.begin(9600);
}
void loop(){
int switchState =digitalRead(switchPin);
if(switchState==LOW){
int state = digitalRead(irPin);
if(state==LOW){
count++;
if(count>9)count=0;
displayDigit(count);
Serial.print("score: ");
Serial.print(count);
digitalWrite(buzzerPin,HIGH);
delay(200);
digitalWrite(buzzerPin,LOW);
delay(500);
}
}
else{
clearDisplay();
}
}
void displayDigit(int num){
for (int i =0 ;i<7;i++){
digitalWrite(segPins[i], digits[num][i]);
}
}
void clearDisplay(){
for (int i=0;i<7;i++){
digitalWrite(segPins[i],LOW);
}
}
Title of Media
Title of Media