<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Programming on Bald Engineer</title><link>https://www.baldengineer.com/category/tutorials/programming</link><description>Recent content in Programming on Bald Engineer</description><generator>Hugo</generator><language>en-US</language><lastBuildDate>Thu, 31 Mar 2022 05:05:29 +0000</lastBuildDate><atom:link href="https://www.baldengineer.com/category/tutorials/programming/index.xml" rel="self" type="application/rss+xml"/><item><title>7+ Python Engineering Modules for Electronics Engineers</title><link>https://www.baldengineer.com/7-python-engineering-modules.html</link><pubDate>Thu, 11 Jul 2019 12:50:23 +0000</pubDate><guid isPermaLink="false">https://www.baldengineer.com/?p=7907</guid><description>&lt;p&gt;Python is everywhere. Its capabilities continue to grow. Not only can you create simple scripts, but you can create full-blown applications with it. The core has been scaled down to run on 32-bit microcontrollers like the ESP32 and Adafruit Feather M0. You can even use Python engineer modules to design stuff like circuits. There are electronics Python modules that create schematics, simulate circuits, and make solving math a cinch. Here are some of the modules I found that make Python usable for (electronics) engineering.&lt;/p&gt;</description></item><item><title>Arduino millis() plus addition does not add up</title><link>https://www.baldengineer.com/arduino-millis-plus-addition-does-not-add-up.html</link><pubDate>Wed, 10 Oct 2018 13:00:23 +0000</pubDate><guid isPermaLink="false">https://www.baldengineer.com/?p=7571</guid><description>&lt;p&gt;In the past, I&amp;rsquo;ve covered how to reset Arduino millis() and have provided a growing list of examples using millis(). While reviewing the code for the &lt;a href="https://www.baldengineer.com/penguin-bot-from-elegoo-hands-on-review-of-the-kit-build.html"&gt;elegoo Penguin Bot&lt;/a&gt;, I was reminded of a millis() mistake I see often: addition. The only way to properly handle millis() rollover is with subtraction. Let&amp;rsquo;s look at why (and how.)&lt;/p&gt;&#10;&lt;h2 id="what-is-arduino-millis"&gt;What is Arduino millis()&lt;/h2&gt;&#10;&lt;p&gt;The Arduino library has a function called &lt;a href="https://www.arduino.cc/reference/en/language/functions/time/millis/"&gt;millis()&lt;/a&gt; which returns the number of milliseconds the processor has been running. On other platforms, you might see references to a &amp;ldquo;tick counter.&amp;rdquo; It is the same idea. A hardware timer keeps incrementing a counter at a known rate. In this case, that rate is milliseconds.&lt;/p&gt;</description></item><item><title>Five Arduino math fixes for when it is wrong</title><link>https://www.baldengineer.com/arduino-math-fixes.html</link><pubDate>Wed, 04 Jul 2018 13:16:52 +0000</pubDate><guid isPermaLink="false">https://www.baldengineer.com/?p=7463</guid><description>&lt;p&gt;While the Arduino library does an excellent job of hiding some of C/C++&amp;rsquo;s warts, at the end of the day, it is still just C/C++. This fact causes a few non-intuitive issues for inexperienced programmers. When it looks like Arduino math is wrong, it is probably one of these reasons.&lt;/p&gt;&#10;&lt;p&gt;When people ask me for help with their programming, I check each of these Arduino math mistakes. If your code seems to be hitting a bug, check to make sure it is not how the compiler handles math.&lt;/p&gt;</description></item><item><title>Arduino EEPROM stores any datatype</title><link>https://www.baldengineer.com/arduino-eeprom-stores-any-datatype.html</link><pubDate>Wed, 20 Jun 2018 13:00:46 +0000</pubDate><guid isPermaLink="false">https://www.baldengineer.com/?p=7381</guid><description>&lt;p&gt;Funny how a simple idea can spider out into multiple paths. Arduino EEPROM seemed like a straightforward concept. A few a years ago it was as easy as having either 512 or 1024 bytes of flash memory. The Arduino IDE offered an EEPROM library which let you read and write a single byte. Today, however, with many different processor architectures saving data to EEPROM varies. It is now possible to save any datatype to EEPROM but not on all boards and not all using the same method.&lt;/p&gt;</description></item><item><title>Circuit Python adds Python to Microcontrollers</title><link>https://www.baldengineer.com/circuit-python.html</link><pubDate>Wed, 14 Feb 2018 14:57:09 +0000</pubDate><guid isPermaLink="false">https://www.baldengineer.com/?p=7000</guid><description>&lt;p&gt;Back in 2013, a Kickstarter ran for a project to put a python interpreter on a microcontroller. At the time I could not see the benefit. Cool project, but I asked myself: &amp;ldquo;why?&amp;rdquo; On my last Adafruit order, I received a free &lt;a href="https://www.adafruit.com/product/3333"&gt;Circuit Playground Express.&lt;/a&gt; The board comes with CircuitPython pre-installed. After playing with &lt;a href="https://learn.adafruit.com/welcome-to-circuitpython/what-is-circuitpython"&gt;Circuit Python&lt;/a&gt;, or CP, I finally &amp;ldquo;get it.&amp;rdquo;&lt;/p&gt;&#10;&lt;p&gt;For Valentine&amp;rsquo;s Day, I made an animated LED heart for a new love in my life, Circuit Python. Well, love is a bit of a strong word. The past couple of weeks I have been l &lt;a href="https://learn.adafruit.com/welcome-to-circuitpython/what-is-circuitpython"&gt;earning Circuit Python&lt;/a&gt;, and I am excited by what it offers.&lt;/p&gt;</description></item><item><title>7 Arduino LCD display tips and tricks</title><link>https://www.baldengineer.com/arduino-lcd-display-tips.html</link><pubDate>Thu, 21 Dec 2017 17:30:03 +0000</pubDate><guid isPermaLink="false">https://www.baldengineer.com/?p=6860</guid><description>&lt;p&gt;When I started working on &lt;a href="https://www.baldengineer.com/reflow-oven.html"&gt;Open Vapors&lt;/a&gt;, I thought the stumbling point would be the PID algorithm or safe AC line control. However, it turned out; I spent a significant amount of time understanding how to print to the Arduino LCD display correctly.&lt;/p&gt;&#10;&lt;aside class="legacy-note legacy-note-dark" role="note"&gt;&lt;figure&gt;&lt;img src="https://www.baldengineer.com/wp-content/uploads/2017/12/httpsstatics3.seeedstudio.comimages104030001201-300x225.jpg"&#10;&#9;&#9;&#9;alt="Grove RGB character LCD display" width="300"&gt;&#10;&lt;/figure&gt;&#10;&lt;p&gt;If you need an easy to use RGB LCD, check out the &lt;a href="https://www.seeedstudio.com/Grove-LCD-RGB-Backlight.html"&gt;Grove LCD from SeeedStudio&lt;/a&gt;. They sent me one to check out. The LCD comes with Seeed&amp;rsquo;s &amp;ldquo;&lt;a href="http://wiki.seeedstudio.com/Grove_System/"&gt;grove connector system&lt;/a&gt;&amp;rdquo; which can connect to a variety of their Arduino-compatible boards. You can also pick up the &lt;a href="https://www.seeedstudio.com/Base-Shield-V2.html"&gt;Grove Base Shield&lt;/a&gt; which adds a variety of Grove connectors to an Arduino Uno. The Grove LCD makes it super easy to connect up a character LCD. It is very plug-and-play.&lt;/p&gt;</description></item><item><title>PWM a 3-pin PC fan with an Arduino</title><link>https://www.baldengineer.com/pwm-3-pin-pc-fan-arduino.html</link><pubDate>Wed, 05 Jul 2017 13:13:53 +0000</pubDate><guid isPermaLink="false">https://www.baldengineer.com/?p=6577</guid><description>&lt;p&gt;A question came up on IRC regarding how to PWM a 3-pin PC fan with an Arduino using analogWrite(). Controlling the fan was seemingly straightforward. The problem was that the hall effect sensor, or TACH signal, was incredibly noisy. The noise made it impossible to measure the fan&amp;rsquo;s rotation. Working through the question, I found three issues to tackle:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;You need to use a PNP transistor&lt;/li&gt;&#10;&lt;li&gt;Filter capacitors help&lt;/li&gt;&#10;&lt;li&gt;Create a non-blocking RPM measurement (with millis())&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;This post addresses all three issues regarding how to PWM a 3-pin PC fan with an Arduino.&lt;/p&gt;</description></item><item><title>Soft Power Controller State Machine</title><link>https://www.baldengineer.com/software-power-controller-state-machine.html</link><pubDate>Wed, 21 Jun 2017 13:00:43 +0000</pubDate><guid isPermaLink="false">https://www.baldengineer.com/?p=6516</guid><description>&lt;p&gt;Previously I looked at the hardware needed to build a Raspberry Pi soft power supply. This week I&amp;rsquo;m looking the state machine for the microcontroller. Why is such a complicated circuit necessary? I am replacing a &lt;a href="https://www.hackster.io/baldengineer/retropie-in-a-super-famicom"&gt;Super Famicom (SNES) motherboard&lt;/a&gt; with the Pi. The trick is, I want to use the original power switch to turn the Pi on and off.&lt;/p&gt;&#10;&lt;p&gt;This requirement presents a problem. When the switch goes into the &amp;ldquo;OFF&amp;rdquo; position, power needs to stay on long enough for the Pi to properly shutdown. So the switch itself can&amp;rsquo;t provide power to the Pi directly. With minor changes, the code in this state machine could be made to work with push buttons as well. If I add that feature in the future, I&amp;rsquo;ll update the code on the &lt;a href="https://github.com/baldengineer/Raspberry-Pi-Soft-Power-Controller"&gt;RPSPC GitHub project&lt;/a&gt;.&lt;/p&gt;</description></item><item><title>Arduino Data Logger: Serial Monitor Alternatives</title><link>https://www.baldengineer.com/arduino-data-logger-options.html</link><pubDate>Wed, 05 Apr 2017 13:00:56 +0000</pubDate><guid isPermaLink="false">https://www.baldengineer.com/?p=6396</guid><description>&lt;p&gt;The Arduino serial monitor is usable when you want to watch data from an Arduino. However, it does not have a built-in method for saving the data. Here are some ideas if you want to build an Arduino data logger with or without a PC.&lt;/p&gt;&#10;&lt;h2 id="important-note-on-arduino-data-logger-examples"&gt;Important note on Arduino Data Logger examples&lt;/h2&gt;&#10;&lt;p&gt;With all of these examples, please remember that whenever you open the Arduino&amp;rsquo;s serial port, &lt;a href="http://playground.arduino.cc/Main/DisablingAutoResetOnSerialConnection"&gt;the board will reset&lt;/a&gt;. So if your log file shows &amp;ldquo;Initializing SD card…&amp;rdquo; with a few data lines in between, it is because there is a reset happening.&lt;/p&gt;</description></item><item><title>Sending simple serial commands to an Arduino</title><link>https://www.baldengineer.com/simple-serial-commands-arduino.html</link><pubDate>Wed, 01 Mar 2017 15:00:16 +0000</pubDate><guid isPermaLink="false">https://www.baldengineer.com/?p=6376</guid><description>&lt;p&gt;Sending simple serial commands to an Arduino is the easiest way to communicate between an Arduino and a computer. The computer could be a PC, a Raspberry Pi, or any device that communicates with serial.&lt;/p&gt;&#10;&lt;p&gt;By sending and &amp;ldquo;decoding&amp;rdquo; a single character it is easy to add a simple debug menu or even serial menu. Plus, it is easy to extend.&lt;/p&gt;&#10;&lt;h2 id="single-character-vs-full-words"&gt;Single Character vs. Full Words&lt;/h2&gt;&#10;&lt;p&gt;The mistake I see many people make is that they try to send full-text strings as serial commands. For example, to turn on a LED, I have seen (silly) commands like &amp;ldquo;RED LED ON&amp;rdquo; or &amp;ldquo;RED LED OFF.&amp;rdquo; While you could use something like strcmp(), as I showed on the Multiple MQTT Topics example, that tends to be overkill for most serial commands.&lt;/p&gt;</description></item><item><title>Multiple MQTT Topics with Arduino PubSubClient</title><link>https://www.baldengineer.com/multiple-mqtt-topics-pubsubclient.html</link><pubDate>Wed, 01 Feb 2017 13:00:22 +0000</pubDate><guid isPermaLink="false">https://www.baldengineer.com/?p=6348</guid><description>&lt;p&gt;In my &lt;a href="https://www.baldengineer.com/mqtt-tutorial.html"&gt;Arduino MQTT Examples&lt;/a&gt;, I kept things simple by only subscribing to a single topic. One of the strengths of &lt;a href="https://www.baldengineer.com/mqtt-introduction.html"&gt;MQTT&lt;/a&gt; is that a device can subscribe (or publish) to multiple topics. The broker will sort things out. Even though my first example only showed one, it is straight forward to get the &lt;a href="https://github.com/knolleary/pubsubclient"&gt;Arduino PubSubClient library&lt;/a&gt; to subscribe to Multiple MQTT topics.&lt;/p&gt;&#10;&lt;p&gt;The quick answer is that you need to look at the MQTT response to find out which topic sent the payload.&lt;/p&gt;</description></item><item><title>Fading LED: analogWrite millis() Example</title><link>https://www.baldengineer.com/fading-led-analogwrite-millis-example.html</link><pubDate>Wed, 23 Nov 2016 14:34:21 +0000</pubDate><guid isPermaLink="false">https://www.baldengineer.com/?p=6250</guid><description>&lt;p&gt;It&amp;rsquo;s a well-known fact of engineering: LEDs make everything look better. And that means a Fading LED is &lt;em&gt;even&lt;/em&gt; better. Using Arduino&amp;rsquo;s &lt;a href="https://www.arduino.cc/en/Reference/AnalogWrite"&gt;analogWrite&lt;/a&gt;(), fading a LED is just a matter of a loop. If you use delay(), you can&amp;rsquo;t easily add other actions. What can you do? Well, Fading a LED with &lt;a href="https://www.baldengineer.com/millis-cookbook.html"&gt;millis&lt;/a&gt;() is pretty simple. Here&amp;rsquo;s the code to do it and a quick explanation.&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;//pwm-fade-with-millis.ino&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// Example Fading LED with analogWrite and millis()&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// See baldengineer.com/fading-led-analogwrite-millis-example.html for more information&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// Created by James Lewis&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="n"&gt;byte&lt;/span&gt; &lt;span class="n"&gt;pwmLED&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// define directions for LED fade&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="cp"&gt;#define UP 0&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="cp"&gt;#define DOWN 1&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// constants for min and max PWM&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;minPWM&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;const&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;maxPWM&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;255&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// State Variable for Fade Direction&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;byte&lt;/span&gt; &lt;span class="n"&gt;fadeDirection&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;UP&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// Global Fade Value&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// but be bigger than byte and signed, for rollover&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;fadeValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// How smooth to fade?&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;byte&lt;/span&gt; &lt;span class="n"&gt;fadeIncrement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// millis() timing Variable, just for fading&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;previousFadeMillis&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;// How fast to increment?&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;fadeInterval&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;setup&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="c1"&gt;// put pwmLED into known state (off)&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nf"&gt;analogWrite&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pwmLED&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fadeValue&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;doTheFade&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;thisMillis&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="c1"&gt;// is it time to update yet?&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="c1"&gt;// if not, nothing happens&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;thisMillis&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;previousFadeMillis&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;fadeInterval&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="c1"&gt;// yup, it&amp;#39;s time!&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fadeDirection&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;UP&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;fadeValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fadeValue&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;fadeIncrement&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fadeValue&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;maxPWM&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="c1"&gt;// At max, limit and change direction&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;fadeValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;maxPWM&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;fadeDirection&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;DOWN&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;}&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="c1"&gt;//if we aren&amp;#39;t going up, we&amp;#39;re going down&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;fadeValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fadeValue&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;fadeIncrement&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fadeValue&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;minPWM&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="c1"&gt;// At min, limit and change direction&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;fadeValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;minPWM&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;fadeDirection&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;UP&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;}&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;}&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="c1"&gt;// Only need to update when it changes&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nf"&gt;analogWrite&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pwmLED&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fadeValue&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="c1"&gt;// reset millis for the next iteration (fade timer only)&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;previousFadeMillis&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;thisMillis&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;}&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;loop&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="c1"&gt;// get the current time, for this time around loop&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="c1"&gt;// all millis() timer checks will use this time stamp&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="kt"&gt;unsigned&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;currentMillis&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;millis&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nf"&gt;doTheFade&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;currentMillis&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id="global-variables-for-fading-led"&gt;Global Variables for Fading LED&lt;/h2&gt;&#10;&lt;h3 id="constants"&gt;Constants&lt;/h3&gt;&#10;&lt;p&gt;The constant &amp;ldquo;pwmLED&amp;rdquo; is the pin that gets faded. (Make that pin supports analogWrite().) The two #define statements, UP and DOWN, are simple states for the LED. They make the code easier to read. The &amp;ldquo;maxPWM&amp;rdquo; value is set to 255 for 8-bit AVR boards like the Uno. If you&amp;rsquo;re using the ESP8266, set this value to 1023. (You can also use the constant &amp;ldquo;PWMRANGE.&amp;rdquo;)&lt;/p&gt;</description></item><item><title>Measure PWM Current with a Modified Moving Average</title><link>https://www.baldengineer.com/measure-pwm-current.html</link><pubDate>Wed, 28 Sep 2016 13:00:16 +0000</pubDate><guid isPermaLink="false">https://www.baldengineer.com/?p=6217</guid><description>&lt;p&gt;Pulse Width Modulation (PWM) makes it possible to dim lights, control the speed of motors, and (with the help of filters) generate analog reference voltages. When measuring the voltage or current of a PWM signal, there are unique challenges. You can use this tutorial to measure PWM current with a modified moving average (MMA).&lt;/p&gt;&#10;&lt;p&gt;As I&amp;rsquo;ve mentioned in a few posts, I&amp;rsquo;m working on an RGB LED IoT controller. Soon I&amp;rsquo;ll post some detailed information on that. In the meantime, one choice I made was to include an &lt;a href="http://www.ti.com/lit/ds/symlink/ina219.pdf"&gt;INA219 current sensor&lt;/a&gt; on the LED power supply. For prototyping, I&amp;rsquo;m using the &lt;a href="https://www.adafruit.com/product/904"&gt;Adafruit Breakout Board&lt;/a&gt;, but my final design uses the same parts integrated into a custom PCB.&lt;/p&gt;</description></item><item><title>5 Common Arduino Programming Mistakes</title><link>https://www.baldengineer.com/5-common-arduino-programming-mistakes.html</link><pubDate>Wed, 17 Aug 2016 13:39:41 +0000</pubDate><guid isPermaLink="false">https://www.baldengineer.com/?p=6145</guid><description>&lt;p&gt;Whenever someone sends me some code that doesn&amp;rsquo;t work, there are a few common Arduino programming mistakes that I check. Some of these mistakes I make myself.  In most cases my code will compile just fine. Sometimes, these mistakes won&amp;rsquo;t generate any compiler error.&lt;/p&gt;&#10;&lt;p&gt;When my Arduino code is acting up, these are the first things I check. Here are my 5 common Arduino programming mistakes, I use to debug non-working code.&lt;/p&gt;</description></item><item><title>Raspberry Pi GUI Tutorial</title><link>https://www.baldengineer.com/raspberry-pi-gui-tutorial.html</link><pubDate>Wed, 03 Feb 2016 17:58:53 +0000</pubDate><guid isPermaLink="false">https://www.baldengineer.com/?p=5750</guid><description>&lt;p&gt;My favorite Raspberry Pi add-on is the &lt;a href="https://www.adafruit.com/products/2097"&gt;PiTFT from Adafruit&lt;/a&gt;. With it, you easily get a Raspberry Pi GUI interface and touch screen. The &lt;a href="https://learn.adafruit.com/adafruit-pitft-28-inch-resistive-touchscreen-display-raspberry-pi/easy-install-2"&gt;PiTFT software install&lt;/a&gt; is just a few things and it is good to go.&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://www.baldengineer.com/wp-content/uploads/2016/02/adafruit-pitft-2097-05-300x225.jpg"&#10;&#9;&#9;&#9;alt="Adafruit PiTFT - Click for more info"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;Adafruit PiTFT - Click for more info&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;This screen is what I needed for my IoT project. The Pi+Screen will act as the primary controller for all of my things. The problem is I didn&amp;rsquo;t know much about writing GUI applications in Linux. So what could I do to create a Raspberry Pi GUI?&lt;/p&gt;</description></item><item><title>Use Arduino millis() with buttons to delay events</title><link>https://www.baldengineer.com/use-millis-with-buttons-to-delay-events.html</link><pubDate>Wed, 27 Jan 2016 16:00:16 +0000</pubDate><guid isPermaLink="false">https://www.baldengineer.com/?p=5720</guid><description>&lt;p&gt;One of the common questions related to using the millis() function in Arduino, is around timed events. After an event occurs, you want the code to wait for some time before doing the next step. But you don&amp;rsquo;t want to stop the program with delay().&lt;/p&gt;&#10;&lt;p&gt;In this example, we will use millis() to wait a few seconds after a pushbutton press to turn on an LED. Then a few seconds later, we will turn it off.  All without using delay().&lt;/p&gt;</description></item><item><title>Proper Larson Scanner with Software PWM (Repost)</title><link>https://www.baldengineer.com/proper-larson-scanner-with-sw-pwm.html</link><pubDate>Wed, 21 Oct 2015 16:00:03 +0000</pubDate><guid isPermaLink="false">https://www.baldengineer.com/?p=5501</guid><description>&lt;p&gt;Last week I had a &lt;a href="https://www.baldengineer.com/software-pwm-with-millis.html"&gt;detailed Arduino tutorial on software pulse width modulation&lt;/a&gt; using millis() and micros(). Why? Because I wanted to create a Proper Larson Scanner, with persistence and at least 8 LEDs.&lt;/p&gt;&#10;&lt;figure&gt;&lt;img src="https://www.baldengineer.com/wp-content/uploads/2015/10/knight-rider-season-3-218x300.jpg"&#10;&#9;&#9;&#9;alt="KITT larson scanner example"&gt;&lt;figcaption&gt;&#10;&#9;&#9;&#9;&lt;p&gt;KITT larson scanner example&lt;/p&gt;&#10;&#9;&#9;&lt;/figcaption&gt;&#10;&lt;/figure&gt;&#10;&#10;&lt;p&gt;Even though it is a popular project for the Arduino Uno, most Larson scanner tutorials, &lt;a href="https://www.baldengineer.com/arduino-chasing-leds-with-millis.html"&gt;like my first one&lt;/a&gt;, have a few flaws. First, there is no persistence, or tail, to the LED as it moves back and forth. Persistence could be solved by using pulse-width-modulation. The Uno and other 328p-based micros only have 6 Pulse Width Modulation (PWM) pins. And let&amp;rsquo;s be honest, every project is made better by adding more LEDs. :)&lt;/p&gt;</description></item><item><title>Arduino Software PWM with millis()</title><link>https://www.baldengineer.com/software-pwm-with-millis.html</link><pubDate>Wed, 07 Oct 2015 13:00:04 +0000</pubDate><guid isPermaLink="false">https://www.baldengineer.com/?p=5478</guid><description>&lt;p&gt;The Arduino Uno has six pins dedicated to Pulse Width Modulation (PWM). PWM is great for analog-like control for the speed of motors or LED fading. But what if you want to control more than 6 devices? Or what if you&amp;rsquo;re using the PWM pins to control servo motors, but still want to fade an LED on a 7th pin?&lt;/p&gt;&#10;&lt;p&gt;One option is to change boards and processors. For example, you could move up to the Arduino Mega 2560. That means a bigger board and more cost.&lt;/p&gt;</description></item><item><title>Blink without delay() explained line-by-line</title><link>https://www.baldengineer.com/blink-without-delay-explained.html</link><pubDate>Wed, 05 Aug 2015 13:00:27 +0000</pubDate><guid isPermaLink="false">https://www.baldengineer.com/?p=5275</guid><description>&lt;p&gt;Making the move to millis()-based code can be daunting. You have to rethink your logic, implement flags, program a state machine — and more importantly, start using millis(). Generally in forums and on IRC people will just point to the &amp;ldquo;blink without delay&amp;rdquo; example, hoping the commented code is enough for a new user. It&amp;rsquo;s not enough.&lt;/p&gt;&#10;&lt;p&gt;I have a growing list of millis()-based tasks posted in my millis() cookbook. But sometimes that those examples might be too simple or not close enough to your project&amp;rsquo;s end target. That got me thinking about different ways to help explain how millis() works. I thought &amp;ldquo;wow, you need to understand every line of an example&amp;rdquo; which leads me to: line-by-line.&lt;/p&gt;</description></item><item><title>Microcontroller state machine with enum tutorial</title><link>https://www.baldengineer.com/state-machine-with-enum-tutorial.html</link><pubDate>Wed, 29 Jul 2015 13:00:07 +0000</pubDate><guid isPermaLink="false">https://www.baldengineer.com/?p=5251</guid><description>&lt;p&gt;Flag variables are great, &lt;a href="https://www.baldengineer.com/flag-variables-are-not-evil-for-microcontrollers.html"&gt;and totally not evil&lt;/a&gt;, when you just have two states: ON or OFF. What about when you have multiple states? Is there an option better than creating multiple flag variables?&lt;/p&gt;&#10;&lt;p&gt;The C-language has a declaration type just for this purpose. It is called an &lt;a href="https://en.wikipedia.org/wiki/Enumerated_type"&gt;enumeration&lt;/a&gt;, or enum.&lt;/p&gt;&#10;&lt;p&gt;Setting up a state machine with enum is a surprisingly simple. Arduino and embedded programmers should use them!&lt;/p&gt;&#10;&lt;p&gt;All you need to do is create descriptive tag names, and let the compiler assign them an integer value. Unlike a #define which is just a macro replacement, the compiler treats an enum as your personal variable type.&lt;/p&gt;</description></item><item><title>Flag variables are not evil (for Microcontrollers)</title><link>https://www.baldengineer.com/flag-variables-are-not-evil-for-microcontrollers.html</link><pubDate>Wed, 15 Jul 2015 13:00:13 +0000</pubDate><guid isPermaLink="false">https://www.baldengineer.com/?p=5206</guid><description>&lt;p&gt;Getting programming questions answered on the internet can be problematic. Programmers love to have opinions, stick to those ideas, and express them to you even when their opinion has nothing to do with your question(s).&lt;/p&gt;&#10;&lt;p&gt;Not only am I going to explain how to use flag variables in your code, I am going to encourage their use—which most programmers avoid.&lt;/p&gt;&#10;&lt;p&gt;However, this advice comes with two caveats.&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;This information only applies to limited resource environments like an Arduino, LaunchPad or PIC.&lt;/li&gt;&#10;&lt;li&gt;Use flag variables very carefully when you do use them.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;The following &lt;a href="https://www.baldengineer.com/flag-variables-are-not-evil-for-microcontrollers.html"&gt;flag variable usage examples&lt;/a&gt; are Arduino-centric but apply to any microcontroller platform, including the Energia project for TI Launchpads.&amp;gt; Flag variables are a Good Thing(tm) &amp;ndash; when used properly&lt;/p&gt;</description></item><item><title>Pushbutton and Flashing LED tutorial with millis()</title><link>https://www.baldengineer.com/pushbutton-and-flashing-led-tutorial-with-millis.html</link><pubDate>Wed, 06 May 2015 14:00:21 +0000</pubDate><guid isPermaLink="false">https://www.baldengineer.com/?p=4930</guid><description>&lt;h2 id="how-to-blink-or-flasha-led-without-delay-and-detect-button-pushes"&gt;How to blink (or flash) a LED without delay() and detect button pushes&lt;/h2&gt;&#10;&lt;p&gt;One of the limitations of the delay() function is that nothing else can really be done. This presents a problem when you want to flash a LED while waiting for a pushbutton to be pressed.&lt;/p&gt;&#10;&lt;p&gt;Flashing the LED with millis() and using a flag variable to find if the LED should be flashing solves this problem.&lt;/p&gt;&#10;&lt;p&gt;Consider this another example to my &lt;a href="https://www.baldengineer.com/millis-cookbook.html" title="Arduino: Millis() Cookbook"&gt;virtual millis() cookbook&lt;/a&gt;.&lt;/p&gt;</description></item><item><title>Case Study in Optimization: Faster SPI on AVRs</title><link>https://www.baldengineer.com/case-study-in-optimization-faster-spi-on-avrs.html</link><pubDate>Mon, 30 Mar 2015 13:03:48 +0000</pubDate><guid isPermaLink="false">https://www.baldengineer.com/?post_type=link&amp;p=4781</guid><description>&lt;p&gt;The Arduino Library provides functions like &lt;a href="http://arduino.cc/en/Reference/shiftOut" title="Arduino reference for shiftOut()"&gt;shiftOut()&lt;/a&gt; and &lt;a href="http://arduino.cc/en/Reference/digitalWrite" title="Arduino Reference for digitalWrite()"&gt;digitalWrite()&lt;/a&gt;.  These functions are simple and effective, but they are slow. Of course, they&amp;rsquo;re doing a lot more than just toggling bits. Faster isn&amp;rsquo;t always necessary and can sometimes lead to more difficult debugging.  And as &lt;a href="http://c2.com/cgi/wiki?DonKnuth" title="Background on author Donald Knuth"&gt;Donald Knuth&lt;/a&gt; said,&lt;/p&gt;&#10;&lt;blockquote&gt;&#10;&lt;p&gt;&amp;hellip;premature optimization is the root of all evil.&lt;/p&gt;&#10;&lt;/blockquote&gt;&#10;&lt;p&gt;So what happens, when you do need to optimize? For example, if shiftOut() is too slow for your project, what do you do?  In Ralph&amp;rsquo;s post, &lt;a href="http://nerdralph.blogspot.com/2015/03/fastest-avr-software-spi-in-west.html" title="Optimized SPI Code for AVRs"&gt;Fastest AVR software SPI in the West&lt;/a&gt;, he breaks down different SPI code implementations into their assembly code.&lt;/p&gt;</description></item></channel></rss>