Go Down

Topic: Commenting Serial.println line change behaviour (Read 441 times) previous topic - next topic

flash2307

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: [Select]

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 ?

johnwasser

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: [Select]

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;
      }
    }
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

flash2307

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  :) if not, I will post exemple of working and non-working code!

Thank you for helping me  :)

PaulS

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: [Select]
    //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.

UKHeliBob

Code: [Select]
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.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

flash2307

Ok ... I'll retry :

I was wondering why this code doesn't work :

Code: [Select]

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 !

Arrch


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.

PaulS

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!"?

Go Up