If statement not executing more than once "Serial.available();"

hi, im stuck in this project, the thing is that when i try to rewrite the starting value of the array, it just execute the acction of rewriting once, it dont rewrite a 2* time without restarting the arduino, which isnt available for my project… please help!

CODE:

char myval[5] = “0800”;
char inChar;
byte index = 0;
void setup(){
Serial.begin(9600);}
void loop()
{
if (Serial.available() > 0) // Don’t read unless
// there you know there is data
{
if(index < 4) // One less than the size of the array
{
inChar = Serial.read(); // Read a character
myval[index] = inChar; // Store it
index++; // Increment where to write next
myval[index] = ‘\0’; // Null terminate the string
}
}
char nb1 = myval[0] - ‘0’;
char nb2 = myval[1] - ‘0’;
char nb3 = myval[2] - ‘0’;
char nb4 = myval[3] - ‘0’;
int result1;int result2;

result1=nb11000 + nb2100 + nb3*10 + nb4; // get minites
Serial.print(“result1=”);Serial.println(result1);
delay(50);
}

charArray_to_int.ino (796 Bytes)

Put this line at the start/end of function loop().

void loop()
{
index = 0;
.
.
.
}

It will intialize the index of array myval[zero].

Please always use the insert code option to insert code.

thanks now i can rewrite every time y put a number but, its not the correct convertion,example:

before: res=800 res=800 inserting by serial "0100" res=25736 res=25736 inserting by serial "1000" res=25736 res=25736

jenkinss17: thanks now i can rewrite every time y put a number but, its not the correct convertion,example:

before: res=800 res=800 inserting by serial "0100" res=25736 res=25736 inserting by serial "1000" res=25736 res=25736

What kind of conversion?

From char array to decimal for the send the decimal throught serial

char nb1 = myval[0] - '0';
 char nb2 = myval[1] - '0';
 char nb3 = myval[2] - '0';
 char nb4 = myval[3] - '0';
 int result1;int result2;

   result1=nb1*1000 + nb2*100 + nb3*10 + nb4;   // get minites     
   Serial.print("result1=");Serial.println(result1);

First it’s really hard to understand what’s your problem?
But i will try my best.

char myval[] = "0800";
char inChar;
byte index = 0;

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

void loop()
{
  // First extract all the data
  while(Serial.available() > 0) 
  {
    if(index < 4) 
    {
      inChar = Serial.read(); 
      myval[index] = inChar; 
      index++; 
      Serial.println(index);
    }
  }
  ////////////////////////////
  //// Null terminator//////////
  myval[index] = '\0'; 

 ///THIS IS YOUR LOGIC AREA///
  if(index >= 4)
  {
    int nb1 = myval[0] - '0';
    int nb2 = myval[1] - '0';
    int nb3 = myval[2] - '0';
    int nb4 = myval[3] - '0';

    int result1;
    result1 = nb1*1000 + nb2*100 + nb3*10 + nb4;   // get minites     
    Serial.print("result1=");
    Serial.println(result1);
    delay(50);
    index = 0;
  }
}

thanx for the support, here it goes a better explanation:

" in the code i have on part which converts that serial string array in to a decimal number"

 if (Serial.available() > 0) // Don't read unless
                               // there you know there is data
   {
     if(index < 4) // One less than the size of the array
       {
           inChar = Serial.read(); // Read a character
           myval[index] = inChar; // Store it
           index++; // Increment where to write next
           myval[index] = '\0'; // Null terminate the string
           delay(1000);
       }
       
   }
 char nb1 = myval[0] - '0';
 char nb2 = myval[1] - '0';
 char nb3 = myval[2] - '0';
 char nb4 = myval[3] - '0';
 int result1;int result2;

   result1=nb1*1000 + nb2*100 + nb3*10 + nb4;   // get minites

and other one that prints every single value in realtime for logging data into labview

 Serial.print("result1=");Serial.println(result1);
   delay(50);
}

so in need to update the value by serial and exchange the values in real time whitout pause the transmition

Hi my Friend!!!

Aquí está la solución a tu problema :smiley: Espero te funcione

Saludos

Saludos from Mexico :smiley:

char myval[5] = "0800";
char inChar;
byte index = 0;
void setup()
{
Serial.begin(9600);
}

void loop()
{
   if (Serial.available() > 0) // Don't read unless
                               // there you know there is data
   {
   //  if(index < 4) // One less than the size of the array
   for (index==0; index<=3; index++)
       {
           inChar = Serial.read(); // Read a character
           myval[index] = inChar; // Store it
          // index++; // Increment where to write next
         //  myval[index] = '\0'; // Null terminate the string
       }
      myval[4]='\0';
     
   }
 char nb1 = myval[0] - '0';
 char nb2 = myval[1] - '0';
 char nb3 = myval[2] - '0';
 char nb4 = myval[3] - '0';
 int result1; int result2;

   result1=nb1*1000 + nb2*100 + nb3*10 + nb4;   // get minites     
   Serial.print("result1=");Serial.println(result1);
   index=0;
   delay(50);
}

Now you have continue data Tx for useit in labview

Ok, did you run the code of “MY last comment” i think so it will solve your problem.

char myval[] = "0800";
char inChar;
byte index = 0;

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

void loop()
{
  // First extract all the data
  while(Serial.available() > 0) 
  {
    if(index < 4) 
    {
      inChar = Serial.read(); 
      myval[index] = inChar; 
      index++; 
      Serial.println(index);
    }
  }
  ////////////////////////////
  //// Null terminator//////////
  myval[index] = '\0'; 

 ///THIS IS YOUR LOGIC AREA///
  if(index >= 4)
  {
    int nb1 = myval[0] - '0';
    int nb2 = myval[1] - '0';
    int nb3 = myval[2] - '0';
    int nb4 = myval[3] - '0';

    int result1;
    result1 = nb1*1000 + nb2*100 + nb3*10 + nb4;   // get minites     
    Serial.print("result1=");
    Serial.println(result1);
    delay(50);
    index = 0;
  }
}

YEAH!!!, it works thanx to the always supporting people in this forums!!! “(chela_chat)” & (Cybernetician):slight_smile:

jenkinss17: YEAH!!!, it works thanx to the always supporting people in this forums!!! :)

Cheers. 8)