Coursework

main.cpp

Committer:
sesa514652
Date:
2022-02-03
Revision:
40:0d55297c8b34
Parent:
39:a31b49f3e65e
Child:
41:16cd48d6e834

File content as of revision 40:0d55297c8b34:

/**
*.....Kory Hamill
*.....IOT Smart Electronics Coursework
*.....Acknowlegements Libary Makers 
*.....18689004
*/
/**
* Section of code to bring in the #includes
*/
#include "mbed.h"
//#include "Joystick.h" Not used in project
#include "N5110.h"
#include "hcsr04.h"
#include "Piezo.h"
#include "string"
#include "array"
#include <iostream>
#include <numeric>

using namespace std;

/**
*...This Section of the code is used to bring in volatile Varible's, These can be wrote to throught the code.
*...The Varibles are used to track button press's on the board.
*/
volatile int g_ButtonAPress;
volatile int g_ButtonBPress;
volatile int LoopCountScan1;
volatile int LoopCountScan2;
volatile int LoopCountScan3;
volatile int LoopCountScan4;
volatile int LoopCountScan5;
volatile int LoopCountScan6;
volatile int g_buttonA_flag = 0; 
volatile int g_buttonB_flag = 0; 
volatile int g_buttonX_flag = 0; 
volatile int g_buttonY_flag = 0; 
volatile int g_buttonStart_flag = 0; 
volatile int g_buttonBack_flag = 0; 
volatile int g_buttonLeft_flag = 0; 
volatile int g_buttonRight_flag = 0;
volatile int wallTotal;
volatile int boxTotal;
volatile int cartonTotal;
/**
*..Bring in 5 arrays of 5 for each of the 5 required scans.
*..array Ref starts at 0,
*/ 
volatile     int Scan1[5];
/* no longer required
volatile     int Scan2[5];
volatile     int Scan3[5];
volatile     int Scan4[5];
volatile     int Scan5[5];
volatile     int wall[5];  
*/
/**
*... Section of code to assign names to IO pins on the K64f
*/
InterruptIn buttonA(PTB9);
InterruptIn buttonB(PTD0);
InterruptIn buttonX(PTC17);
InterruptIn buttonY(PTC12);
InterruptIn buttonStart(PTC5);
InterruptIn buttonBack(PTB19);
InterruptIn buttonLeft(PTB18);
InterruptIn buttonRight(PTB3);
//Joystick joystick(PTB10,PTB11,PTC16); Not used in project
HCSR04 sensor(D14, D15); 
Piezo Buzzer(PTC10);
N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
 
/**
*...For used with the LCD Screen. 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 },
                       };
/**
*...Section for starting the board and seeting up interrupt routines
*...Along with other functions to be called later
*/
void init_K64F();   //  Start K64fBoard
void buttonA_isr(); // Button A interrupt service routine
void buttonB_isr(); // Button B interrupt service routine
void buttonX_isr(); // Button X interrupt service routine
void buttonY_isr(); // Button Y interrupt service routine
void buttonStart_isr(); // Button Start interrupt service routine
void buttonBack_isr(); // Button Back interrupt service routine
void buttonLeft_isr(); // Button Left interrupt service routine
void buttonRight_isr(); // Button Right interrupt service routine
void inputScan1();
void inputScan2();
void inputScan3();
void inputScan4();
void inputScan5();
void inputScan6();
int NoContinueExit();
int buttonCountA();
int buttonCountB();
void printShape();





EventQueue queue; /**< Required to get around issues with printing onto LCD and the Printf */


/** Code which was never used
*
*.. was for initial learning of C++
//volatile int g_buttonA_counter = 0; // Global counter
//Test function after research C++ lanuage 05/01/22
int cube(int num){
     return num*num;
      };
// Menu Items
int page1;
int page2;
int page3;
string Listitem1 = "Game"; // 
string Listitem2 = "Measure"; // 
string Listitem3 = "Detect"; // 
/ Test of the interup an operating the buzzer this kills the board 
 void buzzme(){
        Buzzer.play(200,120);
            wait_ms(5);
       Buzzer.play(200,120);;
 }
       int Scan1[5];
       int Scan2[5];
        int Scan1Input(){;        
         Scan1[0] = sensor.distance();
         Scan1[1] = sensor.distance();;
         Scan1[2] = sensor.distance();;
         Scan1[3] = sensor.distance();;
         Scan1[4] = sensor.distance();;
        return Scan1[0,1,2,3,4];}
        //void returnme(){
//int num;
  //    return num*num;
  //    };
 */
 /*
bool Equal(int ar1[],int ar2[], int n, int m){
if(n!=m)
return false;
sort(ar1,ar1 +n);
sort(ar2,ar2 +m);
for (int i=0;i<n; i++)
if(ar1[i]!=ar2[i])
return false;
return true;
}*/
/* Problems with this and Being exact and repeatable 
bool MatchMade(int arr1[], int arr2[], int n, int m)
{
    // If lengths of array are not equal means
    // array are not equal
    if (n != m)
        return false;
 
    // Sort both arrays
    sort(arr1, arr1 + n);
    sort(arr2, arr2 + m);
 
    // Linearly compare elements
    for (int i = 0; i < n; i++)
        if (arr1[i] != arr2[i])
            return false;
        
 
    // If all elements were same.
    return true;
    */

int main() {
       

/**
*.. Intialise joystick and Lcd
*/
     //  joystick.init(); not used in project
            lcd.init();
  lcd.setContrast(0.5);
/**
*.. Issues were found with the LCd and printf while using interrupts,  to get around
*.. this issue these queue's to the event thread were added.
*/
  Thread eventThread;
  eventThread.start(callback(&queue, &EventQueue::dispatch_forever)); 
  buttonA.rise(queue.event(&buttonA_isr)); 
  buttonB.rise(queue.event(&buttonB_isr));
  buttonX.rise(queue.event(&buttonX_isr));
  buttonY.rise(queue.event(&buttonY_isr));
  buttonStart.rise(queue.event(&buttonStart_isr)); 
  buttonBack.rise(queue.event(&buttonBack_isr));
  buttonLeft.rise(queue.event(&buttonLeft_isr));
  buttonRight.rise(queue.event(&buttonRight_isr));
  /**
  *..Buttons were connected to IO pins which required a pull down resistor to be active.
  */
    buttonA.mode(PullDown);
    buttonB.mode(PullDown);
    buttonX.mode(PullDown);
    buttonY.mode(PullDown);
    buttonStart.mode(PullDown);
    buttonBack.mode(PullDown);
    buttonLeft.mode(PullDown);
    buttonRight.mode(PullDown);
       
/**
*.. Section of code not used in main program.
*
Buzzer.period(10.0f);
//Buzzer.pulsewidth(1);   
Test Struct after research 05/01/22
struct ObjectDefine{
int qwer;
    float  Distance;
    char te;
    double dubs;
 string namestruct;
   // string ;
};
// Test Class 06/01/22
   class myClass{
   public:
   string nameclass ;
   double dubss;
   int inty;
   };
   myClass Class1;
   Class1.nameclass ="firstclass";
   ObjectDefine Object1;
   Object1.qwer = 12;
   Object1.Distance = 34;
   Object1.te = 'A';
   Object1.dubs = 23;
   Object1.namestruct = "name";
*/ 

/*
    int ar1[]= {2,3,4,5};
    int ar2[] = {2,3,4,5};
    int n = sizeof(ar1);
    int m = sizeof(ar2);
    if(Equal(ar1,ar2,n,m)){
    printf("TRUEEEEEEEEEEEEEEEEEEE\n");}
    else {printf("Falseeeeeeeeeeeeeeeeeeeeeeeeeeeddddddddd\n");}
    
    
    
    
    */

 
            
    
    
printf("I SHOULD HAVE BEEN PRINTED !!!!!");

    while(1) {  
    
/**
*.. It was found Cout was not working as expected.
 cout << "Test value collection";
Scan1Input();    
    
cout << Scan1[0];cout <<"/n";
cout << Scan1[1];"/n";
cout << Scan1[2];"/n";
cout << Scan1[3];"/n";
cout << Scan1[4];"/n";
cout << Scan1[5];"/n";
cout << Scan1[6];"/n";
cout << Scan1[7];"/n";
cout << Scan1[8];"/n";
cout << Scan1[9];"/n";
*/
printf("Debug : LoopCount1 : %i\n",LoopCountScan1);
printf("Debug : LoopCount2 : %i\n",LoopCountScan2);
printf("Debug : LoopCount3 : %i\n",LoopCountScan3);
printf("Debug : LoopCount4 : %i\n",LoopCountScan4);
printf("Debug : LoopCount5 : %i\n",LoopCountScan5);
printf("press count = %i\n",g_ButtonAPress);
 printf("Scan1 1 :  %d cm\n", Scan1[0]);
  printf("Scan1 2 :  %d cm\n", Scan1[1]);
   printf("Scan1 3 :  %d cm\n", Scan1[2]);
     printf("Scan1 4 :  %d cm\n", Scan1[3]);
      printf("Scan1 5 :  %d cm\n", Scan1[4]);
      /* NO longer Reuired Section
printf("Scan2 1 :  %d cm\n", Scan2[0]);
  printf("Scan2 2 :  %d cm\n", Scan2[1]);
   printf("Scan2 3 :  %d cm\n", Scan2[2]);
     printf("Scan2 4 :  %d cm\n", Scan2[3]);
      printf("Scan2 5 :  %d cm\n", Scan2[4]);
 printf("Scan3 1 :  %d cm\n", Scan3[0]);
  printf("Scan3 2 :  %d cm\n", Scan3[1]);
   printf("Scan3 3 :  %d cm\n", Scan3[2]);
     printf("Scan3 4 :  %d cm\n", Scan3[3]);
      printf("Scan3 5 :  %d cm\n", Scan3[4]);
 printf("Scan4 1 :  %d cm\n", Scan4[0]);
  printf("Scan4 2 :  %d cm\n", Scan4[1]);
   printf("Scan4 3 :  %d cm\n", Scan4[2]);
     printf("Scan4 4 :  %d cm\n", Scan4[3]);
      printf("Scan4 5 :  %d cm\n", Scan4[4]);
 printf("Scan5 1 :  %d cm\n", Scan5[0]);
  printf("Scan5 2 :  %d cm\n", Scan5[1]);
   printf("Scan5 3 :  %d cm\n", Scan5[2]);
     printf("Scan5 4 :  %d cm\n", Scan5[3]);
      printf("Scan5 5 :  %d cm\n", Scan5[4]);  
      */         
 printf("I always Get printed \n");
     //   cout << Scan1[2];
       // cout << Scan1[6];
printf("I Should Be printed always \n"); 
/**
*..Code section on Comparing data Cout was causing issue
*
                if (Scan1[1] > Scan1[2]){
            cout <<'+';
            }else if (Scan1[1]=Scan1[2]){
            cout <<'=';}
            else {
                cout <<'-';}
                      if (Scan1[2] > Scan1[3]){
            cout <<'+';
            }else if (Scan1[2]=Scan1[3]){
            cout <<'=';}
            else {
                cout <<'-';}
                
                      if (Scan1[3] > Scan1[4]){
            cout <<'+';
            }else if (Scan1[3]=Scan1[4]){
            cout <<'=';}
            else {
                cout <<'-';}
                      if (Scan1[4] > Scan1[5]){
            cout <<'+';
            }else if (Scan1[4]=Scan1[5]){
            cout <<'=';}
            else {
                cout <<'-';}
                             if (Scan1[5] > Scan1[6]){
            cout <<'+';
            }else if (Scan1[5]=Scan1[6]){
            cout <<'=';}
            else {
                cout <<'-';}
                if (Scan1[6] > Scan1[7]){
            cout <<'+';
            }else if (Scan1[6]=Scan1[7]){
            cout <<'=';}
            else {
                cout <<'-';}
                      if (Scan1[7] > Scan1[8]){
            cout <<'+';
            }else if (Scan1[7]=Scan1[8]){
            cout <<'=';}
            else {
                cout <<'-';}
                
                      if (Scan1[8] > Scan1[9]){
            cout <<'+';
            }else if (Scan1[8]=Scan1[9]){
            cout <<'=';}
            else {
                cout <<'-';}
                      if (Scan1[9] > Scan1[10]){
            cout <<'+';
            }else if (Scan1[9]=Scan1[10]){
            cout <<'=';}
            else {
                cout <<'-';}
*/
        
/////////////////////////////////////////////////////////////////////////////////////////////////////
/**
*..Initial state when starting the Board.
*/
        switch(g_ButtonAPress){
     default:
     lcd.clear();   
     lcd.printString("Main Menu",0,0);
     lcd.drawLine(0,10,80,10,2);      
     lcd.printString("Press A",0,2);
     lcd.printString("New Scan",0,3);
     lcd.printString("Press B",0,4);
     lcd.printString("Decicde",0,5);
     LoopCountScan1 =0;
     LoopCountScan2 =0;
     LoopCountScan3 =0;
     LoopCountScan4 =0;
     LoopCountScan5 =0;
     LoopCountScan6 =0;
     

        
 /* Code Section No longer required
 *
          
for(int i=0;  i<5; i++){
     
     printf("hello Scan: %i The value of : %i \n",i, Scan1[i]);
     if (Scan1[i]>95){
         Decide[i]= '+';
     printf("no wall%c/n",Decide[i]);}
     else if (Scan1[i]<85){
         Decide[i]= '-';
         printf("OBJECT ABOUT%c/n",Decide[i]);}
         else if (Scan1[i]>85<95){
             Decide[i]= '=';
             printf("No Object %c/n",Decide[i]);}
             }
 */            
 

     
/**  IF Statement to decide on shape to draw
*.. This section of code compares the each array element with the one proceding it and makes a logical decision if higher lower or equal.
*/
/*
          if (Scan1[0] > Scan1[1])
           {
            printf("+");
            Decide[0]= 'x';
            printf( "chosen 1 %c\n",Decide[0]);
            }
            else if (Scan1[0] == Scan1[1]) 
            {
            printf("=");
            Decide[0]='=';
            printf( "chosen2 %c\n",Decide[0]);
            }
            else if (Scan1[0] < Scan1[1])
            {
            printf("-");
            Decide[0]='-';
            printf( "chosen3 %c\n",Decide[0]);
            }
            if (Scan1[1] > Scan1[2])
           {
            printf("+");
            Decide[1]= 'x';
            printf( "chosen 1 %c\n",Decide[1]);
            }
            else if (Scan1[1] == Scan1[2]) 
            {
            printf("=");
            Decide[1]='=';
            printf( "chosen2 %c\n",Decide[1]);
            }
            else if (Scan1[1] < Scan1[2])
            {
            printf("-");
            Decide[1]='-';
            printf( "chosen3 %c\n",Decide[1]);
            }
            
          if (Scan1[2] > Scan1[3])
           {
            printf("+");
            Decide[2]= 'x';
            printf( "chosen 1 %c\n",Decide[2]);
            }
            else if (Scan1[2] == Scan1[3]) 
            {
            printf("=");
            Decide[2]='=';
            printf( "chosen2 %c\n",Decide[2]);
            }
            else if (Scan1[2] < Scan1[3])
            {
            printf("-");
            Decide[2]='-';
            printf( "chosen3 %c\n",Decide[2]);
            }
            if (Scan1[3] > Scan1[4])
           {
            printf("+");
            Decide[3]= 'x';
            printf( "chosen 1 %c\n",Decide[3]);
            }
            else if (Scan1[3] == Scan1[4]) 
            {
            printf("=");
            Decide[3]='=';
            printf( "chosen2 %c\n",Decide[3]);
            }
            else if (Scan1[3] < Scan1[4])
            {
            printf("-");
            Decide[3]='-';
            printf( "chosen3 %c\n",Decide[3]);
            }
            
          if (Scan2[0] > Scan2[1])
           {
            printf("+");
            Decide2[0]= 'x';
            printf( "chosen 1 %c\n",Decide2[0]);
            }
            else if (Scan2[0] == Scan2[1]) 
            {
            printf("=");
            Decide2[0]='=';
            printf( "chosen2 %c\n",Decide2[0]);
            }
            else if (Scan2[0] < Scan2[1])
            {
            printf("-");
            Decide2[0]='-';
            printf( "chosen3 %c\n",Decide2[0]);
            }
            if (Scan2[1] > Scan2[2])
           {
            printf("+");
            Decide2[1]= 'x';
            printf( "chosen 1 %c\n",Decide2[1]);
            }
            else if (Scan2[1] == Scan2[2]) 
            {
            printf("=");
            Decide2[1]='=';
            printf( "chosen2 %c\n",Decide2[1]);
            }
            else if (Scan2[1] < Scan2[2])
            {
            printf("-");
            Decide2[1]='-';
            printf( "chosen3 %c\n",Decide2[1]);
            }
            
          if (Scan2[2] > Scan2[3])
           {
            printf("+");
            Decide2[2]= 'x';
            printf( "chosen 1 %c\n",Decide2[2]);
            }
            else if (Scan2[2] == Scan2[3]) 
            {
            printf("=");
            Decide2[2]='=';
            printf( "chosen2 %c\n",Decide2[2]);
            }
            else if (Scan2[2] < Scan2[3])
            {
            printf("-");
            Decide2[2]='-';
            printf( "chosen3 %c\n",Decide2[2]);
            }
            if (Scan2[3] > Scan2[4])
           {
            printf("+");
            Decide2[3]= 'x';
            printf( "chosen 1 %c\n",Decide2[3]);
            }
            else if (Scan2[3] == Scan2[4]) 
            {
            printf("=");
            Decide2[3]='=';
            printf( "chosen2 %c\n",Decide2[3]);
            }
            else if (Scan2[3] < Scan2[4])
            {
            printf("-");
            Decide2[3]='-';
            printf( "chosen3 %c\n",Decide2[3]);
            }
            
          if (Scan3[0] > Scan3[1])
           {
            printf("+");
            Decide3[0]= 'x';
            printf( "chosen 1 %c\n",Decide3[0]);
            }
            else if (Scan3[0] == Scan3[1]) 
            {
            printf("=");
            Decide3[0]='=';
            printf( "chosen2 %c\n",Decide3[0]);
            }
            else if (Scan3[0] < Scan3[1])
            {
            printf("-");
            Decide3[0]='-';
            printf( "chosen3 %c\n",Decide3[0]);
            }
            if (Scan3[1] > Scan3[2])
           {
            printf("+");
            Decide3[1]= 'x';
            printf( "chosen 1 %c\n",Decide3[1]);
            }
            else if (Scan3[1] == Scan3[2]) 
            {
            printf("=");
            Decide3[1]='=';
            printf( "chosen2 %c\n",Decide3[1]);
            }
            else if (Scan3[1] < Scan3[2])
            {
            printf("-");
            Decide3[1]='-';
            printf( "chosen3 %c\n",Decide3[1]);
            }
            
          if (Scan3[2] > Scan3[3])
           {
            printf("+");
            Decide3[2]= 'x';
            printf( "chosen 1 %c\n",Decide3[2]);
            }
            else if (Scan3[2] == Scan3[3]) 
            {
            printf("=");
            Decide3[2]='=';
            printf( "chosen2 %c\n",Decide3[2]);
            }
            else if (Scan3[2] < Scan3[3])
            {
            printf("-");
            Decide3[2]='-';
            printf( "chosen3 %c\n",Decide3[2]);
            }
            if (Scan3[3] > Scan3[4])
           {
            printf("+");
            Decide3[3]= 'x';
            printf( "chosen 1 %c\n",Decide3[3]);
            }
            else if (Scan3[3] == Scan3[4]) 
            {
            printf("=");
            Decide3[3]='=';
            printf( "chosen2 %c\n",Decide3[3]);
            }
            else if (Scan3[3] < Scan3[4])
            {
            printf("-");
            Decide3[3]='-';
            printf( "chosen3 %c\n",Decide3[3]);
            }
             
          if (Scan4[0] > Scan4[1])
           {
            printf("+");
            Decide4[0]= 'x';
            printf( "chosen 1 %c\n",Decide4[0]);
            }
            else if (Scan4[0] == Scan4[1]) 
            {
            printf("=");
            Decide4[0]='=';
            printf( "chosen2 %c\n",Decide4[0]);
            }
            else if (Scan4[0] < Scan4[1])
            {
            printf("-");
            Decide4[0]='-';
            printf( "chosen3 %c\n",Decide4[0]);
            }
            if (Scan4[1] > Scan4[2])
           {
            printf("+");
            Decide4[1]= 'x';
            printf( "chosen 1 %c\n",Decide4[1]);
            }
            else if (Scan4[1] == Scan4[2]) 
            {
            printf("=");
            Decide4[1]='=';
            printf( "chosen2 %c\n",Decide4[1]);
            }
            else if (Scan4[1] < Scan4[2])
            {
            printf("-");
            Decide4[1]='-';
            printf( "chosen3 %c\n",Decide4[1]);
            }
            
          if (Scan4[2] > Scan4[3])
           {
            printf("+");
            Decide4[2]= 'x';
            printf( "chosen 1 %c\n",Decide4[2]);
            }
            else if (Scan4[2] == Scan4[3]) 
            {
            printf("=");
            Decide4[2]='=';
            printf( "chosen2 %c\n",Decide4[2]);
            }
            else if (Scan4[2] < Scan4[3])
            {
            printf("-");
            Decide4[2]='-';
            printf( "chosen3 %c\n",Decide4[2]);
            }
            if (Scan4[3] > Scan4[4])
           {
            printf("+");
            Decide4[3]= 'x';
            printf( "chosen 1 %c\n",Decide4[3]);
            }
            else if (Scan4[3] == Scan4[4]) 
            {
            printf("=");
            Decide4[3]='=';
            printf( "chosen2 %c\n",Decide4[3]);
            }
            else if (Scan4[3] < Scan4[4])
            {
            printf("-");
            Decide4[3]='-';
            printf( "chosen3 %c\n",Decide4[3]);
            }
          if (Scan5[0] > Scan5[1])
           {
            printf("+");
            Decide5[0]= 'x';
            printf( "chosen 1 %c\n",Decide5[0]);
            }
            else if (Scan5[0] == Scan5[1]) 
            {
            printf("=");
            Decide5[0]='=';
            printf( "chosen2 %c\n",Decide5[0]);
            }
            else if (Scan5[0] < Scan5[1])
            {
            printf("-");
            Decide5[0]='-';
            printf( "chosen3 %c\n",Decide5[0]);
            }
            if (Scan5[1] > Scan5[2])
           {
            printf("+");
            Decide5[1]= 'x';
            printf( "chosen 1 %c\n",Decide5[1]);
            }
            else if (Scan5[1] == Scan5[2]) 
            {
            printf("=");
            Decide5[1]='=';
            printf( "chosen2 %c\n",Decide5[1]);
            }
            else if (Scan5[1] < Scan5[2])
            {
            printf("-");
            Decide5[1]='-';
            printf( "chosen3 %c\n",Decide5[1]);
            }
            
          if (Scan5[2] > Scan5[3])
           {
            printf("+");
            Decide5[2]= 'x';
            printf( "chosen 1 %c\n",Decide5[2]);
            }
            else if (Scan5[2] == Scan5[3]) 
            {
            printf("=");
            Decide5[2]='=';
            printf( "chosen2 %c\n",Decide5[2]);
            }
            else if (Scan5[2] < Scan5[3])
            {
            printf("-");
            Decide5[2]='-';
            printf( "chosen3 %c\n",Decide5[2]);
            }
            if (Scan5[3] > Scan5[4])
           {
            printf("+");
            Decide5[3]= 'x';
            printf( "chosen 1 %c\n",Decide5[3]);
            }
            else if (Scan5[3] == Scan5[4]) 
            {
            printf("=");
            Decide5[3]='=';
            printf( "chosen2 %c\n",Decide5[3]);
            }
            else if (Scan5[3] < Scan5[4])
            {
            printf("-");
            Decide5[3]='-';
            printf( "chosen3 %c\n",Decide5[3]);
            } */
            
     lcd.refresh();
     wait_ms(50);
     
     break;

//////////////////////////////////////////////////////////////////////////////////////////////////     
/**
*.. When Button A is pressed this section of code is ran to collect data points of the object.
*..Scanning takes marks the LCD with locationof points which data has been collected for.
*/     
     case 1:
     g_ButtonAPress =1;
 /*
 *..Section used for power saving, if the user has not continued after 5 scans
 */ 
    if(LoopCountScan1==10){
        g_ButtonAPress=0;
        LoopCountScan1=0;
        break;}
        else; 
        inputScan1();
         break;
/*
        wait_ms(5); // required to stop Black screen issue}
        inputScan2();
        wait_ms(5); // required to stop Black screen issue}
inputScan3();

         wait_ms(5); // required to stop Black screen issue}
inputScan4();

         wait_ms(5);
inputScan5();
      

      
         g_ButtonAPress=0;
     */
       
////////////////////////////////////////////////////////////////////////////        
     case 2: 
    g_ButtonAPress= 2;
        if(LoopCountScan2==10){
        g_ButtonAPress=0;
        LoopCountScan2=0;
        break;}
        else; 
   /*int secondScan[10];
    
                 secondScan[5] = sensor.distance();
 printf("Distance :  %d cm", secondScan[5]);
 lcd.clear();
       lcd.printString("2nd Scan up down",0,0); 
       lcd.drawLine(0,10,80,10,2);  */
       inputScan2();
     break;
////////////////////////////////////////////////////////////////////     
     case 3:
              g_ButtonAPress= 3;
                  if(LoopCountScan3==10){
        g_ButtonAPress=0;
        LoopCountScan3=0;
        break;}
        else; 
        /* lcd.clear();
       lcd.printString("Menu 3",0,0);        
       int us = sensor.distance(); 
       printf("Distance :  %d cm",us);
         lcd.refresh();
         wait_ms(50); //required to stop Black screen issue*/
         inputScan3();
     break;
     
 /////////////////////////////////////////////////////////////////////////     
 
     case 4:
              g_ButtonAPress= 4;
                  if(LoopCountScan4==10){
        g_ButtonAPress=0;
        LoopCountScan4=0;
        break;}
        else; 
        /* lcd.clear();
       lcd.printString("Menu 3",0,0);        
       int us = sensor.distance(); 
       printf("Distance :  %d cm",us);
         lcd.refresh();
         wait_ms(50); //required to stop Black screen issue*/
         inputScan4();
     break;  
     ///////////////////////////////////////////////////////////////////
          case 5:
              g_ButtonAPress= 5;
                  if(LoopCountScan5==10){
        g_ButtonAPress=0;
        LoopCountScan5=0;
        break;}
        else; 
        /* lcd.clear();
       lcd.printString("Menu 3",0,0);        
       int us = sensor.distance(); 
       printf("Distance :  %d cm",us);
         lcd.refresh();
         wait_ms(50); //required to stop Black screen issue*/
         inputScan5();
     break;  
     
     
     
     /////////////////////////////////////////////////////////////////
     
     
     case 6:
     g_ButtonAPress=6;
                       if(LoopCountScan6==10){
        g_ButtonAPress=0;
        LoopCountScan6=0;
        break;}
        else;
             
       inputScan6();
       break;
        
        /*
int button_counter;
if (g_buttonA_flag){
                    button_counter = button_counter +1;
                    //case (test)
                   g_buttonA_flag = 0;  // if it has, clear the flag

            // send message over serial port - can observe in CoolTerm etc.
            printf("Button A pressed  %f\n", button_counter);
            }
if (g_buttonB_flag){
                   g_buttonB_flag = 0;  // if it has, clear the flag

            // send message over serial port - can observe in CoolTerm etc.
            printf("Button B pressed\n");
            }
if (g_buttonX_flag){
                   g_buttonX_flag = 0;  // if it has, clear the flag
                               // swap direction when button has been pressed
            // (could just use ! but want this to be explicit to aid understanding)
            if (direction == UP) {
                direction = DOWN;    
            }
            else {
                direction = UP;
            }

            // send message over serial port - can observe in CoolTerm etc.
            printf("Button X pressed\n");
                    lcd.clear();
        lcd.printString("Button Pressed!",0,0);
            lcd.refresh();
            wait(1);
            }
if (g_buttonY_flag){
                   g_buttonY_flag = 0;  // if it has, clear the flag

            // send message over serial port - can observe in CoolTerm etc.
            printf("Button Y pressed\n");
            }
       Buzzer.play(200,120);
            wait_ms(5);
       Buzzer.play(200,120);
        long distanced = sensor.distance(); 
        if (distanced >= 400 || distanced <= 2) 
        {
        printf("Out of range");
//Calling dummy function 05/01/22
        int answer = cube(2);
        printf("%f\n",answer);                  
        wait(1.0);
        }
    else    
    {
       printf("Distance :  %d cm",distanced);
       wait(1.0); // 1 sec  
    }
        lcd.drawCircle(WIDTH/2,HEIGHT/2,distanced,FILL_BLACK); 
//lcd.refresh must be used to update the lcd Display
        lcd.refresh();
        wait(5.0);
//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',WIDTH/2,HEIGHT/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");  
            wait(5);
        }
    
    }  
 }
 */
 }
 }
 }
   int buttonCountA(){
        g_ButtonAPress++;
        if (g_ButtonAPress>5){
            g_ButtonAPress= 0;
            }
        return g_ButtonAPress;
        }
           int buttonCountB(){
        g_ButtonBPress++;
        if (g_ButtonBPress>3){
            g_ButtonBPress= 0;
            }
        return g_ButtonBPress;
        }
   int NoContinueExitScan1(){
        LoopCountScan1++;
        if (LoopCountScan1>10){
            LoopCountScan1= 0;
            }
        return LoopCountScan1;
        }
        int NoContinueExitScan2(){
        LoopCountScan2++;
        if (LoopCountScan2>10){
            LoopCountScan2= 0;
            }
        return LoopCountScan2;
        }
        int NoContinueExitScan3(){
        LoopCountScan3++;
        if (LoopCountScan3>10){
            LoopCountScan3= 0;
            }
        return LoopCountScan3;
        }
           int NoContinueExitScan4(){
        LoopCountScan4++;
        if (LoopCountScan4>10){
            LoopCountScan4= 0;
            }
        return LoopCountScan4;
        }
           int NoContinueExitScan5(){
        LoopCountScan5++;
        if (LoopCountScan5>10){
            LoopCountScan5= 0;
            }
        return LoopCountScan5;
        }
                   int NoContinueExitScan6(){
        LoopCountScan6++;
        if (LoopCountScan6>10){
            LoopCountScan6= 0;
            }
        return LoopCountScan6;
        }
        
        
   //Button A event-triggered interrupt
void buttonA_isr()
{
    
    long distanced = sensor.distance(); 
    buttonCountA();
        
        
    
    g_buttonA_flag = 1;   // set flag in ISR
  
        printf("press count = %i\n",g_ButtonAPress);
            
      // lcd.clear(); 
        //lcd.printString("Button A Pressed",0,0);
        
     //    lcd.refresh(); // set flag in ISR
}
 

  //Button B event-triggered interrupt
void buttonB_isr()
{
    buttonCountB();
    g_buttonB_flag = 1;   // set flag in ISR
       lcd.clear(); 
    g_ButtonAPress = 6; // This is is a work around due to time constraints
        lcd.printString("Button B Pressed",0,0);
        
         lcd.refresh(); // set flag in ISR
           printf("press count = %i\n",g_ButtonBPress);
}
  //Button X event-triggered interrupt
void buttonX_isr()
{
    g_buttonX_flag = 1;   // set flag in ISR
       lcd.clear(); 
        lcd.printString("Button X Pressed",0,0);
         lcd.refresh(); // set flag in ISR
}
  //Button Y event-triggered interrupt
void buttonY_isr()
{
    g_buttonY_flag = 1;   // set flag in ISR
       lcd.clear();       
        lcd.printString("Button Y Pressed",0,0);
         lcd.refresh(); // set flag in ISR
}
  //Button Start event-triggered interrupt
void buttonStart_isr()
{
    g_buttonStart_flag = 1;   // set flag in ISR
       lcd.clear(); 
         /////////////////////////////////////////////////        
    int arr1[5] ;
    /*
    * For loop required to assign array values to that of the scan value
    */ /* not required due to repeatablity issues
    for(int i=0;  i<5; i++){
    arr1[i] =Scan1[i];}    
    int arr2[] = { 90, 90, 90, 90, 90 };
    int n = sizeof(arr1) / sizeof(int);
    int m = sizeof(arr2) / sizeof(int);
  */
 
   // if(MatchMade(arr1, arr2, n, m))
   // printf("You are a wall");
   // else
     //   printf("Not THis Object"); //////////////////////////////////
        
        
        lcd.printString("Button Start Pressed",0,0);
         lcd.refresh(); // set flag in ISR
         
         }
           //Button Back event-triggered interrupt
void buttonBack_isr()
{
    g_buttonBack_flag = 1;   // set flag in ISR
       lcd.clear(); 
        lcd.printString("Button Back Pressed",0,0);
         lcd.refresh(); // set flag in ISR
         }
           //Button Left event-triggered interrupt
void buttonLeft_isr()
{
    g_buttonLeft_flag = 1;   // set flag in ISR
       lcd.clear(); 
        lcd.printString("Button Left Pressed",0,0);
         lcd.refresh(); // set flag in ISR
         }
  //Button Right event-triggered interrupt
void buttonRight_isr()
{
    g_buttonRight_flag = 1;   // set flag in ISR
       lcd.clear(); 
       lcd.printString("Button Right Pressed",0,0);
         lcd.refresh(); // set flag in ISR
         }
void inputScan1()
{
      lcd.clear();        
      lcd.printString("First Scan",0,0); 
      lcd.refresh();
      NoContinueExitScan1();
      
       Buzzer.play(300,120);  lcd.printString("X",0,1); lcd.refresh();       Scan1[0] = sensor.distance();
        wait_ms(5);
        /* NO Longer Required Section 
         Buzzer.play(200,120);  lcd.printString("X",0,2);lcd.refresh();        Scan1[1] = sensor.distance();
        wait_ms(5);
         Buzzer.play(200,120);  lcd.printString("X",0,3);lcd.refresh();        Scan1[2] = sensor.distance();
        wait_ms(5);
         Buzzer.play(200,120);  lcd.printString("X",0,4);lcd.refresh();         Scan1[3] = sensor.distance();;
        wait_ms(5);
         Buzzer.play(200,120);  lcd.printString("X",0,5);lcd.refresh();         Scan1[4] = sensor.distance();;
        wait_ms(5); */
         lcd.printString("Scan 1 Done",0,0); lcd.refresh();
         }
         
void inputScan2()
{
         lcd.clear();
         lcd.printString("Second Scan",0,0);
         lcd.refresh();
         NoContinueExitScan2();
         lcd.printString("X",0,1); //No longer Required Line,lcd.printString("X",0,2),lcd.printString("X",0,3),lcd.printString("X",0,4),lcd.printString("X",0,5); lcd.refresh();
         Buzzer.play(300,120);  lcd.printString("X",15,1); lcd.refresh();       Scan1[1] = sensor.distance();
         wait_ms(5);/*
         Buzzer.play(200,120);  lcd.printString("X",15,2);lcd.refresh();        Scan2[1] = sensor.distance();
         wait_ms(5);
         Buzzer.play(200,120);  lcd.printString("X",15,3);lcd.refresh();        Scan2[2] = sensor.distance();
         wait_ms(5);
         Buzzer.play(200,120);  lcd.printString("X",15,4);lcd.refresh();         Scan2[3] = sensor.distance();
         wait_ms(5);
         Buzzer.play(200,120);  lcd.printString("X",15,5);lcd.refresh();         Scan2[4] = sensor.distance();*/
         lcd.printString("Scan 2 Done",0,0); lcd.refresh();

    }
void inputScan3()
{
         lcd.clear();
         lcd.printString("Third Scan",0,0);
         lcd.refresh();
         NoContinueExitScan3();
         lcd.printString("X",0,1);//No Longer required line,lcd.printString("X",0,2),lcd.printString("X",0,3),lcd.printString("X",0,4),lcd.printString("X",0,5); lcd.refresh();
         lcd.printString("X",15,1);//No Longer required line,lcd.printString("X",15,2),lcd.printString("X",15,3),lcd.printString("X",15,4),lcd.printString("X",15,5); lcd.refresh();
         Buzzer.play(300,120);  lcd.printString("X",30,1); lcd.refresh();       Scan1[2] = sensor.distance();
         wait_ms(5);/*
         Buzzer.play(200,120);  lcd.printString("X",30,2);lcd.refresh();        Scan3[1] = sensor.distance();
        wait_ms(5);
         Buzzer.play(200,120);  lcd.printString("X",30,3);lcd.refresh();        Scan3[2] = sensor.distance();
         wait_ms(5);
         Buzzer.play(200,120);  lcd.printString("X",30,4);lcd.refresh();         Scan3[3] = sensor.distance();
         wait_ms(5);
         Buzzer.play(200,120);  lcd.printString("X",30,5);lcd.refresh();         Scan3[4] = sensor.distance(); */
         lcd.printString("Scan 3 Done",0,0); lcd.refresh(); 
    }
void inputScan4()
{
         lcd.clear();
         lcd.printString("Fourth Scan",0,0); 
         lcd.refresh();
         NoContinueExitScan4();
         lcd.printString("X",0,1);//No Longer required line,lcd.printString("X",0,2),lcd.printString("X",0,3),lcd.printString("X",0,4),lcd.printString("X",0,5); lcd.refresh();
         lcd.printString("X",15,1);//No Longer required line,lcd.printString("X",15,2),lcd.printString("X",15,3),lcd.printString("X",15,4),lcd.printString("X",15,5); lcd.refresh();
         lcd.printString("X",30,1);//No Longer required line,lcd.printString("X",30,2),lcd.printString("X",30,3),lcd.printString("X",30,4),lcd.printString("X",30,5); lcd.refresh();        
         Buzzer.play(300,120);  lcd.printString("X",45,1); lcd.refresh();       Scan1[3] = sensor.distance();
         wait_ms(5);/*
         Buzzer.play(200,120);  lcd.printString("X",45,2);lcd.refresh();        Scan4[1] = sensor.distance();
         wait_ms(5);
         Buzzer.play(200,120);  lcd.printString("X",45,3);lcd.refresh();        Scan4[2] = sensor.distance();
         wait_ms(5);
         Buzzer.play(200,120);  lcd.printString("X",45,4);lcd.refresh();         Scan4[3] = sensor.distance();
         wait_ms(5);
         Buzzer.play(200,120);  lcd.printString("X",45,5);lcd.refresh();         Scan4[4] = sensor.distance(); */
         lcd.printString("Scan 4 Done",0,0); lcd.refresh();
    }
void inputScan5()
{
              lcd.clear();
        lcd.printString("Fifth Scan",0,0);
        lcd.refresh();
        NoContinueExitScan5();
         lcd.printString("X",0,1);//No Longer required line,lcd.printString("X",0,2),lcd.printString("X",0,3),lcd.printString("X",0,4),lcd.printString("X",0,5); lcd.refresh();
         lcd.printString("X",15,1);//No Longer required line,lcd.printString("X",15,2),lcd.printString("X",15,3),lcd.printString("X",15,4),lcd.printString("X",15,5); lcd.refresh();
         lcd.printString("X",30,1);//No Longer required line,lcd.printString("X",30,2),lcd.printString("X",30,3),lcd.printString("X",30,4),lcd.printString("X",30,5); lcd.refresh();
         lcd.printString("X",45,1);//No Longer required line,lcd.printString("X",45,2),lcd.printString("X",45,3),lcd.printString("X",45,4),lcd.printString("X",45,5); lcd.refresh(); 
         Buzzer.play(300,120);  lcd.printString("X",60,1); lcd.refresh();       Scan1[4] = sensor.distance();
         wait_ms(5); /*
         Buzzer.play(200,120);  lcd.printString("X",60,2);lcd.refresh();        Scan5[1] = sensor.distance();
         wait_ms(5);
         Buzzer.play(200,120);  lcd.printString("X",60,3);lcd.refresh();        Scan5[2] = sensor.distance();
         wait_ms(5);
         Buzzer.play(200,120);  lcd.printString("X",60,4);lcd.refresh();         Scan5[3] = sensor.distance();
         wait_ms(5);
         Buzzer.play(200,120);  lcd.printString("X",60,5);lcd.refresh();         Scan5[4] = sensor.distance(); */
         lcd.printString("Scan 5 Done",0,0); lcd.refresh();
    }
    
void inputScan6()
{
                  lcd.clear();
        lcd.printString("Decide",0,0);
        lcd.refresh();
           NoContinueExitScan6();
         int box[5]= {90,10,10,90,90};// compared to the wall if the box is present this sequence should be recorded
 int carton[5] = {10,10,10,10,10}; // compared to the carton if the box is present this sequence should be recorded
 int wall [5] = {90,90,90,90,90}; // compared to the carton if the box is present this sequence should be recorded
 int isBox[5];
 int isCarton[5];
 int isWall[5];
 //int wallTotal;
 //int cartonTotal;
 //int boxTotal;
 for (int i=0; i<5; i++){
   isBox[i] =abs(Scan1[i]-box[i]);
   printf("DeBUGGING isBOX : %i\n", isBox[i]);
    if (isBox[i]<=5){
    isBox[i]=0;}
else if (isBox[i]>5){
isBox[i]=1;}
printf("DeBUGGING isBOX : %i\n", isBox[i]);
boxTotal = accumulate(begin(isBox), end(isBox), 0, plus<int>());
printf("DEBUGGING BOX total %i\n", boxTotal);

  isCarton[i] =abs(Scan1[i]-carton[i]);
   printf("DeBUGGING isCarton : %i\n", isCarton[i]);
    if (isCarton[i]<=5){
    isCarton[i]=0;}
else if (isCarton[i]>5){
isCarton[i]=1;}
printf("DeBUGGING isCarton : %i\n", isCarton[i]);
cartonTotal = accumulate(begin(isCarton), end(isCarton), 0, plus<int>());
printf("DEBUGGING CARTON total %i\n", cartonTotal);

  isWall[i] =abs(Scan1[i]-wall[i]);
   printf("DeBUGGING isWall : %i\n", isWall[i]);
    if (isWall[i]<=5){
    isWall[i]=0;}
else if (isWall[i]>5){
isWall[i]=1;}
printf("DeBUGGING isWall : %i\n", isWall[i]);
wallTotal = accumulate(begin(isWall), end(isWall), 0, plus<int>());
printf("DEBUGGING Wall total %i\n", wallTotal);

;}
printf("Im Free of the For Debug WallTotoal::  %i\n", wallTotal);
printf("Im Free of the For Debug BoxTotoal::  %i\n", boxTotal);
printf("Im Free of the For Debug cartonTotoal::  %i\n", cartonTotal);
printShape();
     
        }
void printShape(){
    if(wallTotal == 0){
     printf("We Have a wall\n");
                   lcd.clear();
        lcd.printString("Wall",0,2);
        lcd.drawLine(20,0,20,5,1);
        lcd.refresh();;}
     else if(boxTotal=0){
         printf("We Have Box\n");}
         else;
         printf("Not in the Database \n");}