Coursework

Committer:
sesa514652
Date:
Thu Dec 16 14:40:46 2021 +0000
Revision:
1:757728321abd
Parent:
0:1f799c7cce2b
Child:
2:bb3dbda4d1f5
reading  distance

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