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:41] – 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 55: | Line 58: | ||
| <code c> | <code c> | ||
| - | // | ||
| // The structure of the parameters of the IR distance sensors | // The structure of the parameters of the IR distance sensors | ||
| - | // | ||
| typedef const struct | typedef const struct | ||
| { | { | ||
| Line 66: | Line 67: | ||
| ir_distance_sensor; | ir_distance_sensor; | ||
| - | // | ||
| // The object of the parameters of GP2Y0A21YK sensor | // The object of the parameters of GP2Y0A21YK sensor | ||
| - | // | ||
| const ir_distance_sensor GP2Y0A21YK = { 5461, -17, 2 }; | const ir_distance_sensor GP2Y0A21YK = { 5461, -17, 2 }; | ||
| - | // | ||
| // Converting the values of the IR distance sensor to centimeters | // Converting the values of the IR distance sensor to centimeters | ||
| // Returns -1, if the conversion did not succeed | // Returns -1, if the conversion did not succeed | ||
| - | // | ||
| signed short ir_distance_calculate_cm(ir_distance_sensor sensor, | signed short ir_distance_calculate_cm(ir_distance_sensor sensor, | ||
| unsigned short adc_value) | unsigned short adc_value) | ||
| Line 90: | Line 87: | ||
| <code c> | <code c> | ||
| - | // | ||
| // The example program of the IR distance sensor of the HomeLab | // The example program of the IR distance sensor of the HomeLab | ||
| // Measured results in centimeters is displayed on the LCD | // Measured results in centimeters is displayed on the LCD | ||
| - | // | ||
| #include < | #include < | ||
| #include < | #include < | ||
| Line 100: | Line 95: | ||
| #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 130: | 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 142: | 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); | ||
| } | } | ||
| } | } | ||
| </ | </ | ||
| + | /* | ||
| ===== Extra materials ===== | ===== Extra materials ===== | ||
| * {{: | * {{: | ||
| * [[http:// | * [[http:// | ||
| + | |||
| + | */ | ||