I am trying to implement non return to zero space coding for a preset data.
The algorithm works but i get an additional 0x80 when I serial.print my results out.(I used termite software HEX view mode to make sure my algorithm works) Both the original and resulted values are appended with 0x80 at the end.
When I separated the code in concern onto a blank project, it works.
Can someone help me here? I have inserted a screenshot of the 0x80 extra byte that appears after the serial.print.
String outputString="qwert",inputString = ""; // a string to hold incoming data
boolean flag=0,previous=0,stringComplete = false; // whether the string is complete
char c[4]={0x80,0x55,0x6d,0xb0};
void setup() {
// initialize serial:
Serial.begin(9600);
// reserve 200 bytes for the inputString:
inputString.reserve(5);
outputString.reserve(5);
}
void loop() {
// print the string when a newline arrives:
if (stringComplete) {
flag=0;
for(int i=0;i<5;i++){
for(int j=0;j<8;j++){
if(bitRead(inputString[i],7-j)==0){
flag=!flag;
}
bitWrite(inputString[i], 7-j, flag);
}
}
String a=c;
Serial.print(a);
delay(1);
flag=0;
for(int i=0;i<4;i++){
for(int j=0;j<8;j++){
if(bitRead(a[i],7-j)==0){
flag=!flag;
}
bitWrite(a[i], 7-j, flag);
}
}
Serial.print(a);
delay(1);
for(int i=0;i<5;i++){
for(int j=0;j<8;j++){
if (j==0 && i==0)
{
if (bitRead(inputString[i],7)==0){
previous=0;
bitWrite(outputString[i],7,1);
}
else if(bitRead(inputString[i],7)==1){
previous=1;
bitWrite(outputString[i],7,0);
}
}
else
if ( bitRead(inputString[i],7-j)==previous){
bitWrite(outputString[i],7-j,1);
previous=bitRead(inputString[i],7-j);
}
else{
previous=bitRead(inputString[i],7-j);
bitWrite(outputString[i],7-j,0);
}
}
}
// clear the string:
inputString = "";
stringComplete = false;
}
}
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '\n') {
stringComplete = true;
}
}
}
you assume that the String inputstring contains 8 values when stringcomplete. If this is not true the code breaks down on several places.
the use of String class is not preferred (understatement) as it uses Dynamic memory which is very difficult for the Arduino (UNO) which only has 2K RAM. (Arduino has no garbage collector, so RAM will become swiss cheese after a while)
The assignment here makes an assumption that is NOT valid. You get away with it, in one case, by being lucky, not good. It is better to be good.
The assumption is that c is a string that can be assigned to a String. It is NOT. A string is a NULL-terminated array of chars. Your array of chars is NOT NULL -terminated.
The assignment here makes an assumption that is NOT valid. You get away with it, in one case, by being lucky, not good. It is better to be good.
The assumption is that c is a string that can be assigned to a String. It is NOT. A string is a NULL-terminated array of chars. Your array of chars is NOT NULL -terminated.
Thanks for the feedback PaulS. I'm pretty inexperienced with coding as you can see. I'm simply coding to make things work.
Still it doesnt solve the problem of the mysterious 0x80 serial.print when i view the print outs in HEX view mode. This is even after I included the 0x0A aka newline.
Any suggestions or feedbacks are greatly appreciated
Still it doesnt solve the problem of the mysterious 0x80 serial.print when i view the print outs in HEX view mode. This is even after I included the 0x0A aka newline.
Any suggestions or feedbacks are greatly appreciated
I made one. You ignored it. I'm not sure why I'm bothering making another one.
Your String may include garbage because the string that it is incorporating is not NULL terminated. As a result, you can NOT rely on the String being anything other than garbage.
Fix that problem, by properly NULL terminating your char array. NOW!
Still it doesnt solve the problem of the mysterious 0x80 serial.print when i view the print outs in HEX view mode. This is even after I included the 0x0A aka newline.
Any suggestions or feedbacks are greatly appreciated
I made one. You ignored it. I'm not sure why I'm bothering making another one.
Your String may include garbage because the string that it is incorporating is not NULL terminated. As a result, you can NOT rely on the String being anything other than garbage.
Fix that problem, by properly NULL terminating your char array. NOW!
Thanks PaulS i really appreciate your help. FYI, I did include the newline too. Here is a screenshot of my new result.
Still it doesnt solve the 0x80 problem as you can see