Go Down

Topic: 2 way serial arduino to vb6 (Read 12131 times) previous topic - next topic

PaulS

Code: [Select]
        data0 = (values[0]);
        data1 = (values[1]);
        data2 = (values[2]);
        data3 = (values[3]);
        data4 = (values[4]);
        data5 = (values[5]);
        data6 = (values[6]);

None of the parentheses are necessary. The names on the right really don't mean anything.

Code: [Select]
        values[0] = 0; // set the values to zero, ready for the next message
        values[1] = 0; // set the values to zero, ready for the next message
        values[2] = 0; // set the values to zero, ready for the next message
        values[3] = 0; // set the values to zero, ready for the next message
        values[4] = 0; // set the values to zero, ready for the next message
        values[5] = 0; // set the values to zero, ready for the next message
        values[6] = 0; // set the values to zero, ready for the next message

The for loop was doing this part just fine.
The art of getting good answers lies in asking good questions.

backbone

unfortunenatly
Code: [Select]
values[i] = 0; // set the values to zero, ready for the next message
in the for loop did not worked.
Maybe it is against what it should be but what I opted for was the only way I could get it to work.
Other options are welcome anyway.

Paco
Never to old to learn and I learn every day

AWOL

Code: [Select]
values[0] = 0; // set the values to zero, ready for the next message
        values[1] = 0; // set the values to zero, ready for the next message
        values[2] = 0; // set the values to zero, ready for the next message
        values[3] = 0; // set the values to zero, ready for the next message
        values[4] = 0; // set the values to zero, ready for the next message
        values[5] = 0; // set the values to zero, ready for the next message
        values[6] = 0;

aka
Code: [Select]
for (byte i = 0; i < 7; ++i) {
  values [i] = 0;
}

or memset.

PaulS

Quote
in the for loop did not worked.

I'd need proof of that. What didn't work was assigning value[ i ] to another variable inside the loop. The assignment of value[ 0 ] to another variable worked, when i was 0, but the assignment of value[ 0 ] to that variable produced unexpected results, when i was 1, because by then value[ 0 ] has been set to 0.

The same problem occurred assigning value[ 1], when i was 2, because value[ 2 ] has been set to 0.

Try this:
Code: [Select]

for(int i=0; i<7; i++)
{
   Serial.print("values[");
   Serial.print(i);
   Serial.print("] = ");
   Serial.print(values[i]);
   Serial.println("]");
}

for(int i=0; i<7; i++)
{
   values[i] = 14;
}

for(int i=0; i<7; i++)
{
   Serial.print("values[");
   Serial.print(i);
   Serial.print("] = ");
   Serial.print(values[i]);
   Serial.println("]");
}


Show us the serial output.

Then, of course, you can change the 14 to 0, and try again. Then, obviously, you can get rid of the first and third for loop. But, try this first, just to prove that the for loops do indeed work, or not work.
The art of getting good answers lies in asking good questions.

backbone

Here the screenshot of the added code and the serial monitor result.
In serialmonitor I pasted 1,2,3,4,5,6,7:    one time and then once more.
Code: [Select]
/*
* SerialReceiveMultipleFields sketch
* This code expects a message in the format: 12,345,678,121,16,89,74:
* This code requires a : character to indicate the end of the data
*/

const int NUMBER_OF_FIELDS = 7; // how many comma separated fields we expect
int fieldIndex = 0;            // the current field being received
int values[NUMBER_OF_FIELDS];   // array holding values for all the fields
int data0= 0;
int data1= 0;
int data2= 0;
int data3= 0;
int data4= 0;
int data5= 0;
int data6= 0;
int i;

void setup()
{
  Serial.begin(9600); // Initialize serial port to send and receive at 9600 baud
}

void loop()
{
  if( Serial.available())
  {
    char ch = Serial.read();
    if(ch >= '0' && ch <= '9') // is this an ascii digit between 0 and 9?
    {
      // yes, accumulate the value
      values[fieldIndex] = (values[fieldIndex] * 10) + (ch - '0');
    }
    else if (ch == ',')  // comma is our separator, so move on to the next field
    {
      if(fieldIndex < NUMBER_OF_FIELDS-1)
        fieldIndex++;   // increment field index
    }
    else
    {
      // any character not a digit or comma ends the acquisition of fields
      // in this example it's the : character sent
      Serial.print( fieldIndex +1);
      Serial.println(" fields received:");
     
      for(int i=0; i<7; i++)
      {
        Serial.print("values[");
        Serial.print(i);
        Serial.print("] = ");
        Serial.print(values[i]);
        Serial.println("]");
      }

      for(int i=0; i<7; i++)
      {
        values[i] = 14;
      }

      for(int i=0; i<7; i++)
      {
        Serial.print("values[");
        Serial.print(i);
        Serial.print("] = ");
        Serial.print(values[i]);
        Serial.println("]");
      }

    }
  }
}
Never to old to learn and I learn every day

PaulS

OK. So, think about this. Where in the values array does the program store values when the first set of values is entered. If you said positions 0 to 6, you'd be right.

Now, where does it store values when the second set arrives? If you said 0 to 6 again, you'd be wrong. It stores them in positions 7 to 13 of the 7 element array, because you do not set fieldIndex back to 0.
The art of getting good answers lies in asking good questions.

Go Up