*VIDEO* Program react differently regards of POWER source - USB/BATTERY

That's the seccond time i visit this forum. Now i am smarter then before. This is my first arduino project - selfriding robot that avoides obstacles. SETUP: -4WD platform with 4 DC motors. -Romeo V2.2 board. -Servo SG90 -distance sensor - ultrasonic HC-SR04 I downloaded the code from similiar project and modified this on my purpose, especialy for my sensor. This is quite strange, when i connect my romeo board via USB and upload same code with 5V, everything starts quickly and another time when i connect board also via USB ---- robot starts moving slowly back (servo doesnt work) and stops. The White diode on the board with "L" name lights. And the same thing is when i connect 8 x 1,5V AA battery which are connected in series. Here's the photo of my robot: http://imgur.com/a/6Y6w4 and code below https://github.com/TruePatrick/ROMEO-V2

:EDIT: - video here how's it looking - https://www.youtube.com/watch?v=SoNNOrAiGtc - check out my last answer, i have almost fixed this.

I don't see anything connected to "ServoPWR".

The Romeo board uses a 32U4 processor like the Leonardo. You should put in the:

     Serial.begin(9600);
     while (!Serial) {}

That will wait for the USB connection to complete. It may be that your sketch can't use Serial when there is no USB connection. In that case you may need to put "if(Serial)" before any Serial calls like Serial.read() or Serial.print(). You might also need to use:

     Serial.begin(9600);
     while (!Serial && millis() < 5000) {}

That way the waiting for a USB connection will time out after 5 seconds.

Let's see:

johnwasser: Serial.begin(9600);     while (!Serial) {}

Arduino:1.8.3 (Windows 10), Płytka:"Arduino Leonardo"

RoverCommander:24: error: no match for call to '(HardwareSerial) ()'

  while(!Serial1()){

                ^

exit status 1 no match for call to '(HardwareSerial) ()'

Ten raport powinien zawierać więcej informacji jeśli w File -> Preferencje zostanie włączona opcja "Pokaż szczegółowe informacje podczas kompilacji"

Here's the thing. When i upload code on the board everything looks fine. Servo moving, sensor sees obstacles and robot try to avoid it. BUT right after i plug it out and THEN plug it in again via USB robot behave differently. Starts moving slowly back and servo and sensor don't move. Until i plug it again via usb and upload code from the beggining

Is it becamuse once for like 0.2 sec i switched on the switch for external batery when usb cable was connected by mistake. I've heard that you mustn't do it!

TruePatrick: Let's see:

RoverCommander:24: error: no match for call to '(HardwareSerial) ()'

  while(!Serial1()){

Where did that line come from?!?

Whoever wrote that did not understand why the Leonardo needs:

     Serial.begin(9600);
     while (!Serial) {}

(Even though I explained why when I recommended it.)

This doesn't work, even when i delete ALL this lines which are responsible for serial kontrol with pc. When i upload the code for the first time motorst starts move forward, then i normally plug it out from the pc and switch it on on external batery. Robot starts slowly move backward - seems like this line is responsible for that reaction

if(distance<=10){
      back_off (100,100);
    }

Yep it is this line about, when i change "back_off (200,200)", and repeat all the stuff that i do - motors starts going backward faster. It is all about the sensor i think. Here's the code that i'm working on currently. https://github.com/TruePatrick/Here-it-is - let me know if this site is working.

  distance = (duration/2) / 29.1;


   // if distance outside 5 - 20 cm range
   if(distance>15){
     obstacle = false;
     advance (200,200);
   }
   else {
     if(distance<=10){
       back_off (200,200);
     }
     else{

A: If the sensor isn't working or no echo is detected pulseIn() returns 0 which would cause the repeated back_off(). You might get better results if you treat 0 the same as >15.

B: Your comment about 5-20 cm range doesn't match the code

Don't look at the commends, they are from the other code. now i have this code and the same problem - soon i'll upload the video on youtube and show you.

void Explore(){

  long duration, distance;
  digitalWrite(trigPin, LOW);  // Added this line
  delayMicroseconds(2); // Added this line
  digitalWrite(trigPin, HIGH);
//  delayMicroseconds(1000); - Removed this line
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;

  
  distance = (duration/2) / 29.1;

  
 if(distance>12 || distance<0){
    obstacle = false;
    advance (120,120); 
  }
  else{
    
    // too close, distance < 15 cm
    if(distance<=1){
      back_off (150,150);
    }
    else{
      
      obstacle = true;
      
      if(GetPosition() > GetMiddlePosition()){
        turn_R (200,200); 
        
        
        SetServoDirection(0);
      }
      else{
        turn_L (200,200);
     
        
        SetServoDirection(1);
      }
  }
}
}

I'm working on this like whole day and now night. I refresh this site every 10 minutes with hope that you adwise me. VIDEO - https://www.youtube.com/watch?v=SoNNOrAiGtc THE code works only when i will upload it via usb. After i plug out and then again plug in, robot behave like on the beginning of the video

@johnwasser

You should never use while(!Serial) in a 32U4 based Arduino if it needs to run from external power; it will hang forever.

Even if i delete this whole lines. The problem still appears. Here’s the robot of this guy form who i have this code.
He have different sensor so i modify my code that it can fit to my sensor - 4WD Rover - basic obstacle avoidance - YouTube

Seems like I repaired the code for exploration. Now it works everytime i plug it into usb but not on external baterry!

/*
  # This code has been inspired by an article from Dominique Meurisse : http://arduino103.blogspot.fr/2011/06/detecteur-de-proximite-infrarouge-sharp.html
  #
  # this script is distributed under Creative Commons Attribution-ShareAlike 3.0 License
*/


void Explore(){

  #define trigPin 13
  #define echoPin 12
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  
  long duration, distance;
  digitalWrite(trigPin, LOW);  // Added this line
  delayMicroseconds(2); // Added this line
  digitalWrite(trigPin, HIGH);
//  delayMicroseconds(1000); - Removed this line
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;

  
  distance = (duration/2) / 29.1;

   Serial.print(distance);
   Serial.println(" cm");

   if(distance>15){
     obstacle = false;
     advance (200,200);
   }
   else {
     if(distance<10 && distance>0){
       back_off (200,200);
     }
    else{
      if(distance>0){
      obstacle = true;
      
      if(GetPosition() > GetMiddlePosition()){
        turn_R (200,200); 
        SetServoDirection(0);
      }
      else{
        turn_L (200,200);
        SetServoDirection(1);
      }
      }
  }
}
}

I add my previous project to find out if sensor works. It measures the distance in cm!

CAN anyone help me? I'm feeling done now...

Maybe you should add some LEDs to light up based on the distance reading. That way you can see if the problem is your sensor or your motor driver.

I will tomorrow. I need a break from all this. The thing is, i've discover that when i will plug board via usb IT ALSO gather the power from the output baterry even when switch is off. I measured this by multimetr. That's strange. I tried adding more baterry, i had 18,5V but still was the same. It's not about voltage. I tried to charge this vai USB from POWERBANK 5V. Worked fine ... but i had to have connected external battery to work, even if it was on OFF switch.

Have you measured voltage to ultrasonic sensor when on USB power and on battery power?

How am i suppose to measure it? Sorry for asking, i'm a beginner. I measured the voltage from the external battery when usb cable was pluged into board. In theory usb has 5V (distance sensor is connected to the red 5V on the board) and when i measured the voltage which was in the external battery which was connected to the board (but the switch was OFF) it dropped like 2V!!! From 10V to 8V. Program worked perfectly. How is this possible that besides the switch on the board from the external battery was off (http://imgur.com/a/6Y6w4 - first left Green on the top, lower is the switch), board still collect voltage from the external battery. --------- 3 days ago i accidentaly switched that switch ON when USB was connected, and program schuted down that day. I've read that you musnt do that. 10V from batteries + 5V USB = 15. Max voltage that board can handle is 20V so i didn't worry about it much, i switched it off after 0.5 sec.

Motors are always powered from external source. Switch only separate system power from motor power.

To measure voltage, measure on the red pins between GND and 5V, GND and 3,3V and finally between GND and Vin. Do it for both USB power and battery power. I would start with 6 volt battery power as power regulator might be broken.

https://www.dfrobot.com/wiki/index.php/Romeo_V2-All_in_one_Controller_(R3)_(SKU:DFR0225)
Big warning from DF Robot.
“Please Turn OFF the Motor Power Switch when debugging Romeo through USB cable. Or the external power supply(>12V) will destroy your Romeo.”

It is possible power regulator on your board is toast from having both USB and battery power source connected. Board can’t give you 5V any longer and no power to ultrasonic sensor.

If your servo doesn’t move you need to connect 5V to servo power terminal

Do Debugging mean - reuploading code that has a little change in it? I hope i didn't toast any circuit on the board. I will try to connect SERVO to

Gabriel_swe: Motors are always powered from external source. Switch only separate system power from motor power.

That's helpfull answer. When i connect board via usb, and deconnect external power, servo and distance sensor works(i'm sure) BUT not motors.

Gabriel_swe: I would start with 6 volt battery power as power regulator might be broken.

In the house i've found little battery from rc car - 7,2V 500mAh. Tried it but i chave to charge it. Here's one thing, Seems like board doesn't like external battery at all. When i only on EX battery -code works weird(like i sad before), but when i connect powerbank 5V to the usb port - WORKS perfectly like from PC. I can't find any problem in the code. Ehhh

I will check the voltage later - tomorrow cuz it's 10:30 p.m. and i'm tired. UPdate: Voltage USB (powerbank 5V) + 8x 1,5V AA battery (their actuall voltage is ~10V) and switch OFF: 5V - GND: ~5V 3V3 - GND: ~3,3V Vin - GND: 0 (nothing shows) 8x 1,5V AA battery and switch ON: 5V - GND: 5V 3V - GND: 3,3V Vin - GND: 9,8V - program starts runnig in circle - starts stoping - the L diode flashing - and the runing for like 1 sec and voltage drops to ~6,3V. - do you want a video that shows how program works on external battery?

Better than expected, even if I thought Vin would be 5V running from USB power.

Voltage drop when battery powered is too much. From 9.8 down to 6.3 is 35% drop and will most likely reboot the unit. Fresh alkaline will be closer to 12 V with low voltage drop. Even fully charged NiMH I would expect to be a little more than 10V and less than 1V drop.

Voltage drop most likely comes when motors start to rotate for first time. To get a visual indication of a reboot you can add a for loop to blink a led in setup() for 10 or 20 seconds. If you after the blinking just get a tiny movement and it start to blink again - you had a reboot.