// for turning a number into lights
// which lights to turn on?
// variable for the number you want to turn into lights
byte number = 15;
// variables that show, for each light,
// whether that light should be on or off
// true means on; false means off
boolean forty, twenty, ten, eight, four, two, one;
int leftover = number;
boolean forty = (leftover >= 40);
if (forty) leftover -= 40;
boolean twenty = (leftover >= 20);
if (twenty) leftover -= 20;
boolean ten = (leftover >= 10);
if (ten) leftover -= 10;
boolean eight = (leftover >= 8);
if (eight) leftover -= 8;
boolean four = (leftover >= 4);
if (four) leftover -= 4;
boolean two = (leftover >= 2);
if (two) leftover -= 2;
boolean one = (leftover >= 1);
if (one) leftover -= 1;
I know that this code could be made "cleaner", but I wrote it this way to make it easier for a newbie to read.
And here is a worked example to help you understand:
// WORKED EXAMPLE
byte number = 15;
// OK, for this example, the number is 15
// and these variables will hold the results
boolean forty, twenty, ten, eight, four, two, one;
int leftover = number;
// this makes a copy of our number
// now we can change the copy without changing the original number
// so now, leftover is 15
boolean forty = (leftover >= 40);
// 15 is less than 40, therefore, (15 >= 40) is false
// so this boolean gets set to false
if (forty) leftover -= 40;
// since the boolean is false, nothing happens here
// now on to the next boolean
boolean twenty = (leftover >= 20);
// 15 is less than 20, therefore, (15 >= 20) is false
// so this boolean gets set to false, too
if (twenty) leftover -= 20;
// again a false boolean, so again, nothing happens
// and now for the next boolean
boolean ten = (leftover >= 10);
// 15 is greater than 10, therefore, (15 >= 10) is true
// so this boolean gets set to true
if (ten) leftover -= 10;
// since the boolean is true, we follow the instruction
// the instruction says to decrease the value of leftover by 10
// leftover was 15, 15 minus 10 leaves 5, so leftover becomes 5
// remember, leftover is 5 now
boolean eight = (leftover >= 8);
// 5 is less than 8, therefore, (5 >= 8) is false
// so this boolean gets set to false
if (eight) leftover -= 8;
// the boolean is false, so here, nothing happens
boolean four = (leftover >= 4);
// 5 is greater than 4, therefore, (5 >= 4) is true
// so this boolean gets set to true
if (four) leftover -= 4;
// the boolean is true, so we follow the instruction
// the most recent value for leftover was 5
// 5 minus 4 leaves 1, so leftover becomes 1
// keep in mind that leftover is now 1
boolean two = (leftover >= 2);
// 1 is less than 2, therefore, (1 >= 2) is false
// so this boolean gets set to false
if (two) leftover -= 2;
// the boolean is false, so nothing happens here
// leftover is still 1
boolean one = (leftover >= 1);
// 1 is equal to 1, therefore (1 >= 1) is true
// so this boolean gets set to true
if (one) leftover -= 1;
// the boolean is true, so we follow the instruction
// 1 minus 1 leaves 0, so leftover becomes 0