This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:examples:sensor:ir_distance [2015/11/10 10:48] – heikopikner | en:examples:sensor:ir_distance [2020/07/20 09:00] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Infrared distance sensor ====== | ====== Infrared distance sensor ====== | ||
| - | //Necessary knowledge: | + | //Necessary knowledge: |
| + | [HW] [[en: | ||
| + | [AVR] [[en: | ||
| + | [LIB] [[en: | ||
| ===== Theory ===== | ===== Theory ===== | ||
| Line 91: | Line 94: | ||
| #include < | #include < | ||
| #include < | #include < | ||
| + | |||
| + | #define ADC_CHANNEL 0 | ||
| // Main program | // Main program | ||
| int main(void) | int main(void) | ||
| { | { | ||
| - | unsigned | + | signed |
| - | signed short distance; | + | |
| char text[16]; | char text[16]; | ||
| + | |||
| + | // Robotic HomeLab II external sensors pin of Sensor module | ||
| + | //pin ex_sensors = PIN(G, 0); | ||
| + | // | ||
| + | // | ||
| - | // External sensor selection | ||
| - | pin ex_sensors = PIN(G, 0); | ||
| - | pin_setup_output(ex_sensors); | ||
| - | pin_set(ex_sensors); | ||
| - | |||
| // Initialization of LCD | // Initialization of LCD | ||
| - | lcd_gfx_init(); | + | lcd_gfx_init(); |
| - | + | lcd_gfx_clear(); | |
| - | // Clearing the LCD | + | lcd_gfx_goto_char_xy(1, |
| - | lcd_gfx_clear(); | + | |
| - | + | ||
| - | // Line selection | + | |
| - | lcd_gfx_goto_char_xy(1, | + | |
| - | + | ||
| - | // Name of the program | + | |
| lcd_gfx_write_string(" | lcd_gfx_write_string(" | ||
| Line 120: | Line 118: | ||
| // Endless loop | // Endless loop | ||
| - | while (true) | + | while (1) |
| { | { | ||
| // Reading the 4 times rounded value of output voltage | // Reading the 4 times rounded value of output voltage | ||
| - | value = adc_get_average_value(0, 4); | + | value = adc_get_average_value(ADC_CHANNEL, 4); |
| // Conversing ADC value to distance | // Conversing ADC value to distance | ||
| distance = ir_distance_calculate_cm(GP2Y0A21YK, | distance = ir_distance_calculate_cm(GP2Y0A21YK, | ||
| + | |||
| + | lcd_gfx_goto_char_xy(1, | ||
| // Was the calculation successful? | // Was the calculation successful? | ||
| Line 132: | Line 132: | ||
| { | { | ||
| // Conversing distance to text | // Conversing distance to text | ||
| - | sprintf(text, | + | sprintf(text, |
| } | } | ||
| else | else | ||
| { | { | ||
| // Creating the text for unknown distance | // Creating the text for unknown distance | ||
| - | sprintf(text, | + | sprintf(text, |
| } | } | ||
| - | + | ||
| - | // Displaying the text on the LCD | + | |
| lcd_gfx_goto_char_xy(1, | lcd_gfx_goto_char_xy(1, | ||
| lcd_gfx_write_string(text); | lcd_gfx_write_string(text); | ||
| - | |||
| - | // Break | ||
| sw_delay_ms(500); | sw_delay_ms(500); | ||
| } | } | ||