I am going to make a pwm drive circuit with 6 idential channels. How can I make the lookup table for the input value and the math code one block that is reused for each of the 6 channels ?

If they’re identical, one block of code should be sufficient.

You probably want a simple function, but maybe I didn’t understand the question.

Lookup table in an array perhaps and a function for the maths.

Can you explain in more detail what you are doing ?

Maybe use switch-case for the input values and a single function for the math?

Or just pass the math function the input values as args; mathFunction( input1, input2 )

Their are literally dozens and dozens of way to do this. A possible outline follows ...

```
#define ELEMENT_COUNT(ARRAY) (sizeof(ARRAY) / sizeof(ARRAY[0]))
struct channel_t
{
const uint8_t _pin;
// ... your instance variables here ...
// ... constructor and initializers ...
channel_t(uint8_t pin)
: _pin(pin)
{
// ... anything else needing intialization that doesn't use Arduino API fuctions ...
}
void begin()
{
// ... anything else needing intialization using Arduino API fuctions ...
}
// ... your methods here ...
// ... math routines to specify PWM values perhaps ...
};
channel_t channels[] =
{
channel_t(A0) // use desired PWM pin refernece
, channel_t(A1) // use desired PWM pin refernece
, channel_t(A2) // use desired PWM pin refernece
, channel_t(A3) // use desired PWM pin refernece
, channel_t(A4) // use desired PWM pin refernece
, channel_t(A5) // use desired PWM pin refernece
};
void loop()
{
for ( int i = ELEMENT_COUNT(channels); i--; )
{
// iterate over all channel objects performing calculations ...
// ... and setting value using 'analogWrite(channels[i]._pin, value)' ...
}
}
void setup()
{
for ( int i = ELEMENT_COUNT(channels); i--; )
{
channels[i].begin();
}
}
```

I'd need to use a function I think, if I recall this is a portion of code with it's own setup and constants and variables. It could turn out long so I'd rather not repeat it. I need to read an analog value from the sensor, possibly have a lookup table to find the temperature because the sensor is not linear and then make a decision for the channel

A function is a portion of code that can be called from your program either once, or many times. It can have its own local variables or can use global ones. References to, or values of variables can be passed to a function to enable it to do common tasks using different parameters and a value that is the result of a function can be passed back to the calling program. Using functions avoids the need to write and maintain several sections of code that do the same thing. They are also useful because a they can be tested in isolation from the main code. Giving them meaningful names also helps to improve code readability.

Arduino switch-case will let you make cases that cover a range of values. There’s your lookup.

Otherwise you might do:

```
if ( value < 200 )
{
// do this
}
else if ( value < 400 )
{
// do this
}
else if ( value < 700 )
{
// do this
}
else
{
// do this
}
```

Like was stated above, dozens and dozens of ways…

Yes that is probably what I'll do. Although the sensor response is a curve (that with another resistor in series will have another curve) it can be considered linear for short ranges so rather than have to have a case for every one of the degree's from 50-70C I can use a constant for each 5C gap and keep it down.

sparkylabs: Yes that is probably what I'll do. Although the sensor response is a curve (that with another resistor in series will have another curve) it can be considered linear for short ranges so rather than have to have a case for every one of the degree's from 50-70C I can use a constant for each 5C gap and keep it down.

With a constant or math function you won't need branching logic or lookup table. But you -could- probably fit a per-degree-or-less table in PROGMEM (flash memory) with associated pre-calculated results.

I'll have to see if i can get hold of the sensor formula first. As it's only a 20C gap not a huge problem either way.

sparkylabs:

I’d need to use a function I think, if I recall this is a portion of code with it’s own setup and constants and variables. It could turn out long so I’d rather not repeat it. I need to read an analog value from the sensor, possibly have a lookup table to find the temperature because the sensor is not linear and then make a decision for the channel

Still a good starting point/

http://arduino.cc/forum/index.php/topic,157534.msg1179975.html#msg1179975

If you don’t understand ask!

sparkylabs: I'll have to see if i can get hold of the sensor formula first. As it's only a 20C gap not a huge problem either way.

There is likely a parabolic section that matches your curve.

I don't do maths well, hoping to just follow a supplier formula or use different fixed constants on small "quasi" linear portions

If you have a graph, you can make a table.

Oh i have the table for every 5C so can consider each 5C window as liner and work out the average coefficient for each window

The math you want to use there is called interpolation.

If 5 degrees makes change X the 3 degrees makes 3X/5 change.

GoForSmoke: The math you want to use there is called interpolation.

If 5 degrees makes change X the 3 degrees makes 3X/5 change.

Yes, I'll need to work out the average coefficient for that 5c window and then apply the right one based on which window it is in.

sparkylabs:

GoForSmoke: The math you want to use there is called interpolation.

If 5 degrees makes change X the 3 degrees makes 3X/5 change.

Yes, I'll need to work out the average coefficient for that 5c window and then apply the right one based on which window it is in.

Right. Depending on how you have your data the code will look different. If the data is like a sine table that has say a sine value for every 5 degrees of angle and you want to know the interpolated sine value for 38 degrees angle you would use the vales for 35 and 40 degrees.

A = sine 35 B = sine 40

sine 38 = A + ((38 - 35) * (B - A)) / (40 - 35)

You won't be using sine but your own table instead, this formula I show assumes sine increases with angle. As long as your data increases with temperature it's good, otherwise some small changes need to happen. Sorry, it's what I can give you quick without seeing your data. A more general method can be worked that data direction doesn't matter but I doubt it's needed in your case.

Well i have to look at the sensor and then consider the resistor that will go in series with it and what voltage that will translate to at various temperatures. i think the series resistor will help make the sensor's response linear to a degree