if (count == 1 || count == 2 || count == 3) z = 1;
if (count == 4 || count == 5 || count == 6) z = 2;
if (count == 7 || count == 8 || count == 9) z = 3;
if (count == 10 || count == 11 || count == 12) z = 4;
if (count == 13 || count == 14 || count == 15) z = 5;
if (count == 16 || count == 17 || count == 18) z = 6;
if (count == 19 || count == 20 || count == 21) z = 7;
if (count == 22 || count == 23 || count == 24) z = 8;
if (count == 25 || count == 26 || count == 27) z = 9;
if (count == 28 || count == 29 || count == 30) z = 10;
if (count == 31 || count == 32 || count == 33) z = 11;
if (count == 34 || count == 35 || count == 36) z = 12;
Is there an easier way to do this?
Thanks
You can simplify the if statements.
if(count >= 1 && count <= 3)
{
z = 1;
}
and so on and so forth.
Bill
SouthernAtHeart:
if (count == 1 || count == 2 || count == 3) z = 1;
if (count == 4 || count == 5 || count == 6) z = 2;
if (count == 7 || count == 8 || count == 9) z = 3;
if (count == 10 || count == 11 || count == 12) z = 4;
if (count == 13 || count == 14 || count == 15) z = 5;
if (count == 16 || count == 17 || count == 18) z = 6;
if (count == 19 || count == 20 || count == 21) z = 7;
if (count == 22 || count == 23 || count == 24) z = 8;
if (count == 25 || count == 26 || count == 27) z = 9;
if (count == 28 || count == 29 || count == 30) z = 10;
if (count == 31 || count == 32 || count == 33) z = 11;
if (count == 34 || count == 35 || count == 36) z = 12;
Is there an easier way to do this?
Thanks
Maybe something like this:
z = (count + 2) / 3;
Hi,
Try this...
int count = 0;
int z = 0;
void setup()
{
Serial.begin (9600);
}
void loop()
{
for (count = 1; count < 37; count++)
{
// from HERE to THERE is the conversion bit, the rest is to test the code
for (int t = 0; t < 12; t++)
{
if (count >= (1 + (3 * t)) && count <= ((t + 1) * 3))
{
z = t + 1;
}
}
// THERE
Serial.print ("count");
Serial.println(" z ");
Serial.print(count);
Serial.print(" ");
Serial.println( z);
delay(500);
}
}
Tom....
Hi,
I dips me lid to you christop.
Tom....
Have you been watching; Dara O Briain: School Of Hard Sums
I'll present an alternative, just in case the solution doesn't lend itself to simple arithmetic.
const byte conversions [] = { 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4 }; // and so on ...
z = conversions [count - 1];
Now you have a simple table lookup, which would handle all sorts of values that might jump around.
Put the table into PROGMEM if you want to save RAM.
+1 Christop
if the range of count is guaranteed between 1 and 36 the above solutions work.
Otherwise one need either us constrain or do the formula (lookup) conditionally.
count = constrain(count, 1, 36);
z = (count+2)/3;
or
if (1 <= count && count <= 36)
z = (count+2)/3;
else
// to be defined
Robin2
August 30, 2015, 9:57am
9
A series of IF statements like that can be greatly simplified if you start at the other end
if (count > 36) {
// do nothing, I think
}
else if (count > 33) {
z = 12;
}
else if (count > 30) {
z = 11;
}
// etc
However for this specific example I am not proposing this as an alternative to @christop 's suggestion.
...R
I'll present an alternative, just in case the solution doesn't lend itself to simple arithmetic.
const byte conversions [] = { 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4 }; // and so on ...
z = conversions [count - 1];
Now you have a simple table lookup, which would handle all sorts of values that might jump around.
Put the table into PROGMEM if you want to save RAM.
Gammon Forum : Electronics : Microprocessors : Putting constant data into program memory (PROGMEM)
This is good. I can see applications where I can use this type of lookup table.