Andrew Ferguson / Mbed 2 deprecated Ferguson_A2_Digital_Input

Dependencies:   mbed

Committer:
a_ferguson
Date:
Thu Oct 06 14:44:47 2022 +0000
Revision:
2:af576070220a
Parent:
1:c186d9b63164
Child:
3:896a932d47fc
Pretty much everything works but sometimes it doesn't count a press if too quick.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
a_ferguson 0:1cf58db95ce6 1 #include "mbed.h"
a_ferguson 0:1cf58db95ce6 2
a_ferguson 0:1cf58db95ce6 3 Serial pc(USBTX,USBRX); //do not need when we aren't communicating with pc
a_ferguson 1:c186d9b63164 4 DigitalOut myled1(LED1);
a_ferguson 1:c186d9b63164 5 DigitalOut myled2(LED2);
a_ferguson 0:1cf58db95ce6 6 DigitalIn button(p17); // button attached to p17
a_ferguson 0:1cf58db95ce6 7
a_ferguson 0:1cf58db95ce6 8 int main() {
a_ferguson 0:1cf58db95ce6 9
a_ferguson 2:af576070220a 10 int currentstate=0;
a_ferguson 2:af576070220a 11 int previousstate=0;
a_ferguson 2:af576070220a 12 int counter=0;
a_ferguson 2:af576070220a 13
a_ferguson 0:1cf58db95ce6 14 while(1) {
a_ferguson 2:af576070220a 15
a_ferguson 2:af576070220a 16 currentstate=button.read(); // set current state
a_ferguson 2:af576070220a 17
a_ferguson 2:af576070220a 18 if (currentstate==1){ //blinks 1s interval when pressed
a_ferguson 1:c186d9b63164 19 myled2=!myled2;
a_ferguson 1:c186d9b63164 20 wait(1);
a_ferguson 2:af576070220a 21
a_ferguson 2:af576070220a 22 if (currentstate==previousstate){
a_ferguson 2:af576070220a 23 }
a_ferguson 2:af576070220a 24
a_ferguson 2:af576070220a 25 else{ // change in state adds counter
a_ferguson 2:af576070220a 26 counter=counter+1;
a_ferguson 2:af576070220a 27 pc.printf("count=%i \r\n",counter);
a_ferguson 2:af576070220a 28 }
a_ferguson 2:af576070220a 29
a_ferguson 1:c186d9b63164 30 } //if
a_ferguson 2:af576070220a 31
a_ferguson 2:af576070220a 32 else { //blinks 0.3s interval when released
a_ferguson 1:c186d9b63164 33 myled2=!myled2;
a_ferguson 1:c186d9b63164 34 wait(0.3);
a_ferguson 2:af576070220a 35
a_ferguson 2:af576070220a 36 if (currentstate==previousstate){
a_ferguson 2:af576070220a 37 }
a_ferguson 2:af576070220a 38
a_ferguson 2:af576070220a 39 else{ // change in state adds counter
a_ferguson 2:af576070220a 40 counter=counter+1;
a_ferguson 2:af576070220a 41 pc.printf("count=%i \r\n",counter);
a_ferguson 2:af576070220a 42 }
a_ferguson 2:af576070220a 43
a_ferguson 2:af576070220a 44 } //else
a_ferguson 2:af576070220a 45
a_ferguson 2:af576070220a 46 if (counter==10){ // counter reaches 10 turns on led1
a_ferguson 2:af576070220a 47 myled1=1;
a_ferguson 1:c186d9b63164 48 } //if
a_ferguson 2:af576070220a 49
a_ferguson 2:af576070220a 50 previousstate=currentstate; //set previous state
a_ferguson 2:af576070220a 51
a_ferguson 1:c186d9b63164 52 } //while
a_ferguson 2:af576070220a 53
a_ferguson 1:c186d9b63164 54 } //main