Could i read PWM.pulsewidth from 6 Inputs in one Array in a "for next Loop"?

16 Feb 2012

Hello,

until now i use some code like the following one to read 6 PWM-Signals in an array to work with them:

#include "mbed.h"
#include "PwmIn.h"
...
PwmIn K1_Sig_In (p5);
PwmIn K2_Sig_In (p6);
PwmIn K3_Sig_In (p7);
PwmIn K4_Sig_In (p8); 
PwmIn K5_Sig_In (p9);
PwmIn K6_Sig_In (p10); 
...
main(){
...
   Signal_input[0] = K1_Sig_In.pulsewidth();
   Signal_input[1] = K2_Sig_In.pulsewidth();
   Signal_input[2] = K3_Sig_In.pulsewidth();
   Signal_input[3] = K4_Sig_In.pulsewidth();
   Signal_input[4] = K5_Sig_In.pulsewidth();
   Signal_input[5] = K6_Sig_In.pulsewidth();
   ...
   for (int z = 0; z < 6; z++) {
      if (Signal_input[z] < Signal_input_alt[z] - Aenderungsgrenze) Signal_input[z] = Signal_input_alt[z] - Aenderungsgrenze;
      if (Signal_input[z] > Signal_input_alt[z] + Aenderungsgrenze) Signal_input[z] = Signal_input_alt[z] + Aenderungsgrenze;
      ...
   }
...
}

My quastion is how could i do something like this:

...
main(){
...
   for (int z = 0; z < 6; z++) {
      Signal_input[z] = K[z]_Sig_In.pulsewidth();
                          ^
                          |
   //*****                This part is my problem ;-)

      if (Signal_input[z] < Signal_input_alt[z] - Aenderungsgrenze) Signal_input[z] = Signal_input_alt[z] - Aenderungsgrenze;
      if (Signal_input[z] > Signal_input_alt[z] + Aenderungsgrenze) Signal_input[z] = Signal_input_alt[z] + Aenderungsgrenze;
      ...
   }
...
}

Of course there is much more code inclusive definitions (all marked with these "...") of the variables and so on in my program, but this is the relevant part of it i think.

Knows anyone a way to do something like this? It is not very important, program works also in my way, but it looks nicer i think... ;-)

Thanks and with best reguards, Steff

16 Feb 2012

Don't know what's PwmIn but the following seems to compile:

PwmOut K[6] = {p5, p6, p7, p8, p9, p10};

Alternatively you can allocate objects on the heap:

//declare
PwmOut *K[6];

...

//allocate
K[0] = new PwmOut(p5);
K[1] = new PwmOut(p6);
...
K[5] = new PwmOut(p9);

// work
...

// cleanup
for ( int i=0; i<6; i++)
  delete K[i];
17 Feb 2012

OMG...Igor...

thank you for the right hint, i was blind as i wrote my code and too stupid as i have looked fpr my fault i think... I was so fixed on my Channeldesriptor K1 to K6 cause this is the only different...forgot the rest of the world around it, it seems... In german we named it "Betriebsblindheit" (or "dusseligkeit" ;-) ), don't know the right name for it in english, sry...

Of course...now as you have said... K[*]_Sign_In don't work, cause i have just to write it like Sign_In_K[*] :-/

Thank you for bringing me back on the right way again :)