Servo Motor angle

Please i am new in using Arduino, I want to rotate the servo a specific angle " its angle is zero and i want to move it to 30 degree and stop"

i try "write" function but it continues rotation function.
and try write the delay then detach but i found that the angle depend on the delay not degree that passed in write function

THX

Post the program, using code tags ("</>" button).

MostafaAbdallah42:
. . .i want to move it to 30 degree and stop"

i try "write" function but it continues rotation function.

If the servo can continuously rotate, you can't set its position. A continuous rotation servo uses the pulse sent by the Arduino to set the speed.

Hi! I'm having the same problem and I couldn't understand what your advice meant.

Here's my code.

I'm a beginner so be gentle!

<void loop() {
int ServoAngle = 45;
int angle;
int inPin1;
int ServoTime = 250;
inPin1 = 0;

int time_check = millis();
while (millis() - time_check < 400000) {
inPin1 = digitalRead(A0);
if (inPin1 == HIGH ){
for (angle = 0; angle < ServoAngle; angle = angle + 1) ; {
Servo1.write(angle);
delay(1000);
}
}
}}/>

So this obviously makes it sweep, but even when I use this:
<
void loop() {
int ServoAngle = 45;
int angle;
int inPin1;
int ServoTime = 250;
inPin1 = 0;

int time_check = millis();
while (millis() - time_check < 400000) {
inPin1 = digitalRead(A0);
if (inPin1 == HIGH ) {
Servo1.write(ServoAngle);
delay(1000);
}
}
}/>
it runs continuously. If you could help, I'd appreciate it.

it runs continuously. If you could help, I'd appreciate it.

Typical hobby servos will only rotate 180 degrees. If yours rotates more than 180 degrees and keeps rotating, it is probably a continuous rotation servo. Below is some servo test code that might be useful in testing a continuous rotation servo.

// zoomkat 3-28-14 serial servo incremental test code
// using serial monitor type a character (s to increase or a 
// to decrease) and enter to change servo position 
// (two hands required, one for letter entry and one for enter key)
// use strings like 90x or 1500x for new servo position 
// for IDE 1.0.5 and later
// Powering a servo from the arduino usually *DOES NOT WORK*.

#include<Servo.h>
String readString;
Servo myservo;
int pos=1500; //~neutral value for continous rotation servo
//int pos=90;

void setup()
{
  myservo.attach(7, 400, 2600); //servo control pin, and range if desired
  Serial.begin(9600);
  Serial.println("serial servo incremental test code");
  Serial.println("type a character (s to increase or a to decrease)");
  Serial.println("and enter to change servo position");
  Serial.println("use strings like 90x or 1500x for new servo position");
  Serial.println();
}

void loop()
{
  while (Serial.available()) {
    char c = Serial.read();  //gets one byte from serial buffer
    readString += c; //makes the string readString
    delay(2);  //slow looping to allow buffer to fill with next character
  }
  if (readString.length() >0) {
    if(readString.indexOf('x') >0) { 
      pos = readString.toInt();
    }

    if(readString =="a"){
      (pos=pos-1); //use larger numbers for larger increments
      if(pos<0) (pos=0); //prevent negative number
    }
    if (readString =="s"){
      (pos=pos+1);
    }

    if(pos >= 400) //determine servo write method
    {
      Serial.println(pos);
      myservo.writeMicroseconds(pos);
    }
    else
    {   
      Serial.println(pos);
      myservo.write(pos); 
    }
  }
  readString=""; //empty for next input
}
for (angle = 0; angle < ServoAngle; angle = angle + 1) ;

Lose the last semicolon.

Use code tags.

Hi! I would like to thank you for your time. Your advice helped. I now have another question (or two) about the same code.. should I start another thread or ask it in here?

Thanks!

should I start another thread or ask it in here?

Ask it here.

Hi, and again thank you for your time. I'm using an Arduino Uno and an Adafruit motor shield. I am working on a project for mechanical engineering and I need lots of amps for lots of torque for my mechanism. I'm including my code although I know it doesn't work, I haven't had the chance to debug it yet, and I'm really not sure if I'm on the right track.

So I know that it's possible to draw more amperage than is strictly allowed from the arduino. What I don't know for sure is how to code it. Attached is my preliminary attempt.. I kind of assumed that the maximum speed would be the maximum allowable speed of the motor, regardless of the arduino's capabilities, and scaled the output MAXampDrawSpeed as a function of that. To be specific, my motor can draw up to 20 amps, which would be too much for the arduino to handle, so I scaled it down accordingly, with 255 being that maximum 20 amp output. I'm not sure if I'm on the right track there.

So the other problem is - the TA for my class made a simplified code reference file, sort of predefining a lot of the things that I manually defined in my code (or tried to at least). I didn't realize this, and when I asked him a question, he sort of shot me down and said "you should use my reference code" --- but his code doesn't compile for me. I had some problems with libraries (I fixed them!), but this isn't a library.. it's a reference file, like the tabs up at the top. But it won't even compile for me. My feeling is that I may have annoyed him with my many many stupid questions up til this point and he's just kind of like "girl get it together"... So the question there is -- am I almost there with my track or should I figure out what's wrong with his code and try to fix it his way so that I can ask him questions without being such a bother to him? And also is that likely to be a hindrance on drawing the extra amperage that I really really want? I am prepared to use heat dissipation methods as are necessary, and any advice you could give me there would also be appreciated.

If you need any more information, about my mechanism or anything I'd be glad to share that as well. I'm parallel mounting one motor to the four dc motor mounts on my arduino in order to draw maximum current in that way as well. I have two switches controlling the output, one of the switches will engage the program for the maximum distance competition and the other for the accuracy competition.

So again, this code isn't right.. I haven't even started on debugging it.. because I didn't want to waste my time. Maybe you'll be able to follow my logic.. i know there aren't many notes. If you need me to add notes for clarity I would also be glad to take the time to do that.

Thanks!

<#include <Stepper.h>
#include <AccelStepper.h>
#include <Servo.h>
#include <Adafruit_MotorShield.h>
#include <Wire.h>
#include "Arduino.h"

#define STEP_TYPE_1   SINGLE
#define STEP_TYPE_2    SINGLE
#define STEPS_1    200
#define STEPS_2    200


Adafruit_MotorShield AFMS = Adafruit_MotorShield();



Adafruit_DCMotor *DC1 = AFMS.getMotor(1);
Adafruit_DCMotor *DC2 = AFMS.getMotor(2);
Adafruit_DCMotor *DC3 = AFMS.getMotor(3);
Adafruit_DCMotor *DC4 = AFMS.getMotor(4);

Servo Servo1;



void setup() {
  digitalWrite(A0, HIGH); //Turns on the pullup resistor for A0
  int i = analogRead(A0); //Reads the value on the analog pin and saves this as variable "i"

  AFMS.begin(); //Initializes the motorshield board

  int motorPin1 = 1;
  int motorPin2 = 2;
  int motorPin3 = 3;



  pinMode(A0, INPUT); //Sets the Analog Pin 0 as an input
  digitalWrite(A0,HIGH);
  pinMode(A1, INPUT);
  digitalWrite(A1,HIGH);
  pinMode(A1, INPUT);
  pinMode(1, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  Servo1.attach(9);






}



void loop()  { 
  int distancepin = 0;  //Just integer quantities, i like to have too many of them 
  int accuracypin = 0;
  int inPin1 = 0;             //switch pin values
  int inPin2 = 0;  
  int MAXampDrawTime = 75;
  int MAXampDrawSpeed = 178;
  int NORMampDrawTime = 225;
  int NORMampDrawSpeed = 61.2;
  int DrawTime = MAXampDrawTime+NORMampDrawTime;
  int ServoTime = 450;
  long previousMillis =0;
  long intervalMAXamps = 100;
  long intervalNORMamps = 400;
  DC1->setSpeed(MAXampDrawSpeed);
  DC2->setSpeed(MAXampDrawSpeed);
  DC3->setSpeed(MAXampDrawSpeed);
  DC4->setSpeed(MAXampDrawSpeed);
  inPin1 = digitalRead(A0);
  inPin2 = digitalRead(A1);
  if (inPin1 == HIGH){
  unsigned long time_zero = millis();
  unsigned long current_time = millis();
  digitalWrite(1,HIGH);   //turn motor on to maximum draw amperage
  digitalWrite(2,HIGH);
  digitalWrite(3,HIGH);
  digitalWrite(4,HIGH);
  while((millis()-time_zero)<(400000)){
  if( millis() >= (time_zero+NORMampDrawTime)){
  DC1->setSpeed(NORMampDrawSpeed);   //set motor on to normal draw amperage
  DC2->setSpeed(NORMampDrawSpeed);
  DC3->setSpeed(NORMampDrawSpeed);
  DC4->setSpeed(NORMampDrawSpeed);
  digitalWrite(1,HIGH);  //turn motor on to normal draw amperage  
  digitalWrite(2,HIGH);
  digitalWrite(3,HIGH);
  digitalWrite(4,HIGH);
  }
  if ( millis() >= (time_zero+DrawTime));
  {
  digitalWrite(1,0);   //turn off motors
  digitalWrite(2,0);
  digitalWrite(3,0);
  digitalWrite(4,0);
  }
  if (millis() >= (time_zero+ServoTime));{
    
 
      Servo1.write(0);
      delay(400);
      Servo1.write(90);
    }

  
}
  }
  

  
  
 if (inPin2 = HIGH);{ 
  int MAXampDrawTime = 75;
  int MAXampDrawSpeed = 59;
  int NORMampDrawTime = 225;
  int NORMampDrawSpeed = 20;
  int DrawTime = MAXampDrawTime+NORMampDrawTime;
  int ServoTime = 250;
  unsigned long previousMillis =0;
  unsigned long intervalHIamps = 100;
  unsigned long intervalNORMALamps = 400;
  DC1->setSpeed(MAXampDrawSpeed);
  DC2->setSpeed(MAXampDrawSpeed);
  DC3->setSpeed(MAXampDrawSpeed);
  DC4->setSpeed(MAXampDrawSpeed);
  
  unsigned long time_zero = millis();
  unsigned long current_time = millis();
  digitalWrite(1,HIGH);
  digitalWrite(2,HIGH);
  digitalWrite(3,HIGH);
  digitalWrite(4,HIGH);
  while((millis()-time_zero)<(500)){
  if( millis() >= (time_zero+MAXampDrawTime)){
  DC1->setSpeed(NORMampDrawSpeed);
  DC2->setSpeed(NORMampDrawSpeed);
  DC3->setSpeed(NORMampDrawSpeed);
  DC4->setSpeed(NORMampDrawSpeed);
  digitalWrite(1,HIGH);
  digitalWrite(2,HIGH);
  digitalWrite(3,HIGH);
  digitalWrite(4,HIGH);
  }
  else( millis() >= (time_zero+DrawTime));
  {
  digitalWrite(1,0);
  digitalWrite(2,0);
  digitalWrite(3,0);
  digitalWrite(4,0);
  }
  if (millis() >= (time_zero+ServoTime));{
    int  time_check = millis();
  while (millis() - time_check < 400000) {
    inPin1 = digitalRead(A0);
    if (inPin1 == HIGH ) {
      Servo1.write(0);
      delay(400);
      Servo1.write(90);
    }
  }

  
}
  }
  }}

TeamOne:
To be specific, my motor can draw up to 20 amps, which would be too much for the arduino to handle, so I scaled it down accordingly, with 255 being that maximum 20 amp output. I'm not sure if I'm on the right track there.

The amps you motor draws has nothing to do with the Arduino. It's a concern for the motor controller (h-bridge) not the Arduino. The Arduino isn't powering the motor.

I suppose you're worried the shield can't provide the current. I see you mentioned an Adafruit motor shield. Which one? This is very important to know. There are several very different Adafruit motor shields.

Are you supposed to be using 20A motors with this shield? The shield I'm most familiar with can control motors up to about 0.5A. I'm not aware of an Adafruit motor shield capable of anywhere near 20A but I don't know the specs of all their shields.

Do you have the code your TA gave you? We would need all the tabs. Tabs are just a way to make large programs manageable. The program would behave the same if code from the tabs were moved to the main file.

We might be able to help you find the missing libraries.

Please tell us more about the motors you are using.

It sounds like you're using four motors but the code includes stepper libraries. Are you using DC motors or steppers?

I take it there's also a hobby servo being used?