DataVault Concept: Example Code
I have created two programs which transmit a string over a wire, from one arduino to another. The transmitter converts the string into characters, and then converts those into 8 bits, which is sends to the receiver by using digitalWrite().
An Ascii lookup table is available here.
The hardware for this is a simple wire connecting two arduino microcontrollers (I am using a Due and a Nano), both using pin 13.
This is a concept test, with a very slow transmission speed. The speed can be changed by altering the “delaytime” variable.
This setup is the absolute simplest that a serial interface could realistically be. There is no data compression, or or data redundancy, CRC checks, hashes, etc. I have not used any libraries.
The way the code works, is that for each byte:
First, the line is set to low, by sending a stream of 1’s.
Then a single one is sent, to indicate that a message is on the way. This is neccessary because if the message begins with a 0, the receiver will not be able to differentiate it from the stream of 1’s.
After this, 8 bits are sent, corresponding to the binary value of an Ascii character.
I have notices that increasing the speed of transmission even a moderate amount results in data corruption. This is the reason why complex algorithms are used when transmitting data. However, if these algorithms were to become obsolete in the future, they data may be impossible to decode.
This system is so simple, that, assuming binary and Ascii formats are still in use in years to come, the data being sent by the transmitter could easily be parsed and understood.
This is the code for the Transmitter:
int SENDpin = 13;
String message = "DataVault Code Example Concept Test2";
int delaytime = 100;
void setup() {
pinMode(SENDpin,OUTPUT);
Serial.begin(9600);
}
void sendByte(byte bmsg){
digitalWrite(SENDpin,0); //Set Line Low
delay(delaytime*10);
digitalWrite(SENDpin,1); //Send Message Indicator
delay(delaytime);
for(int i = 0; i < 8; i++){ //Send individual bits from byte
if(bitRead(bmsg,i)==1){
digitalWrite(SENDpin,1);
Serial.print(1);
}
else{
digitalWrite(SENDpin,0);
Serial.print(0);
}
delay(delaytime);
}
Serial.println(“”);
}
void loop() {
for(int s = 0; s < message.length();s++){ //read each character from the message string
byte bmsg = message[s];
sendByte(bmsg);
Serial.println(bmsg);
}
}
This is the code for the Receiver:
int recvpin = 13;
byte msg = 0;
boolean messagereceived = 0;
int delaytime = 100; //This must be the same on the transmitter and the receiver
void setup() {
pinMode(recvpin, INPUT);
Serial.begin(9600);
}
void loop() {
if(digitalRead(recvpin) == 1){
messagereceived = 1;
msg = 0;
}
delay(delaytime);
if(messagereceived){
for(int i = 0; i < 8; i++){
boolean val = digitalRead(recvpin); // read the input pin
bitWrite(msg,(i),val);
//Serial.print(val);
delay(delaytime);
}
Serial.print((char)msg);
//Serial.println(“”);
messagereceived = 0;
}
}
}
