Attach interrupt and serial print once issue

Hi, i have a code that uses a push button and a attach interrupt, the problem i have encountered is that when i select a character it sends the character more than once as i cannot physically press the button in for a short enough amount of time to send it once. is there a way to send the a character once with the attach interrupt ?

Code is Attached

:o

Arduino_code.ino (13.7 KB)

attachInterrupt(digitalPinToInterrupt(2), ButtonClick,LOW);

It is almost never appropriate to use LOW. Use CHANGE to get called whenever the pin changes state, or RISING or FALLING whenever the pin goes HIGH or goes LOW.

    x_position= ++x_position;
  }
   else if (mouse_Lft > 0)
  {
    x_position= --x_position;

Wrong. Use either x = x + 1 OR x++ OR ++x. Do NOT try to combine ++ and = in the same statement. The behavior is undefined. You do NOT want undefined behavior in your code.

Serial.print as well as delay are a No-No in ISRs.

Your ISR has only these statements.

void ButtonClick(){

  Serial.print(AlphaNum);
  
  delay(100);
}
char A,a,B,b,C,c,D,d,E,e,F,f,G,g,H,h,I,i,J,j,K,k,L,l,M,m,N,n,O,o,P,p,Q,q,R,r,S,s,T,t,U,u,V,v,W,w,X,x,Y,y,Z,z;
char One, Two, Three, Four, Five, Six, Seven, Eight, Nine;

Did you ever hear of arrays?

Serial.print as well as delay are a No-No in ISRs.

Actually, with the latest IDE, Serial.print() is OK. The HardwareSerial class re-enables the appropriate interrupt handlers, so that serial data can be shifted out.

It's nevertheless a bad idea. IMHO

And I personally dislike this fix.

Whandall: char A,a,B,b,C,c,D,d,E,e,F,f,G,g,H,h,I,i,J,j,K,k,L,l,M,m,N,n,O,o,P,p,Q,q,R,r,S,s,T,t,U,u,V,v,W,w,X,x,Y,y,Z,z; char One, Two, Three, Four, Five, Six, Seven, Eight, Nine;

Did you ever hear of arrays?

Holy, even if you make it an array, what is the idea behind 52 badly names variables? ::)

Whandall: It's nevertheless a bad idea. IMHO

And I personally dislike this fix.

Won't get any argument from me.

Thanks Paul i made the changes that you suggested to the interrupt and it now works fine, the x_position= ++x_position; worked fine but iv changed it to your suggestion, the code works how i want it to now cheers.

hi, Whandall those char are not used so iv deleted them.

PaulS: Actually, with the latest IDE, Serial.print() is OK. The HardwareSerial class re-enables the appropriate interrupt handlers, so that serial data can be shifted out.

That seems even more crazy than calling Serial from within an ISR. What were they thinking would happen to the stuff that was expected to be halted during the ISR.

Probably comes from the same school of thought as "yield()" within the delay() function.

...R