Wiring Forum
http://wiring.org.co/cgi-bin/yabb/YaBB.pl
Other questions >> Other questions >> Issue with coding ofr loop and breaking up a char
http://wiring.org.co/cgi-bin/yabb/YaBB.pl?num=1306137602

Message started by cbohart on 05/23/11 at 08:00:02

Title: Issue with coding ofr loop and breaking up a char
Post by cbohart on 05/23/11 at 08:00:02
My purpose is to pass a string to an array by serial connection and break up the array to pass each value i++ to other variables for other use.

This code receives the serial transmission and I can print the contents of the array.
char devicecommands[9];

void setup()
{

 
  Serial.begin(9600);
 
}

void loop()
{
  int i = 0;
    //begin send value to variable by serial to lite LED
  if( Serial.available() ){ // if data is available to read
 
      devicecommands[8] = Serial.read();  // fill the  array

  }
       
        Serial.println(devicecommands[8]);  // prints the array
         
  delay(5000);
}


A problem exists here in that after printing the string it continues to print the final number continuously and I have not been able to stop that behaviour.

This code with a for loop prints the array most oddly - i then i++ but with the vertical space for all the items ( a number plus empty space to the value of i.

char devicecommands[ 8 ];  //variable to contain sent string

void setup()
{

  Serial.begin(9600);
}
 
void loop()
{
//begin send value to variable by serial check at 5 second interval
if( Serial.available() ) // if data is available to read
{
   int i = 0;
   devicecommands[i] = Serial.read();  // read it and store it in 'devicecommands[i ]'
}   
     for(int i=0; i < 8; i++) {  // print array
     Serial.println(devicecommands[i]);

}

//End send variable
   delay(5000);  // wait for half a second
}

this code also continues after the loop test should be false to print the last number/character. it line 8 deep iterating through but if I don't use ...ln then it prints one position after another

I need help to:

1.Stop the loop when done and therefore stop printing the last number in the array, AND more importantly,

2:  Break up the array into its parts so that I may assign those values to other variables.  This is the most pressing concern.  If I can break up the array and assign the values the loop issue will be mute as I may not need the for loop.

Other methods I use say in html after pulling an array from a database do not work in the wiring environment.

Please help.

Thanks.

Title: Re: Issue with coding ofr loop and breaking up a char
Post by cbohart on 05/23/11 at 17:15:52
Sorry.  I did not study all of the material available before jumping in.  After posting I then looked into the library functions and found item on point.  I think I will relax and review all of the material before jumping back in and especially before posting again.

THanks.

Title: Re: Issue with coding ofr loop and breaking up a char
Post by barragan on 05/26/11 at 02:55:16
no worries :)

Wiring Forum » Powered by YaBB 2.5 AE!
YaBB Forum Software © 2000-2010. All Rights Reserved.