1

I have been trying to control my 2 servo motors trought serial monitor. When i enter number 1-5 to serial monitor it follows the command as told in code. I write in the numbers 1-5 a couple of times and servos suddenly stop and i cant enter anything in serial monitor anymore. It worked fine when i only had the moving forward part of the code.

enter image description here

#include <Servo.h> 

Servo servo1;
Servo servo2;            

int servodata;

void setup() 
{ 
Serial.begin(9600);  
Serial.println("Redy");
Serial.println("1 stop ");
Serial.println("2 forward");
Serial.println("3 backward");
Serial.println("4 Turn left");
Serial.println("5 Turn right");

   servo1.attach(D7) ;
   pinMode(D7, OUTPUT);
    servo2.attach(D8) ;
    pinMode(D8, OUTPUT);




} 

void loop() 
{

  if (Serial.available() > 0)
  {
     servodata = Serial.read();

 if(servodata == '1') // Single Quote! This is a character.
  {
  Serial.println("Stop");
  {                                 
servo1.write(90); //stop 
servo2.write(90);   
 delay(3000);                 
  } 
    }


    if(servodata == '4')
    {
      Serial.println("Turn left");
      {
  servo1.write(0);  //Turn left
  servo2.write(0); 
  delay(3000);
} 
    }

    if(servodata == '5')
    {
      Serial.println("Turn right");
      {
  servo1.write(180);  //Turn right
  servo2.write(180); 
  delay(3000);
    } 
        }


    if(servodata == '2')
    {
      Serial.println("Forward");
      {
  servo1.write(0);  //Forward
  servo2.write(180); 
  delay(3000);
} 
    }

   if(servodata == '3')
    {
      Serial.println("Backward");
  {
servo1.write(180); //Backward
  servo2.write(0);
   delay(3000);
    }
    }


    Serial.println(" ");    // End the line


  }
    }
in2d
  • 544
  • 9
  • 19

1 Answers1

1

I think it happens because of the Serial communication. Serial at 9600 BAUD is VERY SLOW and stalls the server. Try removing the Serial prints, if it's still freezing then the problem is something else.

Cihan Kurt
  • 343
  • 1
  • 5
  • 21
  • Thank you for your answer. I tryed baud rates 56000 and 115200 but it still "crashes". – in2d Jan 26 '17 at 08:29
  • 1
    What if you just remove them. Is it still crashing? – Cihan Kurt Jan 26 '17 at 13:10
  • Works fine without the servomotors. Can the problem be that my USB ports give out too little voltage for these servomotors? What to you think? – in2d Jan 26 '17 at 17:48
  • Also work with one servo at the time but only when i connect it into "servo 1" pins but when i connect one servo to "servo 2" pins then it crashes and my computer makes the sound when you connect the USB. http://imgur.com/a/vjJf8 <-- where i connect my servos. This is my motorshield. – in2d Jan 26 '17 at 18:00
  • I tryed to connect servomotors seprately but it still crashes. http://imgur.com/a/AGnDr The problem seems to be with the D7 pin. I also would like to mention that ArduCAM and regular Arduino have different pin names D7 pin = pin 9 pretty much. – in2d Jan 26 '17 at 18:14
  • I tryed to powering motor shield with lipo battery(11.1v) and now the servos doesnt move at all but the red power light is on. When i power it with 9v battery the red light is on but servos wont move. When i power it trought the USB then the red light isn't on but the servos are moving for few seconds and then pass out slowly. – in2d Jan 26 '17 at 19:52
  • 1
    Powering the servos through the Arduino is generally not a good idea. You can probably get away with it if you're using "micro" sized servos and not working them hard, but standard-sized servos (even 1, if it's heavily loaded), are likely to overtax the Arduino's regulator. If the servo sucks too much current, it could overload the power supply to the point where it drops out of regulation, or shuts itself down due to overheating. Plus, a servo is a motor, and most DC motors will spit noise into their power supply. Sometimes enough to make the system unreliable. – Cihan Kurt Jan 26 '17 at 23:23
  • 1
    Google your servo's and you will prob. get the same problem with solutions online. – Cihan Kurt Jan 26 '17 at 23:24