input is detected for a longer time than the actual input time

hi; i have been struggling for a couple of days with this simple problem: a function (sound) is supposed to be triggered when an input is turned on, or when a input changes (i have tried both with same results). the function simply turns on an off the audio output at intervals determined by the note, so is looped suposedly as long as there is input signal. the thing is that the sound keeps going on around 1/3 of the total time pressed, as if it has been queuing the task...
here i paste the concerned pieces:

int ledPin = 0;
int triggeri=A3;
int triggero= 2;
int speakerOut = 1;
//
void setup() {
  pinMode(ledPin, OUTPUT); 
  pinMode(triggeri, INPUT); 
  pinMode(triggero, OUTPUT);
}
//
void loop() {

  analogWrite(speakerOut, 0); 
  if (digitalRead(triggeri)){
    S();
  }
void S(){


  //for (count = 0; count < MAX_COUNT; count++) {
  statePin = !statePin;
  digitalWrite(ledPin, statePin);
  for (count2=0;count2<8;count2++) {

    analogWrite(speakerOut,500);
    delayMicroseconds(tones[nota]);
    analogWrite(speakerOut, 0);
    delayMicroseconds(tones[nota]);
  }

Well, its true that the S function has two delays, but those are just microseconds! when the loop repeats and the button is not pressed, it souldnt run S(); but looks like it does!
i have tried many differend

How can triggerX be an output?

Why is S() (what a lousy name) declared inside loop()?

Why don't you post some code that actually compiles?

Perhaps your trigger input is floating.
But please, post your code and a schematic.

Ok. i'm the worst person in programming. ill paste the whole thing because it takes me long to make something new compilable; anyway, attention is to be placed in the parts cited before,
The problem is not to be attributed to any syntax mistake since i've been rewriting the thing a lot of times.

int ledPin = 0;//quiza el trigger se hace cuando ledpin se apaga, ahorrando una salida :)
int triggeri=A3;
int triggero= 3;
int speakerOut = 2;


byte names[] = {
  'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'};  
int nota=2;
int tones[] = {
  1915, 1700, 1519, 1432, 1275, 1136, 1014, 956};
//byte melody = "d";

// count length: 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
//                                10                  20                  30

int count=0;
int count1 = 0;
int count2 = 0;

int previoustriggerinput=0;

int sonando=3;
int trigando=0;


int MAX_COUNT = 1;
int statePin = LOW;

void setup() {
  pinMode(ledPin, OUTPUT); 
  pinMode(triggeri, INPUT); 
  pinMode(triggero, OUTPUT);
}

void loop() {

  analogWrite(speakerOut, 0); 

  /*while (digitalRead(triggeri)){
   
   }*/
  if (digitalRead(triggeri)){
    S();
  }

  if (digitalRead(triggeri)<previoustriggerinput){
    sonando=2;
    trigando=300;

  }
  /*
  if(sonando>0){
   R();
   sonando-=1;
   }
   else {
   
   }
   */
  
  if(trigando>0){
    digitalWrite(triggero, HIGH);
    trigando--;
  }
  else{
    digitalWrite(triggero, LOW);
  }

  int previoustriggerinput=digitalRead(triggeri);

}
void S(){


  //for (count = 0; count < MAX_COUNT; count++) {
  statePin = !statePin;
  digitalWrite(ledPin, statePin);
  for (count2=0;count2<8;count2++) {

    analogWrite(speakerOut,500);
    delayMicroseconds(tones[nota]);
    analogWrite(speakerOut, 0);
    delayMicroseconds(tones[nota]);
  }

  // }

}
void R(){

  /*for (count = 0; count < MAX_COUNT; count++) {
   
   statePin = !statePin;
   digitalWrite(ledPin, statePin);
   for (count1=0;count1<8;count1++) {
   analogWrite(speakerOut,count1);
   delayMicroseconds(tones[nota]);
   analogWrite(speakerOut, 0);
   delayMicroseconds(tones[nota]);
   }
   
   }*/

}

So, what is connected to the pin identified in triggeri, and how is that wired? It really sounds like a floating pin condition.

The problem is not to be attributed to any syntax mistake since i've been rewriting the thing a lot of times.

If the code compiles, there are no syntax errors. If it does, you should not have posted it.

  if (digitalRead(triggeri)<previoustriggerinput){

The digitalRead() function returns HIGH (1) or LOW (0). How can the value be less than something, unless it is LOW and the something is HIGH?

  int previoustriggerinput=digitalRead(triggeri);

}

A local variable with the same name as a global variable, that immediately goes out of scope, is worthless. You might as well delete this line for all the good that it does.

Which means, of course, that you never actually set previoustriggerinput to a new value, so the name is wrong, because it ISN'T a previous value of anything.

If you think I am picking on your choice of names, it is because I am. Good names make it easy to remember what is in the variable, and make it obvious what variable should be used where. Poor names make it harder to understand code. Since you need to understand it to write it, poor names make it harder to write the code.

Function names, in particular, should be meaningful, so you know you are calling the write function. With names like S(), T(), U(), V(), hoe do you know which one to call to get the right stuff to happen?

i promise i will be tidier from now on :sweat_smile:.
the embarrasing re-declaration (int previoustrigger) solved another problem i had, but the timing problem persists. i also switched the trigger detection into analog read in order to compare well the change.
i would attribute also the problem to a lousy cable if the problem wasnt so strictly persistent. here im attaching a fritzig also...
thank you for your answers so far

i also switched the trigger detection into analog read in order to compare well the change.

Reading a digital device (a switch is on or off) on an analog pin is not going to help.

The problem IS a floating pin. When the switch is not pressed, there is no voltage into the pin, but it is not strongly pulled down.

You should connect the switch to ground and to a digital pin. Then, enable the pullup resistor (look up what that means and how to do it). Then, the pin will be HIGH when the switch is not pressed, and LOW when it is pressed, with no waffling around in between.

wowww! that completely changed the behaviour.. i had no idea what floating pin meant, i thought it was about a loose connection.
i connected the input directly into ground using a resistor, and the 5v at the other side of the button.. that is the official way to do it, isnt it?
and where was that energy stored, to keep the voltage up when it was not being given any?
well, thank you very much for your help. i hope this post helps any other lost diy's
thanks!

you can't connect anything directly via a resistor.
Lose the resistor, connect the switch to ground and to the input and use the built in pullup..