2 years, 7 months ago.

digital interrupts, printf

Hi I am trying to figure out an experiment. The InterruptIn interface is used to configure digital I/O pins as an interrupt input. Configure the pins available on the Joystick as InterruptIn and using a printf statement to print the direction in which the Joystick is pushed.

I have the code working but there is too many printfs coming onto the terraterm is there a way to limit this. eg I press the joystick up and it comes onto the terra term 11 times

I have a #include "mbed.h"

Serial pc(USBTX,USBRX); initilizes output to Tera Term InterruptIn DN(p12); assigns DN (down)to pin 12 InterruptIn LH(p13); assigns LH (left hand)to pin 13 InterruptIn CN(p14); assigns CN (centre) to pin 14 InterruptIn UP(p15); assigns UP (up) to pin 14 InterruptIn RH(p16); assigns RH (right hand)to pin 16

int main() { main function printf("Hello\r\n"); print function hello

while(1) { while true if (DN==1) if down(p12) is pressed printf("Down Pressed\r\n"); print down press

else if (LH==1) or if left(p13) is pressed print left pressed printf("Left Pressed\r\n");

else if (CN==1) printf("Centre Pressed\r\n");

else if (UP==1) printf("Up Pressed\r\n");

else if (RH==1) printf("Right Pressed\r\n"); wait(2); } }

is there any hardware solution

1 Answer

2 years, 7 months ago.

Well you could try using interrupts.

You set the pins as interrupts but then use them as if they were standard digital inputs.

Or you could set a variable to indicate the current state and then only print something if the state has changed.

I'm not going to supply code on how to do either since this is clearly a homework question but if you make an attempt at either of those solutions and hit problems then I'm sure people will be happy to help you debug it.

When posting code please use

<<code>>
your code here
<</code>>

So that the code is shown with the correct formatting.

Thanks do you know why the mbed prints the same thing 11 times.

posted by David Houlihan 19 Apr 2019

It'll print every 2 seconds while the button is down.

posted by Andy A 19 Apr 2019