Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
10 years, 7 months ago.
How to read Analog Channels in for..next loop?
Hello,
I am new to mbed..
I assigned pins to analog inputs as below
AnalogIn ms1(PA_1); AnalogIn ms2(PA_4); AnalogIn ms3(PA_6); AnalogIn ms4(PA_7);
Now i want to read them in a for next loop assignig each channel output into an array such as analogMeasurement[4]
For one analogical channel it is easy to read and get the value. But for, for example, 10 channles how could be possible to read in a loop?
Thanks..
2 Answers
10 years, 7 months ago.
Do you practice programming ?
If yes, what languace do you usually use ? if no, it would be better searching on the internet some courses or howtos about C language...
this one could works...
AnalogIn ms1(PA_1);
AnalogIn ms2(PA_4);
AnalogIn ms3(PA_6);
AnalogIn ms4(PA_7);
float analogMeasurement[4];
main()
{
while(1)
{
analogMeasurement[0]=ms1.read();
analogMeasurement[1]=ms2.read();
analogMeasurement[2]=ms5.read();
analogMeasurement[3]-ms4.read();
printf("\n values are 1 : %1.03f 2 : %1.03f 3 : %1.03f 4 : %1.03f", analogMeasurement[0], analogMeasurement[1], analogMeasurement[2], analogMeasurement[3]);
}
}
therefore is you need more values, with more AnalogIn you can use arrays with an for loop to read the values... so with 4 AnalogIn as upper :
Analogin ms[4];
AnalogIn ms1(PA_1);
AnalogIn ms2(PA_4);
AnalogIn ms3(PA_6);
AnalogIn ms4(PA_7);
float analogMeasurement[4];
main()
{
ms[0]=ms1;
ms[1]=ms2;
ms[2]=ms3;
ms[3]=ms4;
while(1)
{
int i;
for (i=0; i<4; i++)
analogMeasurement[i] = ms[i].read(); //store values using arrays
printf("\n values are : ");
for (i=0; i<4; i++)
printf(" %d : %1.03f", i+1, analogMeasurement[i]);
}
}
Hop it can helps you
The answer you send is not related to my question... I want to make myself little bit clear... Below code may be wrong but I want to do similar like that:
//string analogpin[4] = {PA_1,PA_4,PA_6,PA_7};
string analogname[4] = {ms1,ms2,ms3,ms4};
float analogread[4];
int main () {
AnalogIn ms1(PA_1);
AnalogIn ms2(PA_4);
AnalogIn ms3(PA_6);
AnalogIn ms4(PA_7);
for (int i=0; i<4; i++){
analogread[i] = analogname[i].read();
}
I meant something like that...
Thanks for support..
posted by 20 Apr 2015I tried array version but no hope i think....There are also compiler erros in your second example...
posted by 20 Apr 2015some errors in fact with the second source. But Erik gives a better answer.
Therefore, I publish my corrected source
// same line that the previous source posted
// just corrected with pointers
Analogin *ms[4]; // an array of pointer of 4 AnalogIn ref.
main()
{
ms[0]=&ms1;
ms[1]=&ms2;
ms[2]=&ms3;
ms[3]=&ms4;
while(1)
{
int i;
for (i=0; i<4; i++)
analogMeasurement[i] = ms[i]->read(); //store values using arrays
printf("\n values are : ");
for (i=0; i<4; i++)
printf(" %d : %1.03f", i+1, analogMeasurement[i]);
}
}