dtmf.generate('3', 300); works but, I need a push in the right direction please

Hello I'm Sox, and would like a little help please.

I have put together this DTMF generator which works well but am having trouble as i have no codeing knowledge to achieve this despite reading tutorials.

I want the tone to continue all the time the button is pressed. I can make it produce the tone for a number of milliseconds but that's not what i require. Only while the button is pressed. Upon release stop dtmf.generate this tone ready for another or no tone. Had a fiddle with while or else and only got confused. Came to the conclusion i ought to ask people who know a better way or a push in the right direction.


This is the DTMF generator i expect most of you are aware of already.

https://create.arduino.cc/projecthub/dilshan-jayakody/generate-dtmf-tones-only-using-arduino-7d3713?f=1


This is the code i'm using at the moment as i try to understand.

#include "dtmfgen.h"
  DTMFGenerator dtmf;

  const int buttonPin = 8;  //the number of pushbutton:
 const int buttonPin2 = 9;
  
  int buttonState = 0; // variable for reading pushutton:
void setup()
{
  pinMode(buttonPin, INPUT);
  pinMode(buttonPin2, INPUT);
}
  
void loop()
{
  buttonState = digitalRead(buttonPin); //reads buttonstate:
  
  if (buttonState == HIGH){
  
  
 dtmf.generate('3', 300);   // Generate 300ms long DTMF tone for digit 3.
  }
  buttonState = digitalRead(buttonPin2);

  if (buttonState == HIGH){

    dtmf.generate('1', 300); // Generate 300ms DTMF tone for digit 1.
  }
  }

So how do i ask it to generate dtmf.generate('3') or any other tone and stop the tone when the button is released ready for another?

As i wish to add a few more buttons other than the two you can see in the sketch. Am using the button wiring from the tutorials here with resistor and works well.

Is it a buttonState i need to look at?

Respect to any answers and time spent looking at this request as i expect i'm missing the elephant in the room.

Regards Sox

You have done good so far, try while button is pushed in code? Also you can use the if (something == something) statement to control the tone generator. This response is to help you get started in solving your problem, not solve it for you. Good Luck & Have Fun! Gil

Thank you for your time gilshultz.

I have changed the code to using ‘while’ instead of ‘if’ which works but with the same results as i seem to have to add a duration after the output tone required for that button.

dtmf.generate(‘1’, 300);

I have tried replacing 300 with HIGH but all i got was a strange tone.

Am still missing something, perhaps enough brain cells lol.

#include "dtmfgen.h"
  DTMFGenerator dtmf;

  const int buttonPin = 8;  //the number of pushbutton:
 const int buttonPin2 = 9;
  
  int buttonState = 0; // variable for reading pushutton:
void setup()
{
  pinMode(buttonPin, INPUT);
  pinMode(buttonPin2, INPUT);
}
  
void loop()
{
  buttonState = digitalRead(buttonPin); //reads buttonstate:
  
  while (buttonState == HIGH){
  
 dtmf.generate('3', 300);   // Generate 300ms long DTMF tone for digit 3.

delay(10);

buttonState = digitalRead(buttonPin);

  }
  buttonState = digitalRead(buttonPin2);

  while (buttonState == HIGH){

    dtmf.generate('1', 300); // Generate 300ms DTMF tone for digit 1.

  delay(10);

  buttonState = digitalRead(buttonPin2);
  
  }
  }

Another push maybe required.

Regards Sox