continuous servo rotation always returns 0 position

hello;

i made an automatic guitar tuner project. i used as3103 pg servo motor but servo always returns same position after pulse.it is spining right direction for 1 sec then it goes 0 degrees again.is there any problem in my code ? help me please .
the video of the servo http://sendvid.com/zh3knlv9

Edit: i powered the servo from diffrent 5v source then i rus sweep code and it worked.then i run this code and here is the servos react:http://sendvid.com/jrsliwmw
here is my code

dsmithfr:

#include <Servo.h>

Servo Servo1; //declaration of servo object
int angleStill = 90; //stores servo position
int angle = 90; //stores servo position
int trim = 0; //trim adjustment to zero position
int servoPin = 8; //Arduino pin assigned to servo
int clockwisespeed = 120; //the speed to go in the clockwise direction
int countclockwisespeed = 60; //the speed to go in the counter clockwise direction

//clipping indicator variables
boolean clipping = 0;
//data storage variables
byte newData = 0;
byte prevData = 0;
unsigned int time = 0;//keeps time and sends vales to store in timer[] occasionally
int timer[10];//storage for timing of events
int slope[10];//storage for slope of events
unsigned int totalTimer;//used to calculate period
unsigned int period;//storage for period of wave
byte index = 0;//current storage index
float frequency;//storage for frequency calculations
int maxSlope = 0;//used to calculate max slope as trigger point
int newSlope;//storage for incoming slope data
int minfreq; //it will be changed according to its string
int maxfreq; //it will be hanged according to to its string
int basefreq = 0; //define baseline noise to ignore any frequency less than this value

//variables for decided whether you have a match
byte noMatch = 0;//counts how many non-matches you've received to reset variables if it's been too long
byte slopeTol = 3;//slope tolerance- adjust this if you need
int timerTol = 10;//timer tolerance- adjust this if you need

//variables for amp detection
unsigned int ampTimer = 0;
byte maxAmp = 0;
byte checkMaxAmp;
byte ampThreshold = 20;//raise if you have a very noisy signal
int notenum = 0; //set the guitar string number I want to tune
int notenumPrior = 0; //holds the previously selected guitar string

//-------------------------------------------------

void setup(){
 
 Serial.begin(9600);
 pinMode(13, OUTPUT); //clipping indicator
pinMode(12, OUTPUT); //wave period indicator
pinMode(11, OUTPUT); //freq too low
pinMode(10, OUTPUT); //freq spot on
pinMode(9, OUTPUT); //freq too high

//sets all three pitch indicator lights off
digitalWrite(11, LOW);
digitalWrite(10, LOW);
digitalWrite(9, LOW);

pinMode(7, INPUT);// different paths from dip switch indicating the notenum value (output)
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(4, INPUT);
pinMode(5, INPUT);
pinMode(6, INPUT);

digitalWrite(7, HIGH);// making sure the input pins are set high, so when pressed they will be on low
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);

Servo1.attach(servoPin); //attaches servo on pin x to servo object

pinMode(13,OUTPUT);//led indicator pin
 pinMode(12,OUTPUT);//output pin
 
 cli();//diable interrupts
 
 //set up continuous sampling of analog pin 0 at 38.5kHz

//clear ADCSRA and ADCSRB registers
 ADCSRA = 0;
 ADCSRB = 0;
 
 ADMUX |= (1 << REFS0); //set reference voltage
 ADMUX |= (1 << ADLAR); //left align the ADC value- so we can read highest 8 bits from ADCH register only
 
 ADCSRA |= (1 << ADPS2) | (1 << ADPS0); //set ADC clock with 32 prescaler- 16mHz/32=500kHz
 ADCSRA |= (1 << ADATE); //enabble auto trigger
 ADCSRA |= (1 << ADIE); //enable interrupts when measurement complete
 ADCSRA |= (1 << ADEN); //enable ADC
 ADCSRA |= (1 << ADSC); //start ADC measurements
 
 sei();//enable interrupts
}

ISR(ADC_vect) {//when new ADC value ready
 
 PORTB &= B11101111;//set pin 12 low
 prevData = newData;//store previous value
 newData = ADCH;//get value from A0
 if (prevData < 127 && newData >=127){//if increasing and crossing midpoint
   newSlope = newData - prevData;//calculate slope
   if (abs(newSlope-maxSlope)<slopeTol){//if slopes are ==
     //record new data and reset time
     slope[index] = newSlope;
     timer[index] = time;
     time = 0;
     if (index == 0){//new max slope just reset
       PORTB |= B00010000;//set pin 12 high
       noMatch = 0;
       index++;//increment index
     }
     else if (abs(timer[0]-timer[index])<timerTol && abs(slope[0]-newSlope)<slopeTol){//if timer duration and slopes match
       //sum timer values
       totalTimer = 0;
       for (byte i=0;i<index;i++){
         totalTimer+=timer[i];
       }
       period = totalTimer;//set period
       //reset new zero index values to compare with
       timer[0] = timer[index];
       slope[0] = slope[index];
       index = 1;//set index to 1
       PORTB |= B00010000;//set pin 12 high
       noMatch = 0;
     }
     else{//crossing midpoint but not match
       index++;//increment index
       if (index > 9){
         reset();
       }
     }
   }
   else if (newSlope>maxSlope){//if new slope is much larger than max slope
     maxSlope = newSlope;
     time = 0;//reset clock
     noMatch = 0;
     index = 0;//reset index
   }
   else{//slope not steep enough
     noMatch++;//increment no match counter
     if (noMatch>9){
       reset();
     }
   }
 }
   
 if (newData == 0 || newData == 1023){//if clipping
   clipping = 1;//currently clipping
   Serial.println("clipping");
 }
 
 time++;//increment timer at rate of 38.5kHz
 
 ampTimer++;//increment amplitude timer
 if (abs(127-ADCH)>maxAmp){
   maxAmp = abs(127-ADCH);
 }
 if (ampTimer==1000){
   ampTimer = 0;
   checkMaxAmp = maxAmp;
   maxAmp = 0;
 }
 
}

void reset(){//clean out some variables
 index = 0;//reset index
 noMatch = 0;//reset match couner
 maxSlope = 0;//reset slope
}

void checkClipping(){//manage clipping indication
 if (clipping){//if currently clipping
   clipping = 0;
 }
}

void loop(){
 
 checkClipping();
 
//Serial.print(checkMaxAmp);
//    Serial.println(" ");
//    Serial.print(ampThreshold);
//    Serial.println(" ");
//    
 
 if (checkMaxAmp>ampThreshold){
   frequency = 38462/float(period);//calculate frequency timer rate/period
 
   //print results
   Serial.print(frequency);
   Serial.println(" hz");
//    Serial.print(notenum);

notenum_val(); //call function to determine value of note/string we want to tune

freq_range_identify(); //set frequency threshold ranges for string previously selected

checkClipping(); //frequency determining and led indicator loop

set_indicator_lights(); //checks value of current frequency and lights correct indicator light. Then moves servo accordingly

//Serial.println(angle);
move_motor(); //move tuning servo motor

//delay(1000); //turn on to control debouncing

notenumPrior = notenum;

} //end of main loop

delay(100);
 
} //End void loop
//-----------------------------------------------------------------------------------------------
//Determine String selected from rotary switch and voltage values on designated arduino pins
void notenum_val()
{
int val_1 = digitalRead(2); //read the value of rotary switch pins
int val_2 = digitalRead(3);
int val_3 = digitalRead(4);
int val_4 = digitalRead(5);
int val_5 = digitalRead(6);
int val_6 = digitalRead(7);

.
.
.

.
.
.

angle = 90;

Serial.println("String is tuned! Please move on!");
Serial.println(" ");

//  delay(5000);
}
}
//Turns the tuning motor appropriately
void move_motor()
{
Servo1.write(angle+trim);
delay(50);
Servo1.write(angleStill+trim);
}

here is my code

or at least some of it
Please read this before posting a programming question

halilhilmi67:
here is my code

You mean "part of your code"?

In each forum there is a stick titled "Who to use this forum - please read". Please read it and follow the instructions in item #7 to post your code.

The servo commands are using variables which are defined within the section you posted.

Have you tried using the servo on its own? How did it behave?

I find the title of this thread confusing. Are you using a continuous rotation servo? It seems very very odd to have a CR servo repeatedly stop at the same location.

DuaneDegn:
You mean "part of your code"?

In each forum there is a stick titled "Who to use this forum - please read". Please read it and follow the instructions in item #7 to post your code.

The servo commands are using variables which are defined within the section you posted.

Have you tried using the servo on its own? How did it behave?

I find the title of this thread confusing. Are you using a continuous rotation servo? It seems very very odd to have a CR servo repeatedly stop at the same location.

yes i am using continuous servo motor. i tried it with simpler sweep code and it worked. it seems to me servo always resets its position before the spinnig over.

DuaneDegn:
In each forum there is a stick titled "Who to use this forum - please read".

I'm surprised it allowed me in :slight_smile: :slight_smile:

...R

halilhilmi67:
yes i am using continuous servo motor. i tried it with simpler sweep code and it worked. it seems to me servo always resets its position before the spinnig over.

You were asked twice to upload your complete program - yet you still have not done so ? ?

How is your servo powered? It should NOT be drawing power from the Arduino 5v pin. If it is, that could be the cause of your problem.

...R

Robin2:
You were asked twice to upload your complete program - yet you still have not done so ? ?

How is your servo powered? It should NOT be drawing power from the Arduino 5v pin. If it is, that could be the cause of your problem.

...R

yes it is connected to arduinos 5v pin. should i add extra power source ? what what difference does it make ?

yes i am using continuous servo motor. i tried it with simpler sweep code and it worked

Can you please describe what the servo did when you ran the Sweep program.

UKHeliBob:
Can you please describe what the servo did when you ran the Sweep program.

I was wondering the same thing.

UKHeliBob:
Can you please describe what the servo did when you ran the Sweep program.

servo always spins when i upload the sweep code.

servo always spins when i upload the sweep code.

Does it change direction ?

How is the servo powered ? Direct from the Arduino (wrong) or from an external power source with a common GND to the Arduino (right)

UKHeliBob:
Does it change direction ?

How is the servo powered ? Direct from the Arduino (wrong) or from an external power source with a common GND to the Arduino (right)

i will try it again soon. now i cant. i cant remember the direction but it seems to me servo works fine.

yes i pluged the servo arduinos 5 v pin because of in the diagram i saw .

i will try the servo with diffrent power source and write the the servos behavior here. thank you all for your help

In case it's not clear, we're having a hard time believing a CR servo stops in the same place repeatedly. CR servos generally stop in random positions.

Did you modify the servo for continuous rotation yourself or did you purchase one which had already been converted to CR?

yes i pluged the servo arduinos 5 v pin because of in the diagram i saw .

This is really not a good idea because of the current required to drive most servos. Looking at the specification for the servo the running current is 20mA @ 5V but the stall current is a whopping 300mA. See AS3103 servo

I don't really understand what you are doing but if it involves stalling the servo, which I suspect that it does, then you cannot power it successfully from the Arduino.

UKHeliBob:
This is really not a good idea because of the current required to drive most servos. Looking at the specification for the servo the running current is 20mA @ 5V but the stall current is a whopping 300mA. See AS3103 servo

I don't really understand what you are doing but if it involves stalling the servo, which I suspect that it does, then you cannot power it successfully from the Arduino.

Here is the video of it. Servo is not pluged to the guitar

http://sendvid.com/zh3knlv9

Here is the video of it.

I am not clear what "it" is.

IMO, it would be helpful to establish if this is a continuous rotation servo or not.

At this point, I'd be VERY surprised if the servo is continuous rotation. I'm guessing this is probably fine as far as the project requirements are concerned.

I don't think I understand what the expected behaviour is. What is the servo supposed to do but doesn't? What unexpected behaviour do you see?

Is the servo shown in the video being controlled from the program posted in top post?

DuaneDegn:
IMO, it would be helpful to establish if this is a continuous rotation servo or not.

At this point, I'd be VERY surprised if the servo is continuous rotation. I'm guessing this is probably fine as far as the project requirements are concerned.

I don't think I understand what the expected behaviour is. What is the servo supposed to do but doesn't? What unexpected behaviour do you see?

Is the servo shown in the video being controlled from the program posted in top post?

It is continuous motor.

The servo needs to spin for 2-3 times for guitar tune but it always comes back yo 0.

Yes it is the program in first entry that works.but i couldnt post full code couse it is over 9000 chrctr.

I plug 5v 1.2 amp power source on usb of arduino and looked for servos current. İt is 6 mA. That is not enough i guess.

It sounds like the servo tensions the string until the correct frequency is obtained. Is that right ?

If so then the servo will spend some time stalled or nearly so as the tension is increased and will take up to 300mA according to its specification.

I plug 5v 1.2 amp power source on usb of arduino and looked for servos current. İt is 6 mA. That is not enough i guess.

It does not matter how much current the power source can supply if you are powering the servo from an Arduino 5V pin because there is a limit to how much current that the Arduino voltage regulator can supply. Mind you, I don't understand how you have got your power source connected. Can you please provide a circuit diagram even it is hand drawn and photographed,

Incidentally, how is the guitar string activated so that the frequency can be measured ?