Hi Stefan. Thanks for the comment and trying to help me but I'm still having some trouble. I know it's stupid and I am really stupid for this subject so I'm sorry if you get disappointed for the result. I tried someone's advice to make the timer play. It eventually did but my problem now is the button. I also tried the demonstration code you gave me. Um it did not work for me. I probably did it wrong and put just stuff that did not make sense. The 7 segment display is just not working and I'm really confused. I didn't get a good foundation on what are functions and stuff, I just followed my teacher's code that's literally all I learned. Not blaming it on them though, I need to study more of this. But if you could give me some advice, that would be great. Here is the code I made where the timer works.
// define the pins, LEDs, and variables as constant
const int LED1 = 13;
const int PUSHBTN1 = 12;
const int LED2 = 11;
const int PUSHBTN2 = 10;
const int PIEZO = 9;
// define the pins, LEDs, and variables
int pinA = 2;
int pinB = 3;
int pinC = 4;
int pinD = 5;
int pinE = 6;
int pinF = 7;
int pinG = 8;
int PUSHBTN1Value;
int PUSHBTN2Value;
int counter = 10;
void setup() {
Serial.begin(9600);
pinMode (pinA, OUTPUT); // sets the pins as an OUTPUT
pinMode (pinB, OUTPUT);
pinMode (pinC, OUTPUT);
pinMode (pinD, OUTPUT);
pinMode (pinE, OUTPUT);
pinMode (pinF, OUTPUT);
pinMode (pinG, OUTPUT);
pinMode (LED1, OUTPUT); // sets the LEDs as an OUTPUT
pinMode (LED2, OUTPUT);
pinMode (PIEZO, OUTPUT);
pinMode (PUSHBTN1, INPUT); // sets the push buttons as an INPUT
pinMode (PUSHBTN2, INPUT);
}
void one(){
//1 1111001
digitalWrite (pinA, HIGH);
digitalWrite (pinB, LOW);
digitalWrite (pinC, LOW);
digitalWrite (pinD, HIGH);
digitalWrite (pinE, HIGH);
digitalWrite (pinF, HIGH);
digitalWrite (pinG, HIGH);
}
void two(){
//2 0100100
digitalWrite (pinA, LOW);
digitalWrite (pinB, LOW);
digitalWrite (pinC, HIGH);
digitalWrite (pinD, LOW);
digitalWrite (pinE, LOW);
digitalWrite (pinF, HIGH);
digitalWrite (pinG, LOW);
}
void three(){
//3 0110000
digitalWrite (pinA, LOW);
digitalWrite (pinB, LOW);
digitalWrite (pinC, LOW);
digitalWrite (pinD, LOW);
digitalWrite (pinE, HIGH);
digitalWrite (pinF, HIGH);
digitalWrite (pinG, LOW);
}
void four(){
// 4 0011001
digitalWrite (pinA, HIGH);
digitalWrite (pinB, LOW);
digitalWrite (pinC, LOW);
digitalWrite (pinD, HIGH);
digitalWrite (pinE, HIGH);
digitalWrite (pinF, LOW);
digitalWrite (pinG, LOW);
}
void five(){
// 5 0010010
digitalWrite (pinA, LOW);
digitalWrite (pinB, HIGH);
digitalWrite (pinC, LOW);
digitalWrite (pinD, LOW);
digitalWrite (pinE, HIGH);
digitalWrite (pinF, LOW);
digitalWrite (pinG, LOW);
}
void six(){
// 6 0000010
digitalWrite (pinA, LOW);
digitalWrite (pinB, HIGH);
digitalWrite (pinC, LOW);
digitalWrite (pinD, LOW);
digitalWrite (pinE, LOW);
digitalWrite (pinF, LOW);
digitalWrite (pinG, LOW);
}
void seven(){
// 7 1111000
digitalWrite (pinA, LOW);
digitalWrite (pinB, LOW);
digitalWrite (pinC, LOW);
digitalWrite (pinD, HIGH);
digitalWrite (pinE, HIGH);
digitalWrite (pinF, HIGH);
digitalWrite (pinG, HIGH);
}
void eight(){
// 8 1111111
digitalWrite (pinA, LOW);
digitalWrite (pinB, LOW);
digitalWrite (pinC, LOW);
digitalWrite (pinD, LOW);
digitalWrite (pinE, LOW);
digitalWrite (pinF, LOW);
digitalWrite (pinG, LOW);
}
void nine(){
// 9 1001111
digitalWrite (pinA, LOW);
digitalWrite (pinB, LOW);
digitalWrite (pinC, LOW);
digitalWrite (pinD, LOW);
digitalWrite (pinE, HIGH);
digitalWrite (pinF, LOW);
digitalWrite (pinG, LOW);
}
void zero(){
// 0 0111111
digitalWrite (pinA, LOW);
digitalWrite (pinB, LOW);
digitalWrite (pinC, LOW);
digitalWrite (pinD, LOW);
digitalWrite (pinE, LOW);
digitalWrite (pinF, LOW);
digitalWrite (pinG, HIGH);
}
void stop(){
digitalWrite (pinA, HIGH);
digitalWrite (pinB, HIGH);
digitalWrite (pinC, HIGH);
digitalWrite (pinD, HIGH);
digitalWrite (pinE, HIGH);
digitalWrite (pinF, HIGH);
digitalWrite (pinG, HIGH);
}
void countdown(int countdown10){
if (countdown10 == 1){
one();
} else if (countdown10 == 2){
two();
} else if (countdown10 == 3){
three();
} else if (countdown10 == 4){
four();
} else if (countdown10 == 5){
five();
} else if (countdown10 == 6){
six();
} else if (countdown10 == 7){
seven();
} else if (countdown10 == 8){
eight();
} else if (countdown10 == 9){
nine();
} else if (countdown10 == 0){
zero();
}
}
void loop() {
PUSHBTN1Value = digitalRead(PUSHBTN1);
PUSHBTN2Value = digitalRead(PUSHBTN2);
if(PUSHBTN1 == 1){
exit(0);
digitalWrite (LED1, HIGH);
tone(PIEZO, 800);
}else if (PUSHBTN2 == 1){
exit(0);
digitalWrite (LED2, HIGH);
tone(PIEZO, 800);
}else{
countdown(counter);
counter = counter - 1;
}
delay(1000);
}
Here is the code where I followed your code.
#define ProjectName "Buzzer Beater"
#define unPressed HIGH
#define pressed LOW
// define the pins, LEDs, and variables as constant
const int LED1 = 13;
const int LED2 = 11;
const int PIEZO = 9;
const byte pushBTN1 = A0;
const byte pushBTN2 = A1;
bool activationMode = false;
unsigned long myCounter = 10;
// define the pins, LEDs, and variables
int pinA = 2;
int pinB = 3;
int pinC = 4;
int pinD = 5;
int pinE = 6;
int pinF = 7;
int pinG = 8;
int PUSHBTN1Value;
int PUSHBTN2Value;
int counter = 10;
void setup() {
Serial.begin(115200);
Serial.println( F("Setup-Start") );
pinMode (pinA, OUTPUT); // sets the pins as an OUTPUT
pinMode (pinB, OUTPUT);
pinMode (pinC, OUTPUT);
pinMode (pinD, OUTPUT);
pinMode (pinE, OUTPUT);
pinMode (pinF, OUTPUT);
pinMode (pinG, OUTPUT);
pinMode (LED1, OUTPUT); // sets the LEDs as an OUTPUT
digitalWrite(LED1, LOW);
pinMode (LED2, OUTPUT);
digitalWrite(LED2, LOW);
pinMode (PIEZO, OUTPUT);
pinMode (pushBTN1, INPUT_PULLUP); // sets the push buttons as an INPUT
pinMode (pushBTN2, INPUT_PULLUP);
}
void one(){
//1 1111001
digitalWrite (pinA, HIGH);
digitalWrite (pinB, LOW);
digitalWrite (pinC, LOW);
digitalWrite (pinD, HIGH);
digitalWrite (pinE, HIGH);
digitalWrite (pinF, HIGH);
digitalWrite (pinG, HIGH);
}
void two(){
//2 0100100
digitalWrite (pinA, LOW);
digitalWrite (pinB, LOW);
digitalWrite (pinC, HIGH);
digitalWrite (pinD, LOW);
digitalWrite (pinE, LOW);
digitalWrite (pinF, HIGH);
digitalWrite (pinG, LOW);
}
void three(){
//3 0110000
digitalWrite (pinA, LOW);
digitalWrite (pinB, LOW);
digitalWrite (pinC, LOW);
digitalWrite (pinD, LOW);
digitalWrite (pinE, HIGH);
digitalWrite (pinF, HIGH);
digitalWrite (pinG, LOW);
}
void four(){
// 4 0011001
digitalWrite (pinA, HIGH);
digitalWrite (pinB, LOW);
digitalWrite (pinC, LOW);
digitalWrite (pinD, HIGH);
digitalWrite (pinE, HIGH);
digitalWrite (pinF, LOW);
digitalWArite (pinG, LOW);
delay(1000);
}
void five(){
// 5 0010010
digitalWrite (pinA, LOW);
digitalWrite (pinB, HIGH);
digitalWrite (pinC, LOW);
digitalWrite (pinD, LOW);
digitalWrite (pinE, HIGH);
digitalWrite (pinF, LOW);
digitalWrite (pinG, LOW);
}
void six(){
// 6 0000010
digitalWrite (pinA, LOW);
digitalWrite (pinB, HIGH);
digitalWrite (pinC, LOW);
digitalWrite (pinD, LOW);
digitalWrite (pinE, LOW);
digitalWrite (pinF, LOW);
digitalWrite (pinG, LOW);
}
void seven(){
// 7 1111000
digitalWrite (pinA, LOW);
digitalWrite (pinB, LOW);
digitalWrite (pinC, LOW);
digitalWrite (pinD, HIGH);
digitalWrite (pinE, HIGH);
digitalWrite (pinF, HIGH);
digitalWrite (pinG, HIGH);
}
void eight(){
// 8 1111111
digitalWrite (pinA, LOW);
digitalWrite (pinB, LOW);
digitalWrite (pinC, LOW);
digitalWrite (pinD, LOW);
digitalWrite (pinE, LOW);
digitalWrite (pinF, LOW);
digitalWrite (pinG, LOW);
}
void nine(){
// 9 1001111
digitalWrite (pinA, LOW);
digitalWrite (pinB, LOW);
digitalWrite (pinC, LOW);
digitalWrite (pinD, LOW);
digitalWrite (pinE, HIGH);
digitalWrite (pinF, LOW);
digitalWrite (pinG, LOW);
}
void zero(){
// 0 0111111
digitalWrite (pinA, LOW);
digitalWrite (pinB, LOW);
digitalWrite (pinC, LOW);
digitalWrite (pinD, LOW);
digitalWrite (pinE, LOW);
digitalWrite (pinF, LOW);
digitalWrite (pinG, HIGH);
}
void stop(){
digitalWrite (pinA, HIGH);
digitalWrite (pinB, HIGH);
digitalWrite (pinC, HIGH);
digitalWrite (pinD, HIGH);
digitalWrite (pinE, HIGH);
digitalWrite (pinF, HIGH);
digitalWrite (pinG, HIGH);
}
void my_Action(int countdown10){
myCounter++;
Serial.println(myCounter);
if (countdown10 == 1){
one();
} else if (countdown10 == 2){
two();
} else if (countdown10 == 3){
three();
} else if (countdown10 == 4){
four();
} else if (countdown10 == 5){
five();
} else if (countdown10 == 6){
six();
} else if (countdown10 == 7){
seven();
} else if (countdown10 == 8){
eight();
} else if (countdown10 == 9){
nine();
} else if (countdown10 == 0){
zero();
}
}
void loop() {
activationMode = GetToggleSwitchState(); // must be executed all the time
execute_if_Active(activationMode);
}
bool GetToggleSwitchState(){
// "static" makes variables persistant over function calls
static bool toggleState = false;
static bool lastToggleState = false;
static byte buttonStateOld = unPressed;
static unsigned long buttonScanStarted = 0;
unsigned long buttonDebounceTime = 50;
unsigned long buttonDebounceTimer;
byte buttonStateNew;
if ( TimePeriodIsOver(buttonDebounceTimer, buttonDebounceTime) ) {
// if more time than buttonDebounceTime has passed by
// this means let pass by some time until
// bouncing of the button is over
buttonStateNew = digitalRead(pushBTN1);
if (buttonStateNew != buttonStateOld){
// if button-state has changed
digitalWrite (LED1, HIGH);
if (buttonStateNew == unPressed) {
// if button is released
toggleState = !toggleState; //toggle state-variable
my_Action(counter);
counter = counter - 1;
} // the attention-mark is the NOT operator
} // which simply inverts the boolean state
} // !true = false NOT true is false
// !false = true NOT false is true
return toggleState;
}
void execute_if_Active(bool p_IsActivated) {
static bool lastIsActivated;
}
boolean TimePeriodIsOver (unsigned long &expireTime, unsigned long TimePeriod) {
unsigned long currentMillis = millis();
if (currentMillis - expireTime >= TimePeriod ) {
expireTime = currentMillis; // set new expireTime
return true; // more time than TimePeriod) has elapsed since last time if-condition was true
}
else return false;
delay(1000);
}
Also, I'm really grateful for you helping me. I'm sorry if it feels like your teaching me without me paying you. But if you can't help me with this, it's all good either way.