0

I am currently working on a program for an Arduino uno and want to, in the program, when it receives a 16 digit number from a serial transmition, it will split the 16 digit number into 4 separate 4 digit numbers.

For Example: The computer sends the Arduino the number 1100115011001150 and I want the Arduino to break the number so that "Avalue" would equal 1100, "Bvalue" would equal 1150, "Cvalue" would equal 1100 and so on...

Thanks for any help you can provide!

  • Have you tried indexing the array in loops? – Ignacio Vazquez-Abrams Feb 14 '16 at 23:35
  • How does that work and what does that do? – Buncey Feb 14 '16 at 23:40
  • @Buncey are you not familiar with for and while loops? – CharlieHanson Feb 15 '16 at 0:01
  • What is your actual question? – Chetan Bhargava Feb 18 '16 at 10:14
1
char ArrayBuffer[][4] = {};         // holds four characters, must be 

declared as global to loop()

in

loop()
{
byte Index = 0;                    // initialize array index 
if(Serial.available())             // have data in Serial buffer 
{
byte Count = Serial.readBytes(ArrayBuffer[Index],4); // read       //4             //characters     into array buffer , returns actual number //of        characters //read and times out if less than 4 characters are //currently in Serial buffer 
 Serial.println(ArrayBuffer[Index]); // print buffer to monitor 
}while(++Index != 4);              // read 4 by 4 characters from incoming stream 
}

... continue with loop - process ArrayBuffer as required }

This is one way to get the incoming stream from Serial buffer into for individual buffers, however, it is better, less chance of misdirecting the data into wrong buffer (index) to read the entire buffer at once instead of using he readBytes four times. Just my opinion, your mileage will vary.

PS what is the trick to format the code and text correctly ? This is a mess. I cannot get the code block to indent, I can get only the first ( four spaces line.

1
while (Serial.available() < 16);   // wait for 16 chars to come          easiest way to do this
for (int i = 0; i < 4; i++){      // for each number
    for (int j = 0; j < 4; j++){    // for each digit in the number
        nums[i] *= 10;   
        nums[i] += Serial.read() - '0';

Where did my last post - comment on this code went? It was suppose to wait for "peers" approval, per note I got after I posted it.

This is not the best way to use "available()", beside what will happen if 16 characters never arrive?

  • I do see your other answer, you probably had to wait a little. You can now edit your old answer and delete this one. – Paul Mar 23 '16 at 13:42
0
int nums[] = {0, 0, 0, 0};   // must be always reset to zero if you will repeat this process
while (Serial.available() < 16);   // wait for 16 chars to come in; easiest way to do this
for (int i = 0; i < 4; i++){      // for each number
    for (int j = 0; j < 4; j++){    // for each digit in the number
        nums[i] *= 10;   
        nums[i] += Serial.read() - '0';

//while (Serial.available() == 0);
//Serial.read();  // in case you use an extra character to separate numbers

A = nums[0];
B = nums[1];
C = nums[2];
D = nums[3];

If you are using a character or characters to delimit the 16-digit numbers being received, make sure to read off those extra characters before running the while loop again.

0

Not tested but should work.

int nums[] = {0, 0, 0, 0};
String inString = "";
while (Serial.available() >= 16) {
  for (int k =0;k<4;k++) {
    for (int i =0;i<4;i++) {
      int inChar = Serial.read();
      if (isDigit(inChar)) {
        inString += (char)inChar;
      }
    }
    nums[k]=inString.toInt();
    inString = "";
  }
  Serial.println(nums[0]);
  Serial.println(nums[1]);
  Serial.println(nums[2]);
  Serial.println(nums[3]);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.