Here is the latest upgraded code. Ive found we really needed to slow it down near the happy zone. When sudden open throttle occurs it responds very fast.
#include <Servo.h>
Servo myservo; // create servo object to control a servo
//VG 02 Wide Band Mixture Controller V1.3
//Craeted by Matt Ryder, Vulcan Gasifier LLC May 5, 2015
//Last edit; June 22, 2015
// This code requires an AEM O2 sensor kit with LSU 4.9 sensor Part # 30-4110
// These constants won't change:
const int O2SensePin = A1; // pin that the O2 sensor is attached to
const int O2SwtPin = A2; //Pin Auto Mode Switch is attached to
const int O2ManPin = A0; // Reads the O2 Manual Adjust Potentiometer on pin A1
const int RichCon1 = 650; // Threshold limit for Rich gas condition
const int LeanCon1 = 700; //Threshold limit for lean gas condition
int pos = 0; // variable to store the servo position
int val; // variable to read the value from the analog pin
int lastpos = val; // variable to store the servo last position
int O2SwticthState = 0; // variable for reading the Fuel Mixer Mode status
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
// Need to create a set point for servo start position after manual tuning
{
val = analogRead(O2ManPin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 180); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
lastpos = val; // sets lastpos to equal the position of the manually set position
delay(10); // waits for the servo to get there
}
}
void loop()
{
// read the state of the pushbutton value:
O2SwticthState = digitalRead(O2SwtPin);
// read the value of the O2 sensor:
int sensorReading = analogRead(A1);
if (O2SwticthState == LOW)
{
val = analogRead(O2ManPin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 180); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
lastpos = val; // sets lastpos to equal the position of the manually set position
delay(10); // waits for the servo to get there
}
else
{
delay (10);
}
if (sensorReading <= RichCon1)
{
lastpos = lastpos +1;
myservo.write (lastpos);
val = map(analogRead(O2SensePin), 0, 650, 0, 1500);
delay(val);
}
else if (sensorReading >= LeanCon1)
{
lastpos = lastpos -1;
myservo.write (lastpos);
val = map(analogRead(O2SensePin), 1023, 700, 800, 1500);
delay(val);
}
else {
delay (10);
}
}`