{"id":2169,"date":"2015-08-07T08:28:07","date_gmt":"2015-08-07T06:28:07","guid":{"rendered":"https:\/\/playembedded.org\/?p=2169"},"modified":"2021-07-29T11:00:50","modified_gmt":"2021-07-29T09:00:50","slug":"reading-a-slider","status":"publish","type":"post","link":"https:\/\/playembedded.org\/blog\/reading-a-slider\/","title":{"rendered":"Reading a Slider"},"content":{"rendered":"<h3 id=\"1_Slider_potentiometer\" class=\"level_1\">Slider potentiometer<\/h3>\n<p>Widely used in sound mixers, the slider can be used as input for many applications. There isn&rsquo;t so much to say as this device is made by two potentiometers working together. Indeed, when the cursor slides, it changes the position of two side potentiometers.<\/p>\n<h3 id=\"2_Pin_description\" class=\"level_1\">Pin description<\/h3>\n<figure id=\"attachment_2592\" aria-describedby=\"caption-attachment-2592\" style=\"width: 300px\" class=\"wp-caption alignright\"><a href=\"https:\/\/playembedded.org\/wp-content\/uploads\/2016\/04\/art_014_pinmap.jpg\" rel=\"attachment wp-att-2592\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-2592\" src=\"https:\/\/playembedded.org\/wp-content\/uploads\/2016\/04\/art_014_pinmap-300x114.jpg\" alt=\"Slider with pinmap\" width=\"300\" height=\"114\" srcset=\"https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/04\/art_014_pinmap-300x114.jpg 300w, https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/04\/art_014_pinmap-150x57.jpg 150w, https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/04\/art_014_pinmap-24x9.jpg 24w, https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/04\/art_014_pinmap-36x14.jpg 36w, https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/04\/art_014_pinmap-48x18.jpg 48w, https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/04\/art_014_pinmap.jpg 500w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\"><\/a><figcaption id=\"caption-attachment-2592\" class=\"wp-caption-text\">A photo of our slider showing its header connector with labels.<\/figcaption><\/figure>\n<p>The slider shown in Fig.1 has six pins:<\/p>\n<ol>\n<li><b>VCC<\/b>, (are two connected together) connected to power supply 3.0V DC;<\/li>\n<li><b>GND<\/b>, (are two connected together) connection to ground;<\/li>\n<li><b>OTA<\/b>, middle pin of potentiometer #1;<\/li>\n<li><b>OTB<\/b>, middle pin of potentiometer #2.<\/li>\n<\/ol>\n<p>We can sample both <b>OTA<\/b> and <b>OTB<\/b> making mean to better evaluate slider positioning.<\/p>\n<h3 id=\"3_Proposed_demo\" class=\"level_1\">Proposed demo<\/h3>\n<p>This demo is just an edit of one proposed in <a href=\"https:\/\/playembedded.org\/en\/2015\/02\/16\/sampling-and-dimming\/\">sampling and dimming<\/a>. Main edit is the on the conversion group as we need to read from two channels instead of one. We connected <b>OTA<\/b> to <b>PA0<\/b> (i.e. ADC1 IN0) and <b>OTB<\/b> to <b>PA1<\/b> (i.e. ADC1 IN1). We sample the sequence <b>OTA<\/b>, <b>OTB<\/b> 10 times so we need to provide a buffer of 16-bit unsigned which length is at least 20.<!--more--><\/p>\n<p>As for <a href=\"https:\/\/playembedded.org\/en\/2015\/02\/16\/sampling-and-dimming\/\">sampling and dimming<\/a> demonstrations we made the mean for sampled values and then we print the result using <b>chprintf()<\/b>.<\/p>\n<pre class=\"lang:c decode:true\">\/*\n    PLAY Embedded demos - Copyright (C) 2014-2016 Rocco Marco Guglielmi\n\n    This file is part of PLAY Embedded demos.\n\n    Licensed under the Apache License, Version 2.0 (the \"License\");\n    you may not use this file except in compliance with the License.\n    You may obtain a copy of the License at\n\n        http:\/\/www.apache.org\/licenses\/LICENSE-2.0\n\n    Unless required by applicable law or agreed to in writing, software\n    distributed under the License is distributed on an \"AS IS\" BASIS,\n    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n    See the License for the specific language governing permissions and\n    limitations under the License.\n*\/\n\n\/*\n    Tested under ChibiOS\/RT 3.0.1, Project version 1.0\n *\/\n#include \"ch.h\"\n#include \"hal.h\"\n#include \"chprintf.h\"\n\nBaseSequentialStream * chp = (BaseSequentialStream *) &amp;SD2;\nstatic int32_t mean;\nstatic bool flag = FALSE;\nstatic float lastvalue;\n\n\/*===========================================================================*\/\n\/* ADC related code                                                          *\/\n\/*===========================================================================*\/\n\/*\n * In this demo we want to use a single channel to sample voltage across\n * the potentiometer.\n *\/\n#define MY_NUM_CH                                              2\n#define MY_SAMPLING_NUMBER                                    10\n\nstatic adcsample_t sample_buff[MY_NUM_CH * MY_SAMPLING_NUMBER];\n\n\/*\n * ADC conversion group.\n * Mode:        Linear buffer, 10 samples of 2 channel, SW triggered.\n * Channels:    IN0 IN1.\n *\/\nstatic const ADCConversionGroup my_conversion_group = {\n  FALSE,                            \/*NOT CIRCULAR*\/\n  MY_NUM_CH,                        \/*NUMB OF CH*\/\n  NULL,                             \/*NO ADC CALLBACK*\/\n  NULL,                             \/*NO ADC ERROR CALLBACK*\/\n  0,                                \/* CR1 *\/\n  ADC_CR2_SWSTART,                  \/* CR2 *\/\n  0,                                \/* SMPR1 *\/\n  ADC_SMPR2_SMP_AN0(ADC_SAMPLE_144) |\n  ADC_SMPR2_SMP_AN4(ADC_SAMPLE_144),\/* SMPR2 *\/\n  ADC_SQR1_NUM_CH(MY_NUM_CH),       \/* SQR1 *\/\n  0,                                \/* SQR2 *\/\n  ADC_SQR3_SQ1_N(ADC_CHANNEL_IN0) |\n  ADC_SQR3_SQ2_N (ADC_CHANNEL_IN4)  \/* SQR3 *\/\n};\n\n\/*===========================================================================*\/\n\/* Common functions                                                          *\/\n\/*===========================================================================*\/\n\n\/*\n * Retrieve the integer part of value\n *\/\nstatic int32_t ftomod(float value){\n  if (value &gt;= 0)\n    return (int32_t) value;\n  else\n    return (int32_t) -1 * value;\n}\n\n\/*\n * Retrieve the decimal part of value\n *\/\nstatic uint32_t ftodp(float value) {\n\n  if (value &gt;= 0)\n    return (uint32_t) ((value - ftomod (value)) * 1000);\n  else\n    return (uint32_t) ((-value - ftomod (value)) * 1000);\n}\n\n\/*===========================================================================*\/\n\/* Generic code.                                                             *\/\n\/*===========================================================================*\/\n\nstatic THD_WORKING_AREA(waThd1, 256);\nstatic THD_FUNCTION(Thd1, arg) {\n\n  (void) arg;\n  chRegSetThreadName(\"Led handler\");\n  while(TRUE) {\n    palTogglePad(GPIOA, GPIOA_LED_GREEN);\n    chThdSleepMilliseconds(250);\n  }\n}\n\nstatic THD_WORKING_AREA(waThd2, 512);\nstatic THD_FUNCTION(Thd2, arg) {\n  unsigned ii;\n  (void) arg;\n  chRegSetThreadName(\"Led handler\");\n  \/*\n   * Activates the ADC1 driver.\n   *\/\n  adcStart(&amp;ADCD1, NULL);\n  while(TRUE) {\n    adcConvert(&amp;ADCD1, &amp;my_conversion_group, sample_buff, MY_SAMPLING_NUMBER);\n\n    \/* Making mean of sampled values. Note that samples refers to OTA and OTB\n       but since they we are looking for Rcm (common mode) we can make a simple\n       mean *\/\n    mean = 0;\n    for (ii = 0; ii &lt; MY_NUM_CH * MY_SAMPLING_NUMBER; ii++) {\n      mean += sample_buff[ii];\n    }\n    mean \/= MY_NUM_CH * MY_SAMPLING_NUMBER;\n    lastvalue = (float)mean * 3 \/ 4095;\n    flag = TRUE;\n  }\n}\n\n\n\n\/*\n * Application entry point.\n *\/\nint main(void) {\n\n  \/*\n   * System initializations.\n   * - HAL initialization, this also initializes the configured device drivers\n   *   and performs the board-specific initializations.\n   * - Kernel initialization, the main() function becomes a thread and the\n   *   RTOS is active.\n   *\/\n  halInit();\n  chSysInit();\n\n  \/*\n   * Setting up analog inputs used by the demo.\n   *\/\n  palSetPadMode(GPIOA, 0, PAL_MODE_INPUT_ANALOG);\n  palSetPadMode(GPIOA, 4, PAL_MODE_INPUT_ANALOG);\n  \/*\n   * Activates the serial driver 2 using the driver default configuration.\n   *\/\n  sdStart(&amp;SD2, NULL);\n\n  chThdCreateStatic(waThd1, sizeof(waThd1), NORMALPRIO + 1, Thd1, NULL);\n  chThdCreateStatic(waThd2, sizeof(waThd2), NORMALPRIO + 1, Thd2, NULL);\n  \/*\n   * Normal main() thread activity, in this demo it checks flag status. If flag\n   * is true, last value is printed and then flag is lowered. If error is true\n   * an error message is printed.\n   *\/\n  while (TRUE) {\n\n    if (flag) {\n      chprintf(chp, \"PLAY Embedded + ChibiOS\\\\RT: Slider demo \\r\\n\");\n      chprintf(chp, \"Last value: %d.%03.d V \\r\\n\", ftomod(lastvalue),\n               ftodp(lastvalue));\n      flag = FALSE;\n      chThdSleepMilliseconds(150);\n      chprintf(chp, \"\\033[2J\\033[1;1H\");\n    }\n    chThdSleepMilliseconds(1);\n  }\n}\n<\/pre>\n<h3 id=\"4_Project_download\" class=\"level_1\">Project download<\/h3>\n<p>The attached demo has been tested under ChibiOS 20.3.x.<\/p>\n<p><a href=\"https:\/\/playembedded.org\/wp-content\/uploads\/2021\/07\/RT-STM32F401RE-NUCLEO64-ADC-Slider-216.7z\" rel=\"\">RT-STM32F401RE-NUCLEO64-ADC-Slider-216<\/a><\/p>\n\n","protected":false},"excerpt":{"rendered":"<p>Slider potentiometer Widely used in sound mixers, the slider can be used as input for many applications. There isn&rsquo;t so much to say as this device is made by two potentiometers working together. Indeed, when the cursor slides, it changes the position of two side potentiometers. Pin description The slider shown in Fig.1 has six [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":2399,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1225],"tags":[],"coauthors":[241],"class_list":["post-2169","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-interfacing-externals","red"],"views":8185,"jetpack_featured_media_url":"https:\/\/playembedded.org\/blog\/wp-content\/uploads\/2016\/04\/Reading-a-Slider.jpg","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/playembedded.org\/blog\/wp-json\/wp\/v2\/posts\/2169","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/playembedded.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/playembedded.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/playembedded.org\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/playembedded.org\/blog\/wp-json\/wp\/v2\/comments?post=2169"}],"version-history":[{"count":0,"href":"https:\/\/playembedded.org\/blog\/wp-json\/wp\/v2\/posts\/2169\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/playembedded.org\/blog\/wp-json\/wp\/v2\/media\/2399"}],"wp:attachment":[{"href":"https:\/\/playembedded.org\/blog\/wp-json\/wp\/v2\/media?parent=2169"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/playembedded.org\/blog\/wp-json\/wp\/v2\/categories?post=2169"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/playembedded.org\/blog\/wp-json\/wp\/v2\/tags?post=2169"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/playembedded.org\/blog\/wp-json\/wp\/v2\/coauthors?post=2169"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}