Sensor Calibration With Servo?

Hi there, I recently got my arduino up and running and am having a little bit of trouble. I know alot about the electronics but not much about the c-ish coding... I first tried using the code to control the servo with a potentiometer and that worked fine except with a little bit of delay and jitter. I figured it was regular servo jitter. Then I tried the calibrate sensor code with the led and that worked fine. Then I tried combining the two to calibrate the potentiometer for the servo and it didn't work so fine. here is my code.

#include <Servo.h> 

Servo myservo;  //  

int potpin = 0;  // 

// 
int sensorValue = 0;
int sensorMin = 1023;
int sensorMax = 0;
 void setup() {
  //
   while (millis() < 5000) {sensorValue = analogRead(potpin);

     // 
     if (sensorValue > sensorMax) {
       sensorMax = sensorValue;
     }

     // 
     if (sensorValue < sensorMin) {
       sensorMin = sensorValue;
     }
   

   // 

 {
  myservo.attach(9);  //  
}

       //
  sensorValue = map(sensorValue, 0, 1023, 0, 179);      
  myservo.write(sensorValue);                  
  delay(15);   }

Anyways, I get a error in function void setup saying a function definition is not allowed here before '{' token. Sorry if this error is really stupid, but please help.

This compiles but probably does not yet do the calibration you need.

#include <Servo.h> 

Servo myservo;  
int potpin = 0;   

int sensorValue = 0;
int sensorMin = 1023;
int sensorMax = 0;

void setup() {
  myservo.attach(9);  
  while (millis() < 5000) 
  {
    sensorValue = analogRead(potpin);
    if (sensorValue > sensorMax) {
      sensorMax = sensorValue;
    } 
    if (sensorValue < sensorMin) {
      sensorMin = sensorValue;
    }  
  }
  sensorValue = map(sensorValue, 0, 1023, 0, 179);      
  myservo.write(sensorValue);                  
  delay(15); 
} 

void loop(){

}

You're right it does comply, however you're right on the second part too. It does not really have the calibration I am looking for. Have any suggestions? Even if they are totally different from the code I posted?

You would need to say more about the application does, what is happening during the calibration process, and what the calibration should do (don't worry about how it would be coded)

I am hoping that the application sets minimum and maximum values for the sensor during the first 5 seconds like the calibration sketch in the learning section of the arduino website. I'm hoping that by calibrating the potentiometer it will make the servo more in sync with the potentiometer (more precise, and less delay and jitter).

what kind of sensor are you using?

I'm not sure if this is technically a sensor, but it is an analog input. I am using 10k potentiometer.

why do you think it needs calibration?

what do you want your sketch to do?

I want the sketch to do control the servo just like the servo in the beginning of this video.How-To Tuesday: Arduino 101 potentiometers and servos - YouTube When I do this there is a 2-3 second delay after the potentiometer moves before the servo moves and it is not very accurate. I thought/think that calibrating the potentiometer would eliminate this.

If you mean that you want the angle of the servo to match the angle of the pot then you can do this by only using 180 degrees of pot movement (the pot is capable of 270 degrees)

I have not run this sketch but it should get you going in the right direction. You may need to play with the potSpan value to reduce or increase the movement needed to rotate the servo

#include <Servo.h> 

Servo myservo;  
int potpin = 0;   

int sensorValue = 0;
int potMin = 0;    // starting value of the pot.  
int potSpan=  700;  // 700 is an approx reading when the pot is 
                    // rotated 180 degrees away from the zero position  

void setup() {
  myservo.attach(9);  
} 

void loop(){

  sensorValue = analogRead(potpin);
  constrain(sensorValue, potMin, potMin + potSpan);
  int angle = map(sensorValue, potMin, potMin + potSpan, 0, 179);      
  myservo.write(angle);                  
  delay(15); 
}

Thanks, that worked really well. It does exactly what I wanted it to do!