add smoothing to the pwm lightsense, reduce Fail idle delay from 5 to 3s (based on experience on makerfaire prague)

master
Ondřej Hruška 6 years ago
parent 239d17abca
commit c504012991
Signed by: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 27
      display.c
  2. 3
      game.c

@ -15,6 +15,13 @@ const uint8_t disp_digits[10] = {
DIGIT_0, DIGIT_1, DIGIT_2, DIGIT_3, DIGIT_4, DIGIT_5, DIGIT_6, DIGIT_7, DIGIT_8, DIGIT_9
};
#define adc_samps_len 16
uint8_t adc_samps[adc_samps_len];
uint8_t adc_samp_i = 0;
#define light_sens_countdown_max 800
uint16_t light_sens_countdown = 0;
volatile uint8_t disp_brightness;
void display_show(uint8_t dig0, uint8_t dig1)
@ -57,9 +64,23 @@ ISR(TIMER2_OVF_vect)
{
// convert in background
if (adc_ready()) {
disp_brightness = 255 - adc_read_8bit(); // inverse
if (disp_brightness < 10) disp_brightness = 10;
adc_start_conversion(LIGHT_ADC_CHANNEL);
if (light_sens_countdown++ == light_sens_countdown_max) {
light_sens_countdown = 0;
uint8_t brt = 255 - adc_read_8bit(); // inverse
if (brt < 10) brt = 10;
adc_start_conversion(LIGHT_ADC_CHANNEL);
// averaging
adc_samps[adc_samp_i++] = brt;
if (adc_samp_i == adc_samps_len) adc_samp_i = 0;
uint16_t sum = 0;
for (uint8_t i = 0; i < adc_samps_len; i++) {
sum += adc_samps[i];
}
sum /= adc_samps_len;
disp_brightness = sum;
}
}
OCR2B = disp_brightness;

@ -324,9 +324,10 @@ void game_main(void)
enum GameState_enum last_state = STATE_NEW_GAME;
while (1) {
// loop period is 100 ms
if (GameState == last_state) {
if (GameState == STATE_NEW_GAME) {
if (!first_start && idle_cnt == 50 && !holding_new_game_button) {
if (!first_start && idle_cnt == 30 && !holding_new_game_button) {
usart_puts("clear highscore display\r\n");
display_show(0, 0);

Loading…
Cancel
Save