How to Program Specific Button Sequence and Others

Hello all, so I was having trouble with coding a project I was doing. I am in the process of learning C++ and right now I am programming something that requires more than the little knowledge I possess at the moment and I was hoping I could get some help or have someone point me in the right direction.

My project consists of a buzzer and two push buttons and my idea of how it would work was: if you push button one, it would trigger the buzzer and it would stay on for the duration, whether the button is held down or not and after the buzzer goes off button one should do nothing. If button two is pushed after the buzzer has been triggered by the first button, it will silence the buzzer, but only for as long as the button is held down. Then, if you reset the arduino or kill power to the circuit, the program restarts and awaits the press of button one.

So here was my attempt at conjuring up a set of code for my project, which obviously didn't work the way I actually wanted it to:

int buz = 1;
int but = 2;
int but2 = 3;
int val;
int val2;

void setup() {
  pinMode(buz, OUTPUT);
  pinMode(but, INPUT);
  pinMode(swi, INPUT);
}
void loop() {
 val = analogRead(but);
 val2 = analogRead(swi);
  if (val == LOW && val2 == LOW); {
    digitalWrite(buz, LOW);
  }
  if (val == LOW && val2 == HIGH); {
    digitalWrite(buz, LOW);
  }
  if (val == HIGH && val2 == HIGH); {
    digitalWrite(buz, LOW);
  }
  if (val == HIGH && val2 == LOW); {
    digitalWrite(buz, HIGH);
  }
}

Why are you analog reading buttons?

if (val == LOW && val2 == LOW); {

We typically don't put semicolons after if statements...

AJ: My project consists of a buzzer and two push buttons and my idea of how it would work was: if you push button one, it would trigger the buzzer and it would stay on for the duration, whether the button is held down or not and after the buzzer goes off button one should do nothing.

This tells me that you want something in setup that waits (blocks) until the first switch is pressed. Only then, should you move into the loop which only handles the second switch.

I can see button1 in the setup(), **pinMode(but, INPUT); ** but not button2. Instead, you have pinMode(swi, INPUT); which is not the same as but2

Also as Arrch pointed out, why are you using analogRead and not digitalRead like you should be using?

Being that you want button1 to not do anything after button2 is pressed, I would think you need to make a “lockout logic”.

Pseudo:

Boolean lockout; // set globally as false

if( button1 == HIGH && lockout == false)
 {
  /* buzzer on */
 }

if(button2 == HIGH)
 {
  /* buzzer off */
  lockout = true;
 }

HazardsMind: I can see button1 in the setup(), pinMode(but, INPUT); ** but not button2. Instead, you have pinMode(swi*, INPUT); which is [u]not[/u] the same as **but2*

Also as Arrch pointed out, why are you using analogRead and not digitalRead like you should be using?

Sorry, last night I changed up my code and tried to use the buttons as analog inputs before I gave up and came for help. I thought I didn't save those changes, but I guess I might've by accident. I also changed a switch in my circuit to a button and changed the name to but2 when posting the code the on here, but looks like I missed one. Thanks for the help, guys, I'll see if I can get it to work.