Pages: [1]   Go Down
Author Topic: Commenting Serial.println line change behaviour  (Read 389 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I'm building a little robot and I try to make him avoid objects, so I put an IR Sensor on it and did some tests.
Here is the interesting part of the code :

Code:
if( IR_Detection_Angle > 100 ) {       // Detected on the Left
    //Serial.println(String("Detection on the left at ") + IR_Detection_Angle);
    //Serial.println(voltage);
    //Serial.println(String("---------------------------------"));
      if(IR_Detection_Angle > 110){
        State_Current = STATE_LOW_RIGHT;
      }
      else{
        State_Current = STATE_HARD_RIGHT;
      }
    }
    else if( IR_Detection_Angle < 80 ) {   // Detected on the Right
    //Serial.println(String("Detection on the right at ") + IR_Detection_Angle);
    //Serial.println(voltage);
    //Serial.println(String("---------------------------------"));
      if(IR_Detection_Angle > 70){
        State_Current = STATE_LOW_LEFT;
      }
      else{
        State_Current = STATE_HARD_LEFT;
      }
    }
    else {                                 // Detected in Front
    //Serial.println(String("Detection in the center at ") + IR_Detection_Angle);
    //Serial.println(voltage);
    //Serial.println(String("---------------------------------"));
      State_Current = STATE_REVERSE;
    }

As you can see there is nine commented lines.
if these lines stay commented, the robot State will never change !
but if I uncomment anyone of them, the "if" block within the line is will execute.
Curious ... can anybody help me please ?
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 212
Posts: 8952
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you ae using a lot of memory then any of those string constants an push your memory use beyond the space available which can cause any number of symptoms.  Try keeping those string constants in FLASH:

Code:
if( IR_Detection_Angle > 100 ) {       // Detected on the Left
    Serial.print(F("Detection on the left at "));
    Serial.println(IR_Detection_Angle);
    Serial.println(voltage);
    Serial.println(F("---------------------------------"));
      if(IR_Detection_Angle > 110){
        State_Current = STATE_LOW_RIGHT;
      }
      else{
        State_Current = STATE_HARD_RIGHT;
      }
    }
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for the information, it will be helpful for my further projects!

But this is not the point, the code STOP working when I COMMENT these Serial.println lines.
When I uncomment them, the code execute fine, but it is slow because of these lines!

I hope the problem is clear now  smiley if not, I will post exemple of working and non-working code!

Thank you for helping me  smiley
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 631
Posts: 50163
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If commenting out some Serial.print() statements in that snippet of code causes problems, then, the problem is not in that snippet of code.

This is an atrocity:
Code:
    //Serial.println(String("---------------------------------"));
Wrapping a char array in a String so that the println() method has to unwrap it is a complete waste of time and resources.

I'm willing to bet that that is not the only code like that in your sketch.
Logged

East Anglia (UK)
Online Online
Faraday Member
**
Karma: 116
Posts: 4323
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
Serial.println(String("Detection on the right at ") + IR_Detection_Angle);
What type is IR_Detection_Angle declared as ?

You test its value against a number, which implies that it is an integer, then appear to concatenate it with a String that has been cast from a literal.  What is that all about ?

Please post all of your code.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok ... I'll retry :

I was wondering why this code doesn't work :

Code:
void CheckIRSensor(){
  int sensorValue = analogRead(A5);
  float voltage = sensorValue * (5.0 / 1023.0);
 
  if( tCnt == 0 && voltage > 0.5) {
    IR_Detection_Angle = Servo_Position;
    State_Last = State_Current;

    if( IR_Detection_Angle > 100 ) {       // Detected on the Left
      if(IR_Detection_Angle > 120 || voltage < 1.3){
        State_Current = STATE_LOW_RIGHT;
      }
      else{
        State_Current = STATE_HARD_RIGHT;
      }
    }
    else if( IR_Detection_Angle < 80 ) {   // Detected on the Right
      if(IR_Detection_Angle > 60 || voltage < 1.3){
        State_Current = STATE_LOW_LEFT;
      }
      else{
        State_Current = STATE_HARD_LEFT;
      }
    }
    else {                                 // Detected in Front
      State_Current = STATE_REVERSE;
    }
  }
  else if( tCnt == 0 && voltage > 2){
    //TODO
  }
  else if(State_Current != STATE_FORWARD){
    State_Last = State_Current;
    State_Current = STATE_FORWARD;
  }
}

Can you help me please ?
Thanks in advance !
Logged

California
Online Online
Faraday Member
**
Karma: 92
Posts: 3434
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I was wondering why this code doesn't work :

The code does something. You want it to something else. If you want help, you'll need to explain how these differ.
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 631
Posts: 50163
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I was wondering why this code doesn't work
That's easy. No setup() and no loop() functions. How many times do you need to hear "POST ALL OF YOUR CODE!"?
Logged

Pages: [1]   Go Up
Jump to: