Analog input/Serial commnication problem

Hello all,
plz help me!!!
I have to get input from 4 AI at the same time, store it somewhere and compare with previous reading. Code compliles, but don’t give me anything. to check if the program working I wrote some serial communication functions, but unsuccessfully. It’s now working. plz help me =)

#include

int SV[40]; // every 4 entries are for 4 sensors
int pin[4]={A0, A1, A2, A3};
int i=0;
int k;
int TOA[4]={0,0,0,0};
int sig;
int sumElements ( int arr, int a) {

int summ=0;

if (arr[36+a]==0){return 0;}
for (int m=0; m<10; m++){
summ+=sqrt(arr[4*m+a]^2);
}
return summ;

}

void setup() {

// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
SV[36]=SV[37]=SV[38]=SV[39]=0;

}

void loop() {
// read the input on analog pin 0…3:

for (k; k<4; k++){
//read data
sig = analogRead(pin[k]);
//save it to array
SV[4*i+k]=sig;
//print. use it just to check if it’s working
Serial.println(sig);
if (sig> sumElements(SV,k)/10) { TOA[k]=millis(); Serial.println(“Signal detected”);}

}

i++;
if (i==10) i=0;
k=0;
delay(100);
}

I also get this warning before compilation.

sketch_jul15b.cpp.o: In function __static_initialization_and_destruction_0': /home/gina/.arduino15/packages/arduino/tools/arm-none-eabi-gcc/4.8.3-2014q1/arm-none-eabi/include/c++/4.8.3/iostream:74: warning: undefined reference to std::ios_base::Init::Init()'
/home/gina/.arduino15/packages/arduino/tools/arm-none-eabi-gcc/4.8.3-2014q1/arm-none-eabi/include/c++/4.8.3/iostream:74: warning: undefined reference to `std::ios_base::Init::~Init()'

First, please read Nick Gammon's two posts at the top of this Forum for the proper way to post code on this Forum using code tags. Second:

 summ+=sqrt(arr[4*m+a]^2);

Do you mean to perform an XOR on arr[] (some people think the ^ operator does exponentiation)? Also, because sqrt() returns a float, you need to cast the result to an int for assignment into summ.

I have to get input from 4 AI at the same time

There is only one analog to digital converter. So, you can not read 4 analog values at the same time. You can read them in order, with small delays between each reading.

Whether that is sufficient, or not, is not clear.

  summ+=sqrt(arr[4*m+a]^2);

The ^ may be the mathematical symbol for squaring, but it is not the C symbol. Taking the square root of n squared hardly makes sense.

for (k; k<4; k++){

Only a fool fails to initialize variables in for loops. Surely this does not include you.

I see no reason to include iostream in the sketch. Why are you?

I also get this warning before compilation.

Not possible. Getting that AT compilation is.

It’s now working. plz help me

You need help with working code?

Also, because sqrt() returns a float, you need to cast the result to an int for assignment into summ.

As opposed to having the compiler implicitly perform a cast? No, you do not need to explicitly cast anything.

messy089:
It’s now working. plz help me =)

If it is now working why do you need help

Assuming you meant to type “It’s not working” all I can say is I’m not surprised.

But since you have not told us what it does do and what it should do I can’t say much more.

I can’t image that the sqrt() function makes much sense with integers
And in C++ the ^ does not mean exponetiation - you need to use the pow() function or simply x * x

Why do you need IOSTREAM ?

Strip your code of all the complications and make sure it can read the analog inputs correctly and display the values. Then add the othe stuff piece by piece.

…R

thank you for your sharp but useful comments. yeah, I'm not good in programming, hope you are not offended by my ignorance.

@AWOL: You're right: Anytime you assign a smaller (i.e., in memory requirements) data type into a larger data type, the compiler is perfectly capable of silently making the cast for you. My own personal opinion is that silent casts often lead to forgetting to use a cast where it is needed. Using a cast even though the compiler can do it behind your back helps document that you know what you're doing and I think is a better way of doing it. I think not using a cast is sloppy coding.

messy089:
hope you are not offended by my ignorance.

I am not offended - I would like to help.

But you MUST be conscious that program code is very unforgiving of the smallest error or typo.

...R