Can I program a process to run from being triggered until my arduino is off?

Hello! I am trying to figure out what line of code I would need to have something run until the Arduino is powered off. Let me explain.

I want a button to trigger a piezo buzzer, but I don't want the buzzer to turn off, even if the button is pressed again, leaving the only way to turn it off being the powering down of the Arduino.

Any idea where to start or helpful links? Thanks!

You are really overthinking this...

const byte BuzzerPin = 13;
const byte ButtonPin = 10;

void setup(){
  pinMode(BuzzerPin, OUTPUT);
  pinMode(ButtonPin, INOUT_PULLUP);
}

void loop(){
  if(!digitalRead(ButtonPin)){
    digitalWrite(BuzzerPin, HIGH);
  }
}

septillion: You are really overthinking this...

const byte BuzzerPin = 13;
const byte ButtonPin = 10;

void setup(){   pinMode(BuzzerPin, OUTPUT);   pinMode(ButtonPin, INOUT_PULLUP); }

void loop(){   if(!digitalRead(ButtonPin)){     digitalWrite(BuzzerPin, HIGH);   } }

Well, I am a newbie, haha. Thank you!

It's like, if you don't want the Arduino to turn it off, then don't write code to turn it off. It's that simple.

most people use a concept called a flag. A flag can be named anything you like so on this example we will call it a hold. The flag should be assigned global so it will not be reset

byte hold=0;

setup(){ }

loop(){

if (buttonPressed==1 ){ hold=1; }

If (hold==1){ //buzzer on }

as hold is changed to 1 and no other code resets hold back to zero other than restarting the arduino the buzzer will stay on

Why would you set a flag? It works, but what is the point if you simply don't care after turning on the buzzer? also, you can simply read the BuzzerPin to check if the buzzer is turned on of not... Now you relay on the fact the flag and the buzzer do actually match.

septillion: Now you relay on the fact the flag and the buzzer do actually match.

No. The snipped forces the buzzer to stay on if the flag is set.

If (hold==1){
//buzzer on
}

the usual

} else {
//buzzer off
}

is not needed here, because hold will never become false again.

That's true for this simple example. But you really on the fact you have that piece of code. And for this example it seems to be way over complicated to set a flag if all you do with it is to turn on a buzzer. All in all, the use of the Arduino here is already over complicated... A button to the set of a flipflop does the same...

septillion: But you really on the fact you have that piece of code.

Shure, I always rely on the code I write. ;)

The answer 'just start and don't terminate the process' would not have helped the OP (IMHO).

Using the flag method uncouples the button event from the state 'buzzer on', while your code ties the buzzer to the button so it will only buzz while the button stays pressed.

theres are 2 types of buzzer. One is a digital output which once set will stay on and a second one that will require the code to make the tone. As the OP didn't mention which type he is using I just wrote something simple that will work with either type