Arduino and servo 'crazy' actions

Hello guys, I am running a project with a simple servo and my UNO arduino. I have a problem here...

here is the code

/************************************************************************
* This file was created by Tzivaras Vasilis
* it can be modified and be used for any purpose
*
* The setup() function is called when a sketch starts. Use it to initialize
* variables, pin modes, start using libraries, etc. The * setup function will 
* only run once, after each powerup or reset of the Arduino board.
*
* The loop() function does precisely what its name suggests, and loops 
* consecutively, allowing your program to change and respond. Use it to actively
* control the Arduino board.
************************************************************************/

#include <Servo.h> 

// create servo object to control a servo 
// a maximum of eight servo objects can be created 
Servo myservo;  
 
int pos = 0;    		// the position that the servo starts
 
void setup() {
	Serial.begin(9600);
	myservo.attach(9);  	// attaches the servo on pin 9 to the servo object 
} 
 
 
void loop() { 
	for(pos = 0; pos < 180; pos += 1) {
		myservo.write(pos);              // tell servo to go to position in variable 'pos' 
		delay(15);                       // waits 15ms for the servo to reach the position
		Serial.print("Currest Position(+): ");
                Serial.print(pos);
                Serial.print("\n");		
	} 
	
	for(pos = 180; pos>=0; pos-=1) {                                
		myservo.write(pos);              // tell servo to go to position in variable 'pos' 
		delay(15);                       // waits 15ms for the servo to reach the position 
		Serial.print("Currest Position(-): ");
                Serial.print(pos);
                Serial.print("\n");	
	} 
}

and here is the output from serial monitor

Currest Position(+): 0
Currest Position(+): 1
Currest Position(+): 2
Currest Position(+): 3
Currest Position(+): 4
Currest Position(+): 5
Currest Position(+): 6
Currest Position(+): 7
Currest Position(+): 8
Currest Position(+): 9
Currest Position(+): 10
Currest Position(+): 11
Currest Position(+): 12
Currest Position(+): 13
Currest Position(+): 14
Currest Position(+): 15
Currest Position(+): 16
Currest Position(+): 17
Currest Position(+): 18
Currest Position(+): 19
Currest Position(+): 20
Currest Position(+): 21
Currest Position(+): 22
Currest Position(+): 23
Currest Position(+): 24
Currest Position(+): 25
Currest Position(+): 26
Currest Position(+): 27
Currest Position(+): 28
Currest Position(+): 29
Currest Position(+): 30
Currest Position(+): 31
Currest Position(+): 32
Currest Position(+): 33
Currest Position(+): 34
Currest Position(+): 35
Currest Position(+): 36
Currest Position(+): 37
Currest Position(+): 38
Currest Position(+): 39
Currest Position(+): 40
Currest Position(+): 41
Currest Position(+): 42
Currest Position(+): 43
Currest Position(+): 44
Currest Position(+): 45
Currest Position(+): 46
Currest Position(+): 47
Currest Position(+): 48
Currest Position(+): 49
Currest Position(+): 50
Currest Position(+): 51
Currest Position(+): 52
Currest Position(+): 53
Currest Position(+): 54
Currest Position(+): 55
Currest Position(+): 56
Currest Position(+): 57
Currest Position(+): 58
Currest Position(+): 59
Currest Position(+): 60
Currest Position(+): 61
Currest Position(+): 62
Currest Position(+): 63
Currest Position(+): 64
Currest Position(+): 65
Currest Position(+): 66
Currest Position(+): 67
Currest Position(+): 68
Currest Position(+): 69
Currest Position(+): 70
Currest Position(+): 71
Currest Position(+): 72
Currest Position(+): 73
Currest Position(+): 74
Currest Position(+): 75
Currest Position(+): 76
Currest Position(+): 77
Currest Position(+): 78
Currest Position(+): 79
Currest Position(+): 80
Currest Position(+): 81
Currest Position(+): 82
Currest Position(+): 83
Currest Position(+): 84
Currest Position(+): 85
Currest Position(+): 86
Currest Position(+): 87
Currest Position(+): 88
Currest Position(+): 89
Currest Position(+): 90
Currest Position(+): 91
Currest Position(+): 92
Currest Position(+): 93
Currest Position(+): 94
Currest Position(+): 95
Currest Position(+): 96
Currest Position(+): 97
Currest Position(+): 98
Currest Position(+): 99
Currest Position(+): 100
Currest Position(+): 101
Currest Position(+): 102
Currest Position(+): 103
Currest Position(+): 104
Currest Position(+): 105
Currest Position(+): 106
Currest Position(+): 107
Currest Position(+): 108
Currest Position(+): 109
Currest Position(+): 110
Currest Position(+): 111
Currest Position(+): 112
Currest Position(+): 113
Currest Position(+): 114
Currest Position(+): 115
Currest Position(+): 116
Currest Position(+): 117
Currest Position(+): 118
Currest Position(+): 119
Currest Position(+): 120
Currest Position(+): 121
Currest Position(+): 122
Currest Position(+): 123
Currest Position(+): 124
Currest Position(+): 125
Currest Position(+): 126
Currest Position(+): 127
Currest Position(+): 128
Currest Position(+): 129
Currest Position(+): 130
Currest Position(+): 131
Currest Position(+): 132
Currest Position(+): 133
Currest Position(+): 134
Currest Position(+): 135
Currest Position(+): 136
Currest Position(+): 137
Currest Position(+): 138
Currest Position(+): 139
Currest Position(+): 140
Currest Position(+): 141
Currest Position(+): 142
Currest Position(+): 143
Currest Position(+): 144
Currest Position(+): 145
Currest Position(+): 146
Currest Position(+): 147
Currest Position(+): 148
Currest Position(+): 149
Currest Position(+): 150
Currest Position(+): 151
Currest Position(+): 152
Currest Position(+): 153
Currest Position(+): 154
Currest Position(+): 15

what it does is turning the servo from 0 to 180 deggres then stop then it does some crazy things...like stop for 5 sec then start for a little time then stop for 1 min start veeeery slowly etc...I am a programmer and i know how these kind of stuff works but I am sure something is missing here... :expressionless: yellow wire is signal red 5 V and brown is gnd...i ahve connected them directly to my arduino without resistors etc...and i connect the arduino with a cable to a usb port. Dunno if i need an external source of power like a battery. Please give me some answers :slight_smile:

thanks!! for any more info tell me!

Dunno if i need an external source of power like a battery

You almost certainly do.

And before you make another classic mistake, that battery had better not be a 9V smoke alarm type - put yourself together some AAs that give you the voltage you need - likely 6V.

wildbill:
And before you make another classic mistake, that battery had better not be a 9V smoke alarm type - put yourself together some AAs that give you the voltage you need - likely 6V.

Obligatory "Amen"

I was 99% sure that it was fault of the servo... :expressionless: Or bad programming. Really now? Its battery problem? I only have 9V battery. Um what exactly must i do? Connect 3 batteries 1.5 V? :confused: how?

You need something like this for 4x AAs to give 6V. You can get them to take 5x cells if you use rechargeables @ 1.2V each. But check your servo datasheet for the right voltage, most are 4.8-6V.

Put the pack straight to the servo's red and black, and join the black also to the Arduino ground; see attached pic.

servo power.png

Got it. Is there any specific reason why 9 V or even the usb cable creates the problem? I just want to learn why... :confused: Also this is the part 1. For part 2 I want to do the same but i have a bruseless motor and ESC. This the same thing i guess i just have one question. Will i give input from 0 to 180 again? How is the motor working? With degrees? Something else? I just want to know the MIN and MAX value that i can set in the write funcution. Thank you

The problem's that the Arduino 5V can't provide enough current to run a servo: forum wisdom is to allow 1Amp for each one.

JimboZA:
You need something like this for 4x AAs to give 6V. You can get them to take 5x cells if you use rechargeables @ 1.2V each. But check your servo datasheet for the right voltage, most are 4.8-6V.

Put the pack straight to the servo's red and black, and join the black also to the Arduino ground; see attached pic.

this is fine? http://www.ebay.com/itm/AA-Battery-Batteries-Holder-Box-with-DC-2-1-Power-Jack-for-Arduino-Tracing-Car-/400701166131?pt=UK_BOI_Electrical_Components_Supplies_ET&hash=item5d4ba69233

is there a problem that there are 6 slots? can i use only the 4?

JimboZA:
The problem's that the Arduino 5V can't provide enough current to run a servo: forum wisdom is to allow 1Amp for each one.

Ok so i need more than 5V. Um, 1Amp how many Volts? :confused: I am newbie sry...and also if you need more than 5 when i put a 9V battery ? Whats wrong? I need more than 9V???

If you've got a 4.5 to 6 V servo, 5V is perfectly OK, BUT it has to be at at least 1A.

You don't ever want to use a 9V battery for anything other than a smoke alarm.

Does your project need to be self sufficient? If not, see if you have a wall wart lying around that provides enough power. Most folks have quite a number left over from old phones and other electronics that needed charging.

ITs my first steps to make a quadcopter. I just want to make the motor work...turn :expressionless: And its toooo hard damn. Now i have connected the ESC to my arduino and after the beeps. it beep for ever...every 1 second i have a beep. and i dont like this :expressionless: Something is going wrong.

If you want to control an ESC (or four), what's the servo for?

Now i have connected the ESC to my arduino and after the beeps. it beep for ever...every 1 second i have a beep. and i dont like this smiley-neutral Something is going wrong.

I presume that you have a brushless motor attached to the ESC and it the motor that is making the beeps.

Some questions
What signals are you sending signals to the ESC ?
Does the ESC need to receive a sequence of commands to arm it ?
Is the ESC expecting an input and signalling that none is present ?

AWOL:
If you want to control an ESC (or four), what's the servo for?

Because I didnt know if the ESC is the same with servo and i am testing the servo as i am 99% sure that the example on the IDE and the connections are right. IF these will work I ll connect the ESC and do the same thing. AS i said I dont know the MIN and MAX value that i cna write to the ESC. But in the servo is 0 180.

UKHeliBob:

Now i have connected the ESC to my arduino and after the beeps. it beep for ever...every 1 second i have a beep. and i dont like this smiley-neutral Something is going wrong.

I presume that you have a brushless motor attached to the ESC and it the motor that is making the beeps.

Some questions
What signals are you sending signals to the ESC ?
Does the ESC need to receive a sequence of commands to arm it ?
Is the ESC expecting an input and signalling that none is present ?

I have no idea man. I am running this one now and nothing happen whne i give 10-20 50 340 54 12 53 lol fire g h t etc...

/*
*  This code is in the public domain.
*  (Do whatever you want with it.)
*/

// Need the Servo library
#include <Servo.h>

// This is our motor.
Servo myMotor;

// This is the final output
// written to the motor.
String incomingString;


// Set everything up
void setup()
{
  // Put the motor to Arduino pin #9
  myMotor.attach(9);

  // Required for I/O from Serial monitor
  Serial.begin(9600);
  // Print a startup message
  Serial.println("initializing");
}


void loop()
{
  // If there is incoming value
  if(Serial.available() > 0)
  {
    // read the value
    char ch = Serial.read();
  
    /*
    *  If ch isn't a newline
    *  (linefeed) character,
    *  we will add the character
    *  to the incomingString
    */
    Serial.print(ch);
    Serial.print("\n");
    if (ch != 10){
      // Print out the value received
      // so that we can see what is
      // happening
      Serial.print("I have received: ");
      Serial.print(ch, DEC);
      Serial.print('\n');
    
      // Add the character to
      // the incomingString
      incomingString += ch;
    }
    // received a newline (linefeed) character
    // this means we are done making a string
    else
    {
      // print the incoming string
      Serial.println("I am printing the entire string");
      Serial.println(incomingString);
    
      // Convert the string to an integer
      int val = incomingString.toInt();
    
      // print the integer
      Serial.println("Printing the value: ");
      Serial.println(val);
    
      /*
      *  We only want to write an integer between
      *  0 and 180 to the motor. 
      */
      if (val > -1 && val < 181)
     {
       // Print confirmation that the
       // value is between 0 and 180
       Serial.println("Value is between 0 and 180");
       // Write to Servo
       myMotor.write(val);
     }
     // The value is not between 0 and 180.
     // We do not want write this value to
     // the motor.
     else
     {
       Serial.println("Value is NOT between 0 and 180");
      
       // IT'S a TRAP!
       Serial.println("Error with the input");
     }
    
      // Reset the value of the incomingString
      incomingString = "";
    }
  }
}

Before you try making the motor move using Serial inputs, how about trying it with the Sweep example from the Servo library ? That way you will know that the ESC and motor work.

What ESC and motor are you using ?

Fellow "big bang theory" fan ( IT'S A TRAP!),
are you positive your servo can go 0 to 180 ? I would check the data sheet.
I looks as you servo "stalled" and pulled the source down for the processor to misbehave / or reset.
BTW you last post has no "wait" for the hardware provision.
I am not sure 15 ms is long enough when the refresh rate is nominally 20 ms , just saying.

Cheers Vaclav

I have also tested this code and nothing happends.

#include <Servo.h>

Servo motor;

void setup() {
 motor.attach(9);
 delay(1);
 Serial.begin(9600);
  // Print a startup message
  Serial.println("initializing");
}

void loop() {
 motor.write(90);
delay(10); 
}

btw should i connect the 5V pin to the arduino or not? There shoudl be only GND and signel pink connected?