#define INTERVALS_HALF_HOUR 1024 //example #define BLIND_LEVELS 4 #define INTENSITY_MAX 255 typedef enum { MODE_CALIBRATE, MODE_MANUAL, MODE_AUTO_WARM, MODE_AUTO_COOL } blind_mode; typedef enum { BUTTON_NONE, BUTTON_MODE, BUTTON_UP, BUTTON_DOWN, BUTTON_CALIBRATE } button_input; int current_position; //the current "position" the blind is in int position_levels[BLIND_LEVELS]; //an array of the intervals we should keep our blind at int max_position; //the maximum "position" we can extend the blind to int intensity_levels[BLIND_LEVELS]; int current_intensity; // 0 - 3 (representing intensity classes) int interval_since_move; //how long it's been since we've last moved blind_mode current_mode; void setup() { //Set up data direction registers //Set up interrupts //Set up intensity classes intensity_levels[0] = (INTENSITY_MAX + 1) / BLIND_LEVELS; for (i = 1; i < BLIND_LEVELS; i++) intensity_levels[i] = intensity_levels[0] * i; //Put unit manual mode by default } void main() { setup(); for( ; ; ) { switch (current_mode) { case MODE_MANUAL: manual_mode(); break; case MODE_AUTO_WARM: case MODE_AUTO_COOL: auto_mode(); break; case MODE_CALIBRATE: calibrate_mode(); break } } } void manual_mode() { button_input button; while (MODE_MANUAL == current_mode) { button = check_buttons(); if (BUTTON_UP == button) { //roll up } else if (BUTTON_DOWN == button) { //roll down } else if (BUTTON_MODE == button) { //toggle_mode(); } } } void auto_mode(auto_mode mode) { int intensity_level, new_position_level = -1; while (MODE_AUTO_WARM == current_mode || MODE_AUTO_COOL == current_mode) { if (interval_since_move > INTERVALS_HALF_HOUR) { //we have waited long enough, check if we should move intensity_level = check_intensity(); if (intensity_level != current_intensity) { if (MODE_AUTO_WARM == current_mode) new_position_level = (BLIND_LEVELS - 1) - intensity_level; else if (MODE_AUTO_COOL == current_mode) { new_position_level = intensity_level; roll(position_levels[new_position_level]); } } } } //reads LDR and returns the intensity interval of the sensed light unsigned char check_intensity_level() { unsigned char intensity = 0; //READ INTENSITY FROM SENSOR //SOME DAA / ADD IO return intensity / ((INTENSITY_MAX + 1) / BLIND_LEVELS); } void calibrate_mode() { //set position = max_position = 0 //start timer while (BUTTON_CALIBRATE == check_buttons()) { //keep counting //(timer interrupt function will continue adding to position) } //end timer //save max_position = position position_levels[0] = max_position / BLIND_LEVELS; for (i = 1; i < BLIND_LEVELS; i++) position_levels[i] = position_levels[0] * i; } button_input check_buttons() { } void roll(int new_position) { //if new_position > current position, roll down //if new_position < current position, roll up }