For Next Loop Trigger Brain Teaser

Hi
This is not a problem that I can not work around so only look at it if you want to
But it will bug me to know what i am doing wrong.
I call a sub routine 4 times with a value "InjOffVal" and the subroutine keeps count with "c" the total of" InjOffVal" is 14010
if I set the trigger to " if (c>=14001){ }" it works ok. if I set the trigger to if (c==14006){ } it works
But it will not work with "if (c==14001){ }" or a few other values that I have tried.
Tell me what I am Missing

void getResponse(void){
 for (int a = 1; a<(InjOffVal); a++) {
  if (Serial1.available()) {
    b=b+1;
    char inOBD = Serial1.read();
    if ((b>8)&&(b<18)){
    Torq[b-7]= inOBD;//Serial1.read();  
    } 
  }
  c=c+1;
  }

// Works as below But not with If(c==14001){   but does work with if(c==14006){

  if (c>=14001){
   AHex1=strtol(&Torq[3],0,16);//Good for 1
   AHex2=strtol(&Torq[5],0,16);
  AHex2I=AHex2;
  AHex2IT=(AHex2I-40);
   AHex3=strtol(&Torq[8],0,16);
  AHex4=(((AHex2*256)+(AHex3))/4);
  AHex4I=AHex4;
  AHex5=((AHex2*100)/255);
  AHex5I=AHex5;
 if (Mon==2) {
   String MonRead(Torq);
   Serial.println(MonRead);
 }
 b=0;
 c=0;
  } 
}

The "Mon==2" is for debugging if I set it not to debug with Serial. not started I get the same results.

Thanks for looking if your took the time

The answer is:


Now, I suppose that you're think that it would have been better had I actually posted all of the answer in the box. Well, it would have been better if you'd posted all of your code, so we could actually see what you are doing. That snippet does not allow us to reproduce your problem. Post ALL of your code.

Hi PaulS
Full code as it is is a bit long and messy I am not a coder but copy someone's and make it work VB Java QT4 and now C++

/*
  Thanks for Bits of copy Sketch From
  Mega multple serial test
  created 30 Dec. 2008
  modified 20 May 2012
  by Tom Igoe & Jed Roach
* OBD-II-UART Quickstart Sketch
* Written by Ryan Owens for SparkFun Electronics * Released under the 'beer-me' license
* 7/5/2011 if we ever meet then you buy me a beer
*/
#include <Wire.h>  
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);


int a = 0;
int b = 0;
int c = 0;
int n = 0;
int m = 0;

int intmillis = 0;
unsigned long lastmillis = 0;
unsigned long PIDData = 0;

char inOBD=0;
unsigned long AHex;
unsigned long AHex1;
unsigned long AHex2;
int AHex2I =0;         /// Speed-Map
int AHex2IT= 0;        ///Temp
unsigned long AHex3;
unsigned long AHex4;
int AHex4I=0;          ///RPM
int RPM=0;
int KPH=0;
int InjTorq=0;
int MAP=0;
unsigned long AHex5;
int AHex5I=0;          ///Torque
char Torq[ ]=("EEEEEEEEEE");
char TorqT[ ]="vvvvvvvvvvvvvvvvvvvvvvvvvvvvvv";
int PidRead=1;

int InjOnVal=100;
int LastInjVal=0;
int LcdT=0;
unsigned long InjTot=0;
int MinInjOnVal = 2800;
int InjOffVal=5000;//set by A,B,C Below
int InjOffValA=4300;//1st responce val
int InjOffValB=4300;//2nd responce val
int InjOffValC=1110;//3rd responce val


int sensorValue0 = analogRead(A0);//TempReducing-560=11deg Road running 128
int sensorValue2 = analogRead(A2);//MapIncreasing-300to330=idel Max700
int INJ = 5;
int VSol = 7;
String MonRead;
int Mon=2;//???set as 1,2,3 to Monitor 6 No serial display

void setup() {
 Serial1.begin(38400);//OBD-CanBus
 delay(1000);
 Serial1.println ("ATZ");
 pinMode(INJ, OUTPUT);
 pinMode(VSol, OUTPUT);
 digitalWrite(VSol, LOW);
 digitalWrite(INJ, LOW);
  // initialize both serial ports:
  if (Mon != 6) {Serial.begin(9600); }//LapTop Read
  delay(1000);
  lcd.begin(16,2);
  lcd.backlight();
  lcd.setCursor(0,0);
  lcd.print("YO, UniversE!");
  while (Serial1.read() >=0);
  Serial1.println("ATTP0");
  //delay(1000);
  //Serial1.flush();
  while (Serial1.read() >=0);
  //Serial1.println ("010C");
  //delay(1000);
  //while (Serial1.read() >=0);
  delay(1000);
  lcd.setCursor(0,0);
  lcd.print("               ");
  lcd.setCursor(5,0);
  lcd.print("RPM");//RPM
  lcd.setCursor(5,1);
  lcd.print("TOQ");//Torque
 getConected();
 delay(100);
 Serial1.end();
 delay(500);
 Serial1.begin(38400);//OBD-CanBus
 delay(3000);
 getConected();
 delay(3000);
}

void loop() {
  InjOnVal=0;
  InjTot=0;
  //RPM-Torque Values decide InjVal MicroSeconds
/*  
if (sensorValue0<=300){            //Vap Temp
  digitalWrite(VSol, HIGH);
                      }
  else {
  digitalWrite(VSol, LOW);
                     }
*/
  InjOnVal=((InjTorq*InjTorq)*2.2);
  InjOnVal=InjOnVal+(RPM-500);
 if (InjTorq>=60) { InjOnVal=10000-(InjOnVal-8000);}
 if (InjTorq>=80) { InjOnVal=MinInjOnVal; }
 if (InjTorq<=20) { InjOnVal=0;}
 if (RPM<=1200) { InjOnVal=0; }
 if (RPM>=4000) { InjOnVal=0; }
 
 
 if (InjOnVal<0) { InjOnVal=0; }
 if (InjOnVal>(LastInjVal+250)) {InjOnVal=LastInjVal+(RPM*.1);}
 if (InjOnVal>10000) { InjOnVal=10000; }
 LastInjVal=InjOnVal;
 LcdT=InjOnVal*.1;
 c=0;
 //if(Mon==1) {Serial.print("  ");}
            
 if(PidRead==5) { PidRead=1;}       
 if(PidRead==1) { Serial1.println("010C"); }  //RPM-PID
 if(PidRead==2) { Serial1.println("010D"); }  //Speed-PID
 if(PidRead==3) { Serial1.println("0105"); }  //Temp-PID
 if(PidRead==4) { Serial1.println("010B"); }  //Map-PID
 
 if (InjOnVal>=MinInjOnVal) {
   digitalWrite(INJ, HIGH);
                   }//inj1
 delayMicroseconds(InjOnVal);
 digitalWrite(INJ, LOW);
 InjOffVal=InjOffValA;
 getResponse();
 if(Mon==1) {Serial.println(millis());}
 
 if (InjOnVal>=MinInjOnVal) {
   digitalWrite(INJ, HIGH);
                   }//inj2
 delayMicroseconds(InjOnVal);
 digitalWrite(INJ, LOW);
 InjOffVal=InjOffValB;
 getResponse();
 if(Mon==1) {Serial.println(millis());}
 
 if (InjOnVal>=MinInjOnVal) {
   digitalWrite(INJ, HIGH);
                   }//inj3
 delayMicroseconds(InjOnVal);
 digitalWrite(INJ, LOW);
 InjOffVal=InjOffValB;
 getResponse();
 if(Mon==1) {Serial.println(millis());}
 
 if (InjOnVal>=MinInjOnVal) {
   digitalWrite(INJ, HIGH);
                   }//inj4
 delayMicroseconds(InjOnVal);
 digitalWrite(INJ, LOW); 
 InjOffVal=InjOffValC;
 getResponse();
 c=0;

              
if (PidRead==4) {              //Map
  lcd.setCursor(9,1);
  lcd.print("      ");
  lcd.setCursor(9,1);
  lcd.print(AHex2I);
  MAP=AHex2I;
  lcd.setCursor(13,1);
  lcd.print("Map");
  PidRead=5;
               }
 if (PidRead==3) {               //Temp
  lcd.setCursor(9,1);
  lcd.print("      ");
  lcd.setCursor(9,1);
  lcd.print(AHex2I-40);
  lcd.setCursor(13,1);
  lcd.print("Tem");
  PidRead=1;
               }              
 if (PidRead==2) {               //Speed
  lcd.setCursor(9,0);
  lcd.print("      ");
  lcd.setCursor(9,0);
  lcd.print(AHex2I);
  KPH=AHex2I;
  lcd.setCursor(13,0);
  lcd.print("Kph");
  PidRead=4;
               }              
 if(PidRead==1) {                 //RPM
  lcd.setCursor(0,0);
  lcd.print("        ");
  lcd.setCursor(0,0);
  lcd.print(AHex4I);
  lcd.setCursor(5,0);
  //lcd.print("RPM");
  lcd.print(LcdT);
  RPM=AHex4I;
  PidRead=2;
              }              
  /*lcd.setCursor(10,0);
  lcd.print("      ");
  lcd.setCursor(10,0);
  intmillis=(millis()-lastmillis);
  lcd.print(intmillis);
  lastmillis=millis();
  */
  if(Mon==1) {Serial.println(millis());}
  
 c=0;
 
 Serial1.println("0104");//Torque-PID
 if (InjOnVal>=MinInjOnVal) {
   digitalWrite(INJ, HIGH);
                   }//inj5
 delayMicroseconds(InjOnVal);
 digitalWrite(INJ, LOW);
 InjOffVal=InjOffValA;
 getResponse();
 if(Mon==1) {Serial.println(millis());}
 
 if (InjOnVal>=MinInjOnVal) {
   digitalWrite(INJ, HIGH);
                   }//inj6
 delayMicroseconds(InjOnVal);
 digitalWrite(INJ, LOW);
 InjOffVal=InjOffValB;
 getResponse();
 if(Mon==1) {Serial.println(millis());}
 
 if (InjOnVal>=MinInjOnVal) {
   digitalWrite(INJ, HIGH);
                   }//inj7
 delayMicroseconds(InjOnVal);
 
 digitalWrite(INJ, LOW);
 InjOffVal=InjOffValB;
 getResponse();
 if(Mon==1) {Serial.println(millis());}
 
 if (InjOnVal>=MinInjOnVal) {
   digitalWrite(INJ, HIGH);
                   }//inj8
 delayMicroseconds(InjOnVal);
 digitalWrite(INJ, LOW); 
 InjOffVal=InjOffValC;
 getResponse();
 c=0;
  lcd.setCursor(0,1);
  lcd.print("      ");
  lcd.setCursor(0,1);
  lcd.print(AHex5I);
  InjTorq=AHex5I;
  lcd.setCursor(5,1);
  lcd.print("ToQ");
  /*
  lcd.setCursor(10,1);
  lcd.print("      ");
  lcd.setCursor(10,1);
  intmillis=(millis()-lastmillis);
  lcd.print(intmillis);
  lastmillis=millis();
  */
  if(Mon==1) {Serial.println(millis());}
  if(Mon==1) {Serial.println(millis());}
  if (Mon==2) {
    MonRead="";
    InjTot=InjOnVal;
    InjTot=InjTot*8;
    MonRead=MonRead+","+ RPM+","+ InjTorq+","+MAP+","+KPH+","+InjOnVal+","+InjTot;
    Serial.print(millis());
    Serial.println (MonRead) ;
                }
 if (RPM<=800) {
  getConected();
  PidRead=1;
 }
 if (RPM>=4000) {
  getConected();
  PidRead=1;
 }
 
}

void getResponse(void){
  
 for (int a = 1; a<(InjOffVal); a++) {
  if (Serial1.available()) {
    b=b+1;
    char inOBD = Serial1.read();
    if ((b>8)&&(b<18)){
    Torq[b-7]= inOBD;//Serial1.read();
    
    } 
  }
  c=c+1;
  }
  if (c>=14001){
 //Serial.println ("Start-");
   AHex1=strtol(&Torq[3],0,16);//Good for 1
 //Serial.println (AHex1);
   AHex2=strtol(&Torq[5],0,16);
  //Serial.println (AHex2);
  AHex2I=AHex2;
  AHex2IT=(AHex2I-40);
   AHex3=strtol(&Torq[8],0,16);
  //Serial.println (AHex3);
  AHex4=(((AHex2*256)+(AHex3))/4);
  //Serial.println(AHex4);
  AHex4I=AHex4;
  AHex5=((AHex2*100)/255);
  AHex5I=AHex5;
  //Serial.println(AHex5);
 

 if (Mon==2) {
   String MonRead(Torq);
   Serial.println(MonRead);
 }
 b=0;
 c=0;

  } 
}
void getConected(void){
  for (int m = 1; m<(5); m++) {
  delay(500);
  if(m==1) {Serial1.println("010C");}  
  if (m==2) {Serial1.println("010C");}
  if (m>=3) {Serial1.println("");}
delay(5);  
  for (int n = 1; n<(10000); n++) {
  // read from port 1, send to port 0:
  if (Serial1.available()) {
    b=b+1;
    char inOBD = Serial1.read();
    TorqT[b]= inOBD;//Serial1.read();
   if (Mon==2){Serial.println(inOBD);}
  if (Mon==2){Serial.println(n);} 
  }
  }

   String MonRead(TorqT);
   if (Mon==2){Serial.println(MonRead);}
 b=0;
  }
char TorqT[ ]="vvvvvvvvvvvvvvvvvvvvvvvvvvvvvv"; 
}

I will cut it down to see if its something within the rest of the code affecting it
Thanks

int a = 0;
int b = 0;
int c = 0;
int n = 0;
int m = 0;

One letter global names are a recipe for disaster.

unsigned long AHex;
unsigned long AHex1;
unsigned long AHex2;
unsigned long AHex3;
unsigned long AHex4;
unsigned long AHex5;

When you find yourself numbering (poorly; AHex should be AHex0) variable names, that's a sure sign that an array is a better choice.

  InjOnVal=0;
  InjOnVal=((InjTorq*InjTorq)*2.2);

Why? There seems to be no good reason for two assignment operations when nothing (significant) happens in between them.

/*  
if (sensorValue0<=300){            //Vap Temp
  digitalWrite(VSol, HIGH);
                      }
  else {
  digitalWrite(VSol, LOW);
                     }
*/

I'm pretty sure that this has nothing to do with your problem. Delete commented out code before posting, if the code is not commented out to illustrate the difference between working and non-working code.

  if(Mon==1) {Serial.println(millis());}
  if(Mon==1) {Serial.println(millis());}

Just in case one doesn't work?

for (int a = 1; a<(InjOffVal); a++) {
  if (Serial1.available()) {
    b=b+1;
    char inOBD = Serial1.read();
    if ((b>8)&&(b<18)){
    Torq[b-7]= inOBD;//Serial1.read();
    
    } 
  }

Putting each { on a new line, and using Tools + Auto Format would, in my opinion, make your code more readable.

Suppose that there is no serial data. This loop will be blasted through in nothing flat. Using the number of iterations of a loop to time things is not the way to work.

That code is certainly not going to read 9 characters from the ssslllooowww serial port before 5000 iterations of the loop is over with.

char TorqT[ ]="vvvvvvvvvvvvvvvvvvvvvvvvvvvvvv";

Creating a local variable with the same name as a global variable is a bad idea. Creating a local variable that immediately goes out of scope is silly.

All of your code for reading from Serial1 has the same flaws. I think that when you correct those issues, your issues with specific values having been read will have mysteriously disappeared.

Thanks PaulS
I take my hat off to you for looking at my poor example of code and coming back so quick as i said I am a copier of code with little skill
I have cut down my code to test and as I thought it was me being dumb to an extent
I have Closed the For loop before I call "if (c==14001){ }" so c would alway be higher than 14001
Not sure why it would trigger at 14006 but I will let that rest.
Thanks for your sugestions to my code I will look into them
I will now get back to the rest of the project
Thanks again