0

In recent project, I have to operate my relay based on falling edge of Square wave. Here, falling edge detect by using interrupt. So, whenever Arduino detect falling edge using interrupt, ISR function called and in ISR function I perform my relay's functionality. Means whenever ISR function called, relay turn on for some milliseconds and then turn off. But whenever I connect relay, it's not operate. Here, relay connected with pin 13, so Arduino attached led blinking but not with full brightness. I don't understand why it's happened. Give me some suggestions.

Here, it's my code.

 //----------------------------For MAP calculation----------------------------//

const int kPinMAP = A0;       // MAP / Analog Pin A0

float MAP;

float MAP_MIN = 0.85;
float MAP_MAX = 1.90;

float LOAD_MIN_MAP_MIN = 2.00;
float LOAD_MAX_MAP_MIN = 15.00;

float LOAD_MIN_MAP_MAX = 10.00;
float LOAD_MAX_MAP_MAX = 85.00;

float mMAPmin;
float mMAPmax;

float yMAPmin;
float yMAPmax;

float mMAP;

float yMAP;

//---------------------------------------------------------------------------//

//----------------------------For TPS calculation----------------------------//

const int kPinTPS = A1;       // TPS / Analog Pin A1

float TPS;

float TPS_MIN = 0.00;
float TPS_MAX = 5.00;

float LOAD_MIN_TPS_MIN = 2.00;
float LOAD_MAX_TPS_MIN = 10.00;

float LOAD_MIN_TPS_MAX = 15.00;
float LOAD_MAX_TPS_MAX = 85.00;

float mTPSmin;
float mTPSmax;

float yTPSmin;
float yTPSmax;

float mTPS;

float yTPS;

//---------------------------------------------------------------------------//

//----------------------------For LOAD calculation---------------------------//

int LOAD_MIN = 2;
int LOAD_MAX = 100;

float injTime_MIN_LOAD_MIN = 4.00;
float injTime_MAX_LOAD_MIN = 4.60;

float injTime_MIN_LOAD_MAX = 8.06;
float injTime_MAX_LOAD_MAX = 14.86;

float LOAD;

float mLOADmin;
float mLOADmax;

float yLOADmin;
float yLOADmax;

float mLOAD;

float yLOAD;

//---------------------------------------------------------------------------//


//----------------------------For RPM calculation----------------------------//

const int kPinRPM = 3;        // RPM / Digital Pin 3
const int RPMInterrupt = 1;   // RPM's interrrupt pin

//--------------Pulse per revolution = 2 * cyclinders / cycles--------------//

byte engineCylinders = 2;         // Engine cylinders = 4;
byte engineCycles = 4;            // Engine cycle = 2
int refreshInterval = 750;        // Milliseconds between sensor updates
unsigned long previousMillis = 0;
volatile int RPMpulses = 0;
int RPM, Final_RPM;

int RPM_MIN = 400;
int RPM_MAX = 6000;

float injTime_MIN_RPM_MIN = 4.00;
float injTime_MAX_RPM_MIN = 8.06;

float injTime_MIN_RPM_MAX = 4.60;
float injTime_MAX_RPM_MAX = 14.86;

float mRPMmin;
float mRPMmax;

float yRPMmin;
float yRPMmax;

float mRPM;

float yRPM;

//---------------------------------------------------------------------------//

//------------------------For GAS PRESSURE calculation-----------------------//

const int kPinPressureGAS = A2;      // GAS Pressure / Analog Pin A2
float GAS_Pressure;

float minPosVolGas = 0.00;
float maxPosVolGas = 2.50;

float minInjPerPos = 0.35;
float maxInjPerPos = 0.00;

float minNagVolGas = 2.50;
float maxNagVolGas = 5.00;

float minInjPerNag = 0.00;
float maxInjPerNag = 0.25;

float mGasPos, mGasNag;
float yGasPos, yGasNag;

//---------------------------------------------------------------------------//

//----------------------For INJECTION TIME calculation-----------------------//

float yInjTime, injTime, gasInjTime;
const int kPinInj = 13;
int temp = 0;

//---------------------------------------------------------------------------//

void setup() 
{
  Serial.begin(9600);

  pinMode(kPinRPM, INPUT_PULLUP); // Enable internal pullup
  attachInterrupt(RPMInterrupt, countRPM, FALLING); //Interrupt for RPM

  pinMode(kPinInj, OUTPUT);
  //digitalWrite(kPinInj, HIGH);
}

void loop() 
{
  //----------------------------MAP----------------------------//

  MAP = analogRead(A0) * (5.0 / 1023.0);

  mMAPmin = (LOAD_MAX_MAP_MIN - LOAD_MIN_MAP_MIN) / (MAP_MAX - MAP_MIN);

  //Serial.print("mMAPmin : ");
  //Serial.println(mMAPmin);

  yMAPmin = mMAPmin * (MAP - MAP_MIN) + LOAD_MIN_MAP_MIN;

  //Serial.print("MAP : ");
  //Serial.print(MAP);
  //Serial.print("\t");
  //Serial.print("LOAD : ");
  //Serial.println(yMAPmin);

  mMAPmax = (LOAD_MAX_MAP_MAX - LOAD_MIN_MAP_MAX) / (MAP_MAX - MAP_MIN);

  //Serial.print("mMAPmax : ");
  //Serial.println(mMAPmax);

  yMAPmax = mMAPmax * (MAP - MAP_MIN) + LOAD_MIN_MAP_MAX;

  //Serial.print("MAP : ");
  //Serial.print(MAP);
  //Serial.print("\t");
  //Serial.print("LOAD : ");
  //Serial.println(yMAPmax);

  //-----------------------------------------------------------//

  //----------------------------TPS----------------------------//

  TPS = analogRead(A1) * (5.0 / 1023.0);

  mTPSmin = (LOAD_MAX_TPS_MIN - LOAD_MIN_TPS_MIN) / (TPS_MAX - TPS_MIN);

  //Serial.print("mTPSmin : ");
  //Serial.println(mTPSmin);

  yTPSmin = mTPSmin * (TPS - TPS_MIN) + LOAD_MIN_TPS_MIN;

  //Serial.print("TPS : ");
  //Serial.print(TPS);
  //Serial.print("\t");
  //Serial.print("LOAD : ");
  //Serial.println(yTPSmin);

  mTPSmax = (LOAD_MAX_TPS_MAX - LOAD_MIN_TPS_MAX) / (TPS_MAX - TPS_MIN);

  //Serial.print("mTPSmax : ");
  //Serial.println(mTPSmax);

  yTPSmax = mTPSmax * (TPS - TPS_MIN) + LOAD_MIN_TPS_MAX;

  //Serial.print("TPS : ");
  //Serial.print(TPS);
  //Serial.print("\t");
  //Serial.print("LOAD : ");
  //Serial.println(yTPSmax);

  //-----------------------------------------------------------//

  //----------------TPS/MAP CALLIBRATION / LOAD----------------//

  mTPS = (yMAPmax - yMAPmin) / (TPS_MAX - TPS_MIN);

  //Serial.print("mTPS : ");
  //Serial.println(mTPS);

  yTPS = mTPS * (TPS - TPS_MIN) + yMAPmin;

  Serial.print("TPS : ");
  Serial.print(TPS);
  Serial.print("\t");
  //Serial.print("LOAD : ");
  //Serial.print(yTPS);
  //Serial.print("\t");

  mMAP = (yTPSmax - yTPSmin) / (MAP_MAX - MAP_MIN);

  //Serial.print("mMAP : ");
  //Serial.println(mMAP);

  yMAP = mMAP * (MAP - MAP_MIN) + yTPSmin;

  Serial.print("MAP : ");
  Serial.print(MAP);
  Serial.print("\t");
  //Serial.print("LOAD : ");
  //Serial.print(yMAP);
  //Serial.print("\t");

  LOAD = (yTPS + yMAP) / 2;
  Serial.print("Final LOAD : ");
  Serial.print(LOAD);
  Serial.print("\t");

  //-----------------------------------------------------------//

  //----------------------------LOAD---------------------------//

  mLOADmin = (injTime_MAX_LOAD_MIN - injTime_MIN_LOAD_MIN) / (LOAD_MAX - LOAD_MIN);

  //Serial.print("mLOADmin : ");
  //Serial.println(mLOADmin);

  yLOADmin = mLOADmin * (LOAD - LOAD_MIN) + injTime_MIN_LOAD_MIN;

  //Serial.print("LOAD : ");
  //Serial.print(LOAD);
  //Serial.print("\t");
  //Serial.print("injTime : ");
  //Serial.println(yLOADmin);

  mLOADmax = (injTime_MAX_LOAD_MAX - injTime_MIN_LOAD_MAX) / (LOAD_MAX - LOAD_MIN);

  //Serial.print("mLOADmax : ");
  //Serial.println(mLOADmax);

  yLOADmax = mLOADmax * (LOAD - LOAD_MIN) + injTime_MIN_LOAD_MAX;

  //Serial.print("LOAD : ");
  //Serial.print(LOAD);
  //Serial.print("\t");
  //Serial.print("injTime : ");
  //Serial.println(yLOADmax);

  //-----------------------------------------------------------//

  //----------------------------RPM----------------------------//

  if(millis() - previousMillis > refreshInterval)
  {
    previousMillis = millis();
    RPM = getRPM();
  }

  mRPMmin = (injTime_MAX_RPM_MIN - injTime_MIN_RPM_MIN) / (RPM_MAX - RPM_MIN);

  //Serial.print("mRPMmin : ");
  //Serial.println(mRPMmin);

  yRPMmin = mRPMmin * (RPM - RPM_MIN) + injTime_MIN_RPM_MIN;

  //Serial.print("RPM : ");
  //Serial.print(RPM);
  //Serial.print("\t");
  //Serial.print("injTime : ");
  //Serial.println(yRPMmin);

  mRPMmax = (injTime_MAX_RPM_MAX - injTime_MIN_RPM_MAX) / (RPM_MAX - RPM_MIN);

  //Serial.print("mRPMmax : ");
  //Serial.println(mRPMmax);

  yRPMmax = mRPMmax * (RPM - RPM_MIN) + injTime_MIN_RPM_MAX;

  //Serial.print("RPM : ");
  //Serial.print(RPM);
  //Serial.print("\t");
  //Serial.print("injTime : ");
  //Serial.println(yRPMmax);

  //-----------------------------------------------------------//

  //-----------LOAD/RPM CALLIBRATION / INJECTION TIME----------//

  mLOAD = (yRPMmax - yRPMmin) / (LOAD_MAX - LOAD_MIN);

  //Serial.print("mLOAD : ");
  //Serial.println(mLOAD);

  yLOAD = mLOAD * (LOAD - LOAD_MIN) + yRPMmin;

  Serial.print("LOAD : ");
  Serial.print(LOAD);
  Serial.print("\t");
  //Serial.print("injTime : ");
  //Serial.print(yLOAD);
  //Serial.print("\t");

  mRPM = (yLOADmax - yLOADmin) / (RPM_MAX - RPM_MIN);

  //Serial.print("mRPM : ");
  //Serial.println(mRPM);

  yRPM = mRPM * (RPM - RPM_MIN) + yLOADmin;

  Serial.print("RPM : ");
  Serial.print(RPM);
  Serial.print("\t");
  //Serial.print("injTime : ");
  //Serial.print(yRPM);
  //Serial.print("\t");

  yInjTime = (yLOAD + yRPM) / 2;
  Serial.print("Final injTime : ");
  Serial.print(yInjTime);
  Serial.print("\t");

  injTime = (yInjTime * 1000) / 1000;
  Serial.print("Injection Time : ");
  Serial.print(injTime);
  Serial.print("\t");

  //-----------------------------------------------------------//

  //------------------------GAS_PRESSURE-----------------------//

  GAS_Pressure =  analogRead(A2) * (5.0 / 1023.0);
  Serial.print("GAS Pressure : ");
  Serial.print(GAS_Pressure);
  Serial.print("\t");

  mGasPos = (maxInjPerPos - minInjPerPos) / (maxPosVolGas - minPosVolGas);

  yGasPos = mGasPos * (GAS_Pressure - minPosVolGas) + minInjPerPos;
  //Serial.print("InjTimeY1 : ");
  //Serial.print(yGasPos);
  //Serial.print("\t");

  mGasNag = (maxInjPerNag - minInjPerNag) / (maxNagVolGas - minNagVolGas);

  yGasNag = mGasNag * (GAS_Pressure - minNagVolGas) + minInjPerNag;
  //Serial.print("InjTimeY2 : ");
  //Serial.print(yGasNag);
  //Serial.print("\t");

  if(GAS_Pressure >= 0.00 && GAS_Pressure < 2.50)
  {
    gasInjTime = injTime + (injTime * yGasPos);
    Serial.print("GASinjTimePos : ");
    Serial.print(gasInjTime);
    Serial.print("\n");
  }

  if(GAS_Pressure >= 2.50 && GAS_Pressure <= 5.00)
  {
    gasInjTime = injTime - (injTime * yGasNag);
    Serial.print("GASinjTimeNag : ");
    Serial.print(gasInjTime);
    Serial.print("\n");
  }
  //-----------------------------------------------------------//



}

//----------------------------RPM----------------------------//

void countRPM()
{
  RPMpulses++;

  digitalWrite(kPinInj, HIGH);
  delay(gasInjTime);
  digitalWrite(kPinInj, LOW);
}

int getRPM()
{
  Final_RPM = int(RPMpulses * (60000.0 / float(refreshInterval)) * engineCycles / engineCylinders / 2.0);
  RPMpulses = 0;
  Final_RPM = min(99999, Final_RPM);
  return Final_RPM;
}
//-----------------------------------------------------------//

After suggestion of Majenko and some modification(done by me) my updated code working fine. My relay operate very well. My OUTPUT Pin 13 Square Wave synchronized with INPUT Pin 2 Square Wave Here is my updated code :

//----------------------------For MAP calculation----------------------------//
float MAP_MIN = 0.85;
float MAP_MAX = 1.90;

int LOAD_MIN_MAP[] = {2, 10};
int LOAD_MAX_MAP[] = {15, 85};

float mMAP[2] = {};
float yMAP[2] = {};

int i, j, a;
float MAP, mFinalMAP, yFinalMAP;

//----------------------------For TPS calculation----------------------------//
float TPS_MIN = 0.00;
float TPS_MAX = 5.00;

int LOAD_MIN_TPS[] = {2, 15};
int LOAD_MAX_TPS[] = {10, 85};

float mTPS[2] = {};
float yTPS[2] = {};

int k, l, b;
float TPS, mFinalTPS, yFinalTPS;

//----------------------------For LOAD calculation---------------------------//
int LOAD_MIN = 2;
int LOAD_MAX = 100;

float injTime_MIN_LOAD[] = {4.00, 8.06};
float injTime_MAX_LOAD[] = {4.60, 14.86};

float mLOAD[2] = {};
float yLOAD[2] = {};

int m, n, c;
float LOAD, mFinalLOAD, yFinalLOAD;

//----------------------------For RPM calculation----------------------------//
const int kPinRPM = 3;        // RPM / Digital Pin 3
const int RPMInterrupt = 1;   // RPM's interrrupt pin
byte engineCylinders = 2;         // Engine cylinders = 4;
byte engineCycles = 4;            // Engine cycle = 2
int refreshInterval = 750;        // Milliseconds between sensor updates
unsigned long previousMillis = 0;
volatile int RPMpulses = 0;
int RPM, Final_RPM;

int RPM_MIN = 400;
int RPM_MAX = 6000;

float injTime_MIN_RPM[] = {4.00, 4.60};
float injTime_MAX_RPM[] = {8.06, 14.86};

float mRPM[2] = {};
float yRPM[2] = {};

int o, p, d;
float mFinalRPM, yFinalRPM;

//------------------------For GAS PRESSURE calculation-----------------------//
float minPosVolGas = 0.00;
float maxPosVolGas = 2.50;

float minInjPerPos = 0.35;
float maxInjPerPos = 0.00;

float minNagVolGas = 2.50;
float maxNagVolGas = 5.00;

float minInjPerNag = 0.00;
float maxInjPerNag = 0.25;

float GAS_Pressure, mGasPos, mGasNag, yGasPos, yGasNag;

//----------------------For INJECTION TIME calculation-----------------------//
float yInjTime, injTime, gasInjTime;
volatile uint32_t kInjTime = 0;

const int kPinInj = 13;

void setup() 
{
  Serial.begin(9600);

  pinMode(kPinRPM, INPUT_PULLUP); // Enable internal pullup
  attachInterrupt(RPMInterrupt, countRPM, FALLING); //Interrupt for RPM

  pinMode(kPinInj, OUTPUT);
}

void loop() 
{
  //----------------------------MAP----------------------------//

  MAP = analogRead(A0) * (5.0 / 1023.0);

  for(a = 0; a < 2; a++)
  {
    mMAP[i] = (LOAD_MIN_MAP[a] - LOAD_MIN_MAP[a]) / (MAP_MAX - MAP_MIN);
    yMAP[j] = mMAP[i] * (MAP - MAP_MIN) + LOAD_MIN_MAP[a];
  }

  //----------------------------TPS----------------------------//

  TPS = analogRead(A1) * (5.0 / 1023.0);

  for(b = 0; b < 2; b++)
  {
    mTPS[k] = (LOAD_MAX_TPS[b] - LOAD_MIN_TPS[b]) / (TPS_MAX - TPS_MIN);
    yTPS[l] = mTPS[k] * (TPS - TPS_MIN) + LOAD_MIN_TPS[b];
  }

  //----------------TPS/MAP CALLIBRATION / LOAD----------------//

  mFinalTPS = (yMAP[1] - yMAP[0]) / (TPS_MAX - TPS_MIN);
  yFinalTPS = mFinalTPS * (TPS - TPS_MIN) + yMAP[0];

  mFinalMAP = (yTPS[1] - yTPS[0]) / (MAP_MAX - MAP_MIN);
  yFinalMAP = mFinalMAP * (MAP - MAP_MIN) + yTPS[0];

  LOAD = (yFinalTPS + yFinalMAP) / 2;

  //----------------------------LOAD---------------------------//

  for(c = 0; c < 2; c++)
  {
    mLOAD[m] = (injTime_MAX_LOAD[c] - injTime_MIN_LOAD[c]) / (LOAD_MAX - LOAD_MIN);
    yLOAD[n] = mLOAD[m] * (LOAD - LOAD_MIN) + injTime_MIN_LOAD[c];
  }

  //----------------------------RPM----------------------------//

  if(millis() - previousMillis > refreshInterval)
  {
    previousMillis = millis();
    RPM = getRPM();
  }

  for(d = 0; d < 2; d++)
  {
    mRPM[o] = (injTime_MAX_RPM[d] - injTime_MIN_RPM[d]) / (RPM_MAX - RPM_MIN);
    yRPM[p] = mRPM[o] * (RPM - RPM_MIN) + injTime_MIN_RPM[d];
  }

  //-----------LOAD/RPM CALLIBRATION / INJECTION TIME----------//

  mFinalLOAD = (yRPM[1] - yRPM[0]) / (LOAD_MAX - LOAD_MIN);
  yFinalLOAD = mFinalLOAD * (LOAD - LOAD_MIN) + yRPM[0];

  mFinalRPM = (yLOAD[1] - yLOAD[0]) / (RPM_MAX - RPM_MIN);
  yFinalRPM = mFinalRPM * (RPM - RPM_MIN) + yLOAD[0];

  yInjTime = (yFinalLOAD + yFinalRPM) / 2;
  injTime = (yInjTime * 1000) / 1000;

  //------------------------GAS_PRESSURE-----------------------//

  GAS_Pressure =  analogRead(A2) * (5.0 / 1023.0);

  mGasPos = (maxInjPerPos - minInjPerPos) / (maxPosVolGas - minPosVolGas);
  yGasPos = mGasPos * (GAS_Pressure - minPosVolGas) + minInjPerPos;

  mGasNag = (maxInjPerNag - minInjPerNag) / (maxNagVolGas - minNagVolGas);
  yGasNag = mGasNag * (GAS_Pressure - minNagVolGas) + minInjPerNag;

  if(GAS_Pressure >= 0.00 && GAS_Pressure < 2.50)
  {
    gasInjTime = injTime + (injTime * yGasPos);
  }
  else if(GAS_Pressure >= 2.50 && GAS_Pressure <= 5.00)
  {
    gasInjTime = injTime - (injTime * yGasNag);
  }

  //--------------------Pulse Generation-----------------------//

  if((kInjTime > 0) && ((millis() - kInjTime) >= gasInjTime))
  {
    kInjTime = 0;
    digitalWrite(kPinInj, LOW);
  }
}

//----------------------------RPM----------------------------//

void countRPM()
{
  RPMpulses++;

  kInjTime = millis();
  digitalWrite(kPinInj, HIGH);
}

int getRPM()
{
  Final_RPM = int(RPMpulses * (60000.0 / float(refreshInterval)) * engineCycles / engineCylinders / 2.0);
  RPMpulses = 0;
  Final_RPM = min(99999, Final_RPM);
  return Final_RPM;
}
//-----------------------------------------------------------//

Here, it's my oscilloscope output after suggestion of user Majenko. Purple one is INPUT Pin 2 Square wave and Yellow one is OUTPUT Pin 13 Square wave.It's perfectly synchronized. Synchronized Waves

Now, I want to see my all readings on serial monitor, so for that, I used Serial.print() line. But, now one problem is appeared. Because of this line, my OUTPUT Pin 13 Waveform not synchronized with INPUT Pin 2 Waveform:

Below my code after adding Serial.print() in my code to display readings on serial monitor. You can see bunch of lines of Serial.print() at the end of the code :

//----------------------------For MAP calculation----------------------------//
float MAP_MIN = 0.85;
float MAP_MAX = 1.90;

int LOAD_MIN_MAP[] = {2, 10};
int LOAD_MAX_MAP[] = {15, 85};

float mMAP[2] = {};
float yMAP[2] = {};

int i, j, a;
float MAP, mFinalMAP, yFinalMAP;

//----------------------------For TPS calculation----------------------------//
float TPS_MIN = 0.00;
float TPS_MAX = 5.00;

int LOAD_MIN_TPS[] = {2, 15};
int LOAD_MAX_TPS[] = {10, 85};

float mTPS[2] = {};
float yTPS[2] = {};

int k, l, b;
float TPS, mFinalTPS, yFinalTPS;

//----------------------------For LOAD calculation---------------------------//
int LOAD_MIN = 2;
int LOAD_MAX = 100;

float injTime_MIN_LOAD[] = {4.00, 8.06};
float injTime_MAX_LOAD[] = {4.60, 14.86};

float mLOAD[2] = {};
float yLOAD[2] = {};

int m, n, c;
float LOAD, mFinalLOAD, yFinalLOAD;

//----------------------------For RPM calculation----------------------------//
const int kPinRPM = 3;        // RPM / Digital Pin 3
const int RPMInterrupt = 1;   // RPM's interrrupt pin
byte engineCylinders = 2;         // Engine cylinders = 4;
byte engineCycles = 4;            // Engine cycle = 2
int refreshInterval = 750;        // Milliseconds between sensor updates
unsigned long previousMillis = 0;
volatile int RPMpulses = 0;
int RPM, Final_RPM;

int RPM_MIN = 400;
int RPM_MAX = 6000;

float injTime_MIN_RPM[] = {4.00, 4.60};
float injTime_MAX_RPM[] = {8.06, 14.86};

float mRPM[2] = {};
float yRPM[2] = {};

int o, p, d;
float mFinalRPM, yFinalRPM;

//------------------------For GAS PRESSURE calculation-----------------------//
float minPosVolGas = 0.00;
float maxPosVolGas = 2.50;

float minInjPerPos = 0.35;
float maxInjPerPos = 0.00;

float minNagVolGas = 2.50;
float maxNagVolGas = 5.00;

float minInjPerNag = 0.00;
float maxInjPerNag = 0.25;

float GAS_Pressure, mGasPos, mGasNag, yGasPos, yGasNag;

//----------------------For INJECTION TIME calculation-----------------------//
float yInjTime, injTime, gasInjTime;
volatile uint32_t kInjTime = 0;

const int kPinInj = 13;

void setup() 
{
  Serial.begin(9600);

  pinMode(kPinRPM, INPUT_PULLUP); // Enable internal pullup
  attachInterrupt(RPMInterrupt, countRPM, FALLING); //Interrupt for RPM

  pinMode(kPinInj, OUTPUT);
}

void loop() 
{
  //----------------------------MAP----------------------------//

  MAP = analogRead(A0) * (5.0 / 1023.0);

  for(a = 0; a < 2; a++)
  {
    mMAP[i] = (LOAD_MIN_MAP[a] - LOAD_MIN_MAP[a]) / (MAP_MAX - MAP_MIN);
    yMAP[j] = mMAP[i] * (MAP - MAP_MIN) + LOAD_MIN_MAP[a];
  }

  //----------------------------TPS----------------------------//

  TPS = analogRead(A1) * (5.0 / 1023.0);

  for(b = 0; b < 2; b++)
  {
    mTPS[k] = (LOAD_MAX_TPS[b] - LOAD_MIN_TPS[b]) / (TPS_MAX - TPS_MIN);
    yTPS[l] = mTPS[k] * (TPS - TPS_MIN) + LOAD_MIN_TPS[b];
  }

  //----------------TPS/MAP CALLIBRATION / LOAD----------------//

  mFinalTPS = (yMAP[1] - yMAP[0]) / (TPS_MAX - TPS_MIN);
  yFinalTPS = mFinalTPS * (TPS - TPS_MIN) + yMAP[0];

  mFinalMAP = (yTPS[1] - yTPS[0]) / (MAP_MAX - MAP_MIN);
  yFinalMAP = mFinalMAP * (MAP - MAP_MIN) + yTPS[0];

  LOAD = (yFinalTPS + yFinalMAP) / 2;



  //----------------------------LOAD---------------------------//

  for(c = 0; c < 2; c++)
  {
    mLOAD[m] = (injTime_MAX_LOAD[c] - injTime_MIN_LOAD[c]) / (LOAD_MAX - LOAD_MIN);
    yLOAD[n] = mLOAD[m] * (LOAD - LOAD_MIN) + injTime_MIN_LOAD[c];
  }

  //----------------------------RPM----------------------------//

  if(millis() - previousMillis > refreshInterval)
  {
    previousMillis = millis();
    RPM = getRPM();
  }

  for(d = 0; d < 2; d++)
  {
    mRPM[o] = (injTime_MAX_RPM[d] - injTime_MIN_RPM[d]) / (RPM_MAX - RPM_MIN);
    yRPM[p] = mRPM[o] * (RPM - RPM_MIN) + injTime_MIN_RPM[d];
  }

  //-----------LOAD/RPM CALLIBRATION / INJECTION TIME----------//

  mFinalLOAD = (yRPM[1] - yRPM[0]) / (LOAD_MAX - LOAD_MIN);
  yFinalLOAD = mFinalLOAD * (LOAD - LOAD_MIN) + yRPM[0];

  mFinalRPM = (yLOAD[1] - yLOAD[0]) / (RPM_MAX - RPM_MIN);
  yFinalRPM = mFinalRPM * (RPM - RPM_MIN) + yLOAD[0];

  yInjTime = (yFinalLOAD + yFinalRPM) / 2;
  injTime = (yInjTime * 1000) / 1000;

  //------------------------GAS_PRESSURE-----------------------//

  GAS_Pressure =  analogRead(A2) * (5.0 / 1023.0);

  mGasPos = (maxInjPerPos - minInjPerPos) / (maxPosVolGas - minPosVolGas);
  yGasPos = mGasPos * (GAS_Pressure - minPosVolGas) + minInjPerPos;

  mGasNag = (maxInjPerNag - minInjPerNag) / (maxNagVolGas - minNagVolGas);
  yGasNag = mGasNag * (GAS_Pressure - minNagVolGas) + minInjPerNag;

  if(GAS_Pressure >= 0.00 && GAS_Pressure < 2.50)
  {
    gasInjTime = injTime + (injTime * yGasPos);
  }
  else if(GAS_Pressure >= 2.50 && GAS_Pressure <= 5.00)
  {
    gasInjTime = injTime - (injTime * yGasNag);
  }

  //--------------------Pulse Generation-----------------------//

  if((kInjTime > 0) && ((millis() - kInjTime) >= gasInjTime))
  {
    kInjTime = 0;
    digitalWrite(kPinInj, LOW);
  }

  Serial.print("TPS : ");
  Serial.print(TPS);
  Serial.print("\t");
  Serial.print("MAP : ");
  Serial.print(MAP);
  Serial.print("\t");
  Serial.print("LOAD : ");
  Serial.print(LOAD);
  Serial.print("\t");
  Serial.print("RPM : ");
  Serial.print(RPM);
  Serial.print("\t");
  Serial.print("GAS Pressure : ");
  Serial.print(GAS_Pressure);
  Serial.print("\t");
  Serial.print("Injection Time : ");
  Serial.print(gasInjTime);
  Serial.print("\n");
}

//----------------------------RPM----------------------------//

void countRPM()
{
  RPMpulses++;

  kInjTime = millis();
  digitalWrite(kPinInj, HIGH);
}

int getRPM()
{
  Final_RPM = int(RPMpulses * (60000.0 / float(refreshInterval)) * engineCycles / engineCylinders / 2.0);
  RPMpulses = 0;
  Final_RPM = min(99999, Final_RPM);
  return Final_RPM;
}
//-----------------------------------------------------------//

My oscilloscope output. You can see no synchronization between that waveform. I don't know why this happens. Can I have to switch to ARDUINO MEGA because may be ARDUINO UNO processing speed not able to perform. Give some suggestion : enter image description here

  • Comments are not for extended discussion; this conversation has been moved to chat. – Nick Gammon Feb 16 '17 at 5:24
  • Finally, problem solved. Just keep maximum baud rate. – Hasan Feb 17 '17 at 9:12
3

Here is how I would do it:

volatile uint32_t injTime = 0;

void setup() {
    pinMode(13, OUTPUT);
    pinMode(2, INPUT);
    attachInterrupt(0, countRPM, FALLING);
}

void loop() {
    if ((injTime > 0) && ((millis() - injTime) >= 10)) {
        injTime = 0;
        digitalWrite(13, LOW);
    }
}

void countRPM() {
    injTime = millis();
    digitalWrite(13, HIGH);
}

Basically, when the interrupt triggers you remember when it triggered and turn on the relay.

Then in your main loop, if the interrupt has been triggered, and 10 milliseconds have elapsed since the interrupt was triggered, then forget that the interrupt was triggered (so you can see the next one) then turn the relay off.

The key point is that you remember when the interrupt triggered, and count your time from that point onwards.

enter image description here

Blue is input to pin 2. Yellow is output of pin 13.

  • Hey, again thanks. Your code operate my relay but synchronization is problem there. Not operate relay based on falling edge of square wave. – Hasan Feb 14 '17 at 9:44
  • You do know that relays are really really really slow to switch don't you? – Majenko Feb 14 '17 at 9:45
  • I don't know that things. – Hasan Feb 14 '17 at 9:46
  • 1
    It sounds like for your project you want some form of solid state switching device, but what that device should be depends entirely on what it is you are actually controlling and what power / current / voltage it needs. – Majenko Feb 14 '17 at 10:14
  • With your oscilloscope, compare the output of pin 13 (not the relay switching) with the square wave. You'll see how the relay is giving you false results. – Majenko Feb 14 '17 at 10:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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