int numerator = 5: unsigned char denominators[256] = { 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 15, 15, 15, 15, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 19, 19, 19, 19, 20, 20, 20, 21, 21, 21, 22, 22, 22, 23, 23, 23, 24, 24, 24, 25, 25, 26, 26, 26, 27, 27, 28, 28, 29, 29, 29, 30, 30, 31, 31, 32, 32, 33, 33, 34, 34, 35, 35, 36, 37, 37, 38, 38, 39, 39, 40, 41, 41, 42, 43, 43, 44, 45, 45, 46, 47, 47, 48, 49, 50, 51, 51, 52, 53, 54, 55, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 77, 78, 79, 80, 81, 83, 84, 85, 87, 88, 89, 91, 92, 94, 95, 97, 98, 100, 101, 103, 104, 106, 108, 109, 111, 113, 114, 116, 118, 120, 122, 124, 125, 127, 129, 131, 133, 136, 138, 140, 142, 144, 146, 149, 151, 153, 156, 158, 161, 163, 166, 168, 171, 173, 176, 179, 182, 184, 187, 190, 193, 196, 199, 202, 205, 209, 212, 215, 219, 222, 225, 229, 232, 236, 240, 243, 247, 251, 255 }; int lfo(int speed) { static int current_e = 0; static int current_level = 128 ; static int direction = 1; /* 1:up, -1:down*/ static int last_speed = 0 ; if (last_speed != speed ){ /*is there changeing speed?*/ last_speed = speed; current_e= 0; } current_e += numerator; if (current_e>= denominators[speed] ){ current_e -= denominators[speed] ; current_level += direction; } if (current_level > 255 ){ /*max should be 256*/ direction = -1; current_e = 0; current_level -= 1 ; } if (current_level == 0){ direction = +1; current_e = 0; current_level += 1; } return current_level; } main () { int i; int cnt = 0; for (i = 0 ; i < 256*256 ; i++ ){ printf ("%d\n", lfo(0) ); } }