I cant get my buttons to work

Hi,

I have recently gotten into coding and wanted to make a small jeopardy game on the Arduino. I came up with a small concept that can be made into the game easily. I noticed that the buttons do not work on this concept and have been stumped on it, unable to go forward.

I will greatly appreciate any help

sketch_nov27a.ino (569 Bytes)

OPs code.

int tPin = 3;
int fPin = 4;

int but;

void setup() {
Serial.begin(9600);

pinMode(tPin, INPUT_PULLUP);
pinMode(fPin, INPUT_PULLUP);


}

void loop() {
int BSt = digitalRead(tPin);
int BSf = digitalRead(fPin);

if (BSt == LOW) {
  
  int but = 1;
  
} else if (BSf == LOW) {

int but = 2;
  
} else {
  int but = 0;
}

if (but == 1) {
  but1();
} else if (but == 2) {
  but2();
} else {
  loop();
}



}

void but1() {

  Serial.print("Button 1 is working");
}

void but2() {

  Serial.print("Button 2 is working");
}

can you fill in what you mean by "not working". What happens? How does that compare with what you wanted? Those are pretty important details.

if (BSt == LOW) {
  
  int but = 1;
  
}

Google "C++ scope".

This variable called but is created and gets a value and is immediately destroyed as it goes out of scope. I don't think you want to create a new variable here. I think you want to use the one at global scope that you already have named "but". If so, lose the "int"

} else {
  loop();

No. Never ever ever ever ever call loop. Just let the function end and loop will be called again by main. If you call it then the last iteration never ends. This is called recursion and will crash your program. You should NEVER be calling loop() unless you know EXACTLY what you are doing.