Arduino Manager Development for All

No worries. I knew there was only a slight chance of testing before Argos which is the other reason why I was in a hurry. It might change by the time you get back.

I hear you Matt! Good luck at Argos. Wish I could make it.

One thing for sure - the Arduino / O2 sensor mixing valve will work. Iā€™ve been running one on my generator and one on my tractor for over a year now. Itā€™s been through the paces and works well on both platforms. So donā€™t give up on this.

The nice thing about coding: Once itā€™s doneā€¦itā€™s done. Each iteration from then on is as easy as flashing a chip.

The tractor even sat out under a porch all winter with no ill effects on the electronics. I thought that is something worth noting.

It will work with the right logic. :slight_smile:

I changed it. I was going to use a circular buffer until I found.
http://www.daycounter.com/LabBook/Moving-Average.phtml
Literally it is this:
O2MovingTOT = O2MovingTOT - O2MovingAVG + analogRead(O2SensePin);
O2MovingAVG = O2MovingTOT/O2MACOUNT ;

which isnā€™t the true avgā€¦ but it actually smoothes nicely and it is much simpler, faster, and uses considerably less memory. It takes a few more iterations to get to the real average though. You just have to keep the button engaged for a like 8-12 iterations (.8-1.2 seconds) longer then the last pot adjustment to figure out ā€œidealā€, which I didnā€™t think was a big deal (if it is, it can be programmed around).

#include <Servo.h>
//GAP is the space both +- around the "ideal" 
#define GAP 10 
//O2INTERVAL is the delay time between iterations of the loop. 
#define O2INTERVAL 100
//O2MACOUNT is the number of values used for the moving avg calculation..should be a power of 2.
#define O2MACOUNT 4
// 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


int O2MovingAVG;
int O2MovingTOT;
Servo myservo;  // create servo object to control a servo 


void O2Sensor(void) ;


 //VG 02 Mixture Controller v0.2
//Craeted by Matt Ryder, Vulcan Gasifier LLC May 5, 2015
  //Last edit; May 12, 2015 




int O2SensorIdeal;

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 O2SwitchState = 0;         // variable for reading the Fuel Mixer Mode status
int O2Switch =0;            //variable for determining the whether this is the first time this has been run without the switch.
unsigned long currentMillis=0, O2previousMillis=0; //define variables for the millis function.

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, 20, 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() 
{
  currentMillis=millis(); //sets to current time. 
  if (currentMillis - O2previousMillis >= O2INTERVAL)  //check to see whether it is time to check everything. 
      { 
        O2previousMillis = currentMillis;  //keeps the old value of currentMillis. 
        O2Sensor(); 
      }
}//end loop()

void O2Sensor(void) 
{

  
 
    // read the state of the pushbutton value:
  O2SwitchState = digitalRead(O2SwtPin);
   // read the value of the O2 sensor and add it to the avg buffer
   
  O2MovingTOT =   O2MovingTOT - O2MovingAVG + analogRead(O2SensePin);
  O2MovingAVG = O2MovingTOT/O2MACOUNT ;


  if (O2SwitchState == LOW)
  {
    val = analogRead(O2ManPin);  // reads the value of the potentiometer (value between 0 and 1023) 
    val = map(val, 0, 1023, 20, 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 
    O2Switch = 1;                          //we set this value to one, so we know we are still reading the pot. 

  }else{
    if(O2Switch == 1)
      { O2Switch =0;
         O2SensorIdeal = O2MovingAVG;
      }
  //heart of the program..
     if(O2MovingAVG > (O2SensorIdeal + GAP))
      {
      lastpos-- ;
      myservo.write (lastpos);
      }else if ( O2MovingAVG <(O2SensorIdeal - GAP))
      {
      lastpos++ ;
      myservo.write (lastpos);
      }
      
      
  }//end switch state
}//end milli 

// END OF FILE

Hi Matt Wood and gas friends, I once improve your code.

This has been improved:

  1. program code has been cleaned (so you can read better the program code)
  2. The heart of the program (as was a Servo_Pos_Min and Servo_Pos_Max function added to the servo to protect)
  3. and a lot more. :grin:

What do you think of the program code? :heart_eyes:

Sorry I can not speak English, had to google translator take my language is German. :grin:

/*

VG 02 Mixture Controller v0.2
Craeted by Matt Ryder, Vulcan Gasifier LLC May 5, 2015
Last edit; May 12, 2015 

Edit: by MicroGaser
Last edit; 04.06.2015 Cleaned code.

*/

#include <Servo.h>

// tolerance is the space both +- around the "setpoint" 
#define tolerance 70 

// These constants won't change:
int O2_Servo_Poti_Pin = A0;                 // Reads the O2 Manual Adjust Potentiometer on pin A1
int O2_Sensor_Pin = A1;                     // Analog pin, where the O2 sensor is connected
int O2_Auto_Pin = 4;                        // Digital pin (INPUT), Auto Mode Switch is attached to

// Setpoint
int setpoint = A2;                          // Analog setpoint (Lambda / AFR) for the O2 Sensor

// Servo
#define servo_speed 200                     // Servo Speed
#define Servo_Pos_Min 20                    // Servo Min Position
#define Servo_Pos_Max 180                   // Servo Max Position
int servo_poti;                             // Variable to read the value from the analog O2_Servo_Poti_Pin
int servo_pos;                              // Variable to store the servo position
Servo myservo;                              // Create servo object to control a servo

// Lambda
#define Lambda_Min 68                       // Lambda Min 0.68
#define Lambda_Max 136                      // Lambda Max 1.36

// AFR
#define AFR_Min 100                         // AFR Min 10.0
#define AFR_Max 200                         // AFR Max 20.0

// O2-Sensor for the heart of the program
int O2_Moving = O2_Sensor_Pin;

void setup()
{ 
  Serial.begin(9600);
  pinMode(5 , OUTPUT);                          // LED Auto Mode
  pinMode(12 , OUTPUT);                         // LED Setpoint
  myservo.attach(9);                            // Attaches the servo on pin 9 to the servo object
}// The setup end
   
void loop() 
{
  float voltage = map (analogRead(O2_Sensor_Pin) , 0, 1023, 0, 500) / 100.0;                    // Volt
  float lambda = map (analogRead(O2_Sensor_Pin) , 0, 1023, Lambda_Min, Lambda_Max) / 100.0;     // Lambda
  float AFR_setpoint = map (analogRead(setpoint) , 0, 1023, AFR_Min, AFR_Max) / 10.0;                     // AFR O2_Sensor Setpoint
  float AFR = map (analogRead(O2_Sensor_Pin) , 0, 1023, AFR_Min, AFR_Max) / 10.0;               // AFR O2_Sensor Value

  O2_Sensor_Auto();
  O2_servo_poti();
  setpoint_LED();
  Auto_LED();
  
  
  // Serial_Display
  Serial.println();
  Serial.print("AFR: ");
  Serial.print(AFR);
  Serial.print("   ");
  Serial.print("AFR_Setpoint: ");
  Serial.print(AFR_setpoint);
  Serial.print("   ");
  Serial.print("Lambda: ");
  Serial.print(lambda);
  Serial.print("   ");
  Serial.print(voltage);
  Serial.print(" ");
  Serial.print("Volt ");
  Serial.print("  ");
  Serial.print("Servo Pos: ");
  Serial.print(servo_pos);
  Serial.print("   ");
  Serial.print("Analog Value: ");
  Serial.print(O2_Sensor_Pin);
  
}// The loop end

void Auto_LED(void)
{
  if (digitalRead(O2_Auto_Pin) == LOW)
  {
    digitalWrite(5, LOW);
  }
  else
  {
    digitalWrite(5, HIGH);
  }
}

void setpoint_LED (void)                                                        // Setpoint is reached
{
  if (!(analogRead(O2_Moving) < analogRead(setpoint) - tolerance) && !(analogRead(O2_Moving) > analogRead(setpoint) + tolerance))
  {
    digitalWrite(12, HIGH);
    delay(200);
    digitalWrite(12, LOW);
    delay(300);
  }
}// The setpoint_LED end

void O2_servo_poti(void)                                                        // Need to create a set point for servo start position after manual tuning
{
  if (digitalRead(O2_Auto_Pin) == LOW)
  {
    servo_poti = analogRead(O2_Servo_Poti_Pin);                                 // Reads the value of the potentiometer (value between 0 and 1023) 
    servo_pos = map(servo_poti, 0, 1023, Servo_Pos_Min, Servo_Pos_Max);         // Scale it to use it with the servo (value between 0 and 180) 
    myservo.write(servo_pos);                                                   // Sets the servo position according to the scaled value 
    delay(20);
  }
}// The O2_servo_poti end

// Heart of the program ...
void O2_Sensor_Auto(void)
{
  if(digitalRead(O2_Auto_Pin) == HIGH)
  {
    if(analogRead(O2_Moving) > analogRead(setpoint) + tolerance)
    {
      if (servo_pos > Servo_Pos_Min)
      {
        servo_pos--;
        myservo.write(servo_pos);
        delay(servo_speed);
      }
    }
    else if (analogRead(O2_Moving) < analogRead(setpoint) - tolerance)
    {
      if (servo_pos < Servo_Pos_Max)
      {
        servo_pos++;
        myservo.write(servo_pos);
        delay(servo_speed);
      }
    }
  }
}// The O2_Sensor_Auto end ii

I gave this a try today, it is too fast when close to the happy zone and is too slow when things get out of whack.

However, the lag is much faster. If we could make this react to the different situations this might work. Still trying to wrap my head around it. :fire:

check this out can you put this into that code.

I will have to tinker with this one on our demo unit. Looks like I will need to write some stuff in to get it to work on my application. :slight_smile:

Did it move more smoothly?

Did you muck with any of the settings at the top?
Those were just guesses so I am guessing the happy zone is too small.
and the delay for the loop is too long.

like change (this is the size of sweet spot on both sides of the the pot setting)
#define GAP 10
to
#define GAP 20
and (this is the speed of the loop ie how fast it checks the data and corrects)
#define O2INTERVAL 100
to
#define O2INTERVAL 50

(I am actually working on something to help make it easier to change the settings.) I can probably have some of it fixed by tomorrow.

Very cool,

We have all kinds of new programs now :smile:

My customer that this machine is for happens to be a programing whiz. lol. So he is working on an averaging code too.

Here is my latest and greatest, still need to test this one. But I put this together in the case switch using the VarSpeedServo library. Its pretty cool I now have the delays eliminated in the case code. However, Im keeping short delays in this one. This one is reverting back to the Else If code. With slight tweeking on these delays I can really slow things down but have the speed in other areas.

#include <VarSpeedServo.h>

VarSpeedServo myservo;  //Create a servo object 
 
 //VG 02 Mixture Controller v0.5
//Craeted by Matt Ryder, Vulcan Gasifier LLC May 5, 2015
  //Last edit; May 20, 2015 

// 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 RichCon3 = 550;   //Threshold limits for rich gas condition
const int RichCon2 = 580;   
const int RichCon1 = 610;



const int LeanCon1 = 670;    //Threshold limits for lean gas condition
const int LeanCon2 = 750;
const int LeanCon3 = 800;

int sensorMin = 1023;        // minimum sensor value
int sensorMax = 0;           // maximum sensor value
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);
   
   sensorReading = map(sensorReading, sensorMin, sensorMax, 0, 200);
   
  if (O2SwticthState == LOW)
  {
  val = analogRead(O2ManPin);  // reads the value of the potentiometer (value between 0 and 1023) 
  val = map(val, 0, 1023, 20, 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);
    
 
  }
   int speed1 = 1;
   int speed2 = 2;
   int speed3 = 3;
   int speed4 = 4; 

     if  (sensorReading <= RichCon1)
  {
          myservo.slowmove(180, speed1);

          delay(10); 
                                   
  }        
  
   else  if  (sensorReading >= LeanCon1)
  {
       myservo.slowmove (0, speed1);
       
       delay(10); 
                   
                                   
  }
         else if  (sensorReading <= RichCon3)
  {
       myservo.slowmove(180, speed4);
         
         delay(10); 
                                   
  }
      else if  (sensorReading <= RichCon2)
  {
       myservo.slowmove(180, speed3);
       
       delay(10);                                
  }
    else if  (sensorReading > LeanCon2)
  {
       myservo.slowmove (0, speed1);
       
       delay(10);
  }
  
      else if  (sensorReading > LeanCon3)
  {
       myservo.slowmove (0, speed2);
       
       delay(10);
  }
    else {
         delay (10);
         }
         //might need a delay for the o2 sensor in general for everything to catch up.
         
}

Give this a whirl. I changed a few of the values, it loops faster, but uses more values to avg so it shouldnā€™t bounce around closer to the sweetspotā€¦ then I added a huge correction if the value is way off the avg for more then one reading.

//Version3.2

/*
3.2
-Added automatic automode after a delay.  
-Added support for Hardcoded "ideal" to aim for. Reversed the switch reading to read ON (from LOW to HIGH) 
   in manual mode.. It eliminates the need for the switch/jumper and pin to just run it in automode.
-Added debug to print on the seerial ports values. 
3.1.3
-Added comments, fixed the MovingAvg so it works correctly.changed the wayoff formula. 
3.1.2
-added independent control for hi/low's for better control if ideal is on a non-linear part of the scale, like a narrowband sensor would be. 
-bugfixes :)
-changed increased the gap value to make the sweet spot larger
-increased the number of items in the avg so it moves slower closer to the avg
-added wayoff to move much faster if the value is way off the avg more then once. 
*/
#include <Servo.h>
//turns on serial debugging
#define DEBUG
//O2HARDIDEAL
#define O2HARDIDEAL 510
//O2HARDSET uncomment  if you want to use a Hard set ideal defined in as O2HARDIDEAL
//#define O2HARDSET 
//comment out if you want to use the pot to determine the ideal value to aim for.
#undef O2HARDSET
//O2GAP is the space both +- around the "ideal" 
#define O2GAPHIGH 20
#define O2GAPLOW 20
//O2INTERVAL is the delay time between iterations of the loop. 
#define O2INTERVAL 30
//O2MACOUNT is the number of values used for the moving avg calculation..should be a power of 2.
#define O2MACOUNT 8
//O2WAYOFF do a much faster correction if the sensor reading and the pot setting differ by more then these value
#define O2WAYOFFHIGH 75
#define O2WAYOFFLOW 75
//O2WAYOFFMISSES is how many times in a row the value can be in the "way off" range before a huge correction
#define O2WAYOFFMISSES 1
//O2WAYOFFCORRECTION the number of steps the servo should move to correct if we are way off. 
#define O2WAYOFFCORRECTION 5
//O2POTTIMER time in milli seconds before going into automode. (30000 = 30 seconds)
#define O2POTTIMERDELAY 30000

// 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

//globals for O2
int O2MovingAVG;
int O2MovingTOT;
int O2MovingAVGCount=0;
int O2SensorReading;
int O2SensorIdeal;
int O2wayoffvar=0 ;//var for keeping track of if the reading is way off more then once.
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 O2SwitchState = 0;         // variable for reading the Fuel Mixer Mode status
int O2Switch =0;            //variable for determining the whether this is the first time this has been run without the switch.
unsigned long currentMillis=0, O2previousMillis=0, O2PotTimer=0, O2SerialPrintTime=0; //define variables for the millis function.

Servo myservo;  // create servo object to control a servo 
void O2Sensor(void) ;

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 
  O2PotTimer=millis(); //set a value for PotTimer to make sure it automatically fails the first time through
} 
   
void loop()
{
  currentMillis=millis(); //sets to current time. 
  if (currentMillis - O2previousMillis >= O2INTERVAL)  //check to see whether it is time to check everything. 
      { 
        O2previousMillis = currentMillis;  //keeps the old value of currentMillis. 
        O2Sensor(); 
      }
}//end loop()

//the O2Sensor function 
void O2Sensor(void) 
{
  int val_previous;
// read the state of the pushbutton value:
  O2SwitchState = digitalRead(O2SwtPin);
// read the value of the O2 sensor and add it to the avg buffer
  O2SensorReading = analogRead(O2SensePin);

//figure out moving avg
  if(O2MovingAVGCount < O2MACOUNT)
     {
      O2MovingTOT =   O2MovingTOT + O2SensorReading;
      O2MovingAVGCount++;
      O2MovingAVG = O2MovingTOT/O2MovingAVGCount ;
     }else{
      O2MovingTOT =   O2MovingTOT - O2MovingAVG + O2SensorReading;
      O2MovingAVG = O2MovingTOT/O2MACOUNT ;
     }
#ifdef DEBUG
if(O2SerialPrintTime+1000 > millis())
  {
  Serial.print(" O2Reading: ");
  Serial.print(O2SensorReading);
  Serial.print(" avg: ");
  Serial.print(O2MovingAVG);
  Serial.print(" Ideal: ");
  Serial.println(O2SensorIdeal);
  O2SerialPrintTime=millis();
  }
#endif
  val_previous=val;
  val = analogRead(O2ManPin);  // reads the value of the potentiometer (value between 0 and 1023) 
  //if the switch is on, the pot value changed or we are within the delay time before automode..
  if (O2SwitchState == HIGH || val_previous != val ||((O2PotTimer + O2POTTIMERDELAY) < millis()))
  {
    if (val_previous != val)
      {
      O2PotTimer = millis();
      }
//    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(map(val, 0, 1023, 0, 180));                  // sets the servo position according to the scaled value 
    lastpos = val;                       // sets lastpos to equal the position of the manually set position
    O2Switch = 1;                          //we set this value to one, so we know we are still reading the pot. 
//anything else means automode. 
  }else{         
    //check to see if this is the first time through since the button value changed if so set the O2SensorIdeal var to the MovingAVG
   if(O2Switch == 1)
      { O2Switch =0;
#ifdef O2HARDSET
        O2SensorIdeal = O2HARDIDEAL ;
#else
         O2SensorIdeal = O2MovingAVG;
#endif  
      }
    //check to see if the O2MovingAVG is way off the mark either high or low, and if so set a variable..
    if( ((O2MovingAVG-O2SensorIdeal)>O2WAYOFFHIGH)||((O2SensorIdeal-O2MovingAVG)>O2WAYOFFLOW))
      {
       O2wayoffvar++;
      }
    else
      {
       O2wayoffvar = 0;
      }
      
//heart of the program..
//if O2MovingAVG is outside the high boundary fix it decrease the lastpos var. 
     if(O2MovingAVG > (O2SensorIdeal + O2GAPHIGH))
      {
        //check to see if the wayoff var is set if so do the big correction otherwise the small correction. 
       if(O2wayoffvar>O2WAYOFFMISSES)
         {
         lastpos -=O2WAYOFFCORRECTION;
         O2wayoffvar=0;
         }
       else
         {
         lastpos-- ;
         }
//if O2MovingAVG is outside the low boundary fix it increase the lastpos var. 
      }else if ( O2MovingAVG <(O2SensorIdeal - O2GAPLOW))
      {
//check to see if the wayoff var is set if so do the big correction otherwise the small correction. 
       if(O2wayoffvar >O2WAYOFFMISSES)
         {
         lastpos +=O2WAYOFFCORRECTION;
         O2wayoffvar=0;
         }
       else
         {
         lastpos++ ;
         }
      }else{
        //do nothing
      }
  //write the new position to the servo 
      myservo.write (lastpos);
      
  }//end switch state
}//end milli

Will do, Im hoping to run the lister tomorrow. So if it will start Ill give it try on it.

Ill look at this and see if I can figure it out, but how do I move the sweet spot parameters. Say the code thinks all is good but in reality we are running rich.

Push the button and move the pot, it should adjust on the fly. It isnā€™t a set value. it is the avg of the O2 reading for that particular pot setting. You need wait a couple of seconds after the last pot adjustment before hitting the button to go into automode so it has the right average. The avg in the code is called IdealSensor.

The GAP value determines the size of the sweet spot. it is IdealSensorĀ± GAP

I added a bugfix. The lister moves at a slower rpm, so backing off the loopspeed might help.

Im probably going to play with this again here in the next week. Can you guys send me your code that is your latest and greatest. Iā€™ve lost track, and thought this would bring less clutter to the thread.

[email protected]

I give each one a whirl and post the results.

I also have the narrow band sensor. Ill start out with some existing code to try and get it working.

Another thought, do you think a stepper motor would be a better answer for what we are trying to do?

I have a small stepper and also a nema 17 stepper here I want to play with.

We can change the speed in the code much easier and also add in a ramp up. So the code would be a lot simpler. The only thing is we would need to add in a home switch for full closed and full open.

It is also much easier to adapt one to a ball valve and these little steppers have more power to turn a ball valve. I would rather go this route as it would be a more bullet proof setup.

I think my latest and greatest that I donā€™t think has any bugs is in the thread. I just updated the codeā€¦ The version 3.x one -should- work a lot better, but you probably have to make some adjustments to the #define statements at the top.* I would be happy to get feedback on it or fix it if there is a problem.

I have been trying to get the pi hooked to the arduino to make this easier for me to work on. But i kept crashing the arduino with my program, and it needs a hard reset, which I canā€™t do remotely which is how I work on the pi. :slight_smile: It is a problem specific to that arduino. But I know what is causing it now.

Since, I know you wanted to learn something.
#define BLAH 30
is a preprocessor directive (it is called a macro too). Basically before the program gets compiled, it does pre processing, so during that phase of the compile, anywhere that has BLAH gets a substituted before the compile.
code like:
int a=BLAH ;
after preprocessing before it gets compiled looks like:
int a = 30;

if you use something like
const int a =30;
it is actually counted as a variable for memory allocation.

The real reason I did it was because you could see them and change them without having to dig through the code. More then likely if I get the i2c working right, those will be the adjustment variables.

Very cool, I have both a Banana Pi M-1 and an M-2. I now have the Ras Pi B+ with the 1 gig chip. The Banana pi M-2 does not support wifi with the Raspbian OS. So Im going with the Raspberry for now prolly better that way seems this is the platform most are using.

Shoot me an email with it if you could. What Ill do is create a file for all you guys so I can keep things straight :smile:

[email protected]

Take a look at this code. I am also playing around with some new stuff. I have the 8 channel relay board and stepper motor shield. I found that there is a pot speed control for the stepper motors in the Arduino Examples. So I wondered if there was something existing for a servo. Where it would simply adjust speed not position and sure enough I found it.

I took one of the earlier " If Else code and incorporated it into it with a few mods and it works!! Well at least on the bench. It eliminates a lot of code

#include <Servo.h> 
 
Servo myservo;  // create servo object to control a servo 
 
 //VG 02 Mixture Controller v0.3
//Craeted by Matt Ryder, Vulcan Gasifier LLC May 5, 2015
  //Last edit; May 16, 2015 

// 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 = 610;



const int LeanCon1 = 670;    //Threshold limits 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, 20, 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, 20, 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, 660, 0, 150);
    Serial.println(val);
    delay(val);                       // waits 15ms for the servo to reach the position 
  }        
  
   else  if  (sensorReading >= LeanCon1)
  {                                
          lastpos = lastpos -1;
         myservo.write (lastpos);
   val = map(analogRead(O2SensePin), 1023, 650, 0, 150);
  Serial.println(val); 
    delay(val);                       // waits 15ms for the servo to reach the position 
  } 

    else {
         delay (10);
         }
         //might need a delay for the o2 sensor in general for everything to catch up.
         
}

What pins do you use to set the i2c?

Im going to try this very soon!!