Button saving note (help)

Hello guys, I'm new here and i wanted to know if i can save notes with a push of a button (i have difficulties with the code)
so i have a board with 3 buttons and a buzzer and i wanted to know if i can like save the buzzer note with the first button press and the other press for the second button and for the third is like a play button that plays what I've played with the other two (ive already write a code but it didnt work)

Of course you can do that. Post your code

im sorry but can you just write me a small sketch even if its untested because i rewrite my old one and its completely different from what i was asking (sorry)

Hello mamohalla

Welcome to the worldbest Arduino forum ever.

This is a nice project to get started.

Keep it simple and stupid firstly.

Follow the example code that comes with the library or
run some tutorials for the hardware selected.
If you are happy with the results of the tutorials you can merge these to your project.

Have a nice day and enjoy coding in C++.

Have a nice day and enjoy coding in C++.

thank you so much

here is the code tho if u wanna see it

int buttonL = 3;
int buttonR = 6;
int buttonP = 8;
int buttonC = 10;

int StatusbuttonL = 0;
int StatusbuttonR = 0;
int StatusbuttonP = 0;
int StatusbuttonC = 0;

String Histo = "";

int red = 4;
int green = 2;
int yellow = 9;

bool onPlayMode = false;

void setup() {
  // put your setup code here, to run once:

  pinMode(buttonL, INPUT);
  pinMode(buttonR, INPUT);
  pinMode(buttonP, INPUT);
  pinMode(buttonC, INPUT);

  pinMode(red, OUTPUT);  
  pinMode(green, OUTPUT); 
  pinMode(yellow, OUTPUT); 

  Serial.begin(9600);

}

void loop() {
  // put your main code here, to run repeatedly:
  StatusbuttonL = digitalRead(buttonL);
  StatusbuttonR = digitalRead(buttonR);
  StatusbuttonP = digitalRead(buttonP);
  StatusbuttonC = digitalRead(buttonC);

  if(StatusbuttonL==HIGH){
    digitalWrite(green,HIGH);
    digitalWrite(red,LOW);
    digitalWrite(yellow,LOW);             
    Histo = Histo + "1";
  }

    if(StatusbuttonR==HIGH){
    digitalWrite(red,HIGH);
    digitalWrite(green,LOW);
    digitalWrite(yellow,LOW);
    Histo = Histo + "2";
  }
   if(StatusbuttonC==HIGH){
    digitalWrite(yellow,HIGH);
    digitalWrite(red,LOW);
    digitalWrite(green,LOW);
    Histo = Histo + "3";

   }

  if(StatusbuttonP==HIGH){
    if(onPlayMode==false){
      onPlayMode = true;
      PlayHisto();
    }

  }

  Serial.println(Histo);
  delay(500);

}

void PlayHisto(){
  digitalWrite(red,LOW);
  digitalWrite(green,LOW);
  digitalWrite(yellow,LOW);
  delay(500);
  for (int i = 0; i <= (Histo.length()-1); i++) {
      digitalWrite(red,LOW);
      digitalWrite(green,LOW);
      digitalWrite(yellow,LOW);
    Histo.substring(i, (i+1));
    Serial.println(Histo.substring(i, (i+1)));
    if(Histo.substring(i, (i+1))=="1"){
      digitalWrite(green,HIGH);
      digitalWrite(red,LOW);
      digitalWrite(yellow,LOW);
    delay(500);
    }
    if(Histo.substring(i, (i+1))=="2"){
      digitalWrite(red,HIGH);
      digitalWrite(green,LOW);
      digitalWrite(yellow,LOW);
      delay(500);
    }
    if(Histo.substring(i, (i+1))=="3"){
      digitalWrite(yellow,HIGH);
      digitalWrite(red,LOW);
      digitalWrite(green,LOW);
      delay(500);
    }
    if((i+1)==Histo.length()){
      onPlayMode = false;
    }
  }
  
}

Hi @mamohalla,

I have modified your sketch a little bit and it works as a start point for further development:

/*
  Forum: https://forum.arduino.cc/t/button-saving-note-help/1163834/4
  Wokwi: https://wokwi.com/projects/374972054606400513

*/

constexpr byte buttonL = 3;
constexpr byte buttonR = 6;
constexpr byte buttonP = 8;
constexpr byte buttonC = 10;

String Histo = "";

constexpr byte red = 4;
constexpr byte green = 2;
constexpr byte yellow = 9;

bool onPlayMode = false;

void setup() {
  // put your setup code here, to run once:

  pinMode(buttonL, INPUT_PULLUP);
  pinMode(buttonR, INPUT_PULLUP);
  pinMode(buttonP, INPUT_PULLUP);
  pinMode(buttonC, INPUT_PULLUP);

  pinMode(red, OUTPUT);  
  pinMode(green, OUTPUT); 
  pinMode(yellow, OUTPUT); 

  Serial.begin(9600);

}

void loop() {
  // put your main code here, to run repeatedly:
  
  if(digitalRead(buttonL) == LOW){
    setLedsGRY(HIGH,LOW,LOW);
    Histo = Histo + "1";
    delay(20); // poor man's debouncing
    while(!digitalRead(buttonL)){};
  }

  if(digitalRead(buttonR) == LOW){
    setLedsGRY(LOW,HIGH,LOW);
    Histo = Histo + "2";
    delay(20); // poor man's debouncing
    while(!digitalRead(buttonR)){};
  }
  if(digitalRead(buttonC) == LOW){
    setLedsGRY(LOW,LOW,HIGH);
    Histo = Histo + "3";
    delay(20); // poor man's debouncing
    while(!digitalRead(buttonC)){};
   }

  if(digitalRead(buttonP) == LOW){
    if(onPlayMode==false){
      onPlayMode = true;
      PlayHisto();
    }
  }
}

void setLedsGRY(boolean g, boolean r, boolean y){
    digitalWrite(green,g);
    digitalWrite(red,r);
    digitalWrite(yellow,y);             
}

void PlayHisto(){
  Serial.println("Play Histo");
  setLedsGRY(LOW,LOW,LOW);
  delay(500);
  for (int i = 0; i <= (Histo.length()-1); i++) {
    setLedsGRY(LOW,LOW,LOW);
    Histo.substring(i, (i+1));
    Serial.println(Histo.substring(i, (i+1)));
    if(Histo.substring(i, (i+1))=="1"){
    setLedsGRY(HIGH,LOW,LOW);
    delay(500);
    }
    if(Histo.substring(i, (i+1))=="2"){
      setLedsGRY(LOW,HIGH,LOW);
      delay(500);
    }
    if(Histo.substring(i, (i+1))=="3"){
      setLedsGRY(LOW,LOW, HIGH);
      delay(500);
    }
    if((i+1)==Histo.length()){
      onPlayMode = false;
      setLedsGRY(HIGH,HIGH,HIGH);
      delay(500);
      setLedsGRY(LOW,LOW,LOW);

    }
  }
  
}

See the simulation on Wokwi: https://wokwi.com/projects/374972054606400513

This is the wiring (I used the same pins as you):

There is still a lot of potential for optimization considering repetitive code sequences and also debouncing of buttons, but that might be an issue for future steps ...

Just as an example for optimisation: This works also to play the history:

void PlayHisto() {
  Serial.println("Play Histo");
  Serial.println(Histo);
  Serial.println(Histo.length());
  Serial.println("----------------");
  setLedsGRY(LOW, LOW, LOW);
  delay(500);
  for (int i = 0; i < Histo.length(); i++) {
    char c = Histo[i];
    Serial.print(c);
    switch (c) {
      case '1' : setLedsGRY(HIGH, LOW, LOW);
        break;
      case '2' : setLedsGRY(LOW, HIGH, LOW);
        break;
      case '3' : setLedsGRY(LOW, LOW, HIGH);
        break;
    }
    delay(500);
  }
  Serial.println();
  onPlayMode = false;
  setLedsGRY(HIGH, HIGH, HIGH);
  delay(500);
  setLedsGRY(LOW, LOW, LOW);
}

So you do not need to use the substring() function and the code becomes more readable using the switch/case.

Good luck and have fun!

it helped I'm so thankful to you