There are 5 bytes and 8 bits of data that can be send to or receive from iPhone :
Data received from iPhone will be stored in " received_data[n] " ( n= 0 to 4 ) .
Data for sending to iPhone will be stored in " data2send[n] " ( n= 0 to 4 ) .
8 bits received from iPhone will be stored in " received_bits " .
8 bits for sending to iPhone will be stored in " bits2send " .
You have direct access to all these bytes in your program. It can be read or write on them when it needs.
Following instruction codes can be used to read/ write bits :
bitRead(received_bits, x); // x= bit number(0 to 7)
bitWrite(bits2send, x,y); // y= bit value(0 or 1)
Implement following codes in your program. There is a test program in main routine ( Void loop() ) . This will read data from iPhone and send them back to iPhone again. So, if you make a change in "Set bits / Set values" on iPhone then "Monitor bits / Monitor values " will be changed accordingly.
After test completed , remove test program from main routine ( Void loop() ) and put your own program.
//---------------------------------------------------------------------------------------------------------------------------------------------------------
// ArduCom
// Keyvan Shourvarzi 2015
//---------------------------------------------------------------------------------------------------------------------------------------------------------
byte data2send[5]={10,20,30,40,50}; // data bytes for transmission(5 bytes)
byte received_data[5]; // received data bytes (5 bytes)
byte bits2send={85}; // bits for transmission(8 bits)
byte received_bits; // received bits (8 bits)
void setup()
{
Serial2.begin(4800);
Serial3.begin(4800);
}
void loop()
{
// test program
//---------------------------------------------------------------
// Receive data from iPhone and send them back to iPhone
data2send[0]=received_data[0];
data2send[1]=received_data[1];
data2send[2]=received_data[2];
data2send[3]=received_data[3];
data2send[4]=received_data[4];
bits2send=received_bits;
//---------------------------------------------------------------
}
void serialEvent3() {
#define MAX_MILLIS_TO_WAIT 500
unsigned long starttime;
starttime = millis();
byte SendData[8];
byte GetData[8] ;
SendData[0]=66;
SendData[1]=bits2send;
for (int i = 1; i<6; i++) {SendData[i+1]=data2send[i-1]; }
SendData[7]=99;
//send data to serial port 2
Serial2.write(SendData,sizeof(SendData));
//read data from serial port 3
while ( (Serial3.available()<8) && ((millis() - starttime) < MAX_MILLIS_TO_WAIT) ) { }
if(Serial3.available() ==8)
{
for(int n=0; n<8; n++)
GetData[n] = Serial3.read();
if((GetData[0] == 66)&& (GetData[7] == 99)) {
received_bits=GetData[1];
for (int i = 1; i<6; i++) {
received_data[i-1]= GetData[i+1];
}
}
} else { while ( Serial3.available()>0 ) {byte i= Serial3.read();} }
}
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------