So I have been working for a while now on a controller for the vegetable oil system in my vehicle. I am fairly new to programing and am enjoying teaching myself with an arduino. I have an arduino nano 32 that I plan to use for the project. I have gone through many different variations and different ways of doing the logic. Some that work and then get to complicated and start not working and others that just don't work. I am just wondering how any of you would go about doing this? I don't need a full code per-say as I am trying to do this mostly myself but maybe some examples or different ways to get me started on the right path. Here is what I have up until the loop

#include <elapsedMillis.h>
#include <Wire.h>
#include <LiquidCrystal.h>
#include "OneButton.h"
#include <EEPROM.h>

unsigned int purgeTime = 15000;
unsigned long currentTime = millis();
unsigned long tempTime;
//unsigned long loopTime;
unsigned long previousPurgecount = 0; // last time update
long purgeDelay = 10000; // interval at which to do something (milliseconds)
int feedValve = 12;                   // feed valve is connected to pin 12
int returnValve = 11;                 // return valve is connected to pin 11
int heater = 10;                      // heater is connected to pin 10
//int loopValves = 9;                 // loop valves are connected to pin 9
int pump = 8;                         // pump is connected to pin 8
int buzzer = 7;                       // buzzer is connected to pin 7 
int ignition = 2;                     // ignition is connected to pin 2
int ignition_mode = 0;                // set initial ignition mode to off
boolean valve_state = 0;              // set initial valve_state to off
int buttonMode = 0;                   // set initial button mode to 0
                        // front temp sensor is connected to        analog pin 0
                        // vegetable oil loop temp sensor is connected to analog pin 1
                        // coolant loop temp sensor is connected to analog pin 2
OneButton button(A2, true);
elapsedMillis elapsedTime;

void setup()
  // link the doubleclick function to be called on a doubleclick event.   
  pinMode(feedValve, OUTPUT);                    // Set the Feed Valve pin as output
  pinMode(returnValve, OUTPUT);                  // Set the Return Valve pin as output
  pinMode(heater, OUTPUT);                       // Set the Heater Valve pin as output
  //pinMode(loopValves, OUTPUT);                 // Set the Loop Valves pin as output
  pinMode(pump, OUTPUT);                         // Set Pump pin as output
  pinMode(buzzer, OUTPUT);                       // Set buzzer pin as output
  pinMode(ignition, INPUT);                      // Set ignition as input  

double Thermister1(int RawADC) {
  double Temp1;
  Temp1 = log(((10240000/RawADC) - 10000));
  Temp1 = 1 / (0.001129148 + (0.000234125 * Temp1) + (0.0000000876741 * Temp1 * Temp1 *   Temp1));
  Temp1 = Temp1 - 273.15;           // Convert Kelvin to Celcius
  Temp1 = (Temp1 * 1.8) + 32;    // Convert to F
  Temp1 = round(Temp1);
  return Temp1;

void loop()

Here is the logic that I am trying to create.

Toggle switch to power the system.

One button control. also input from ignition to say engine is running or not.

-if ignition is not on do nothing

-button press

-heater on and regulate temp at 90F using temp sensor 1

-once temp sensor 1 reaches 85F then feed valve opens(Output goes HIGH)

-once feed valve is open wait 45 seconds then return valve opens

-if button pressed or ignition goes off anytime before return valve opens then everything turns back off and goes back to before button press

-once temp sensor 2 reaches 50F then pump turns on

-once feed valve is open wait 5 min then turn on pump if not on, and open loop valve

Now running on vegetable oil

-button press

-turn off feed valve, heater

-wait 45 seconds then turn off return valve, loop valve and pump

-if running on vegetable oil and ignition goes off

-then heater turns off and pump turns off

-alarm beeps

-if button pushed then alarm turns off and valves stay open.

-if ignition comes back on turn on heater and pump

Back to running on vegetable oil

I have a DFROBOT I2C/TWI 4x20 LCD Module that I would like to display the two temperatures and some other stuff that I can fiddle with.

I do not think I have the right LCD library for this LCD.

When ever the heater is on it should regulate the temperature using temp sensor 1

Thank you so much in advance. David

  • 1
    \$\begingroup\$ Step one is to turn it into one of these first. \$\endgroup\$ Commented Nov 14, 2013 at 6:24
  • \$\begingroup\$ First, you need to avoid using so much floating point math, and multiplication for that matter. This is an 8-bit fixed point machine and doesn't have a hardware multiplier. Each of those double variables is going to chew up at least 3-4 bytes of memory. Remember multiplying 2 and 3 digit numbers in grade school? That is how this micro will have to handle that multiplication, but some poor sap already wrote that code so you don't have to. The biggest issue here is speed, just that chunk of code you posted is going to take thousands of instruction cycles... \$\endgroup\$
    – Matt Young
    Commented Nov 14, 2013 at 13:16
  • \$\begingroup\$ ...There are a lot of things you want to do here. You need to forget about some of these libraries, and write the code yourself to make sure this compiles to something as lean and mean as possible. \$\endgroup\$
    – Matt Young
    Commented Nov 14, 2013 at 13:17

1 Answer 1


First, as alluded to earlier, learn about state machines. That's really what you have here and diagramming it as a state machine will make it simpler to understand and to program.

Next, I disagree with the speed and floating point comment. If you need floating point, use it. Speed is not an issue with this controller. However, it looks like you're applying a simple form of the Steinhart-Hart equation to reduce the thermistor? If so, don't. You're controlling over a fairly small range, you can interpolate between a couple of fixed points on the resistance-temperature curve and be accurate enough.

So decide on the states this controller will be in. For starters I see OFF, PREHEAT, HEAT_STAGE1 (temp1 == 85), HEAT_STAGE2 (temp2 == 50), VEG_OIL, etc...

Decide on the events that will cause a transition from state to state and then you can build the code as a switch() statement, or my personal preference: a data-driven state machine with states and transition events as a table.

Next, you have a couple of tasks that will run in all states once you leave the OFF state, they will also need to be handled. I suggest you start with the temperature control tasks and get them working first.

I would also suggest you ask this on a programming StackExchange site (my get better ideas) since it's really not about electronics per se.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.