en:examples:sensor:color
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:examples:sensor:color [2012/06/06 23:24] – mikk.leini | en:examples:sensor:color [2020/07/20 12:00] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Color sensor ====== | ====== Color sensor ====== | ||
| - | //Necessary knowledge: [HW] [[en: | + | //Necessary knowledge: [HW] [[en: |
| ===== Theory ===== | ===== Theory ===== | ||
| - | [{{ : | + | [{{ : |
| - | [{{ : | + | The color sensor |
| - | [{{ : | + | |
| - | The color sensor | + | Measuring colors of the ingredients are basically two ways. The easiest way is to use a color-changing light source and a sensor |
| - | Measuring the colors of the ingredients are basically two ways. The easiest way is to use a color-changing light source and a sensor | + | [{{ :examples:sensor:color: |
| Color sensors have a variety of applications including detection of environment, | Color sensors have a variety of applications including detection of environment, | ||
| + | |||
| Line 20: | Line 20: | ||
| The ColorPAL from Parallax ([[http:// | The ColorPAL from Parallax ([[http:// | ||
| - | The light sensor used in the ColorPAL is a TSL13T, which has the following | + | The light sensor used in the ColorPAL is a TSL13T, which has the spectral sensitivity curve (taken from the TSL13T datasheet) as seen on the figure |
| - | {{: | + | [{{ : |
| - | The sensor | + | Sensor |
| - | The ColorPAL sensor requires only three connections +5V supply, ground and serial data. It can be plugged | + | ColorPAL sensor requires only three connections: +5 V supply, ground and serial data. It can be plugged into Robotic |
| + | |||
| + | [{{ : | ||
| + | |||
| + | Example code enabling to read the RGB values which is printed on the screen using hex format. Measurements with ColorPAL sensor will be made automatically. | ||
| <code c> | <code c> | ||
| #include < | #include < | ||
| #include < | #include < | ||
| - | #include < | ||
| #include < | #include < | ||
| #include < | #include < | ||
| - | // | ||
| // Determining USART interface. | // Determining USART interface. | ||
| - | // | ||
| usart port = USART(0); | usart port = USART(0); | ||
| - | // | + | // Main program |
| - | //Setting pins for buttons. | + | |
| - | // | + | |
| - | pin button1 = PIN(C, 0); | + | |
| - | pin button2 = PIN(C, 1); | + | |
| - | pin button3 = PIN(C, 2); | + | |
| - | + | ||
| - | + | ||
| - | // | + | |
| - | // Initialize | + | |
| - | // | + | |
| - | static inline void init() | + | |
| - | { | + | |
| - | // Button init | + | |
| - | pin_setup_input_with_pullup(button1); | + | |
| - | pin_setup_input_with_pullup(button2); | + | |
| - | pin_setup_input_with_pullup(button3); | + | |
| - | + | ||
| - | // LCD init | + | |
| - | lcd_gfx_init(); | + | |
| - | + | ||
| - | // Clear screen | + | |
| - | lcd_gfx_clear(); | + | |
| - | + | ||
| - | // Turn on backlight. | + | |
| - | lcd_gfx_backlight(true); | + | |
| - | + | ||
| - | // Print program | + | |
| - | lcd_gfx_goto_char_xy(3, | + | |
| - | lcd_gfx_write_string(" | + | |
| - | + | ||
| - | // The set-up of the USART interface. | + | |
| - | usart_init_async | + | |
| - | ( | + | |
| - | port, | + | |
| - | USART_DATABITS_8, | + | |
| - | USART_STOPBITS_ONE, | + | |
| - | USART_PARITY_NONE, | + | |
| - | USART_BAUDRATE_ASYNC(2400) | + | |
| - | ); | + | |
| - | } | + | |
| - | + | ||
| - | // | + | |
| - | //MAIN | + | |
| - | // | + | |
| int main(void) | int main(void) | ||
| { | { | ||
| - | unsigned char new_value1, old_value1 = 0, new_value2, | + | // Variable, which is stored in the read characters. |
| - | old_value2 = 0, new_value3, old_value3 = 0; | + | char c; |
| - | char c; | + | // Variables for each color text which is printed on the screen. |
| - | + | char red[7]; | |
| - | char text[7]; | + | char green[7]; |
| + | char blue[7]; | ||
| - | // Initialize | + | // Variable for all color data. |
| - | init(); | + | char data[9]; |
| - | lcd_gfx_goto_char_xy(3, 2); | + | // The set-up of the USART interface. |
| + | usart_init_async | ||
| + | ( | ||
| + | port, | ||
| + | USART_DATABITS_8, | ||
| + | USART_STOPBITS_ONE, | ||
| + | USART_PARITY_NONE, | ||
| + | USART_BAUDRATE_ASYNC(2400) | ||
| + | ); | ||
| - | while (1) | + | // LCD init |
| - | { | + | lcd_gfx_init(); |
| - | // Reads a button states | + | // Clear screen |
| - | new_value1 = pin_get_debounced_value(button1); | + | lcd_gfx_clear(); |
| - | new_value2 = pin_get_debounced_value(button2); | + | |
| - | new_value3 = pin_get_debounced_value(button3); | + | |
| - | + | ||
| - | // Button S1 is pressed | + | |
| - | if((!new_value1) && (old_value1)) | + | |
| - | { | + | |
| - | usart_send_string(port, | + | |
| - | } | + | |
| - | // Button S2 is pressed | + | // Turn on backlight. |
| - | if((!new_value2) && (old_value2)) | + | lcd_gfx_backlight(true); |
| - | { | + | |
| - | usart_send_string(port, " | + | |
| - | for(int i = 0; i < 14;i++) | + | // Print program name |
| - | { | + | lcd_gfx_goto_char_xy(3, 1); |
| - | while | + | lcd_gfx_write_string(" |
| - | + | ||
| - | c = usart_read_char(port); | + | |
| - | lcd_gfx_write_char(c); | + | |
| - | c=0; | + | |
| - | } | + | |
| - | } | + | while (1) |
| + | { | ||
| + | // Send a command to the sensor. | ||
| + | usart_send_string(port, | ||
| + | |||
| + | // There are 12 character to read (3 command and 9 data character). | ||
| + | for(int i = 0; i < 12; i++) | ||
| + | { | ||
| + | // Waiting for incoming data. | ||
| + | while (!usart_has_data(port)){} | ||
| + | |||
| + | // Read out the received character. | ||
| + | c = usart_read_char(port); | ||
| - | // Button S3 is pressed | + | // The first three characters (=m!), |
| - | if((!new_value3) && | + | // which we have sent, it is not necessary. |
| - | { | + | if(i>3) |
| - | lcd_gfx_clear(); | + | { |
| - | } | + | // Store the received data character. |
| + | data[i-3] = c; | ||
| + | } | ||
| + | } | ||
| + | |||
| + | // Converting the data to the suitable form | ||
| + | // and print it on the screen. | ||
| + | sprintf(red, " | ||
| + | lcd_gfx_goto_char_xy(0, 3); | ||
| + | lcd_gfx_write_string(red); | ||
| + | |||
| + | sprintf(green, " | ||
| + | lcd_gfx_goto_char_xy(0, | ||
| + | lcd_gfx_write_string(green); | ||
| + | |||
| + | sprintf(blue, | ||
| + | lcd_gfx_goto_char_xy(0, | ||
| + | lcd_gfx_write_string(blue); | ||
| - | + | // Delay | |
| - | // Remember last button state | + | sw_delay_ms(500); |
| - | old_value1 = new_value1; | + | |
| - | old_value2 = new_value2; | + | |
| - | old_value3 = new_value3; | + | |
| } | } | ||
| } | } | ||
| - | |||
| - | |||
| </ | </ | ||
en/examples/sensor/color.1339014290.txt.gz · Last modified: (external edit)
