Home \ Help \ Search \ Login RSS

Welcome, Guest. Please Login

This forum is now READ-ONLY! Please visit http://forum.wiring.co/ for the new forum.

Wiring ForumOther questionsOther questions › Issue with coding ofr loop and breaking up a char

Page Index Toggle Pages: 1
Issue with coding ofr loop and breaking up a char (Read 2849 times)
05/23/11 at 08:00:02

cbohart   Offline
YaBB Newbies

Posts: 9
*
 
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.
 
IP Logged
 
Reply #1 - 05/23/11 at 17:15:52

cbohart   Offline
YaBB Newbies

Posts: 9
*
 
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.
 
IP Logged
 
Reply #2 - 05/26/11 at 02:55:16

barragan   Offline
YaBB Administrator

Posts: 939
*****
 
no worries Smiley
 
IP Logged
 
Page Index Toggle Pages: 1