Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
en:examples:sensor:ir_distance [2015/11/10 10:46] heikopikneren: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: [HW] [[en:hardware:homelab:sensor]], [HW] [[en:hardware:homelab:digi]], [AVR] [[en:avr:adc]], [LIB] [[en:software:homelab:library:adc]], [LIB] [[en:software:homelab:library:module:lcd_graphic]], [LIB] [[en:software:homelab:library:module:sensor]]//+//Necessary knowledge:  
 +[HW] [[en:hardware:homelab:digi]],  
 +[AVR] [[en:avr:adc]], \\ 
 +[LIB] [[en:software:homelab:library:adc]], [LIB] [[en:software:homelab:library:module:lcd_graphic]], [LIB] [[en:software:homelab:library:module:sensor]]//
  
 ===== Theory ===== ===== Theory =====
Line 84: 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 <stdio.h> #include <stdio.h>
 #include <homelab/adc.h> #include <homelab/adc.h>
Line 94: Line 95:
 #include <homelab/module/lcd_gfx.h> #include <homelab/module/lcd_gfx.h>
  
-//+#define ADC_CHANNEL 0 
 // Main program // Main program
-// 
 int main(void) int main(void)
 {  {
- unsigned short value+ signed short valuedistance;
- signed short distance; +
  char text[16];  char text[16];
 +
 + // Robotic HomeLab II external sensors pin of Sensor module 
 + //pin ex_sensors = PIN(G, 0);
 + //pin_setup_output(ex_sensors);
 + //pin_set(ex_sensors);
   
- // 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,2);
- lcd_gfx_clear(); +
- +
- // Line selection +
- lcd_gfx_goto_char_xy(1,2); +
-  +
- // Name of the program+
  lcd_gfx_write_string("Distance sensor");  lcd_gfx_write_string("Distance sensor");
    
Line 124: 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, value);  distance = ir_distance_calculate_cm(GP2Y0A21YK, value);
 +
 +                lcd_gfx_goto_char_xy(1,3);
  
  // Was the calculation successful?  // Was the calculation successful?
Line 136: Line 132:
  {   {
  // Conversing distance to text  // Conversing distance to text
- sprintf(text, "%cm   ", distance);+ sprintf(text, "%3d cm   ", distance);
  }  }
  else  else
  {   {
  // Creating the text for unknown distance  // Creating the text for unknown distance
- sprintf(text, "? cm   ");+ sprintf(text, "Error   ");
  }  }
- +
- // Displaying the text on the LCD+
  lcd_gfx_goto_char_xy(1,3);  lcd_gfx_goto_char_xy(1,3);
  lcd_gfx_write_string(text);  lcd_gfx_write_string(text);
- 
- // Break 
  sw_delay_ms(500);  sw_delay_ms(500);
  }  }
 } }
 </code> </code>
 +/*
 ===== Extra materials ===== ===== Extra materials =====
  
   * {{:examples:sensor:ir_distance:ir_distance.ods|Graph of the Sharp GP2Y0A21YK sensor}}   * {{:examples:sensor:ir_distance:ir_distance.ods|Graph of the Sharp GP2Y0A21YK sensor}}
   * [[http://www.acroname.com/robotics/info/articles/irlinear/irlinear.html|Linearizing Sharp Ranger Data]]   * [[http://www.acroname.com/robotics/info/articles/irlinear/irlinear.html|Linearizing Sharp Ranger Data]]
 +
 +*/
en/examples/sensor/ir_distance.1447152378.txt.gz · Last modified: 2020/07/20 09:00 (external edit)
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0