Andrew Ferguson / Mbed 2 deprecated Ferguson_A2_Digital_Input

Dependencies:   mbed

Revision:
2:af576070220a
Parent:
1:c186d9b63164
Child:
3:896a932d47fc
--- a/main.cpp	Thu Oct 06 14:05:24 2022 +0000
+++ b/main.cpp	Thu Oct 06 14:44:47 2022 +0000
@@ -7,15 +7,48 @@
 
 int main() {
 
+int currentstate=0;
+int previousstate=0;
+int counter=0;
+
     while(1) {
-        if (button.read()==1){
+        
+        currentstate=button.read(); // set current state
+        
+        if (currentstate==1){ //blinks 1s interval when pressed
             myled2=!myled2;
             wait(1);
+            
+            if (currentstate==previousstate){
+                }
+                
+            else{ // change in state adds counter
+                counter=counter+1;
+                pc.printf("count=%i \r\n",counter);
+                }
+                
             } //if
-        if (button.read()==0){
+            
+        else { //blinks 0.3s interval when released
             myled2=!myled2;
             wait(0.3);
+            
+            if (currentstate==previousstate){
+                }
+                
+            else{ // change in state adds counter
+                counter=counter+1;
+                pc.printf("count=%i \r\n",counter);
+                }
+            
+            } //else
+        
+        if (counter==10){ // counter reaches 10 turns on led1
+            myled1=1;
             } //if
-
+        
+        previousstate=currentstate; //set previous state
+        
     } //while
+    
 } //main