#include #include #include #include #define RISING 1 #define FALLING 0 #define X_BASE 366 // based off your measurements from Part 1 #define Y_BASE 437 // based off your measurements from Part 1 #define COLOR_SPACE_MAX 150 // Is the range of values the calculations will produce 0 to COLOR_SPACE MAX #define COLOR_SPACE_CONSTANT (COLOR_SPACE_MAX/6 + 1) /* IMPORTANT NOTE: Your PWM generation should be based off COLOR_SPACE_MAX as all RGB values will range from 0 to COLOR_SPACE_MAX */ // update flags volatile unsigned char update_x = 0; volatile unsigned char update_y = 0; volatile unsigned char ADC_update = 0; volatile unsigned char button_update = 0; /* Add additional global variable declarations. Remember to use volatile as needed */ int main(void) { char button_pressed = 0; // store the calculated values from H & S unsigned char calc_red = COLOR_SPACE_MAX; unsigned char calc_green = COLOR_SPACE_MAX; unsigned char calc_blue = COLOR_SPACE_MAX; /* code initialization */ sei(); for(;;) { // calculation of positive duty cycle for x axis if(update_x){ } // calculation of positive duty cycle for x axis if(update_y){ } // update light level if the button is pressed if(button_pressed) { hue_value_x = pos_duty_cycle_avg_x - X_BASE; saturation_value_y = pos_duty_cycle_avg_y - Y_BASE; // make sure calculation results inside the light level range // person can turn off the light too much but the light still should still just be off if(hue_value_x > COLOR_SPACE_MAX) hue_value_x = COLOR_SPACE_MAX; else if(hue_value_x < 0) hue_value_x = 0; if(saturation_value_y > COLOR_SPACE_MAX) saturation_value_y = COLOR_SPACE_MAX; else if(saturation_value_y < 0) saturation_value_y = 0; // calculate color if ( saturation_value_y == 0 ) { calc_red = COLOR_SPACE_MAX; calc_green = COLOR_SPACE_MAX; calc_blue = COLOR_SPACE_MAX; } else { // itermediate values used in calculation unsigned char tmp, tmp_r, tmp_1, tmp_2, tmp_3; unsigned long tmp_adj; tmp = hue_value_x / COLOR_SPACE_CONSTANT; tmp_r = hue_value_x - (tmp* COLOR_SPACE_CONSTANT); tmp_1 = ( COLOR_SPACE_MAX * (COLOR_SPACE_MAX - (long)saturation_value_y ) ) / COLOR_SPACE_MAX; tmp_adj = (saturation_value_y * tmp_r) / COLOR_SPACE_CONSTANT; tmp_2 = ( COLOR_SPACE_MAX * (COLOR_SPACE_MAX - tmp_adj) ) / COLOR_SPACE_MAX; tmp_adj = (saturation_value_y * ( COLOR_SPACE_CONSTANT - tmp_r)) / COLOR_SPACE_CONSTANT; tmp_3 = ( COLOR_SPACE_MAX * (COLOR_SPACE_MAX - tmp_adj) ) / COLOR_SPACE_MAX; if ( tmp == 0 ) { calc_red = COLOR_SPACE_MAX ; calc_green = tmp_3 ; calc_blue = tmp_1; } else if ( tmp == 1 ) { calc_red = tmp_2 ; calc_green = COLOR_SPACE_MAX ; calc_blue = tmp_1; } else if ( tmp == 2 ) { calc_red = tmp_1 ; calc_green = COLOR_SPACE_MAX ; calc_blue = tmp_3; } else if ( tmp == 3 ) { calc_red = tmp_1 ; calc_green = tmp_2 ; calc_blue = COLOR_SPACE_MAX; } else if ( tmp == 4 ) { calc_red = tmp_3 ; calc_green = tmp_1 ; calc_blue = COLOR_SPACE_MAX; } else { calc_red = COLOR_SPACE_MAX ; calc_green = tmp_1 ; calc_blue = tmp_2; } } } if(button_update) { } if(ADC_update) { red = (brightness * calc_red) / 255; green = (brightness * calc_green) / 255; blue = (brightness * calc_blue) / 255; } } return 0; } SIGNAL(SIG_INTERRUPT0) { } INTERRUPT(SIG_INPUT_CAPTURE1) { } INTERRUPT(SIG_OUTPUT_COMPARE0) { } INTERRUPT(SIG_ADC) { }