This program is the first serial communications protocol I have wrote, attached are 2 separate programs, the 2 are corresponding send and receive programs. I have no experience in serial communications, I just thought it might be fun, I may have copied a previous method I may have come up with something new. They are written using the Arduino IDE. The programs are designed to send a byte of data (transmitted as an array) with a starting "measuring" bit before each transmission. I have used what I know about logic, and try to see the program from the reciever's and transmitters point of view. the acronym title "LaC" stands for length and change, the key elements on this protocol, effectively the change is used as a clock pulse and the length represents a 1 or 0 in the array, the measuring bit is always a long bit, this gives the reciever something to work with. The program has no check sum, no security and no collision avoidance, these will be added at a later date, I designed the protocol to be small in memory size, taking up less than a half of an Attiny85's memory and I aim to keep it under half with a checksum and collision avoidance, this will still allow usable space on the microcontroller. If this protocol is completed successfully I hope to make it into a project showcase.
The only thing you may not notice is that the send and recieve functions do eventually align due to the "givelength()" and "Recievelength()" have multiple pulses in the functions, the recieve script will not change the variable "firsttime" until the conditions are met defining a starting position and the length of the long HIGH/LOW. The variables are a little messy, I intend to clear those up once I know whether this protocol is expected to ever work.
I would be appreciative if members pointed out any problems or improvements to the protocol,
transmit:
Recieve:
The only thing you may not notice is that the send and recieve functions do eventually align due to the "givelength()" and "Recievelength()" have multiple pulses in the functions, the recieve script will not change the variable "firsttime" until the conditions are met defining a starting position and the length of the long HIGH/LOW. The variables are a little messy, I intend to clear those up once I know whether this protocol is expected to ever work.
I would be appreciative if members pointed out any problems or improvements to the protocol,
transmit:
Code:
int Recievepin = 0;
int Sendpin = 1;
int Longcount = 0;
int firsttime = 1;
int sfirsttime = 1;
int bytepos = 0;
int bytelengths[8] = {
0, 0, 0, 0, 0, 0, 0, 0};
int bytebinvalue[8] = {
0, 0, 0, 0, 0, 0, 0, 0};
int bytecount = 0;
byte change0 = 0;
byte change1 = 0;
byte calcdat = 0;
int datar = 0; /////////This is the variable that can be transmitted using a copy of the code on a corresponding microcontroller asign actions to values for transmission
int datas = 0; /////////This is the variable that can be sent to a corresponding microcontroller with a copy of the code asign values to actions for reception
/////////////////////////////////////////////////
void setup(){
pinMode(Recievepin, INPUT);
pinMode(Sendpin, OUTPUT);
}
void loop(){
delay(1);
//commands to recieve data
asignsendvalue();
sendlengthofvaluebuffer();
sendbyteloop();
}
//////////////////////////////////////////////////
//functions for recieving data (removed in this version)
// above are the functions for recieving data (removed in this version)
// below are the functions for sending data
void givelength(){
for(int i = 0; i > 16; i++){
digitalWrite(Sendpin, HIGH);
delay(1);
}
digitalWrite(Sendpin, LOW);
delay(30);
for(int i = 0; i > 16; i++){
digitalWrite(Sendpin, HIGH);
delay(1);
}
digitalWrite(Sendpin, LOW);
delay(5);
sfirsttime = 0;
}
void asignsendvalue(){
calcdat = datas;
for(int i = 7; i<=0; i = i - 1)
{
if(calcdat >= 2^i){
bytebinvalue[i] = 1;
}
else{
bytebinvalue[i] = 0;
}
calcdat = calcdat/2;
}
}
void sendlengthofvaluebuffer(){
bytecount = 0;
while(bytecount < 8){
if(bytebinvalue[bytecount] == 0){
bytelengths[bytecount] = 8;
}
if(bytebinvalue[bytecount] == 1){
bytelengths[bytecount] = 16;
}
bytecount = bytecount + 1;
}
for(int i = 0; i<8; i++){
bytebinvalue[i] = 0;
}
}
void sendbyteloop(){
bytecount = 0;
givelength();
if(sfirsttime == 0){
while(bytecount < 8){
if(bytecount < 8){
digitalWrite(Sendpin, HIGH);
delay(bytelengths[bytecount]);
}
if(bytecount < 8){
digitalWrite(Sendpin, LOW);
delay(bytelengths[bytecount]);
}
}
bytecount = 0;
for(int i = 0; i<8; i++){
bytelengths[i] = 0;
}
}
firsttime = 1;
}
Recieve:
Code:
int Recievepin = 0;
int Sendpin = 1;
int Longcount = 0;
int firsttime = 1;
int sfirsttime = 1;
int bytepos = 0;
int bytelengths[8] = {
0, 0, 0, 0, 0, 0, 0, 0};
int bytebinvalue[8] = {
0, 0, 0, 0, 0, 0, 0, 0};
int bytecount = 0;
byte change0 = 0;
byte change1 = 0;
byte calcdat = 0;
byte recievelength0 = 0;
byte recievelength1 = 0;
int datar = 0; /////////This is the variable that can be transmitted using a copy of the code on a corresponding microcontroller asign actions to values for transmission
int datas = 0; /////////This is the variable that can be sent to a corresponding microcontroller with a copy of the code asign values to actions for reception
/////////////////////////////////////////////////
void setup(){
pinMode(Recievepin, INPUT);
pinMode(Sendpin, OUTPUT);
}
void loop(){
delay(1);
//commands to recieve data
byteloop();
if(firsttime == 0){
lengthofvaluebuffer();
asignvalue();
}
}
//commands to send data
//////////////////////////////////////////////////
//functions for recieving data
void measurelength(){
int Longcountdef;
int recievelength0;
int recievelength1;
if(digitalRead(Recievepin == HIGH)){
Longcount = Longcount + 1;
delay(1);
recievelength0 = 1;
}
if(digitalRead(Recievepin == LOW) && recievelength0 == 1){
recievelength1++;
}
if(digitalRead(Recievepin == HIGH) && recievelength1 > 20){
Longcountdef++;
}
if(Longcountdef * 1.1 > Longcount > Longcountdef * 0.9){
firsttime = 0;
}
}
void byteloop(){
measurelength();
if(firsttime == 0){
while(bytecount < 8){
while(digitalRead(Recievepin == HIGH) && bytecount < 8){
bytelengths[bytecount] = bytelengths[bytecount] + 1;
delay(1);
if(change1 = 1){
bytecount = bytecount + 1;
}
change0 = 1;
change1 = 0;
}
while(digitalRead(Recievepin == LOW) && bytecount < 8){
bytelengths[bytecount] = bytelengths[bytecount] + 1;
delay(1);
if(change0 = 1){
bytecount = bytecount + 1;
}
change1 = 1;
change0 = 0;
}
}
bytecount = 0;
}
}
void lengthofvaluebuffer(){
bytecount = 0;
while(bytecount < 8){
if(bytelengths[bytecount] < Longcount * 0.7){
bytebinvalue[bytecount] = 0;
}
if(bytelengths[bytecount] >= Longcount * 0.7){
bytebinvalue[bytecount] = 1;
}
bytecount = bytecount + 1;
}
for(int i = 0; i<8; i++){
bytelengths[i] = 0;
}
}
void asignvalue(){
for(int i = 0; i<8; i++)
{
if(bytebinvalue[i] == 1){
datar = datar + 2^i;
}
else{
datar = datar;
}
}
for(int i = 0; i<8; i++){
bytelengths[i] = 0;
}
firsttime = 1;
}
// above are the functions for recieving data
// below are the functions for sending data (removed from this version)