Coursework

Committer:
sesa514652
Date:
Thu Jan 06 21:58:39 2022 +0000
Revision:
9:cdd7d9a123f9
Parent:
8:770d168713cc
Child:
10:42e70b596099
Interrupt causing issues when button is pressed

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sesa514652 0:1f799c7cce2b 1 #include "mbed.h"
sesa514652 0:1f799c7cce2b 2 #include "Joystick.h"
sesa514652 0:1f799c7cce2b 3 #include "N5110.h"
sesa514652 0:1f799c7cce2b 4 #include "hcsr04.h"
sesa514652 3:6ecb75a2675c 5 #include "Piezo.h"
sesa514652 8:770d168713cc 6 #include "string"
sesa514652 5:9b2c976ca318 7 // y x button
sesa514652 0:1f799c7cce2b 8 Joystick joystick(PTB10,PTB11,PTC16);
sesa514652 0:1f799c7cce2b 9 HCSR04 sensor(D14, D15);
sesa514652 3:6ecb75a2675c 10 Piezo Buzzer(PTC10);
sesa514652 8:770d168713cc 11 //Bringing in buttons
sesa514652 8:770d168713cc 12 InterruptIn buttonA(PTB9);
sesa514652 8:770d168713cc 13 InterruptIn buttonB(PTD0);
sesa514652 8:770d168713cc 14 InterruptIn buttonX(PTC17);
sesa514652 8:770d168713cc 15 InterruptIn buttonY(PTC12);
sesa514652 0:1f799c7cce2b 16
sesa514652 5:9b2c976ca318 17 //rows,cols
sesa514652 0:1f799c7cce2b 18 int sprite[8][5] = {
sesa514652 0:1f799c7cce2b 19 { 0,0,1,0,0 },
sesa514652 0:1f799c7cce2b 20 { 0,1,1,1,0 },
sesa514652 0:1f799c7cce2b 21 { 0,0,1,0,0 },
sesa514652 0:1f799c7cce2b 22 { 0,1,1,1,0 },
sesa514652 0:1f799c7cce2b 23 { 1,1,1,1,1 },
sesa514652 0:1f799c7cce2b 24 { 1,1,1,1,1 },
sesa514652 0:1f799c7cce2b 25 { 1,1,0,1,1 },
sesa514652 0:1f799c7cce2b 26 { 1,1,0,1,1 },
sesa514652 5:9b2c976ca318 27 };
sesa514652 8:770d168713cc 28 void init_K64F();
sesa514652 8:770d168713cc 29 // Button A interrupt service routine
sesa514652 8:770d168713cc 30 void buttonA_isr();
sesa514652 8:770d168713cc 31 volatile int g_buttonA_flag = 0;
sesa514652 8:770d168713cc 32 //Test function after research C++ lanuage 05/01/22
sesa514652 4:167ce930c9d5 33 int cube(int num){
sesa514652 4:167ce930c9d5 34 int result;
sesa514652 4:167ce930c9d5 35 result = num*num;
sesa514652 4:167ce930c9d5 36 return result;
sesa514652 5:9b2c976ca318 37 };
sesa514652 4:167ce930c9d5 38
sesa514652 5:9b2c976ca318 39 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
sesa514652 9:cdd7d9a123f9 40 /* Test of the interup an operating the buzzer this kills the board
sesa514652 9:cdd7d9a123f9 41 void buzzme(){
sesa514652 9:cdd7d9a123f9 42 Buzzer.play(200,120);
sesa514652 9:cdd7d9a123f9 43 wait_ms(5);
sesa514652 9:cdd7d9a123f9 44 Buzzer.play(200,120);;
sesa514652 9:cdd7d9a123f9 45 }
sesa514652 9:cdd7d9a123f9 46 */
sesa514652 9:cdd7d9a123f9 47 void printme(){
sesa514652 9:cdd7d9a123f9 48
sesa514652 9:cdd7d9a123f9 49 printf("A Pressed");
sesa514652 9:cdd7d9a123f9 50 }
sesa514652 0:1f799c7cce2b 51 int main() {
sesa514652 5:9b2c976ca318 52 //initialise Joystic
sesa514652 5:9b2c976ca318 53 joystick.init();
sesa514652 5:9b2c976ca318 54
sesa514652 5:9b2c976ca318 55 //first need to initialise display
sesa514652 5:9b2c976ca318 56 lcd.init();
sesa514652 8:770d168713cc 57
sesa514652 8:770d168713cc 58 // Button A is connected between the pin and 3.3 V, we therefore need to turn on the internal pull-down resister
sesa514652 8:770d168713cc 59 buttonA.mode(PullDown);
sesa514652 8:770d168713cc 60
sesa514652 8:770d168713cc 61 // It will return 0 by default and a 1 when pressed i.e. cause a rising edge
sesa514652 9:cdd7d9a123f9 62 // buttonA.rise(&buttonA_isr);
sesa514652 8:770d168713cc 63
sesa514652 8:770d168713cc 64
sesa514652 5:9b2c976ca318 65
sesa514652 5:9b2c976ca318 66 //change set contrast in range 0.0 to 1.0
sesa514652 5:9b2c976ca318 67 //0.5 appears to be a good starting point
sesa514652 5:9b2c976ca318 68 lcd.setContrast(0.5);
sesa514652 5:9b2c976ca318 69
sesa514652 5:9b2c976ca318 70 //Buzzer.period(10.0f);
sesa514652 5:9b2c976ca318 71 //Buzzer.pulsewidth(1);
sesa514652 6:f7f30e0e3bed 72 //Test Struct after research 05/01/22
sesa514652 6:f7f30e0e3bed 73 struct ObjectDefine{
sesa514652 7:ce70a873aa70 74 int qwer;
sesa514652 6:f7f30e0e3bed 75 float Distance;
sesa514652 7:ce70a873aa70 76 char te;
sesa514652 7:ce70a873aa70 77 double dubs;
sesa514652 7:ce70a873aa70 78 string namestruct;
sesa514652 8:770d168713cc 79 // string ;
sesa514652 6:f7f30e0e3bed 80 };
sesa514652 7:ce70a873aa70 81 // Test Class 06/01/22
sesa514652 7:ce70a873aa70 82 class myClass{
sesa514652 7:ce70a873aa70 83 public:
sesa514652 7:ce70a873aa70 84 string nameclass ;
sesa514652 7:ce70a873aa70 85 double dubss;
sesa514652 7:ce70a873aa70 86 int inty;
sesa514652 7:ce70a873aa70 87 };
sesa514652 7:ce70a873aa70 88 myClass Class1;
sesa514652 7:ce70a873aa70 89 Class1.nameclass ="firstclass";
sesa514652 6:f7f30e0e3bed 90 ObjectDefine Object1;
sesa514652 7:ce70a873aa70 91 Object1.qwer = 12;
sesa514652 6:f7f30e0e3bed 92 Object1.Distance = 34;
sesa514652 7:ce70a873aa70 93 Object1.te = 'A';
sesa514652 7:ce70a873aa70 94 Object1.dubs = 23;
sesa514652 7:ce70a873aa70 95 Object1.namestruct = "name";
sesa514652 9:cdd7d9a123f9 96 //buttonA.rise(&buzzme); Omitted due to error
sesa514652 9:cdd7d9a123f9 97 buttonA.rise(&printme);
sesa514652 0:1f799c7cce2b 98 while(1) {
sesa514652 5:9b2c976ca318 99 //Tune(Buzzer,So6,8);
sesa514652 5:9b2c976ca318 100 //Stop_tunes(Buzzer);
sesa514652 8:770d168713cc 101 //Section of code to try to get the button press to be recorded
sesa514652 9:cdd7d9a123f9 102 //bool button = buttonA.read();
sesa514652 8:770d168713cc 103 printf("%f\n", g_buttonA_flag );
sesa514652 8:770d168713cc 104
sesa514652 5:9b2c976ca318 105 Buzzer.play(200,120);
sesa514652 5:9b2c976ca318 106 wait_ms(5);
sesa514652 5:9b2c976ca318 107 Buzzer.play(200,120);
sesa514652 1:757728321abd 108 long distanced = sensor.distance();
sesa514652 1:757728321abd 109 if (distanced >= 400 || distanced <= 2)
sesa514652 5:9b2c976ca318 110 {
sesa514652 1:757728321abd 111 printf("Out of range");
sesa514652 5:9b2c976ca318 112 //Calling dummy function 05/01/22
sesa514652 4:167ce930c9d5 113 int answer = cube(2);
sesa514652 5:9b2c976ca318 114 printf("%f\n",answer);
sesa514652 1:757728321abd 115 wait(1.0);
sesa514652 5:9b2c976ca318 116 }
sesa514652 5:9b2c976ca318 117 else
sesa514652 5:9b2c976ca318 118 {
sesa514652 5:9b2c976ca318 119 printf("Distance : %d cm",distanced);
sesa514652 5:9b2c976ca318 120 wait(1.0); // 1 sec
sesa514652 1:757728321abd 121 }
sesa514652 5:9b2c976ca318 122 lcd.drawCircle(WIDTH/2,HEIGHT/2,distanced,FILL_BLACK);
sesa514652 5:9b2c976ca318 123 //lcd.refresh must be used to update the lcd Display
sesa514652 5:9b2c976ca318 124 lcd.refresh();
sesa514652 5:9b2c976ca318 125 wait(5.0);
sesa514652 5:9b2c976ca318 126 //these are default settings so not strictly needed
sesa514652 0:1f799c7cce2b 127 lcd.normalMode(); // normal colour mode
sesa514652 5:9b2c976ca318 128 lcd.setBrightness(0.5); // put LED backlight on 50%
sesa514652 0:1f799c7cce2b 129 lcd.clear();
sesa514652 0:1f799c7cce2b 130 lcd.printString("Hello, World!",0,0);
sesa514652 0:1f799c7cce2b 131 Vector2D coord = joystick.get_coord();
sesa514652 0:1f799c7cce2b 132 lcd.refresh();
sesa514652 0:1f799c7cce2b 133 printf("Coord = %f,%f\n",coord.x,coord.y);
sesa514652 0:1f799c7cce2b 134 char buffer[14];
sesa514652 0:1f799c7cce2b 135 Vector2D mapped_coord = joystick.get_mapped_coord();
sesa514652 0:1f799c7cce2b 136 printf("Mapped coord = %f,%f\n",mapped_coord.x,mapped_coord.y);
sesa514652 0:1f799c7cce2b 137
sesa514652 5:9b2c976ca318 138 int length = sprintf(buffer,"T = %2f",coord.x);
sesa514652 5:9b2c976ca318 139 //it is important the format specifier ensures the length will fit in the buffer
sesa514652 0:1f799c7cce2b 140 if (length <= 14) // if string will fit on display (assuming printing at x=0)
sesa514652 5:9b2c976ca318 141 lcd.printString(buffer,0,1); // display on screen
sesa514652 0:1f799c7cce2b 142 lcd.refresh();
sesa514652 5:9b2c976ca318 143 lcd.clear();
sesa514652 5:9b2c976ca318 144 //times by 50 to try get the point moving across screen
sesa514652 0:1f799c7cce2b 145 float x50 = mapped_coord.x*5;
sesa514652 0:1f799c7cce2b 146 float y50 = mapped_coord.y*8;
sesa514652 5:9b2c976ca318 147 //to see what the values are
sesa514652 5:9b2c976ca318 148 printf(" *50 = %f,%f\n",x50,y50);
sesa514652 5:9b2c976ca318 149 //example of how to draw circles
sesa514652 1:757728321abd 150 lcd.printChar('o',WIDTH/2,HEIGHT/2);
sesa514652 5:9b2c976ca318 151 lcd.refresh();
sesa514652 0:1f799c7cce2b 152 float mag = joystick.get_mag();
sesa514652 0:1f799c7cce2b 153 float angle = joystick.get_angle();
sesa514652 0:1f799c7cce2b 154 printf("Mag = %f Angle = %f\n",mag,angle);
sesa514652 0:1f799c7cce2b 155 Direction d = joystick.get_direction();
sesa514652 0:1f799c7cce2b 156 printf("Direction = %i\n",d);
sesa514652 0:1f799c7cce2b 157 if (joystick.button_pressed() ) {
sesa514652 0:1f799c7cce2b 158 printf("Button Pressed\n");
sesa514652 9:cdd7d9a123f9 159 wait(5);
sesa514652 0:1f799c7cce2b 160 }
sesa514652 8:770d168713cc 161
sesa514652 8:770d168713cc 162 }
sesa514652 8:770d168713cc 163 }
sesa514652 8:770d168713cc 164 // Button A event-triggered interrupt
sesa514652 9:cdd7d9a123f9 165 //void buttonA_isr()
sesa514652 9:cdd7d9a123f9 166 //{
sesa514652 9:cdd7d9a123f9 167 // g_buttonA_flag = 1; // set flag in ISR
sesa514652 9:cdd7d9a123f9 168 //}