Go Down

Topic: convert int array to int (Read 154 times) previous topic - next topic

3DPiper

I have an integer array that is read in one character at a time from another chip over the serial line.

Code: [Select]
int F_Data[10];
int test;



Once I have that number, I would like to assign it to another normal integer variable so I can perform some math function on it (test).

Code: [Select]
test = F_Data;

However, this errors out with

invalid conversion from 'int*' to 'int' [-fpermissive]

If I try to use atoi:

Code: [Select]
test = atoi (F_Data);

I get this error:

cannot convert 'int*' to 'const char*' for argument '1' to 'int atoi(const char*)'


How do I convert the integer array into a normal integer?


vaj4088

Because you do not show all of your code, only snippets, we can only take guesses.  Perhaps you want a snippet of a correct answer?

Perhaps you should not use an array of int to hold a series of char?

I also hope that you put a zero into F_Data in the right place.


BulldogLowell

I have an integer array that is read in one character at a time from another chip over the serial line.
...

How do I convert the integer array into a normal integer?
don't store it in an integer array, use a char array. 

NULL terminate the char array and you can use atoi() to do what you want:

Code: [Select]
char myCharArray[] = "12345";  // a NULL terminated char array.

void setup()
{
  Serial.begin(9600);
  int test = atoi(myCharArray);
  Serial.println(test);
}

void loop() {
  // put your main code here, to run repeatedly:

}


parse the transmission into a char array and tack on a NULL...

Blue Eyes

To get an integer into the test variable you need to reference a specific entry in the integer array by indexing into it, for example  F_Data[0] is the first entry.

Code: [Select]
   for(int i = 0; i < 10; i ++)
  {
    Serial.println(F_Data[i]);
   
  }

3DPiper

Quote
don't store it in an integer array, use a char array. 

NULL terminate the char array and you can use atoi() to do what you want
That is very interesting.

The only info that will be in F_Data will be numbers from a frequency counter IC. You would think it would be easy to convert int array into another int.

Thank you! That helps a lot!

sterretje

The only info that will be in F_Data will be numbers from a frequency counter IC.
Can you post a link to the datasheet?

At this moment we don't know if you receive a bunch of text characters or a bunch of integers. In your opening post you mention that you receive characters so one value from the IC would be a bunch of characters. In that case the value 1000 would be received as 0x31 0x30 0x30 0x30 (and probably a terminating 0x00). If the IC however sends the number, you will receive something like 0x03 0xE8 (or 0xE8 0x03 depending on endiannes); it might be more bytes depending on the size (16 bits, 32 bits, 64bits).

So it's important to understand what you're receiving to handle the data correctly.

As mentioned, full code will also help ;)
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

robtillaart

#6
Jan 31, 2016, 10:12 am Last Edit: Jan 31, 2016, 10:13 am by robtillaart
check this
Code: (not tested) [Select]
int F_Data[5] = { 1, 2, 3, 4, 5 };

void setup()
{
  Serial.begin(115200);
 
  long value = 0;
  for (int i=0; i<5; i++)
  {
    value *= 10;
    value += F_Data[i];
  }
  Serial.println(value);
  // do some math
  Serial.println(sqrt(value)); 
}

void loop()
{
}
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

3DPiper

#7
Jan 31, 2016, 06:46 pm Last Edit: Jan 31, 2016, 10:09 pm by 3DPiper
Sorry.
I am modifying this code to just read in frequencies:

http://www.jianpingusa.com/JP_Frequency_Counter_Test_Code.txt


Once I have F_Data, I need to perform some math on it (is it larger than a target frequency? smaller?)

I guess I said 'characters' when I should have said integers. It will only ever receive numbers (with no leading zeroes).

Here is the code as I have it now:

Code: [Select]

/*==============================================================
 File........JP Serial Frequency & Counter Module Test code for Arduino
 Purpose.....This test code for JP Serial Frequency & Counter Module
 MCU.........Arduino Mega 2560
 Author......Jianping Sun
 Website.....www.JianpingUSA.com
 Email.......JP@JianpingUSA.com
 Started.....July, 7, 2012
 Updated.....
 Note: The Freq & Counter module Rx <===> Tx1 of Arduino
           The Freq & Counter module Tx <===> Rx1 of Arduino
           The SerLCD module Rx <===> Tx0 of Arduino
==============================================================*/
#include <SoftwareSerial.h> // the JP Serial Frequency & Counter module only has 3 inputs,
                            // so we have to use 2 chips. This software serial is for communication
                            // for the second chip (that connects to the Chanter)

 
int i = 0;     // variable
int j = 0;
int F1_test = 0;        // The first frequency to test against the target frequency
int F2_test = 0;        // The second frequency to test against the target frequency
int F3_test = 0;        // The third frequency to test against the target frequency
int F4_test = 0;        // The fourth frequency to test against the target frequency
int TargetFrequency = 500;  // The target frequency in Hz. It can be adjusted with buttons for up/down at any time

char F_Data[10];  // Frequency Data Variable


void setup()
{
  SoftwareSerial uart_chanter(2,3);  // The software serial input/output for the JP Serial Frequency & Counter Module 2
  Serial.begin(19200);  // For JP Serial Frequency & Counter Module 1
  uart_chanter.begin(19200);  // For JP Serial Frequency & Counter Module 2
  Serial.print("The Target Frequency is  "); Serial.println(TargetFrequency);  // Just to let you know what the starting target frequency is
 
}
void loop(){

//================= READ F1 FREQUENCY =========================

//x = 48;            // First Channel Frequency (no zero)
//x = 54;            // First Channel Frequency (with zero)
//x = 51;            // First Channel counter (no zero)
Serial.write(48);     // request FIRST CHANNEL frequency data with no zeroes
delay(10);
Rec_Data();        // Receive Frequency Data
delay(10);
F1_test = atoi(F_Data);
Serial.print("F1 Frequency = "); Serial.println(F1_test);
delay(500);

//================= READ F2 FREQUENCY  ========================

//x = 49;            // Second Channel Frequency (no zero)
//x = 55;            // Second Channel Frequency (with zero)
//x = 52;            // Second Channel counter (no zero)
Serial.write(49);     // request SECOND CHANNEL frequency data with no zeroes
delay(10);
Rec_Data();        // Receive Frequency Data
delay(10);
F2_test = atoi(F_Data);
Serial.print("F2 Frequency = "); Serial.println(F2_test);
delay(500);

//================ READ F3 FREQUENCY  =======================

//x = 50;            // Third Channel Frequency (no zero)
//x = 56;            // Third Channel Frequency (with zero)
//x = 53;            // Third Channel counter (no zero)
Serial.write(50);     // request THIRD CHANNEL frequency data (no zero)
delay(10);
Rec_Data();        // Receive Frequency Data
delay(10);
F3_test = atoi(F_Data);
Serial.print("F3 Frequency = "); Serial.println(F3_test);
delay(500);

//================= READ F4 FREQUENCY ==============================

//x = 48;            // First Channel Frequency of second chip (no zero)
//x = 54;            // First Channel Frequency of second chip (with zero)
//x = 51;            // First Channel counter of second chip (no zero)
Serial1.write(48);     // request FIRST CHANNEL frequency data of second chip (no zero)
delay(10);
Rec_Data1();        // Receive Frequency Data
delay(10);
F4_test = atoi(F_Data);
Serial.print("F4 = "); Serial.println(F4_test);
delay(500);



}




//=================== Receive Frequency Data from chip 1 (drones) ========
void Rec_Data()   {
  j = 0;
while ( j < 10) {
      if (Serial.available() > 0) {
             // get incoming byte:
         F_Data[j] = Serial.read();
         j++;         
      }
  }
}

//=================== Receive Frequency Data from chip 2 (chanter) =============
void Rec_Data1()   {
  j = 0;
while ( j < 10) {
      if (Serial1.available() > 0) {
             // get incoming byte:
         F_Data[j] = Serial1.read();
         j++;         
      }
  }
}


//================ Dispaly Frequency Data (6 digi) ==================
void Display_Freq_Data() {
  for (int i = 4; i < 10; i++){ 
//  Serial.write(Ch_Line + i - 4);   
  Serial.write(F_Data[i]);
  delay(10);
  }
}


Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy