Hi guys,
I’ve been trying pretty hard lately to write some basic code up for my water methanol injection kit – to be used on my car. The idea is that it’ll inject a water/methanol mix alongside the normal fuel to aid performance.
- I have a high pressure dc pump (to be controlled via mosfet with pwm)
- I will be taking a 0-5v reading from the mass airflow sensor
- I will also be taking a 0-5v reading from the manifold absolute pressure sensor.
I was hoping someone may be able to look over my code and give me some advice/suggestions as to whether it’ll actually achieve what I’m wanting it to.
I want the Arduino to change the pwm output to the pump relative to the change in airflow from the mass air flow meter. In short, as mass air flow increases I would like pump duty cycle to increase between two values.
This above statement I would ONLY like to happen once the signal from the manifold absolute pressure sensor reaches a certain voltage (3.12v which I have converted into the 0-1023 value, 638).
There is one other issue, the spray nozzle for this water methanol will only mist properly after 40psi so I can’t afford for the pump to come on at a low duty cycle as soon as the 3.12v from the manifold absolute pressure sensor is reached as I will not be injecting the extra fuel correctly and it will not atomise. So in my sketch you will see I have mapped the values of min and max mass air flow as well as a minimum duty cycle of 180 and a max of 255 (I believe 180-255) will give me the pressure I require.
I have tried to write this well enough so that it doesn’t confuse everyone but if I have not been totally clear please let me know as my knowledge of the correct terms to be used is not very good at the moment.
Hopefully what I have written in my sketch is correct for my theoretical idea, though I’m sure you guys will be able to point me in the direction of how best to implement my idea into a sketch and make it a reality.
Best regards and thanks in advance,
Dave!
water-methanol-sketch-done.ino (549 Bytes)
//methanol-injection
int boostPin = A0; //boost pressure input 0-5v
int mafPin = A1; //Mass air flow input 0-5v
int pump = A3; // water-methanol pump output
int maf; // Mass air flow
int pumpDC; //duty cycle of pump
int boost;
void setup () {
pinMode(pump, OUTPUT);
pinMode(boostPin, INPUT);
pinMode(mafPin, INPUT);
}
void loop () {
int val = analogRead (mafPin);
val = constrain (val, 54, 910);
int pumpDC = map(val, 54, 910, 180, 255);
int boost = analogRead(boostPin);
if (boost > 635);
analogWrite (pump, pumpDC);
}
Based on your recommendations i have edited my sketch.
- I have changed the 'A3' to just a 3, since i believe pin 3 is used for pwm output?
- Global variables you mentioned i assumed were pumpDC and boost since they were mentioned outside of the setup or loop? so i have removed them and kept them as local variables?
- Added an 'else' statement so that if the value of boost is below the desired, the pump should be low (is this the same as off? or should i change it to analogWrite (pump, 0)?
//methanol-injection
int boostPin = A0; //boost pressure input 0-5v
int mafPin = A1; //Mass air flow input 0-5v
int pump = 3; // water-methanol pump output
int maf; // Mass air flow
void setup () {
pinMode(pump, OUTPUT);
pinMode(boostPin, INPUT);
pinMode(mafPin, INPUT);
}
void loop () {
int val = analogRead (mafPin);
val = constrain (val, 54, 910);
int pumpDC = map(val, 54, 910, 180, 255);
int boost = analogRead(boostPin);
if (boost > 635)
analogWrite (pump, pumpDC);
else (boost < 635);
analogWrite (pump, LOW);
}
spycatcher2k:
Analog pins are for reading, you cannot output on A3,
No, analog pins can function as digital outputs. They just won't function as analog outputs {on most Arduino boards, not all.}
And while they also have digital pin numbers, it's most convenient to refer to them by their A_ numbers as that's what's written on the board.
Oh no! Haha I'm confused now, does it look ok now I've amended the sketch? I'll try and build the circuit now to test. Though this is my first sketch that hasn't just been a copied project from guides so I'm guessing it'll be way off working properly
else (boost < 635);
else is either unconditional or you use an if to make it conditional. You have a mixture of both and a semicolon that would prevent the code doing what you want in any case.
Could I then just change that to else if (boost <635) or is that worse. I'll try and do some more reading relating to if and else statements. Finding it quite hard to read tutorials then apply it to my specific problem. Hopefully it'll all make sense eventually.
else should be unconditional. Just
else
Not
else if
If the value isn't >635, what else could it be?
What happens if boost == 635?
//methanol-injection
int boostPin = A0; //boost pressure input 0-5v
int mafPin = A1; //Mass air flow input 0-5v
int pump = 3; // water-methanol pump output
int maf; // Mass air flow
void setup () {
pinMode(pump, OUTPUT);
pinMode(boostPin, INPUT);
pinMode(mafPin, INPUT);
}
void loop () {
int val = analogRead (mafPin);
val = constrain (val, 54, 910);
int pumpDC = map(val, 54, 910, 180, 255);
int boost = analogRead(boostPin);
if (boost >= 635){
analogWrite (pump, pumpDC);
}
else {
analogWrite(pump, LOW);
}
}
Ok so i took a look online and found a way to make sure I've dealt with above and equal to 635 and if its below i want the pump to be off. Not sure if i need to put LOW or if i should do something different?
Thanks a lot for the quick reply. I'll edit that now since that's the best way to do it. Really appreciate you guys spending time looking at my nooby code. If that looks ok now (it verifies ok also) ill build it and eventually put it into my car
One of the things I'm struggling to understand is where the curly brackets should/shouldn't be and where i need semi colon's and where I don't need them. I feel i need to understand these basics of writing code still, as eventually I'd like to do more complex projects.
thanks again everyone