int main(void) { signed char kordus=0; unsigned char i; pin leds[3] = { PIN(C, 5), PIN(C, 4), PIN(C, 3) }; pin buttons[3] = { PIN(C, 2), PIN(C, 1), PIN(C, 0) }; for (i = 0; i < 3; i++) { pin_setup_output(leds[i]); pin_setup_input(buttons[i]); pin_set(leds[i]); } unsigned char new_value1; unsigned char new_value2; algustekst(); /* servode algväärtustamine */ init_servos(); /* enable interrupts */ sei(); while (true) { pin_toggle(leds[2]); hw_delay_ms(200); pin_set(leds[2]); hw_delay_ms(200); signed char loendur=0; new_value1 = pin_get_debounced_value(buttons[2]); while(!new_value1){ pin_set(leds[2]); unsigned short value; signed short distance; dcmootor(); // ADC muunduri seadistamine adc_init(ADC_REF_AVCC, ADC_PRESCALE_8); // Anduri väljundpinge 4-kordselt ümardatud väärtuse lugemine value = adc_get_average_value(0, 4); // ADC väärtuse kauguseks ümberarvutamine distance = ir_distance_calculate_cm(GP2Y0A21YK, value); if(kordus==4) { kordus=0; algustekst(); break; } if (distance <=11) { kordus=0; SERVO6 = SERVO_MAX; hw_delay_ms(500); char text[16]; unsigned char result; float tulemus; // Võrdluspingeks AREF viigu valimine // Multiplekseriga kanali 3 valimine // Tulemus on vasak-asetusega ADMUX = (1 << REFS0) | (1 << ADLAR) | (3); lcd_alpha_clear(); lcd_alpha_write_string("Patarei taituvus"); // ADC üksuse käivitamine, // teisendustakti seadmine 16 korda aeglasemaks töötaktist ADCSRA = (1 << ADEN) | (1 << ADPS2) | (1 << ADSC); // Mõõtmise lõpetamise ootamine while (ADCSRA & (1 << ADSC)) continue; // 8-bitise tulemuse lugemine result =ADCH; tulemus=(float)result*0.02; sprintf(text, "%3.0f %%",(tulemus*100)/0.88); lcd_alpha_goto_xy(0, 1); lcd_alpha_write_string(text); hw_delay_ms(500); SERVO6 = SERVO_MID-400; hw_delay_ms(400); if(tulemus<0.7) { pin_set(leds[2]); SERVO5 = SERVO_MIN-300; hw_delay_ms(400); SERVO5 = SERVO_MID-400; hw_delay_ms(400); pin_toggle(leds[0]); } else { pin_set(leds[0]); SERVO5 = SERVO_MAX; hw_delay_ms(400); SERVO5 = SERVO_MID-400; hw_delay_ms(400); pin_toggle(leds[2]); loendur++; } } kordus++; } if(loendur>=1) { char text1[16]; char text2[16]; for(int i=10;i>0;i--){ lcd_alpha_clear(); pin_toggle(leds[1]); sprintf(text1, "Tais patareid %d",loendur); lcd_alpha_write_string(text1); sprintf(text2, "%d",i); lcd_alpha_goto_xy(0, 1); lcd_alpha_write_string("Hoia nuppu S2"); hw_delay_ms(1000); pin_set(leds[1]); lcd_alpha_clear(); lcd_alpha_write_string(text2); hw_delay_ms(1000); new_value2 = pin_get_debounced_value(buttons[1]); if(!new_value2) { lcd_alpha_clear(); SERVO4=SERVO_MAX; hw_delay_ms(400); SERVO4 = SERVO_MID; algustekst(); break; } } lcd_alpha_clear(); SERVO4=SERVO_MIN; hw_delay_ms(400); SERVO4 = SERVO_MID; algustekst(); } } }