SInce you are using the statements in an infinite loop [ for(;;) ], the UART will send the value it reads continuously.
If the value has to be printed via UART only once when the button is pushed, you may use an Interrupt component.
Setting it to Rising edge or Falling edge triggered as per your requirement will enable you to send the value on the required edge. Holding the botton for a longer time will not result in sending of more than one value via UART.
This is one way to overcome this issue.
Awesome! Thanks, I will try both methods and see type of result best suit us. Thanks for your help!!
Do you mean to create a new interrupt component or the interrupt component in UART? More clarification please. Thanks.
P.S. I had done both ways and I still get the same result.
My suggestion was to use an ISR Component connected to the Button_Pin. The schematic will look something like this.
Assume that the Button is connected to ground when pushed, and is left floating when not pushed. Then for this logic, the drive mode of the pin should be configured as Resistive Pull-Up.
In the ISR routine, a flag can be set. In the infinite loop of the main( ) function, the flag can be polled. When the flag is set, data is transmitted through UART and flag is reset. This way, data is transmitted only once upon push of the button.
For the data to be transmitted again, the button should be pressed again.
The ISR can be configured to be Rising Edge Triggered.