![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Coursework
Diff: main.cpp
- Revision:
- 13:034ffa96643d
- Parent:
- 12:ceea378d5691
- Child:
- 14:2eddfd5fc1ce
diff -r ceea378d5691 -r 034ffa96643d main.cpp --- a/main.cpp Sat Jan 08 17:35:22 2022 +0000 +++ b/main.cpp Sat Jan 08 23:26:11 2022 +0000 @@ -4,6 +4,7 @@ #include "hcsr04.h" #include "Piezo.h" #include "string" +#include "PinDetect.h" // FSM Menu // defines directions as 0/1. Note UPPERCASE #define UP 0 @@ -22,7 +23,9 @@ HCSR04 sensor(D14, D15); Piezo Buzzer(PTC10); //Bringing in buttons -InterruptIn buttonA(PTB9); +//InterruptIn buttonA(PTB9); +PinDetect buttonA(PTB9, PullDown ); + InterruptIn buttonB(PTD0); InterruptIn buttonX(PTC17); InterruptIn buttonY(PTC12); @@ -50,6 +53,7 @@ void buttonB_isr(); // Button B interrupt service routine void buttonX_isr(); // Button X interrupt service routine void buttonY_isr(); // Button Y interrupt service routine +int Presscount(); void buttonStart_isr(); // Button Start interrupt service routine void buttonBack_isr(); // Button Back interrupt service routine void buttonLeft_isr(); // Button Left interrupt service routine @@ -63,6 +67,10 @@ volatile int g_buttonLeft_flag = 0; // Global Flag volatile int g_buttonRight_flag = 0; // Global Flag + + + + //volatile int g_buttonA_counter = 0; // Global counter //Test function after research C++ lanuage 05/01/22 int cube(int num){ @@ -99,12 +107,14 @@ int main() { //initialise Joystic joystick.init(); - + Timer t; + t.start(); + buttonA.setSampleFrequency(20); // create a thread that'll run the event queue's dispatch function Thread eventThread; eventThread.start(callback(&queue, &EventQueue::dispatch_forever)); - buttonA.rise(queue.event(&buttonA_isr)); // This is used to call the function putting it in queue allowing the screen toupdat no muxt error + buttonA.attach_asserted(&buttonA_isr); // This is used to call the function putting it in queue allowing the screen toupdat no muxt error buttonB.rise(queue.event(&buttonB_isr)); buttonX.rise(queue.event(&buttonX_isr)); buttonY.rise(queue.event(&buttonY_isr)); @@ -116,7 +126,7 @@ lcd.init(); // Button A is connected between the pin and 3.3 V, we therefore need to turn on the internal pull-down resister - buttonA.mode(PullDown); + // buttonA.mode(PullDown); // Button B is connected between the pin and 3.3 V, we therefore need to turn on the internal pull-down resister buttonB.mode(PullDown); // Button X is connected between the pin and 3.3 V, we therefore need to turn on the internal pull-down resister @@ -297,12 +307,25 @@ } } } + int Presscount(){ + int currenttime; + int lasttime; + + + Press++; + return Press; + } //Button A event-triggered interrupt void buttonA_isr() { - - Press++; - + + Presscount(); + + + if(Press>3){ + Press = 0; + } + g_buttonA_flag = 1; // set flag in ISR printf("press count = %i\n",Press);