{"id":8045,"date":"2022-09-11T15:12:16","date_gmt":"2022-09-11T09:42:16","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-creating-a-program-that-slowly-increases-the-brightness-of-an-led-as-a-start-up\/"},"modified":"2022-09-11T15:12:16","modified_gmt":"2022-09-11T09:42:16","slug":"solved-creating-a-program-that-slowly-increases-the-brightness-of-an-led-as-a-start-up","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-creating-a-program-that-slowly-increases-the-brightness-of-an-led-as-a-start-up\/","title":{"rendered":"[Solved] Creating a Program that slowly Increases the brightness of an LED as a start-up"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-71986153\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"71986153\" data-parentid=\"71984742\" data-score=\"4\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>For pulse width modulation, you&#8217;d want to turn the LED off for a certain amount of time, then turn the LED on for a certain amount of time; where the amounts of time depend on how bright you want the LED to appear and the total period (&#8220;on time + off time&#8221;) is constant.<\/p>\n<p>In other words, you want a relationship like <code>period = on_time + off_time<\/code> where <code>period<\/code> is constant.<\/p>\n<p>You also want the LED to increase brightness slowly. E.g. maybe go from off to max. brightness over 10 seconds. This means you&#8217;ll need to loop <code>total_time \/ period<\/code> times.<\/p>\n<p>How bright the LED should be, and therefore how long the <code>on_time<\/code> should be, will depend on how much time has passed since the start of the 10 seconds (e.g. 0 microseconds at the start of the 10 seconds and <code>period<\/code> microseconds at the end of the 10 seconds). Once you know the <code>on_time<\/code> you can calculate <code>off_time<\/code> by rearranging that &#8220;<code>period = on_time + off_time<\/code>&#8221; formula.<\/p>\n<p>In C it might end up something like:<\/p>\n<pre><code>#define TOTAL_TIME    10000000               \/\/ 10 seconds, in microseconds\n#define PERIOD        1000                   \/\/ 1 millisecond, in microseconds\n#define LOOP_COUNT    (TOTAL_TIME \/ PERIOD)\n\n    int on_time;\n    int off_time;\n\n    for(int t = 0; t &lt; LOOP_COUNT; t++) {\n         on_time = period * t \/ LOOP_COUNT;\n         off_time = period - on_time;\n\n         turn_LED_off();\n         __delay_us(off_time);\n\n         turn_LED_on();\n         __delay_us(on_time);\n    }\n<\/code><\/pre>\n<p>Note: <code>on_time = period * t \/ LOOP_COUNT;<\/code> is a little tricky. You can think it as <code>on_time = period * (t \/ LOOP_COUNT);<\/code> where <code>t \/ LOOP_COUNT<\/code> is a fraction that goes from 0.00000 to 0.999999 representing the fraction of the period that the LED should be turned on, but if you wrote it like that the compiler will truncate the result of <code>t \/ LOOP_COUNT<\/code> to an integer (round it towards zero) so the result will be zero. When written like this; C will do the multiplication first, so it&#8217;ll behave like <code>on_time = (period * t) \/ LOOP_COUNT;<\/code> and truncation (or rounding) won&#8217;t be a problem. Sadly, doing the multiplication first solves one problem while possibly causing another problem &#8211; <code>period * t<\/code> might be too big for an <code>int<\/code> and might cause an overflow (especially on small embedded systems where an <code>int<\/code> could be 16 bits). You&#8217;ll have to figure out how big an <code>int<\/code> is for your computer (for the values you use &#8211; changing <code>TOTAL_TIME<\/code> or <code>PERIOD<\/code> with change the maximum value that <code>period * t<\/code> could be) and use something larger (e.g. a <code>long<\/code> maybe) if an <code>int<\/code> isn&#8217;t enough.<\/p>\n<p>You should also be aware that the timing won&#8217;t be exact, because it ignores time spent executing your code and ignores anything else the OS might be doing (IRQs, other programs using the CPU); so the &#8220;10 seconds&#8221; might actually be 10.5 seconds (or worse). To fix that you need something more complex than a <code>__delay_us()<\/code> function (e.g. some kind of <code>__delay_until(absolute_time)<\/code> maybe).<\/p>\n<p>Also; you might find that the LED doesn&#8217;t increase brightness linearly (e.g. it might slowly go from off to dull in 8 seconds then go from dull to max. brightness in 2 seconds). If that happens; you might need a lookup table and\/or more complex maths to correct it.<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\"><\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Creating a Program that slowly Increases the brightness of an LED as a start-up <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] For pulse width modulation, you&#8217;d want to turn the LED off for a certain amount of time, then turn the LED on for a certain amount of time; where the amounts of time depend on how bright you want the LED to appear and the total period (&#8220;on time + off time&#8221;) is constant. &#8230; <a title=\"[Solved] Creating a Program that slowly Increases the brightness of an LED as a start-up\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-creating-a-program-that-slowly-increases-the-brightness-of-an-led-as-a-start-up\/\" aria-label=\"More on [Solved] Creating a Program that slowly Increases the brightness of an LED as a start-up\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[324],"class_list":["post-8045","post","type-post","status-publish","format-standard","hentry","category-solved","tag-c"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Creating a Program that slowly Increases the brightness of an LED as a start-up - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-creating-a-program-that-slowly-increases-the-brightness-of-an-led-as-a-start-up\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Creating a Program that slowly Increases the brightness of an LED as a start-up - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] For pulse width modulation, you&#8217;d want to turn the LED off for a certain amount of time, then turn the LED on for a certain amount of time; where the amounts of time depend on how bright you want the LED to appear and the total period (&#8220;on time + off time&#8221;) is constant. ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-creating-a-program-that-slowly-increases-the-brightness-of-an-led-as-a-start-up\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-09-11T09:42:16+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-creating-a-program-that-slowly-increases-the-brightness-of-an-led-as-a-start-up\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-creating-a-program-that-slowly-increases-the-brightness-of-an-led-as-a-start-up\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Creating a Program that slowly Increases the brightness of an LED as a start-up\",\"datePublished\":\"2022-09-11T09:42:16+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-creating-a-program-that-slowly-increases-the-brightness-of-an-led-as-a-start-up\/\"},\"wordCount\":458,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"c++\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-creating-a-program-that-slowly-increases-the-brightness-of-an-led-as-a-start-up\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-creating-a-program-that-slowly-increases-the-brightness-of-an-led-as-a-start-up\/\",\"name\":\"[Solved] Creating a Program that slowly Increases the brightness of an LED as a start-up - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-09-11T09:42:16+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-creating-a-program-that-slowly-increases-the-brightness-of-an-led-as-a-start-up\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-creating-a-program-that-slowly-increases-the-brightness-of-an-led-as-a-start-up\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-creating-a-program-that-slowly-increases-the-brightness-of-an-led-as-a-start-up\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Creating a Program that slowly Increases the brightness of an LED as a start-up\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Creating a Program that slowly Increases the brightness of an LED as a start-up - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-creating-a-program-that-slowly-increases-the-brightness-of-an-led-as-a-start-up\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Creating a Program that slowly Increases the brightness of an LED as a start-up - JassWeb","og_description":"[ad_1] For pulse width modulation, you&#8217;d want to turn the LED off for a certain amount of time, then turn the LED on for a certain amount of time; where the amounts of time depend on how bright you want the LED to appear and the total period (&#8220;on time + off time&#8221;) is constant. ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-creating-a-program-that-slowly-increases-the-brightness-of-an-led-as-a-start-up\/","og_site_name":"JassWeb","article_published_time":"2022-09-11T09:42:16+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-creating-a-program-that-slowly-increases-the-brightness-of-an-led-as-a-start-up\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-creating-a-program-that-slowly-increases-the-brightness-of-an-led-as-a-start-up\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Creating a Program that slowly Increases the brightness of an LED as a start-up","datePublished":"2022-09-11T09:42:16+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-creating-a-program-that-slowly-increases-the-brightness-of-an-led-as-a-start-up\/"},"wordCount":458,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["c++"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-creating-a-program-that-slowly-increases-the-brightness-of-an-led-as-a-start-up\/","url":"https:\/\/jassweb.com\/solved\/solved-creating-a-program-that-slowly-increases-the-brightness-of-an-led-as-a-start-up\/","name":"[Solved] Creating a Program that slowly Increases the brightness of an LED as a start-up - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-09-11T09:42:16+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-creating-a-program-that-slowly-increases-the-brightness-of-an-led-as-a-start-up\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-creating-a-program-that-slowly-increases-the-brightness-of-an-led-as-a-start-up\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-creating-a-program-that-slowly-increases-the-brightness-of-an-led-as-a-start-up\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Creating a Program that slowly Increases the brightness of an LED as a start-up"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/8045","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=8045"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/8045\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=8045"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=8045"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=8045"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}