MBED GAME PROJECT: SPACE MISSION (ARM CODES)

TRANSMITTER FOR PLAYER1 (ARM1)

#include "mbed.h"

DigitalIn d1(p5);        //push button input
Serial device(p9, p10);  // tx, rx (to transmit data using RF transmitter)
AnalogIn Ain1(p20);      // Accelerometer input

int main() 
{
      device.baud(2400);
      //Rf transmitter code

      while(1)
      { 
           //transmit data in form of string Accelerometer_value#button_value
           device.printf("%3.0f#%d",Ain1.read()*1000,d1.read());
           wait(0.1);
      }
}


TRANSMITTER FOR PLAYER2 (ARM3)

#include "mbed.h"

DigitalIn d1(p5);        //push button input
Serial device(p9, p10);  // tx, rx (to transmit data)
AnalogIn Ain1(p20);      // Accelerometer input

int main() 
{
      device.baud(2400);
      // transmitter code

      while(1)
      { 
           //transmit data in form of string Accelerometer_value#button_value
           device.printf("%3.0f#%d",Ain1.read()*1000,d1.read());
           wait(0.1);
      }
}

RECEIVER FOR PLAYER1 (ARM2)

#include "mbed.h"
 
Serial pc(USBTX, USBRX); // tx, rx for serial communication with laptop
Serial device(p9, p10);  // tx, rx (to receive data using RF receiver)

int main()
 {
    char temp[6]; 
    
    device.baud(2400);

    while (1) 
    {                //RF Receive Code
             if(device.readable())
             {
                     device.gets(temp,6);         //get the string Accelerometer_value#button_value
                     pc.printf("%s\n\r",temp);    //send the string to laptop
                  
             }
        
      }
 
 }

RECEIVER FOR PLAYER2 (ARM4)

#include "mbed.h"
 
Serial pc(USBTX, USBRX); // tx, rx for serial communication with laptop
Serial device(p9, p10);  // tx, rx (to receive data)
LocalFileSystem local("local"); // to save the score

int main() 
{
 char temp[6];
 char ch;
 device.baud(2400);

    while (1) 
    {         //Receiver Code

             if(device.readable())
             {
                    device.gets(temp,6);         //get the string Accelerometer_value#button_value
                    pc.printf("%s\n\r",temp);    //send the string to laptop
                  
             }
             
             if(pc.readable())           //check if the game is over then time to save the score
             {
                    break;     //to come out of loop
             }
    }
    
    FILE *fp = fopen("/local/SCORE.TXT", "w"); //to open file in write mode

    while(pc.readable())
    {
          ch=pc.getc();
          fprintf(fp,"%c",ch); //for writing highest score in flash memory
    }
    
    fclose(fp); //close file

}


Please log in to post comments.