Coursework

Committer:
sesa514652
Date:
Wed Jan 05 20:09:15 2022 +0000
Revision:
5:9b2c976ca318
Parent:
4:167ce930c9d5
Child:
6:f7f30e0e3bed
Formatting

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 5:9b2c976ca318 6 // y x button
sesa514652 0:1f799c7cce2b 7 Joystick joystick(PTB10,PTB11,PTC16);
sesa514652 0:1f799c7cce2b 8 HCSR04 sensor(D14, D15);
sesa514652 3:6ecb75a2675c 9 Piezo Buzzer(PTC10);
sesa514652 0:1f799c7cce2b 10
sesa514652 5:9b2c976ca318 11 //rows,cols
sesa514652 0:1f799c7cce2b 12 int sprite[8][5] = {
sesa514652 0:1f799c7cce2b 13 { 0,0,1,0,0 },
sesa514652 0:1f799c7cce2b 14 { 0,1,1,1,0 },
sesa514652 0:1f799c7cce2b 15 { 0,0,1,0,0 },
sesa514652 0:1f799c7cce2b 16 { 0,1,1,1,0 },
sesa514652 0:1f799c7cce2b 17 { 1,1,1,1,1 },
sesa514652 0:1f799c7cce2b 18 { 1,1,1,1,1 },
sesa514652 0:1f799c7cce2b 19 { 1,1,0,1,1 },
sesa514652 0:1f799c7cce2b 20 { 1,1,0,1,1 },
sesa514652 5:9b2c976ca318 21 };
sesa514652 4:167ce930c9d5 22 //Test Void after research C++ lanuage 05/01/22
sesa514652 4:167ce930c9d5 23 int cube(int num){
sesa514652 4:167ce930c9d5 24 int result;
sesa514652 4:167ce930c9d5 25 result = num*num;
sesa514652 4:167ce930c9d5 26 return result;
sesa514652 5:9b2c976ca318 27 };
sesa514652 4:167ce930c9d5 28
sesa514652 5:9b2c976ca318 29 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
sesa514652 5:9b2c976ca318 30
sesa514652 0:1f799c7cce2b 31 int main() {
sesa514652 5:9b2c976ca318 32 //initialise Joystic
sesa514652 5:9b2c976ca318 33 joystick.init();
sesa514652 5:9b2c976ca318 34
sesa514652 5:9b2c976ca318 35 //first need to initialise display
sesa514652 5:9b2c976ca318 36 lcd.init();
sesa514652 5:9b2c976ca318 37
sesa514652 5:9b2c976ca318 38 //change set contrast in range 0.0 to 1.0
sesa514652 5:9b2c976ca318 39 //0.5 appears to be a good starting point
sesa514652 5:9b2c976ca318 40 lcd.setContrast(0.5);
sesa514652 5:9b2c976ca318 41
sesa514652 5:9b2c976ca318 42 //Buzzer.period(10.0f);
sesa514652 5:9b2c976ca318 43 //Buzzer.pulsewidth(1);
sesa514652 3:6ecb75a2675c 44
sesa514652 4:167ce930c9d5 45
sesa514652 0:1f799c7cce2b 46 while(1) {
sesa514652 5:9b2c976ca318 47 //Tune(Buzzer,So6,8);
sesa514652 5:9b2c976ca318 48 //Stop_tunes(Buzzer);
sesa514652 5:9b2c976ca318 49 Buzzer.play(200,120);
sesa514652 5:9b2c976ca318 50 wait_ms(5);
sesa514652 5:9b2c976ca318 51 Buzzer.play(200,120);
sesa514652 1:757728321abd 52 long distanced = sensor.distance();
sesa514652 1:757728321abd 53 if (distanced >= 400 || distanced <= 2)
sesa514652 5:9b2c976ca318 54 {
sesa514652 1:757728321abd 55 printf("Out of range");
sesa514652 5:9b2c976ca318 56 //Calling dummy function 05/01/22
sesa514652 4:167ce930c9d5 57 int answer = cube(2);
sesa514652 5:9b2c976ca318 58 printf("%f\n",answer);
sesa514652 1:757728321abd 59 wait(1.0);
sesa514652 5:9b2c976ca318 60 }
sesa514652 5:9b2c976ca318 61 else
sesa514652 5:9b2c976ca318 62 {
sesa514652 5:9b2c976ca318 63 printf("Distance : %d cm",distanced);
sesa514652 5:9b2c976ca318 64 wait(1.0); // 1 sec
sesa514652 1:757728321abd 65 }
sesa514652 5:9b2c976ca318 66 lcd.drawCircle(WIDTH/2,HEIGHT/2,distanced,FILL_BLACK);
sesa514652 5:9b2c976ca318 67 //lcd.refresh must be used to update the lcd Display
sesa514652 5:9b2c976ca318 68 lcd.refresh();
sesa514652 5:9b2c976ca318 69 wait(5.0);
sesa514652 5:9b2c976ca318 70 //these are default settings so not strictly needed
sesa514652 0:1f799c7cce2b 71 lcd.normalMode(); // normal colour mode
sesa514652 5:9b2c976ca318 72 lcd.setBrightness(0.5); // put LED backlight on 50%
sesa514652 0:1f799c7cce2b 73 lcd.clear();
sesa514652 0:1f799c7cce2b 74 lcd.printString("Hello, World!",0,0);
sesa514652 0:1f799c7cce2b 75 Vector2D coord = joystick.get_coord();
sesa514652 0:1f799c7cce2b 76 lcd.refresh();
sesa514652 0:1f799c7cce2b 77 printf("Coord = %f,%f\n",coord.x,coord.y);
sesa514652 0:1f799c7cce2b 78 char buffer[14];
sesa514652 0:1f799c7cce2b 79 Vector2D mapped_coord = joystick.get_mapped_coord();
sesa514652 0:1f799c7cce2b 80 printf("Mapped coord = %f,%f\n",mapped_coord.x,mapped_coord.y);
sesa514652 0:1f799c7cce2b 81
sesa514652 5:9b2c976ca318 82 int length = sprintf(buffer,"T = %2f",coord.x);
sesa514652 5:9b2c976ca318 83 //it is important the format specifier ensures the length will fit in the buffer
sesa514652 0:1f799c7cce2b 84 if (length <= 14) // if string will fit on display (assuming printing at x=0)
sesa514652 5:9b2c976ca318 85 lcd.printString(buffer,0,1); // display on screen
sesa514652 0:1f799c7cce2b 86 lcd.refresh();
sesa514652 5:9b2c976ca318 87 lcd.clear();
sesa514652 5:9b2c976ca318 88 //times by 50 to try get the point moving across screen
sesa514652 0:1f799c7cce2b 89 float x50 = mapped_coord.x*5;
sesa514652 0:1f799c7cce2b 90 float y50 = mapped_coord.y*8;
sesa514652 5:9b2c976ca318 91 //to see what the values are
sesa514652 5:9b2c976ca318 92 printf(" *50 = %f,%f\n",x50,y50);
sesa514652 5:9b2c976ca318 93 //example of how to draw circles
sesa514652 1:757728321abd 94 lcd.printChar('o',WIDTH/2,HEIGHT/2);
sesa514652 5:9b2c976ca318 95 lcd.refresh();
sesa514652 0:1f799c7cce2b 96 float mag = joystick.get_mag();
sesa514652 0:1f799c7cce2b 97 float angle = joystick.get_angle();
sesa514652 0:1f799c7cce2b 98 printf("Mag = %f Angle = %f\n",mag,angle);
sesa514652 0:1f799c7cce2b 99 Direction d = joystick.get_direction();
sesa514652 0:1f799c7cce2b 100 printf("Direction = %i\n",d);
sesa514652 0:1f799c7cce2b 101 if (joystick.button_pressed() ) {
sesa514652 0:1f799c7cce2b 102 printf("Button Pressed\n");
sesa514652 0:1f799c7cce2b 103 }
sesa514652 0:1f799c7cce2b 104 }
sesa514652 0:1f799c7cce2b 105 }
sesa514652 5:9b2c976ca318 106