Skip to content

Instantly share code, notes, and snippets.

@fernandozamoraj
Last active January 23, 2019 21:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fernandozamoraj/7411021edbf0f36ba098 to your computer and use it in GitHub Desktop.
Save fernandozamoraj/7411021edbf0f36ba098 to your computer and use it in GitHub Desktop.
fzj_message_receiver_arduino_sketch
/*
fzj_msg_sender
converts bytes into binary digits and sends them out
by Fernando Zamora
*/
const int MAX_MESSAGE_BIT_SIZE = 128; //nice even sixteen bytes of data
const int MAX_MESSAGE_CHAR_SIZE = 16; //nice even sixteen bytes of data
const int DELAY = 10;
int sensorPin = A0;
int loopCount = 0;
int headerBuffer[] = {0,1,0,1,0,1,0,1, 0,1,0,1,0,1,0,1, 0,1,0,1,0,1,0,1, 0,0,0,0,0,0,0,0};
int messageBuffer[MAX_MESSAGE_BIT_SIZE] = {0};
int currentBufferPos = 0;
int messagePos = 0;
int messageBits[MAX_MESSAGE_BIT_SIZE] = {0};
char message[MAX_MESSAGE_CHAR_SIZE] = {0};
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin 13 as an output.
pinMode(13, OUTPUT);
Serial.begin(9600);
}
// the loop function runs over and over again forever
void loop() {
//int currentTime = millis();
int sensorValue = analogRead(sensorPin);
byte bitValue = 0;
if(loopCount > 7 || loopCount == 0){
Serial.println("");
loopCount = 0;
}
//Serial.print(sensorValue);
//Serial.print(" ");
if(sensorValue < 100){
bitValue = 0;
Serial.print("0");
}
else{
bitValue = 1;
Serial.print("1");
}
if(bitValue == headerBuffer[currentBufferPos] || currentBufferPos > 24)
{
currentBufferPos++;
}
else{
currentBufferPos = 0;
}
loopCount = currentBufferPos % 8;;
//header is done...which means it's all message time
if(currentBufferPos == 32){
//Serial.println("");
//Serial.println("matched********");
loopCount = 0;
messagePos = 0;
currentBufferPos = 0;
//delay(4);
goReadMessage();
}
delay(DELAY);
}
void goReadMessage(){
//delay(6);
int currentTime = millis();
int i = 0;
while( i < MAX_MESSAGE_BIT_SIZE){
int newTime = millis();
if((newTime - currentTime) > (DELAY-1))
{
messageBits[i] = analogRead(sensorPin) < 100 ? 0 : 1;
currentTime = newTime;
i++;
}
delayMicroseconds(500);
}
//messageBits[MAX_MESSAGE_BIT_SIZE-1] = '\0';
decodeMessage(messageBits, message);
digitalWrite(13, HIGH);
Serial.println("");
Serial.println("***********MESSAGE***********");
Serial.println("");
Serial.println(message);
Serial.println("");
Serial.println("***********END OF MESSAGE***********");
delay(1000);
digitalWrite(13,LOW);
delay(5000);
}
void transmitMessage(){
char message[] = "hello world\0";
int encodedMessage[MAX_MESSAGE_BIT_SIZE]={0};
encodeMessage(message, encodedMessage);
transmitMessage(encodedMessage);
}
void runTests(){
testEncoding();
testTransmission();
testDecodeByte();
testDecodeMessage();
}
/***********************************************
TESTS FOR TRANSMISSION
***********************************************/
void testTransmission(){
Serial.println("");
Serial.println("**** TESTING TRANSMIT ****");
Serial.println(" transmitting \"hello\" ");
Serial.println("");
char message[] = "hello\0";
int encodedMessage[MAX_MESSAGE_BIT_SIZE]={0};
encodeMessage(message, encodedMessage);
transmitMessage(encodedMessage);
}
/************************************************
TESTS FOR ENCODING
*************************************************/
void testEncoding(){
Serial.println("");
Serial.println("**** TESTING ENCODING ****");
Serial.println(" encoding \"hello\" ");
Serial.println("");
char message[] = "hello\0fool";
int encodedMessage[MAX_MESSAGE_BIT_SIZE]={0};
encodeMessage(message, encodedMessage);
displayFullEncodedMessage(encodedMessage);
Serial.println("");
Serial.println("*** TEST ENCODING IS COMPLETE *****");
Serial.println("");
delay(4000);
}
void displayFullEncodedMessage(int* encodedMessage){
for(int i = 0; i < MAX_MESSAGE_BIT_SIZE; i++){
if(i%8==0){
Serial.println("");
}
Serial.print(encodedMessage[i]);
}
}
void displayBitArray(byte val, int* bitArray){
Serial.println("");
Serial.println(val);
Serial.println((char)val);
for(int i = 0; i < 8; i++){
Serial.print(bitArray[i]);
}
Serial.println("");
}
void testDecodeByte(){
Serial.println("**************TESTING DECODING BYTE**********");
Serial.println("");
for(int i = 0; i <24;i++){
int bitArray[8] = {0};
encodeByte(i, bitArray);
int result = decodeByte(bitArray);
Serial.println("");
Serial.print("DECODED: ");
Serial.print(result);
Serial.println("");
}
Serial.print("");
Serial.println("*******************DECODING********************");
}
void testDecodeMessage(){
Serial.println("");
Serial.println("**** TESTING DECODING ****");
Serial.println(" encoding and then decoding \"hello\" ");
Serial.println("");
char message[] = "decoded hello\0";
int encodedMessage[MAX_MESSAGE_BIT_SIZE]={0};
char decodedMessage[MAX_MESSAGE_CHAR_SIZE]={0};
encodeMessage(message, encodedMessage);
decodeMessage(encodedMessage, decodedMessage);
Serial.println("DECODED MESSAGE: ");
Serial.println(decodedMessage);
Serial.println("");
Serial.println("*** TEST DECODING IS COMPLETE *****");
Serial.println("");
delay(4000);
}
/**********************************************************
ENCODE API SECTION
***********************************************************/
//converts bytes to bit array
//so it would take the value 5 for example
//and convert it to the array with values 0000 0101
//it uses bitShifting so that only the last byte has to be tested
void encodeByte(byte byteValue, int* values){
int tempValue = byteValue;
for(int i = 7; i > -1; i--){
values[i] = tempValue & 1;
tempValue = tempValue >> 1;
}
}
/*
decodeByte converts the 8 element array of 1's and0's
and turns it into a single numeric value
*/
int decodeByte(int* bitArray){
int tempValue = 0;
int result = 0;
for(int i = 7; i > -1; i--){
tempValue = bitArray[i];
//shift the bit to give it the appropriate positional value
tempValue = tempValue << (7-i);
//add that value to the running total
result = result + tempValue;
}
return result;
}
/*
Encodes the string in message into the encodedMessage
the message is in ascii characters
the encoded message is an array of 0's and 1's
*/
void encodeMessage(char* message, int* encodedMessage){
//loop through all the characters in the message array
//e.g. h e l l o in the message "hello"
for(int i = 0; i < MAX_MESSAGE_CHAR_SIZE; i++){
if((byte)message[i] == 0){
break;
}
int bitArray[8]; //holder for the individual bits
encodeByte((byte)message[i], bitArray);
append(encodedMessage, i*8, bitArray);
}
}
/*
decodeMessage takes the array of 1's and 0's in encodedMessage
and converts to a string into the decodedMessage
*/
void decodeMessage(int* encodedMessage, char* decodedMessage){
for(int i = 0; i < MAX_MESSAGE_CHAR_SIZE; i++){
int bitArray[8] = {0};
//every 8 elements creates a single byte to decode
int startingIndex = i*8;
for(int j=0; j<8; j++){
bitArray[j] = encodedMessage[startingIndex+j];
}
//decode the byte and append it to the decoded message
decodedMessage[i] = (char)decodeByte(bitArray);
}
//the last element of the message is reserved for the terminating character
decodedMessage[MAX_MESSAGE_CHAR_SIZE-1] = '\0';
}
//since characters are encoded one byte at a time
//this append function takes care of appending each 8 bit array
//to a full message bit array
void append(int* encodedMessage, int startingIndex, int* bitArray){
for(int i=0; i < 8; i++){
encodedMessage[startingIndex+i] = bitArray[i];
}
}
/**********************************************************
TRANSMIT/RECEIVE API SECTION
***********************************************************/
void transmitMessage(int* encodedMessage){
int onOrOff[2] = {LOW,HIGH};
//transmit start packet
//three bytes of alternating 0's and 1's
for(int i = 0; i < 3; i++){
for(int j=0; j < 4; j++){
digitalWrite(13, LOW);
delay(DELAY);
digitalWrite(13, HIGH);
delay(DELAY);
}
}
//end of packet off for 1 byte
for(int i = 0; i < 8; i++){
digitalWrite(13, LOW);
delay(DELAY);
}
//send message
for(int i = 0; i < MAX_MESSAGE_BIT_SIZE; i++){
digitalWrite(13, onOrOff[encodedMessage[i]]);
delay( DELAY );
}
delay(DELAY*24);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment