FINAL PROJECT isn't it

Fork of ELEC351 by Plymouth ELEC351 Group T

Revision:
47:6d128e500875
Parent:
46:bd9e7e40b3f9
Child:
48:244d6d81bb52
--- a/main.cpp	Mon Jan 08 19:03:49 2018 +0000
+++ b/main.cpp	Mon Jan 08 21:53:40 2018 +0000
@@ -15,767 +15,47 @@
 
 #include "SETUP.hpp"
 #include "NETWORK.hpp"
-
-#define Do_Read_Data 1
-#define Dont_Read_Data 0
-#define Do_Delete_Data 1
-#define Dont_Delete_Data 0
-
-Mutex Time_Lock_Main;
-Mutex Data_Buffer_Lock;
-
-int Write_Data(DATA Data_Store, int Write_Pointer)//Writes data passed in to the array at address Write Pointer
-{
-    Data_Buffer_Lock.lock();//Appling lock for critial section
-    Data_Buffer[Write_Pointer] = Data_Store;
-    if(Write_Pointer < mailsize - 1)//Checks if write pointer will be greater than mail size
-    {
-        Write_Pointer = Write_Pointer + 1;//Increment write pointer
-    }
-    else
-    {
-        Write_Pointer = 0;//Else set it to 0 thus a FIFO system   
-    }
-    Data_Buffer_Lock.unlock();//Releasing lock for critial section
-    return Write_Pointer;//New address of Write pointer
-    
-}
-DATA Read_Data(int Read_Pointer)
-{
-    Data_Buffer_Lock.lock();//Appling lock for critial section
-    DATA Temp_Data = Data_Buffer[Read_Pointer];
-    Data_Buffer_Lock.unlock();//Releasing lock for critial section
-    
-    return Temp_Data;
-}
-void Delete_Data(int Delete_Pointer)
-{
-    Data_Buffer_Lock.lock();//Appling lock for critial section
-    Data_Buffer[Delete_Pointer].set_all_zero();
-    Data_Buffer_Lock.unlock();//Releasing lock for critial section
-}
-
+#include "FIFO.hpp"
+#include "SD_CARD.hpp"
+#include "SERIAL_COMMANDS.hpp"
+#include "SAMPLE.hpp"
+#include "THREADS.hpp"
 void SD_Card()//Writes data to the SD card
 {
     while(1)
     {
         Thread::wait(Sample_Rate*1000); //Waits until a new is taken before checking again
-        if(Write_Pointer == mailsize - 1)
-        {
-            if(Log_Value==1){pc.printf("In SD_Card Thread\n");}
-            
-            //Open to WRITE
-            FILE* fp = fopen("/sd/test.csv","a"); 
-            if(Log_Value==1){pc.printf("File Opened\n");}  
-                   
-            for(int SD_Card_Data_Pointer = 0; SD_Card_Data_Pointer != mailsize; SD_Card_Data_Pointer++)
-            {
-                time_t Time = Data_Buffer[SD_Card_Data_Pointer].get_time();
-                tm* Time_Pointer = localtime(&Time);
-                int temp_day = Time_Pointer->tm_mday;
-                int temp_month = (Time_Pointer->tm_mon+1);//Set to current month
-                int temp_year = (Time_Pointer->tm_year+1900);//Set to current year
-                    
-                int temp_hours = Time_Pointer->tm_hour;
-                int temp_minute = Time_Pointer->tm_min;
-                int temp_seconds = Time_Pointer->tm_sec;
-                    
-                float temp_temperature = Data_Buffer[SD_Card_Data_Pointer].get_temperature();
-                float temp_pressure = Data_Buffer[SD_Card_Data_Pointer].get_pressure();
-                float temp_light = Data_Buffer[SD_Card_Data_Pointer].get_light();
-                
-                fprintf(fp, "Date: %02d/%02d/%d,",temp_day,temp_month,temp_year);//Date
-                fprintf(fp, "Time: %02d:%02d:%02d,",temp_hours,temp_minute, temp_seconds);//Time
-                fprintf(fp, "Temperature: %1.1f,",temp_temperature);//Temperature
-                fprintf(fp, "Pressure: %1.1f,",temp_pressure);//Pressure
-                fprintf(fp, "Light: %5.3f,",temp_light);//Light
-                fprintf(fp, "End of Data \n");//End of data
-            }
-            if(Log_Value==1){pc.printf("Dumped data to SD Card\n");}
-            //Close File
-            fclose(fp);
-        }
+        SD_Card_Write();
     }
 }
-
-bool Decimal_Check(char Input[100])
-{
-    for(int x = 0; x < mailsize; x++)
-    {
-        if(Input[x] == '.'){return true;}
-    }
-    return false;   
-}
-
-int Decimal_Position(char Input[100])
-{
-    for(int x = 0; x < mailsize; x++)
-    {
-        if(Input[x] == '.'){return x;}
-    }
-    return 0;    
-}
 void Network()
 {
     while(1)
     {
         Thread::wait(NetworkWait);//Waits Network Wait amount of time
-        if(Log_Value==1){pc.printf("In Network Thread\n");}
-        
-        time_t Time = Data_Buffer[(Write_Pointer - 1)].get_time();
-        tm* Time_Pointer = localtime(&Time);
-        int temp_day = Time_Pointer->tm_mday;
-        int temp_month = (Time_Pointer->tm_mon+1);//Set to current month
-        int temp_year = (Time_Pointer->tm_year+1900);//Set to current year
-        
-        int temp_hours = Time_Pointer->tm_hour;
-        int temp_minute = Time_Pointer->tm_min;
-        int temp_seconds = Time_Pointer->tm_sec;
-        
-        float temp_temperature = Data_Buffer[(Write_Pointer - 1)].get_temperature();
-        float temp_pressure = Data_Buffer[(Write_Pointer - 1)].get_pressure();
-        float temp_light = Data_Buffer[(Write_Pointer - 1)].get_light();
-
-        
-       Networking(temp_day,temp_month,temp_year,temp_hours,temp_minute,temp_seconds,temp_temperature,temp_pressure,temp_light);//Pass in networking data and time values
+        Networking();
     }
 }
-void LCD_Write_Year()
-{
-        Time_Lock_Main.lock();//Appling lock for critial section
-        if(Log_Value == 1){pc.printf("In LCD_Write_Year Time lock taken\n");}
-        time_t Time = time(NULL);
-        tm* Time_Pointer = localtime(&Time);
-        int Years = 1900 + Time_Pointer->tm_year;
-        Time_Lock_Main.unlock();//Releasing lock for critial section
-        if(Log_Value == 1){pc.printf("In LCD_Write_Year Time lock released\n");}
-        stringstream ss;
-        ss << Years;
-        string Year_String = ss.str();
-        LCD.DDRAM_Address(0x00);
-        LCD.Write_String("Set Year");
-        LCD.DDRAM_Address(0x40);
-        LCD.Write_String(Year_String);   
-}
-void LCD_Write_Month()
-{
-        Time_Lock_Main.lock();//Appling lock for critial section
-        if(Log_Value == 1){pc.printf("In LCD_Write_Month Time lock taken\n");}
-        time_t Time = time(NULL);
-        tm* Time_Pointer = localtime(&Time);
-        int Months = 1 + Time_Pointer->tm_mon;
-        Time_Lock_Main.unlock();//Releasing lock for critial section
-        if(Log_Value == 1){pc.printf("In LCD_Write_Month Time lock released\n");}
-        stringstream ss;
-        ss << Months;
-        string Month_String = ss.str();
-        LCD.DDRAM_Address(0x00);
-        LCD.Write_String("Set Month");
-        LCD.DDRAM_Address(0x40);
-        LCD.Write_String(Month_String);   
-}
-void LCD_Write_Day()
-{
-        Time_Lock_Main.lock();//Appling lock for critial section
-        if(Log_Value == 1){pc.printf("In LCD_Write_Day Time lock taken\n");}
-        time_t Time = time(NULL);
-        tm* Time_Pointer = localtime(&Time);
-        int Days = Time_Pointer->tm_mday;
-        Time_Lock_Main.unlock();//Releasing lock for critial section
-        if(Log_Value == 1){pc.printf("In LCD_Write_Day Time lock released\n");}
-        stringstream ss;
-        ss << Days;
-        string Day_String = ss.str();
-        LCD.DDRAM_Address(0x00);
-        LCD.Write_String("Set Day");
-        LCD.DDRAM_Address(0x40);
-        LCD.Write_String(Day_String);   
-}
-void LCD_Write_Hour()
+void LCD_Output()
 {
-        Time_Lock_Main.lock();//Appling lock for critial section
-        if(Log_Value == 1){pc.printf("In LCD_Write_Hour Time lock taken\n");}
-        time_t Time = time(NULL);
-        tm* Time_Pointer = localtime(&Time);
-        int Hours = Time_Pointer->tm_hour;
-        Time_Lock_Main.unlock();//Releasing lock for critial section
-        if(Log_Value == 1){pc.printf("In LCD_Write_Hour Time lock released\n");}
-        stringstream ss;
-        ss << Hours;
-        string Hour_String = ss.str();
-        LCD.DDRAM_Address(0x00);
-        LCD.Write_String("Set Hour");
-        LCD.DDRAM_Address(0x40);
-        LCD.Write_String(Hour_String);   
-}
-void LCD_Write_Minute()
-{
-        Time_Lock_Main.lock();//Appling lock for critial section
-        if(Log_Value == 1){pc.printf("In LCD_Write_Minute Time lock taken\n");}
-        time_t Time = time(NULL);
-        tm* Time_Pointer = localtime(&Time);
-        int Minutes = Time_Pointer->tm_min;
-        Time_Lock_Main.unlock();//Releasing lock for critial section
-        if(Log_Value == 1){pc.printf("In LCD_Write_Minute Time lock released\n");}
-        stringstream ss;
-        ss << Minutes;
-        string Minute_String = ss.str();
-        LCD.DDRAM_Address(0x00);
-        LCD.Write_String("Set Minute");
-        LCD.DDRAM_Address(0x40);
-        LCD.Write_String(Minute_String);   
-}
-void LCD_Write_Seconds()
-{
-        Time_Lock_Main.lock();//Appling lock for critial section
-        if(Log_Value == 1){pc.printf("In LCD_Write_Seconds Time lock taken\n");}
-        time_t Time = time(NULL);
-        tm* Time_Pointer = localtime(&Time);
-        int Seconds = Time_Pointer->tm_sec;
-        Time_Lock_Main.unlock();//Releasing lock for critial section
-        if(Log_Value == 1){pc.printf("In LCD_Write_Seconds Time lock released\n");}
-        stringstream ss;
-        ss << Seconds;
-        string Second_String = ss.str();
-        LCD.DDRAM_Address(0x00);
-        LCD.Write_String("Set Second");
-        LCD.DDRAM_Address(0x40);
-        LCD.Write_String(Second_String);   
-}
-
-void LCD_Output()
-{      
     while(1)
     {
         Thread::wait(10);//Small wait
-        if(mode == 0)//Default mode
-        { 
-            if(Log_Value == 1){pc.printf("In mode 0 \n");}
-            
-            Thread::wait(Default_Mode_Toggle_Time);//Wait for this amount of time
-            if(Log_Value == 1){pc.printf("Writing Data to LCD\n");}
-            sprintf (LCD_buffer, "%1.1f  %1.0f  %1.2f",Data_Active.get_temperature(),Data_Active.get_pressure(),Data_Active.get_light());//Used for converting to a sting  
-             
-            LCD.DDRAM_Address(0x00);
-            LCD.Write_String("Temp  Pres  Lite");
-            LCD.DDRAM_Address(0x40);
-            LCD.Write_String(LCD_buffer);//Print out current data values to the LCD
-
-            Thread::wait(Default_Mode_Toggle_Time); //Wait for this amount of time   
-            if(Log_Value == 1){pc.printf("Writing Time and Date to LCD\n");}
-            Time_Lock_Main.lock();//lock Time_lock for critial section
-            time_t Time = Data_Active.get_time();
-            tm* Time_Pointer = localtime(&Time);
-            LCD.Display_Clear();
-            sprintf (LCD_buffer, "%02d:%02d    %02d,%d",Time_Pointer->tm_hour,Time_Pointer->tm_min,(Time_Pointer->tm_mon+1),(Time_Pointer->tm_year+1900));//Used for converting to a sting  
-            Time_Lock_Main.unlock();//unlock Time_lock for critial section
-            
-            LCD.DDRAM_Address(0x00);
-            LCD.Write_String("Current Time:");
-            LCD.DDRAM_Address(0x40);
-            LCD.Write_String(LCD_buffer);//Print the current time to the LCD
-            
-            if(Log_Value == 1){pc.printf("Checking Switches for next mode\n");}
-            if(SW1.read() & SW2.read() == 1)
-            {
-                mode = 1;   
-            }
-        }
-        else if(mode == 1)//Choose either date setting or time setting
-        {
-            if(Log_Value == 1){pc.printf("In Mode 1\n");}
-            LCD.Display_Clear();
-            while(1)
-            {
-                LCD.DDRAM_Address(0x00);
-                LCD.Write_String("Date        Time");
-                LCD.DDRAM_Address(0x40);
-                LCD.Write_String("<              >");
-                Thread::wait(1000);
-                if(Log_Value == 1){pc.printf("Checking SW1 to go to Date setting\n");}
-                if(SW1.read() == 1 & SW2.read() == 0)
-                {
-                    mode = 2;//Date Setting
-                    break;   
-                }
-                if(Log_Value == 1){pc.printf("Checking SW2 to go to Time setting\n");}
-                if(SW2.read() == 1 & SW1.read() == 0)
-                {
-                    mode = 5;//Time Setting
-                    break;
-                }
-            }
-        } 
-        else if(mode == 2)//Set the Year
-        {
-            if(Log_Value == 1){pc.printf("In Mode 2\n");}
-            LCD.Display_Clear();
-            while(1)
-            {          
-                LCD_Write_Year();
-                Thread::wait(1000);
-                 if(Log_Value == 1){pc.printf("Checking SW1 and SW2 to go to Month setting\n");}
-                if(SW1.read() & SW2.read() == 1)
-                {
-                    mode = 3;
-                    break;   
-                }
-                if(Log_Value == 1){pc.printf("Checking SW1 to add Year\n");}
-                if(SW1.read() == 1 & SW2.read() == 0)
-                {
-                    Add_Year();
-                }
-                if(Log_Value == 1){pc.printf("Checking SW2 to subtract Year\n");}
-                else if(SW2.read() == 1 & SW1.read() == 0)
-                {
-                    Subtract_Year();   
-                }
-                LCD_Write_Year();
-            }
-        } 
-        else if(mode == 3)//Set the Month
-        {
-            if(Log_Value == 1){pc.printf("In Mode 3\n");}
-            LCD.Display_Clear();
-            while(1)
-            {
-                LCD_Write_Month();
-                Thread::wait(1000);
-                if(Log_Value == 1){pc.printf("Checking SW1 and SW2 to go to Day setting\n");}
-                if(SW1.read() & SW2.read() == 1)
-                {
-                    mode = 4;
-                    break;   
-                }
-                if(Log_Value == 1){pc.printf("Checking SW1 to add Month\n");}
-                if(SW1.read() == 1 & SW2.read() == 0)
-                {
-                    Add_Month();
-                }
-                if(Log_Value == 1){pc.printf("Checking SW2 to subtract Month\n");}
-                else if(SW2.read() == 1 & SW1.read() == 0)
-                {
-                    Subtract_Month();   
-                }                
-
-            }
-        }
-        else if(mode == 4)//Set the Day
-        {
-            if(Log_Value == 1){pc.printf("In Mode 4\n");}
-            LCD.Display_Clear();
-            while(1)
-            {      
-                LCD_Write_Day();
-                Thread::wait(1000);
-                if(Log_Value == 1){pc.printf("Checking SW1 and SW2 to go Default setting\n");}
-                if(SW1.read() & SW2.read() == 1)
-                {
-                    mode = 0;   
-                    break;
-                }
-                if(Log_Value == 1){pc.printf("Checking SW1 to add Day\n");}
-                if(SW1.read() == 1 & SW2.read() == 0)
-                {
-                    Add_Day();
-                }
-                if(Log_Value == 1){pc.printf("Checking SW2 to subtract Day\n");}
-                else if(SW2.read() == 1 & SW1.read() == 0)
-                {
-                    Subtract_Day();   
-                }                
-            }
-        } 
-        else if(mode == 5)//Set the Hour
-        {
-            if(Log_Value == 1){pc.printf("In Mode 5\n");}
-            LCD.Display_Clear();
-            while(1)
-            {
-                LCD_Write_Hour();
-                Thread::wait(1000);
-                if(Log_Value == 1){pc.printf("Checking SW1 and SW2 to go Minute setting\n");}
-                if(SW1.read() & SW2.read() == 1)
-                {
-                    mode = 6;  
-                    break; 
-                }
-                if(Log_Value == 1){pc.printf("Checking SW1 to add Hour\n");}                
-                if(SW1.read() == 1 & SW2.read() == 0)
-                {
-                    Add_Hour();
-                }
-                if(Log_Value == 1){pc.printf("Checking SW2 to subtract Hour\n");} 
-                else if(SW2.read() == 1 & SW1.read() == 0)
-                {
-                    Subtract_Hour();   
-                }
-            }
-        } 
-        else if(mode == 6)//Set the Minute
-        {
-            if(Log_Value == 1){pc.printf("In Mode 6\n");}
-            LCD.Display_Clear();
-            while(1)
-            {
-                LCD_Write_Minute();
-                Thread::wait(1000);
-                if(Log_Value == 1){pc.printf("Checking SW1 and SW2 to go Seconds setting\n");}
-                if(SW1.read() & SW2.read() == 1)
-                {
-                    mode = 7;  
-                    break; 
-                }                
-                if(Log_Value == 1){pc.printf("Checking SW1 to add Minute\n");}
-                if(SW1.read() == 1 & SW2.read() == 0)
-                {
-                    Add_Minute();
-                }
-                if(Log_Value == 1){pc.printf("Checking SW2 to subtract Minute\n");}
-                else if(SW2.read() == 1 & SW1.read() == 0)
-                {
-                    Subtract_Minute();   
-                }                
-            }
-        }  
-        else if(mode == 7)//Set the Seconds
-        {
-            if(Log_Value == 1){pc.printf("In Mode 7\n");}
-            LCD.Display_Clear();
-            while(1)
-            {
-                LCD_Write_Seconds();
-                Thread::wait(1000);
-                if(Log_Value == 1){pc.printf("Checking SW1 and SW2 to go Default setting\n");}
-                if(SW1.read() & SW2.read() == 1)
-                {
-                    mode = 0;  
-                    break; 
-                }
-                if(Log_Value == 1){pc.printf("Checking SW1 to add Second\n");}                
-                if(SW1.read() == 1 & SW2.read() == 0)
-                {
-                    Add_Second();
-                }
-                if(Log_Value == 1){pc.printf("Checking SW1 to subtract Second\n");}   
-                else if(SW2.read() == 1 & SW1.read() == 0)
-                {
-                    Subtract_Second();   
-                }                
-            }
-        }
-        else
-        {
-            if(Log_Value == 1){pc.printf("Mode Error occured mode now set to 0\n");}   
-            mode = 0;
-        }
+        LCD_Print_Output();
     }
 }
-void Serial_Commands()//Used for getting input from the user to determine the opperations to perform
+void Serial_Commands()
 {
-    char input[100];
-    while(1) {
-        
-        if(Log_Value == 1){pc.printf("In Serial_Commands\n");}   
-         for (int x = 0; x < 100; x++){input[x] = ' ';};
-        
-        pc.printf("Please type in a command\n");//Log this 
-        
-        cin.getline(input,sizeof(input),'\r');
-        
-        //READ ALL
-        if(input[0] == 'R' & input[1] == 'E' & input[2] == 'A' & input[3] == 'D' & input[4] == ' ' & input[5] == 'A' & input[6] == 'L' & input[7] == 'L')
-        {
-            if(Log_Value == 1){pc.printf("READ ALL Confirmed\n");}
-            
-            Data_Buffer_Lock.lock();//Lock data buffer due to critical section
-            int Start_Address_Read_All = Write_Pointer;
-            Data_Buffer_Lock.unlock();//unLock data buffer due to critical section
-             
-            for(int x_Read_All = (Start_Address_Read_All - mailsize); x_Read_All != Start_Address_Read_All; x_Read_All = x_Read_All )
-            {
-                if(x_Read_All < 0){x_Read_All = x_Read_All + mailsize;}
-                else              {x_Read_All = x_Read_All;}
-                
-                DATA Serial_Read_Data = Read_Data(x_Read_All);
-                
-                time_t Time = Serial_Read_Data.get_time();
-                tm* Time_Pointer = localtime(&Time);
-               
-                pc.printf("Date = %02d %02d %d ,\t", Time_Pointer->tm_mday, (Time_Pointer->tm_mon+1),(Time_Pointer->tm_year+1900));
-                pc.printf("Time = %02d:%02d:%02d ,\t", Time_Pointer->tm_hour, Time_Pointer->tm_min, Time_Pointer->tm_sec);                                           //Print the string formatted time
-                pc.printf("Temperature = %f ,\t", Serial_Read_Data.get_temperature());                     //Print Temperature
-                pc.printf("Pressure = %f ,\t", Serial_Read_Data.get_pressure());                           //Print Pressure
-                pc.printf("Light = %f ,\n", Serial_Read_Data.get_light());                               //Print Light
-
-                if(x_Read_All == mailsize - 1){x_Read_All = 0;}
-                else                          {x_Read_All = x_Read_All + 1;}
-            }
-        }
-        
-        //DELETE ALL FIX THIS - Might need to disable sampling during the delete
-        else if(input[0] == 'D' & input[1] == 'E' & input[2] == 'L' & input[3] == 'E' & input[4] == 'T' & input[5] == 'E' & input[6] == ' ' & input[7] == 'A' & input[8] == 'L' & input[9] == 'L')
-        {   
-            if(Log_Value == 1){pc.printf("DELETE ALL Confirmed\n");}
-            
-            Data_Buffer_Lock.lock();
-            int Start_Address = Write_Pointer;
-            Data_Buffer_Lock.unlock();
-             
-            for(int x_Delete_All = (Start_Address - mailsize); x_Delete_All != Start_Address; x_Delete_All = x_Delete_All)
-            {
-                if(x_Delete_All < 0){x_Delete_All = x_Delete_All + mailsize;}
-                else                {x_Delete_All = x_Delete_All;}
-                
-                Delete_Data(x_Delete_All);   
-                
-                if(x_Delete_All == mailsize - 1){x_Delete_All = 0;}
-                else                            {x_Delete_All = x_Delete_All + 1;}
-
-            }
-            pc.printf("DELETED %d RECORDS\n",mailsize);
-        }
-        
-        //READ
-        else if(input[0] == 'R' & input[1] == 'E' & input[2] == 'A' & input[3] == 'D' & input[4] == ' ')
-        {
-            int NumberOfChars = 0; int ArrayAddress = 0; string ReadNumber; int NumberToRead;
-            while(input[ArrayAddress] != '\0'){NumberOfChars++; ArrayAddress++;}
-            for(int x=5; x < NumberOfChars; x++){ReadNumber += input[x];}
-            stringstream Number(ReadNumber);
-            Number >> NumberToRead;
-            
-            if(NumberToRead > mailsize)
-            {
-                NumberToRead = mailsize;
-            }
-            
-            if(Log_Value == 1){pc.printf("Getting Data\n");}
-            
-            Data_Buffer_Lock.lock();//Lock data buffer due to critical section
-            int Start_Address = Write_Pointer;
-            Data_Buffer_Lock.unlock();//unLock data buffer due to critical section
-             
-            for(int x = (Start_Address - NumberToRead); x != Start_Address; x = x )
-            {
-                if(x < 0){x = x + mailsize;}
-                else     {x = x;}
-                
-                DATA Serial_Read_Data = Read_Data(x);
-                
-                time_t Time = Serial_Read_Data.get_time();
-                tm* Time_Pointer = localtime(&Time);
-        
-                pc.printf("Date = %02d %02d %d ,\t", Time_Pointer->tm_mday, (Time_Pointer->tm_mon+1),(Time_Pointer->tm_year+1900));
-                pc.printf("Time = %02d:%02d:%02d ,\t", Time_Pointer->tm_hour, Time_Pointer->tm_min, Time_Pointer->tm_sec);                                           //Print the string formatted time
-                pc.printf("Temperature = %f ,\t", Serial_Read_Data.get_temperature());                     //Print Temperature
-                pc.printf("Pressure = %f ,\t", Serial_Read_Data.get_pressure());                           //Print Pressure
-                pc.printf("Light = %f ,\n", Serial_Read_Data.get_light());                               //Print Light
-    
-                if(x == mailsize - 1){x = 0;}
-                else                 {x = x + 1;}
-            }
-            if(Log_Value == 1){pc.printf("Read %d samples\n",NumberToRead);}
-            
-        } 
-        
-        //DELETE look into this mgiht need to turn off sampling
-        else if(input[0] == 'D' & input[1] == 'E' & input[2] == 'L' & input[3] == 'E' & input[4] == 'T' & input[5] == 'E' & input[6] == ' ')
-        {
-            int NumberOfChars = 0; int ArrayAddress = 0; string DeleteNumber; int NumberToDelete;
-            while(input[ArrayAddress] != '\0'){NumberOfChars++; ArrayAddress++;}
-            for(int x=7; x < NumberOfChars; x++){DeleteNumber += input[x];}
-            stringstream Number(DeleteNumber);
-            Number >> NumberToDelete;
-            
-            if(NumberToDelete > mailsize)
-            {
-                NumberToDelete = mailsize;
-            }
-            if(Log_Value == 1){pc.printf("Deleted %d samples\n",NumberToDelete);}
-            
-            Data_Buffer_Lock.lock();
-            int Start_Address = Write_Pointer;
-            Data_Buffer_Lock.unlock();
-             
-            for(int x_Delete = (Start_Address - NumberToDelete); x_Delete != Start_Address; x_Delete = x_Delete )
-            {
-                if(x_Delete < 0){x_Delete = x_Delete + mailsize;}
-                else            {x_Delete = x_Delete;}
-                
-                Delete_Data(x_Delete);   
-                
-                if(x_Delete == mailsize - 1){x_Delete = 0;}
-                else                        {x_Delete = x_Delete + 1;}
+    while(1)
+    {
+        Serial_Commands_Output();   
+    }
 
-            }
-        pc.printf("DELETED %d RECORDS\n",NumberToDelete);
-        if(Log_Value == 1){pc.printf("Deleted %d samples\n",NumberToDelete);}
-        }
-        
-        //STATE
-        else if(input[0] == 'S' & input[1] == 'T' & input[2] == 'A' & input[3] == 'T' & input[4] == 'E' & input[5] == ' ')
-        {
-            int NumberOfChars = 0; int ArrayAddress = 0; string StateNumber; int NumberToState;
-            while(input[ArrayAddress] != '\0'){NumberOfChars++; ArrayAddress++;}
-            for(int x=6; x < NumberOfChars; x++){StateNumber += input[x];}
-            stringstream Number(StateNumber);
-            Number >> NumberToState;
-            
-            if(NumberToState==0)        {Sampling_Timer.detach(); Console_Output_Timer.detach(); pc.printf("SAMPLING 0\n");}//Stop Sampling
-            else if(NumberToState==1)   {Sampling_Timer.attach(&Sampling_ISR,Sample_Rate);       pc.printf("SAMPLING 1\n");}//Start Sampling
-            else                        {pc.printf("Invalid State\n");}
-            
-        } 
-        
-        //SETDATE
-        else if(input[0] == 'S' & input[1] == 'E' & input[2] == 'T' & input[3] == 'D' & input[4] == 'A' & input[5] == 'T' & input[6] == 'E' & input[7] == ' ' & input[10] == ' ' & input[13] == ' ')
-        {
-            //int NumberOfChars = 0; int ArrayAddress = 0; 
-            string DayNumber, MonthNumber, YearNumber;
-            int NumberToDay, NumberToMonth, NumberToYear;
-            
-            for(int x=8; x < 10; x++){DayNumber += input[x];}
-            stringstream Number_1(DayNumber);
-            Number_1 >> NumberToDay;
-            
-            for(int x=11; x < 13; x++){MonthNumber += input[x];}
-            stringstream Number_2(MonthNumber);
-            Number_2 >> NumberToMonth;
-            
-            for(int x=14; x < 18; x++){YearNumber += input[x];}
-            stringstream Number_3(YearNumber);
-            Number_3 >> NumberToYear;
-            
-            set_new_date(NumberToDay,NumberToMonth,NumberToYear);
-        pc.printf("DATE UPDATED TO %02d %02d %d\n",NumberToDay,NumberToMonth,NumberToYear);
-        }
-        
-        //SETTIME
-        else if(input[0] == 'S' & input[1] == 'E' & input[2] == 'T' & input[3] == 'T' & input[4] == 'I' & input[5] == 'M' & input[6] == 'E' & input[7] == ' ' & input[10] == ' ' & input[13] == ' ')
-        {
-            //int NumberOfChars = 0; int ArrayAddress = 0; 
-            string HourNumber, MinuteNumber, SecondNumber;
-            int NumberToHour, NumberToMinute, NumberToSecond;
-            
-            for(int x=8; x < 10; x++){HourNumber += input[x];}
-            stringstream Number_1(HourNumber);
-            Number_1 >> NumberToHour;
-            
-            for(int x=11; x < 13; x++){MinuteNumber += input[x];}
-            stringstream Number_2(MinuteNumber);
-            Number_2 >> NumberToMinute;
-            
-            for(int x=14; x < 16; x++){SecondNumber += input[x];}
-            stringstream Number_3(SecondNumber);
-            Number_3 >> NumberToSecond;
-            
-            set_new_time(NumberToHour,NumberToMinute,NumberToSecond); 
-            pc.printf("TIME UPDATED TO %02d %02d %02d\n",NumberToHour,NumberToMinute,NumberToSecond);   
-        } 
-        
-        //SETT
-        else if(input[0] == 'S' & input[1] == 'E' & input[2] == 'T' & input[3] == 'T' & input[4] == ' ')
-        {
-            
-            int NumberOfChars = 0; int ArrayAddress = 0; 
-            string SettNumber; double NumberToSett; 
-            int Decimal_Divider = 10; 
-            float Sample_Rate_Integer = 0; string Sample_Rate_Integer_String;
-            float Sample_Rate_Decimal = 0; string Sample_Rate_Decimal_String;
-            while(input[ArrayAddress] != '\0'){NumberOfChars++; ArrayAddress++;}
-                     
-            if (Decimal_Check(input) == true)
-            {
-                for(int x = 5; x < Decimal_Position(input); x++){Sample_Rate_Integer_String += input[x];}
-                stringstream Number_1(Sample_Rate_Integer_String);
-                Number_1 >> Sample_Rate_Integer;
-        
-                for(int x = Decimal_Position(input) + 1; x < NumberOfChars; x++){Sample_Rate_Decimal_String += input[x];}
-                stringstream Number_2(Sample_Rate_Decimal_String);
-                Number_2 >> Sample_Rate_Decimal;
-                
-                for(int tens_power = 1; tens_power != NumberOfChars - Decimal_Position(input) - 1; tens_power++)
-                {
-                    Decimal_Divider = Decimal_Divider * 10;   
-                }
-                NumberToSett = Sample_Rate_Integer + (Sample_Rate_Decimal / float(Decimal_Divider));
-            }
-            else                        
-            {
-                for(int x=5; x < NumberOfChars; x++){SettNumber += input[x];}
-                stringstream Number_3(SettNumber);
-                Number_3 >> NumberToSett;
-            }
-               
-            if(NumberToSett >= 0.1 & NumberToSett <= 60)
-            {
-                Sample_Rate = NumberToSett;
-                pc.printf("T UPDATED TO %1.1f \n",NumberToSett);
-            }
-            else
-            {
-                Sample_Rate = Sample_Rate;//No change
-                pc.printf("OUT OF RANGE\n");
-            }
-             
-            Sampling_Timer.detach();
-            Console_Output_Timer.detach();
-            Sampling_Timer.attach(&Sampling_ISR,Sample_Rate);
-        }
-        //LOGGING
-        else if(input[0] == 'L' & input[1] == 'O' & input[2] == 'G' & input[3] == 'G' & input[4] == 'I' & input[5] == 'N' & input[6] == 'G' & input[7] == ' ')
-        {
-            int NumberOfChars = 0; int ArrayAddress = 0; string LoggingNumber; int NumberToLogging;
-            while(input[ArrayAddress] != '\0'){NumberOfChars++; ArrayAddress++;}
-            for(int x=8; x < NumberOfChars; x++){LoggingNumber += input[x];}
-            stringstream Number(LoggingNumber);
-            Number >> NumberToLogging;
-            if(NumberToLogging != 1)
-            {
-                NumberToLogging = 0;   
-            }
-            Log_Value = NumberToLogging;
-            pc.printf("LOGGING %d\n",NumberToLogging);  
-        } 
-        
-        //HELP
-        else if(input[0] == 'H' & input[1] == 'E' & input[2] == 'L' & input[3] == 'P')// Use this to display all of the availble commands
-        { 
-            HELP();//Run HELP Command
-        }
-        else 
-        {
-            pc.printf("Please enter an acceptable command\n");
-        }
-    }
 }
 
-void Sampling_ISR(){t1.signal_set(SamplingTime);}
-
-
-void Sample()//Samples the hardware and prints the result to the LCD
+void Sample()
 {
-    while(1) 
-    {
-        Thread::signal_wait(SamplingTime);      //Set the time between samples
-        if(Log_Value == 1){pc.printf("Sample Time\n");} 
-        Data_Active.set_temperature(sensor.getTemperature());   //Read Temperature
-        Data_Active.set_pressure(sensor.getPressure());         //Read Pressure
-        Data_Active.set_light(adcIn.read());                    //Read Light
-        
-        Time_Lock_Main.lock();
-        time_t buffer_time = time(NULL);        
-        Time_Lock_Main.unlock();
-
-        Data_Active.set_time(buffer_time);
-
-        Red_led.flash(0.02);//ahh he saved everyone of us
-        Write_Pointer = Write_Data(Data_Active,Write_Pointer);
-        t1.signal_set(NotSamplingTime);
-        if(Log_Value == 1){pc.printf("New Sample avaliable\n");} 
-        t2.signal_set(SD_Data_Ready);
-    }
+    Sample_Event();   
 }
 int main()
 {