Go Down

Topic: Arduino Due Producing Garbage with actual Data. (Read 504 times) previous topic - next topic

Haaz123

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: 

Quote
//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);
   
   
    }
When I Enter "AAAA" in the Serial Monitor I should See Unlocked but instead i see...

   
Quote
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.
   

RayLivingston

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:

Code: [Select]

      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:

Code: [Select]

  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.

Haaz123

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

RayLivingston

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.

Haaz123


Go Up