Arduino Due Producing Garbage with actual Data.

Hi All. Its my first time working with Arduino Due ( or Any Arduino). So Last Nite I was Using My Arduino Due and It was working Fine. But When I used it Today, It Was Not Working so i tried to Check The Output To the Serial Monitor, I saw that its producing “ÿ ÿ” Extra !!!

This Is My Arduino Code:

//enter code here
int lock = 6; //pin 6 on Arduino
int unlock = 7;
int lock2 = 8;
int unlock2 = 9;
int lock3 = 10;
int unlock3 = 11;
char final[4]; //Characters the Arduino will receive
char correct[4] = {‘A’,‘A’,‘A’,‘A’}; //User-Defined Password
char correct2[4] = {‘B’,‘B’,‘B’,‘B’};
char correct3[4] = {‘C’,‘C’,‘C’,‘C’};
int pass_correct = 0; //Does Password match, 0=false 1=true

void setup()
{
pinMode(lock, OUTPUT);
pinMode(lock2, OUTPUT);
pinMode(lock3, OUTPUT);
pinMode(unlock, OUTPUT);
pinMode(unlock2, OUTPUT);
pinMode(unlock3, OUTPUT);
Serial.begin(38400);
Serial2.begin(19200);
Serial3.begin(115200);

digitalWrite(lock, HIGH); //By default, lock is active(locked)
digitalWrite(lock2, HIGH);
digitalWrite(lock3, HIGH);
digitalWrite(unlock, LOW);
digitalWrite(unlock2, LOW);
digitalWrite(unlock3, LOW);
}

void loop()
{
while(Serial.available())
{
Serial.println(“Serial 1 Available”);
for(int i=0; i<4; i++) //While data is available read 4 bytes
{
final = Serial.read(); //Read 4 bytes into the array labled “final”
_ Serial.println(final*);*_

* }*

* for(int i=0; i<4; i++)*
* {*
if(final_==correct*) //Compare each char received to each car in our password in order*
* {
Serial.println(“Serial 1 Block”);_

pass_correct = 1; //If we compare two chars and they match, set the pass_correct variable to true(1)*

* }*
else if(final_==correct2*)
{Serial.println(“In Block 2”);_

pass_correct = 2; //Second Lock*

* break; //End loop and stop comparing chars*
}else if(final_==correct3*){
Serial.println(“Wrong block 3”);_

pass_correct = 3;*

* }else {*
* Serial.println(“Wrong Password”);*
* pass_correct = 0; // if password does not match;
_ break; // end of loop stop comparing*
* }
}
}*_

* while(Serial2.available())*
* {*
* for(int i=0; i<4; i++) //While data is available read 4 bytes*
* {*
_ final = Serial2.read(); //Read 4 bytes into the array labled “final”
* }*_

* for(int i=0; i<4; i++)*
* {*
if(final_==correct*) //Compare each char received to each car in our password in order*
* {_
pass_correct = 1; //If we compare two chars and they match, set the pass_correct variable to true(1)
_ break;
}
else if(final==correct2)
{_

pass_correct = 2; //Second Lock*

* break; //End loop and stop comparing chars*
}else if(final_==correct3*){_
pass_correct = 3;
_ break;
}else {_

pass_correct = 0; // if password does not match;
_ break; // end of loop stop comparing*
* }
}
}*_

* while(Serial3.available())*
* {*
* for(int i=0; i<4; i++) //While data is available read 4 bytes*
* {*
_ final = Serial3.read(); //Read 4 bytes into the array labled “final”
* }*_

* for(int i=0; i<4; i++)*
* {*
if(final_==correct*) //Compare each char received to each car in our password in order*
* {_
pass_correct = 1; //If we compare two chars and they match, set the pass_correct variable to true(1)
_ break;
}
else if(final==correct2)
{_

pass_correct = 2; //Second Lock*

* break; //End loop and stop comparing chars*
}else if(final_==correct3*){_
pass_correct = 3;
_ break;
}else {_

pass_correct = 0; // if password does not match;
_ break; // end of loop stop comparing*
* }
}
}*_

* if(pass_correct==1) //If all chars compared match, deactivate(unlock) the lock for 5 seconds*
* {*
* Serial.println(“Unlocked”);*
* digitalWrite(lock, LOW);*
* digitalWrite(unlock, HIGH);*
* delay(5000);*
* Serial.println(“Locked”);*
* pass_correct = 0;*

* } else if(pass_correct==2) //If all chars compared match, deactivate(unlock) the lock for 5 seconds*
* {*
* Serial.println(“Unlocked”);*
* digitalWrite(lock2, LOW);*
* digitalWrite(unlock2, HIGH);*
* delay(5000);*
* Serial.println(“Locked”);*
* pass_correct = 0;*

* } else if(pass_correct==3) //If all chars compared match, deactivate(unlock) the lock for 5 seconds*
* {*
* Serial.println(“Unlocked”);*
* digitalWrite(lock3, LOW);*
* digitalWrite(unlock3, HIGH);*
* delay(5000);*
* Serial.println(“Locked”);*
* pass_correct = 0;
_ }
else*

* {
digitalWrite(lock, HIGH); //Else if there was not a complete match, keep the lock high(locked)
}*_

_ /* FOR TESTING_
* Serial.print(final[0]);Serial.print(final[1]);Serial.print(final[2]);Serial.print(final[3]);*
* Serial.print(" | ");*
* Serial.print(correct[0]);Serial.print(correct[1]);Serial.print(correct[2]);Serial.print(correct[3]);*
* Serial.print(" ");*
* Serial.print(pass_correct);
_ Serial.println("");
/
delay(500);_

* }[/quote]*
When I Enter “AAAA” in the Serial Monitor I should See Unlocked but instead i see…

> Serial 1 Available
> A
> A
> A
> A
> Serial 1 Block
> Serial 1 Block
> Serial 1 Block
> Serial 1 Block
> Serial 1 Available
*> *
>
*> *
*> *
> ÿ
> ÿ
> Wrong Password
Is It Possible My Arduino Is Damaged?
Is There Any Possible Way To Fix it ?
Please Help As Soon Ass Possible.

There are so many things wrong with the code, I’m surprised it runs at all. First, you check if there are ANY characters available from the serial port, then go ahead and blindly read 4 characters, even though there may have only been 1 available.

Then you read those 4 characters like this:

      for(int i=0; i<4; i++)   //While data is available read 4 bytes
      {
       final = Serial.read();  //Read 4 bytes into the array labled "final"
       Serial.println(final);
       
      }

final is an array, yet you are trying to shove data into it without giving a subscript. Where do you suppose each character goes? Hint: not where you think

Then, you do this:

  if(final==correct)

final and correct are both arrays. This is checking if they both exist at the SAME ADDRESS in memory, which they obviously never will.

I suggest you do some reading on how to work with arrays in c, and how to handle character input. There are many good Arduino-specific tutorials out there that will give you the basics.

Regards,
Ray L.

ok so u r saying its with code and board is fine??? cux it was working fine last nite …

If it was working fine last night, you were very lucky. The code is a mess, and will never work reliably without a lot of changes.

Regards, Ray L.

can u help with the changes ?