Correcting Servo center?

When I run this code, my servo pins hard left and stays there.

#include <Servo.h>

Servo myservo;  // create servo object to control a servo

//Constants
const unsigned char CONTROL = 7;  // digital pin used to detect if the system is on or off
const unsigned char temps = 0;    // analog pin used to connect the temp sensor
const unsigned char MAX_VAL = 10;

//Main global varibles
char trigger = 0;     // varible used to store the control pin value
unsigned int val;     // variable to read the value from the analog pin

unsigned int updateAvgtemp(){
 static int history[MAX_VAL]={0};
 static unsigned char lastHist=0;
 static unsigned char numHist=0;
 unsigned int temp=0;
 unsigned char counter=0;
 unsigned char arcount=0;
 history[lastHist] = analogRead(temps);
 if(numHist<MAX_VAL)
   ++numHist;
 arcount=lastHist;
 ++lastHist;
 if(lastHist>=MAX_VAL)
   lastHist=0;
 temp=0;
 counter=0;
 do{
   temp+=history[arcount];
   arcount--;
   if(arcount>MAX_VAL)
     arcount=(MAX_VAL-1);
   counter++;
 }while(counter < numHist);
 return (temp/numHist);
}

void setup()
{
 pinMode (CONTROL, INPUT);   // sets the control pin to input
 myservo.attach(9);      // attaches the servo on pin 9 to the servo object
 digitalWrite(CONTROL, LOW);   // ensure internal pullup resistor is disabled.
}
void loop()
{
 trigger = digitalRead(CONTROL); // read input of pin CONTROL  and store it
 if (trigger == HIGH){     // reads if pin CONTROL, if true, do this:
   val = updateAvgtemp();    // read the value of the temp sensor (value with range of 1024)
   val = map(val, 350, 700, 80, 160);  // scale it to use it with the servo (value between 160 and 80)
   myservo.write(val);     // sets the servo position according to the scaled value
 }
 else {
   myservo.write(180);     // sets servo position to 180 if above statement is false
 }
 delay(125);               // wait 25ms for the servo to move to it's new position and also 100ms until it gets the new value
}

I have tried adjusting the 120, 80 positions (reversed them in this code). But not sure how to center or put my servo at 0. So it will then move according to temp from their.

What am I missing?

What am I missing?

Code tags!

And use the auto format tool before posting your code.

Mark

Not sure how to do that Mark.

I just copied from Arduino program into here.

(deleted)

DeviceUnknown:
Not sure how to do that Mark.

I just copied from Arduino program into here.

Please read this:-
How to use this forum
it will tell you.

Thanks Spycatcher. I should have known that. Sometimes the simplest things get me.

Here is my layout attached.

setting it to 90 helped, but it seems unresponsive to the thermistor. That or it's too sensitive. I used a VOM to check it, it's 10K.

Code is otherwise good to go. Well, after I add a capacitor inline to stop the bouncing.

Does my wiring look ok?

Does my wiring look ok?

No.
Thermistor is wrong.

Grumpy_Mike:
No.
Thermistor is wrong.

Could you elaborate please?

Given your lack of cooperation concerning the code tags you are asking a lot.

You have the analogue input connected directly to ground. You will therefore not read anything but zero.
Edit:- Ah I see you have decided to cooperate now. Thanks.
The analogue input should be connected to the junction of resistor and thermistor.

Grumpy_Mike:
Given your lack of cooperation concerning the code tags you are asking a lot.

You have the analogue input connected directly to ground. You will therefore not read anything but zero.

I indeed DID fix the code tags.

The analog input is going from A0 to one side of Thermistor. The other side of Thermistor is going to +5v

Ugh I did my drawing wrong. oops.

The resistor side is going to A0 also running from the resistor to the ground.

I got it. Thanks Mike, it got me looking a bit deeper.

I have to fix the bouncing though. I don't have a .1 capacitor laying around here. Unless someone has a better idea on fixing the bouncing. (using HS-485HB servos)

Can you explain a bit more what this bouncing problem is and how you have tried to fix it.

I don't understand the pin 7 control it seems to be just a resistor connected to ground, why would this ever read high

To be honest, I'm not sure. My understanding was it tells the Arduino when to run the code. i.e. connected to a PC, it will not run if the PC is off. Instead of hooking it to ground on the Arduino, I would connect it to ground on PSU. This is just a guess.

More than likely i will figure out how to just remove it and relating code and just have the arduino come on when the PC is on.
A Guy I know did this back in 2010, but I lost contact with him.

Oh, the bouncing. The servo just bounced back and forth constantly a few degrees. The code will run. But even at 90 it will sit there and bounce.

In that horrible Fritzing diagram that you've shown, the resistor from pin 7 to ground permanently holds that input low. (Most of us prefer conventional schematic diagrams over Fritzing diagrams. Your's is fairly simple and easy to read, but that's not always the case.)

Do you have a switch or something connected to pull that pin high as a trigger?

If you attach a pushbutton switch between pin 7 and +5V, the first line below will read the value on pin 7 and when you press the button the code between the braces will execute:-

trigger = digitalRead(CONTROL); // read input of pin CONTROL  and store it
 if (trigger == HIGH){     // reads if pin CONTROL, if true, do this:
   val = updateAvgtemp();    // read the value of the temp sensor (value with range of 1024)
   val = map(val, 350, 700, 80, 160);  // scale it to use it with the servo (value between 160 and 80)
   myservo.write(val);     // sets the servo position according to the scaled value
 }

but as long as pin 7 is tied to ground, the above code will never be executed and instead this will happen, driving the servo fully CCW:-

else {
   myservo.write(180);     // sets servo position to 180 if above statement is false
 }

Except I think you've now changed the 180 to 90, so the servo will be driven to it's centre position.

If you connect a wire to the point where the 220 ohm resistor connects to pin 7, then connect the other end of that wire to 5V, 'trigger' will read as HIGH and this will be executed:-

val = updateAvgtemp();    // read the value of the temp sensor (value with range of 1024)
   val = map(val, 350, 700, 80, 160);  // scale it to use it with the servo (value between 160 and 80)
   myservo.write(val);     // sets the servo position according to the scaled value

If you want it to work continuously, just connect pin 7 directly to +5V and remove that 220 ohm resistor that connects to ground.

I have no idea what's causing the 'bounce'. From what you've shown, it's an unrelated problem. At the moment, while pin 7 is held low, your servo should be fixed in one position. Makes me wonder if the Arduino is possibly constantly resetting. How do you power the Arduino?

OldSteve:
In that horrible Fritzing diagram that you've shown, the resistor from pin 7 to ground permanently holds that input low. (Most of us prefer conventional schematic diagrams over Fritzing diagrams. Your's is fairly simple and easy to read, but that's not always the case.)

Do you have a switch or something connected to pull that pin high as a trigger?

If you attach a pushbutton switch between pin 7 and +5V, the first line below will read the value on pin 7 and when you press the button the code between the braces will execute:-

trigger = digitalRead(CONTROL); // read input of pin CONTROL  and store it

if (trigger == HIGH){    // reads if pin CONTROL, if true, do this:
  val = updateAvgtemp();    // read the value of the temp sensor (value with range of 1024)
  val = map(val, 350, 700, 80, 160);  // scale it to use it with the servo (value between 160 and 80)
  myservo.write(val);    // sets the servo position according to the scaled value
}


but as long as pin 7 is tied to ground, the above code will never be executed and instead this will happen, driving the servo fully CCW:-

else {
  myservo.write(180);    // sets servo position to 180 if above statement is false
}



Except I think you've now changed the 180 to 90, so the servo will be driven to it's centre position.

If you connect a wire to the point where the 220 ohm resistor connects to pin 7, then connect the other end of that wire to 5V, 'trigger' will read as HIGH and this will be executed:-

val = updateAvgtemp();    // read the value of the temp sensor (value with range of 1024)
  val = map(val, 350, 700, 80, 160);  // scale it to use it with the servo (value between 160 and 80)
  myservo.write(val);    // sets the servo position according to the scaled value



If you want it to work continuously, just connect pin 7 directly to +5V and remove that 220 ohm resistor that connects to ground.

I have no idea what's causing the 'bounce'. From what you've shown, it's an unrelated problem. At the moment, while pin 7 is held low, your servo should be fixed in one position. Makes me wonder if the Arduino is possibly constantly resetting. How do you power the Arduino?

Thanks for the valuable info. I tried connecting pin 7 directly to 5V (old PSU from computer) and it does exact same thing. it works I can see the movement differences when I heat the thermo. When I unplug pin 7, it centers back to 90.

I WAS powering the Arduino from USB port on a older computer I use just for this. So I tried a solid 5v power supply to it and the bouncing changed some. Less eratic but still constant.
I read here that i needed to add a 0.1 capacitor due to servos being PPM not PWM.

https://onedrive.live.com/redir?resid=142965B2A99F12EA!703&authkey=!AKGN6c6A_opfTrU&ithint=video%2Cmp4

I uploaded a short video of what it's doing.

Thanks for the valuable info. I tried connecting pin 7 directly to 5V (old PSU from computer)

I meant to the 5V header socket of the Arduino, not to a separate 5V

Completely disconnecting pin 7 and leaving it floating as you appear to do in the video isn't a good idea. It should always be held either high or low.
While it's low, absolutely nothing should happen after the servo centres.

When it's high, your thermistor will be read and appropriate action taken according to your 'updateAvgtemp()' function etc.

It's late at night and I'm not about to try to work out what that function is doing. There isn't even a single comment to help follow it.

Have you tried a simple sketch without all of the bells and whistles to determine that your servo is working properly?

It would be a good idea to do that, then if everything appears to be in order, instead of calling 'updateAvgtemp()', you could try hard-coding various values for 'val', and see if your servo responds correctly. If so, you'll know that the fault lies with that function.

Another point, have you done adequate testing to ensure that the analogue readings from your thermistor really fall between 350 and 700?

You could even try a very basic sketch, just reading the thermistor once each time without the averaging function, mapping the returned value from 0-1023 to 0-180, then writing the result to the servo. That'll be a good test of the servo, it's connections, the thermistor and it's connections. Then you could go from there.

I loaded the example sketch "sweep" the servo does sweep, but bounces like in the video.

As far as reading the thermistor, I have no clue how to do that. Not sure how they originally came up with 700 and 500.

OK bouncing is the wrong word. In electronics when we talk of bouncing and debouncing it means the very rapid multiple contacts made when a switch makes or breaks.
Like this:-

What you have is the servo dithering or hunting. Most probably as a result of pin 7 floating and it driving the servo between two places. Your explanation of what you think that pin does is wrong based on your layout diagram, a schematic would have been so much better.

The other possibility is that as your temperature average routine is not very good it is producing unstable results. Simply read the sensor ten times, add them up and divide by ten. Do that each time you want a reading. Use the serial print to see if you are getting a steady reading from that. If they are not steady then your servo will not be steady.