Coursework

main.cpp

Committer:
sesa514652
Date:
2021-12-16
Revision:
0:1f799c7cce2b
Child:
1:757728321abd

File content as of revision 0:1f799c7cce2b:

#include "mbed.h"
#include "Joystick.h"
#include "N5110.h"
#include "hcsr04.h"
//                  y     x     button
Joystick joystick(PTB10,PTB11,PTC16);
HCSR04 sensor(D14, D15); 
   long distance = sensor.distance(); 

//      rows,cols
int sprite[8][5] =   {
    { 0,0,1,0,0 },
    { 0,1,1,1,0 },
    { 0,0,1,0,0 },
    { 0,1,1,1,0 },
    { 1,1,1,1,1 },
    { 1,1,1,1,1 },
    { 1,1,0,1,1 },
    { 1,1,0,1,1 },
};
N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); 
int main() {
    //initialise Joystick
    joystick.init();
        // first need to initialise display
   lcd.init();
    
    // change set contrast in range 0.0 to 1.0
    // 0.5 appears to be a good starting point
   lcd.setContrast(0.5);
    
    while(1) {
    
    
   
        // these are default settings so not strictly needed
        lcd.normalMode();      // normal colour mode
        lcd.setBrightness(0.5); // put LED backlight on 50%
        
        lcd.clear();
        lcd.printString("Hello, World!",0,0);
        Vector2D coord = joystick.get_coord();
        lcd.refresh();
        
        printf("Coord = %f,%f\n",coord.x,coord.y);
        char buffer[14];
        Vector2D mapped_coord = joystick.get_mapped_coord(); 
        printf("Mapped coord = %f,%f\n",mapped_coord.x,mapped_coord.y); 
        
          int length = sprintf(buffer,"T = %2f",coord.x);
                // it is important the format specifier ensures the length will fit in the buffer
        if (length <= 14)  // if string will fit on display (assuming printing at x=0)
           lcd.printString(buffer,0,1);           // display on screen
        lcd.refresh();
        
        
  
         lcd.clear();
         // times by 50 to try get the point moving across screen
         float x50 = mapped_coord.x*5;
         float y50 = mapped_coord.y*8;
         //  to see what the values are 
         printf(" *50 = %f,%f\n",x50,y50);
         // 
        // example of how to draw circles
        lcd.printChar('o',x50/2,y50/2);
        lcd.refresh();
        
        
        float mag = joystick.get_mag();
        float angle = joystick.get_angle();
        printf("Mag = %f Angle = %f\n",mag,angle);
        
        Direction d = joystick.get_direction();
        printf("Direction = %i\n",d);
        
        if (joystick.button_pressed() ) {
            printf("Button Pressed\n");  
        }

   
    if (distance >= 400 || distance <= 2) 
    {
        printf("Out of range");
        wait(1.0);
    }
    if (distance >= 300 || distance <= 50)
    {
    printf("Range one");
    wait(1.0);
    }
    else
    
    {
      printf("Distance :  %d cm",distance);
      wait(1.0); // 1 sec  
    }
      
    }
}