Turbo Timer/Boost Controller

Hi
To the Arduino Community, I'm new here and would like to say hello to you all..
A friend gave me this UNO and LCD and said here have a play
I came up with this idea of making a Turbo Timer Boost Controller in 1 unit.
I have been playing around with a UNO and made an Input from IGN and Output Relay
to IGN for turbo timer, output to solenoid for wastegate control and Pot to set Boost and Time from.
OK, I'm no programmer, just a bit of a hacker, but I had a go, I know its not clean and lean and would like any help in this area..
it almost works 100%..
The Turbo Timer bit of code I cant get the right statement to make it function
Thanks for reading and looking..

/*
  Tony's Turbo Timer Boost Controller V2
    Reads an analog input on pin A5, converts it to voltage.
  Attach the center pin of a potentiometer to pin A5, and the outside pins to +5V and ground.
    ::Smoothing
  Reads repeatedly from an analog input, calculating a running average
  and updating the (average) statement.  Keeps 5 readings in an array and
  continually averages them.
  Define the number of samples to keep track of.  The higher the number,
  the more the readings will be smoothed, but the slower the output will
  respond to the input.  Using a constant rather than a normal variable lets
  use this value to determine the size of the readings array.
*/
const int numReadings = 5;

int readings[numReadings];      // the readings from the analog input
int readIndex = 0;              // the index of the current reading
int total = 0;                  // the running total
int average = 0;                // the average
int voltage = 0;               // create voltage from potPin
int BoostPin = 11; // set output pin for the wastegate boost control
int timerPin = 5; //  set output Relay Turbo Timer count down, IGN Output pin.
int potPin = A5; // A5 Analog Pin potentiometer ref voltage
int ignPin = A4; // A4 Analog Pin IGN Sensing
int potValue = 0; // potentiometer input variable
int pwmValue = 0; // Output Duty Cycle PWM
int ignState = 0; // IGN On or Off
// the setup routine runs once when you press reset:
void setup() {
 // initialize serial communication at 115200 bits per second:
  Serial.begin(9600);
  // initialize all the readings to 0:
  for (int thisReading = 0; thisReading < numReadings; thisReading++)
    readings[thisReading] = 0;
  // Set Output Pins  
 pinMode(BoostPin, OUTPUT);
 pinMode(timerPin, OUTPUT);
 }
// the loop routine runs over and over again forever:
void loop() {
  // read the value from the potentiometer and Ignition:
  int  potValue = analogRead(potPin);
  int ignState = analogRead(ignPin);
  // Convert the analog reading from potPin (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage = average * (5.0 / 1023.0);
  // subtract the last reading:
  total = total - readings[readIndex];
  // read from the sensor:
  readings[readIndex] = analogRead(potPin);
  // add the reading to the total:
  total = total + readings[readIndex];
  // advance to the next position in the array:
  readIndex = readIndex + 1;
 // if we're at the end of the array...
  if (readIndex >= numReadings)
    // ...wrap around to the beginning:
    readIndex = 0;
 // calculate the average:
  average = total / numReadings;
  delay(1);        // delay in between reads for stability

//Print to the serial port
 // Serial.println(voltage);
   
                         

// Turbo Timer - waits for ignState to go from HIGH to LOW then writes timerPin HIGH for selected time then goes LOW.
    if (ignState > 1022) digitalWrite(timerPin, HIGH);  // IGN On
     else {
      }
    if (ignState < 1010) digitalWrite(timerPin, HIGH), delay(voltage * 1000 * 60) digitalWrite(timerPin, LOW); // IGN Off
     else {
      } 
     
                       The above bit Turbo Timer just here is where I cant get the code to work



 // My Version of Analog voltage to PWM, crude but works for this application
   // send the square wave signal to the BD681 Darlinton Transistor:
     if (average > 1010) digitalWrite(BoostPin , HIGH); // Max potValue average to hold open
     else {
       for(int pwmValue = 0; pwmValue <= 255; pwmValue +=50){ // Hz ?
     digitalWrite(BoostPin, HIGH); 
     delay(average / 50); // closed to open moves around the pot span
       }
     if (average < 2) digitalWrite(BoostPin , LOW); // Min potValue average to hold closed
     else {
       for(int pwmValue = 255 ; pwmValue >= 0; pwmValue -=50){ //Hz ?
       digitalWrite(BoostPin, LOW); 
       delay(1);  //open to close
     }
     }
     }  
}

This looks like a good project:

I'm not the most experienced coder and also a bit of a hacker/maker. But it looks to me as if your if/else statements are in the wrong format.

A good tip I picked up is going back to basics: Try pasting what you want to happen into the if/else example here:

Also use debugging to check ignState.

You should end up with something like this:

if (ignState > 1022)
{
 digitalWrite(timerPin, HIGH);
delay(voltage * 1000 * 60);

//debug lines
Serial.println(ignition is on);
Serial.print("turbo timer running for ");
Serial.print(voltage * 1000 * 60);
Serial.print(" seconds ");
}
else
{
digitalWrite(timerPin, LOW);

//debug lines
Serial.println(ignition is off);
}

Before you try that in your sketch - what is your timer doing when (ignState > 1022)? does that mean IGN on? Because really you need to only trigger on IGN off (if I understand a turbo timer properly) - so the logic will be:

  • if IGN off then run turbo timer
  • if IGN on then stop running turbo timer

Which I think may be the opposite of what you're doing.

The delay in your sketch will also make it difficult if the car is restarted and then stopped as the Arduino will not notice (it is effectively frozen during delay). Not a massive practical problem unless you're very ignition key happy.

I took me a day but using millis() to time things avoids this (see the BlinkWithoutDelay sketch).

I'd suggest getting it working with delay first though! Good luck!

Steve

Thanks Steve
Your help has helped lots
using the arduino reference page was a big help, lots more ideas about the code now.
anyway your bit of code with a tweak works well.
Thanks again
I will post the full code when finished.

// Turbo Timmer - waits for ignState to go from HIGH to LOW then writes timerPin HIGH for selected time then timerPin goes LOW.
   if (ignState > 1010) //Ign On
{
digitalWrite(timerPin, HIGH);
//debug lines
// Serial.println(ignition is on);
// Serial.print("turbo timer running for ");
// Serial.println(voltage * 1000 * 60);
// Serial.print(" seconds ");
}
else
{
delay(voltage * 1000 * 60);
 digitalWrite(timerPin, LOW); //IGN Off

Final code..

#define relay A1 // Analog pin to control ign relay
int clockPin = 2; // Digital pin connected to the counter's clock pin
int resetPin = 3; // Digital pin connected to the counter's reset pin
int pwmValue = 0; // Output Duty Cycle PWM
int boostPin = 9;
int potPin = A0; // Analog pin connected to the potentiometer wiper
int val = 0;         // variable to store the read value
#define SECOND 1000UL
#define MINUTE (SECOND * 60UL)
unsigned long time;

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
  pinMode(clockPin, OUTPUT);
  pinMode(resetPin, OUTPUT);
  reset();
  pinMode(relay, OUTPUT);
  TCCR1B = TCCR1B & B11111000 | B00000101; // for PWM frequency of 30.64 Hz
  pinMode(boostPin, OUTPUT);
}
void loop() {

  // Serial.print("\t IgnState = ");
  // Serial.println(ignState);
  ign();
  display();
  boost();
}
void boost() {
  /* Boost  control*/
  val = analogRead(potPin);  // read the input pin
  analogWrite(boostPin, val / 5); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255


}
void display() {


  /* Read the analog value from the potentiometer. */
  int potValue = analogRead(potPin);
  /* Map the value to the range 0-9. */
  int n = potValue * 10 / 1024;

  /* Turn ON/OFF quickly the first n LEDs. The n LEDs
    will appear to be ON at the same time due to the
    "persistence of vision" effect. */
  for ( int i = 0; i < n; i++ ) {
    clock();
  }

  reset();
}

/*
   Sends a clock pulse to the counter making it advance.
*/
void clock() {
  digitalWrite(clockPin, HIGH);
  delay(1);
  digitalWrite(clockPin, LOW);
}
/*
   Resets the counter making it start counting from zero.
*/
void reset() {
  digitalWrite(resetPin, HIGH);
  delay(1);
  digitalWrite(resetPin, LOW);
}

/*
   Looks at ign state on or off.
*/
void ign() {


  int ignState = analogRead(A2); // Read the analog value from ign switch.


  if (ignState > 800)
  {

    digitalWrite(relay, HIGH);
  }
  else
  {

    /* Read the analog value from the potentiometer. */
    int potValue = analogRead(potPin);
    /* Map the value to the range 0-9. */
    int n = potValue * 10 / 1024;
    time = millis();
    delay(n * MINUTE / 2);  //Shutdown time

    digitalWrite(relay, LOW); //IGN Off

  }

}