Hello boys, thanks for thinking with me.
If using a loop it opens another possibility to me, a rotary encoder for timing: http://nl.rs-online.com/web/p/rotary-encoders/2603780/?searchTerm=260-3780&relevancy-data=636F3D3126696E3D4931384E525353746F636B4E756D6265724D504E266C753D656E266D6D3D6D61746368616C6C26706D3D5E5C647B337D5B5C732D2F255C2E5D5C647B332C347D2426706F3D313426736E3D592673743D52535F53544F434B5F4E554D424552267573743D3236302D333738302677633D4E4F4E4526 (no clue how to hock it up)
int posPin = A1; // the input pin for position sensor
int posValue = 0; // variable to store the value from the sensor
int injectorpin1 = 13; // the pin for the injector solenoid1
int injectorpin2 = 14; // the pin for the injector solenoid2
int injectorpin3 = 15; // the pin for the injector solenoid3
int injectorpin4 = 16; // the pin for the injector solenoid4
int sensorPin = A0; // the input pin for the throtttle potentiometer
int sensorValue = 0; // variable to store the value coming from the sensor
long previousMillis = 0; // will store last time injection time was updated
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 1000; // interval at which to fire injectors(milliseconds)
void setup() {
// declare the injector pins as an OUTPUT:
pinMode(injectorpin1, OUTPUT);
pinMode(injectorpin2, OUTPUT);
pinMode(injectorpin3, OUTPUT);
pinMode(injectorpin4, OUTPUT);
}
void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
interval = sensorValue;////////////////////////////////////add mapping here or formula
unsigned long currentMillis = millis(); //update currentMillis
posValue = analogRead(posPin);
if(posPin == 0) // of camshaft position is 0 degrees then fire cylinder 1
{
// turn the injectorpin on
digitalWrite(injectorpin1, HIGH);
previousMillis = currentMillis; //note the time of firing the injector
}
if (posPin != 0 && (currentMillis - previousMillis) > interval) //if pos is not 0 degrees and injector has been on long enough, turn off injector
{
// turn the injectorpin off:
digitalWrite(injectorpin1, LOW);
}
if(posPin == 256) // of camshaft position is 90 degrees then fire cylinder 3
{
// turn the injectorpin on
digitalWrite(injectorpin3, HIGH);
previousMillis = currentMillis; //note the time of firing the injector
}
if (posPin != 256 && (currentMillis - previousMillis) > interval) //if pos is not 90 degrees and injector has been on long enough, turn off injector
{
// turn the injectorpin off:
digitalWrite(injectorpin3, LOW);
}
if(posPin == 512) // of camshaft position is 180 degrees then fire cylinder 4
{
// turn the injectorpin on
digitalWrite(injectorpin4, HIGH);
previousMillis = currentMillis; //note the time of firing the injector
}
if (posPin != 512 && (currentMillis - previousMillis) > interval) //if pos is not 180 degrees and injector has been on long enough, turn off injector
{
// turn the injectorpin off:
digitalWrite(injectorpin4, LOW);
}
if(posPin == 768) // of camshaft position is 270 degrees then fire cylinder 2
{
// turn the injectorpin on
digitalWrite(injectorpin2, HIGH);
previousMillis = currentMillis; //note the time of firing the injector
}
if (posPin != 768 && (currentMillis - previousMillis) > interval) //if pos is not 270 degrees and injector has been on long enough, turn off injector
{
// turn the injectorpin off:
digitalWrite(injectorpin2, LOW);
}
}
Is this the proper way to handle things?
And will this be possible on a nano?
p.s. the (currentMillis - previousMillis) > interval
I don't understand.
In bash to would mean "if $currentTime minus $previousTime is larger then Interval (throttle position) stop injecting"???