I have some new code, this is still untested and only ran under simulation. But this is what I have been looking for.
I ran a simulation on this with a pot switch in place of the O2 Sensor input. So if I simulate a rich condition, the servo will search in increments opening the valve and vise versa if I lean out the sensor input.
The trick is also to get another input signal, so you can manually adjust the valving for start ups. So what the code looks for first is a near 0 voltage signal, if this condition is true I can then move the valve using the pot on the control panel. To ensure i have no voltage coming from the O2 sensor I have an override switch that signal run through. So I can basically find the sweet spot manually during start up and get things into the right parameters.
Then when the engine is up and running I can then turn on the switch supplying voltage from the sensor. The conditions will then be beyond near 0 voltage and the other two sets of code can then intervene.
In the code, is a set of parameters (thresholds) that the code is trying to satisfy. These parameters can be changed so this can be tuned to the sweet spot. The second set of code looks for voltage lower than (threshold 1) and the third set of code looks for a lean condition voltage higher then (threshold 2). I have the parameters set up with a gap somewhere in the middle of the available signal. So if the input signal falls in this gap nothing will change.
In the second and third set is a time delay, this is how fast the code is read. Basically if a condition is not met the controller will out put for the time set in the delay for that part of the code. So if a lean condition is detected, the servo will try and move fully closed. If I give it too much time it will fully close before moving on to the next set of code. We donât want this, we just want the servo to move a tiny bit and then the code to be looked at again to see if conditions are met. If not the servo will move again and so on.
So the delay can also be changed to allow for longer or shorter movement between readings.
In this code I have it set for 2 ms. I think this will be too fast and tweak on this tomorrow. For now Ill post this if any one wants to play with it.
#include < Servo.h >
Servo myservo; // create servo object to control a servo
// These constants wonât change:
const int threshold1 = 450;
const int threshold2 = 550; // an arbitrary threshold level thatâs in the range of the analog input
const int threshold3 = 200;
int analogPin = A0; // pin that the 02 sensor is attached to
int pos = 0; // variable to store the servo position
int potpin = 1; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
// initialize serial communications:
Serial.begin(9600);
}
void loop()
{
// read the value of the potentiometer:
int analogValue = analogRead(analogPin);
if (analogValue < threshold3)
{
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 50, 950, 0, 180); // scale it to use it with the servo (value between 0 and 180)
delay(15); // waits for the servo to get there
}
else if (analogValue < threshold1)
{
for(pos = 180; pos>=0; pos -=1 ); // goes from 180 degrees to 0 degrees
delay(2); // waits 2ms for the servo to reach the position
}
else (analogValue > threshold2);
{
for(pos = 0; pos <= 180; pos += 1) // goes from 0 degrees to 180 degrees
delay(2); // waits 2ms for the servo to reach the position
}
}