<?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/tag/programming</link><description>Recent content in Programming on Bald Engineer</description><generator>Hugo</generator><language>en-US</language><lastBuildDate>Wed, 17 Aug 2016 11:50:14 +0000</lastBuildDate><atom:link href="https://www.baldengineer.com/tag/programming/index.xml" rel="self" type="application/rss+xml"/><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>Whenever someone sends me some code that doesn’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’t generate any compiler error.&#10;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.<p><img src="https://www.baldengineer.com/wp-content/uploads/2016/08/5-common-arduino-programming-mistakes.jpg" alt="5 Common Arduino Programming Mistakes"/></p></description></item><item><title>An example of when comments would have helped</title><link>https://www.baldengineer.com/comments-can-help.html</link><pubDate>Tue, 07 Oct 2014 14:00:05 +0000</pubDate><guid isPermaLink="false">https://www.baldengineer.com/?p=2679</guid><description>What was one of the first things you were taught, when learning to program? “Comment Your Code!” And of course, like all programming students, you ignored that advice. Or, if you are like me, you made vague comments as the lines of “variable called var.”&#10;Tonight I opened up some code I haven’t touched in two years. Code that when I wrote it, made perfect sense to me… at the time.<p><img src="https://www.baldengineer.com/wp-content/uploads/2012/09/comments_binboo_banner.png" alt="An example of when comments would have helped"/></p></description></item><item><title>"Post your code" to the style of "Let it Be"</title><link>https://www.baldengineer.com/post-your-code-to-the-style-of-let-it-be.html</link><pubDate>Mon, 09 Jun 2014 15:00:06 +0000</pubDate><guid isPermaLink="false">https://www.baldengineer.com/?p=2480</guid><description>A friendly reminder video to everyone asking for help in a programming forum.&#10;Lately I’ve been looking for ways to expand on the content in the AddOhms YouTube channel. One of the thoughts I had were taking some of the hard engineering lessons and turning them into a “Public Service Announcement”. You know, like those short films that would run on TV after school or NBC’s famous " The More You Know." Not something so dry, but something at least a little entertaining.<p><img src="https://www.baldengineer.com/wp-content/uploads/2012/09/post-your-code-banner.png" alt="&amp;#34;Post your code&amp;#34; to the style of &amp;#34;Let it Be&amp;#34;"/></p></description></item><item><title>Well formatted Arduino code in one click</title><link>https://www.baldengineer.com/one-click-clean-arduino-code.html</link><pubDate>Fri, 16 May 2014 15:00:06 +0000</pubDate><guid isPermaLink="false">https://www.baldengineer.com/?p=2416</guid><description>Warning, the title of this post is a little bit misleading. It isn’t just one-click to get cleanly formatted code, it’s actually two.&#10;There are two things seasoned programmers will tell new programmers, that they don’t understand (at first):&#10;Always comment your code Properly indent code The problem? As a new programmer, you might not know how to do #2 until you get some experience.&#10;How indenting helps The reason you indent code is to make it easier for a reader to understand the code’s flow. A compiler (well except in Python) doesn’t care if two lines line up or not. T he recent issues around Apple’s “goto fail;” are a great example of how indentation is meaningless to a C compiler.<p><img src="https://www.baldengineer.com/wp-content/uploads/2012/09/autoformat-banner.png" alt="Well formatted Arduino code in one click"/></p></description></item><item><title>Arduino: Assignments != Comparisons</title><link>https://www.baldengineer.com/arduino-assignments-do-not-equal-comparisons.html</link><pubDate>Thu, 03 Apr 2014 15:00:59 +0000</pubDate><guid isPermaLink="false">https://www.baldengineer.com/?p=2321</guid><description>There is one mistake that all C programmers make, regardless of experience. You’ll do it often when you get started, but never completely stop. The mistake? Confusing the “assignment” operator with the “comparison” operator. Take a look at this line of code:&#10;if (something = 0) { Notice the problem? If not, then you might fall into this common trap. The most annoying part? A C-Compiler won’t give an error. That code is legal C. Legal code doesn’t mean you’ll get the results you expect. In fact, this is probably not even close to what you wanted. This simple mistake and why it “works” is explained below.<p><img src="https://www.baldengineer.com/wp-content/uploads/2012/09/assignment-comparison-banner.png" alt="Arduino:  Assignments != Comparisons"/></p></description></item><item><title>When do you use the Arduino's Serial.flush()?</title><link>https://www.baldengineer.com/when-do-you-use-the-arduinos-to-use-serial-flush.html</link><pubDate>Thu, 02 Jan 2014 12:00:47 +0000</pubDate><guid isPermaLink="false">https://www.baldengineer.com/?p=2061</guid><description>In the Arduino library, the Serial object has a method called “flush().” Often users go throwing it into programs without fully understanding what it does. It doesn’t help that it’s functionality changed when version 1.0 of the Arduino IDE was released.&#10;Does Serial.flush() affect the Transmit Buffer or the Receive Buffer and when do you need to use it?&#10;What does Serial.flush() do? From the Arduino reference for Serial.flush (found on this page):<p><img src="https://www.baldengineer.com/wp-content/uploads/2012/09/when_to_use_flush_banner.png" alt="When do you use the Arduino&amp;#39;s Serial.flush()?"/></p></description></item><item><title>Arduino: De-Bounce a Button with micros() or millis()</title><link>https://www.baldengineer.com/arduino-de-bounce-a-button-with-micros.html</link><pubDate>Fri, 13 Dec 2013 12:25:45 +0000</pubDate><guid isPermaLink="false">https://www.baldengineer.com/?p=2095</guid><description>The Arduino is fast, humans are slow. When you push down a button, what seems like a single change to slow humans is really multiple presses to an Arduino. This is known as “bouncing.” Figure 1 is an oscilloscope screenshot showing what could happen when a button is pressed.&#10;Figure 1 - All the bouncing!&#10;The top trace shows the high-low-high transition of the button press. But notice all of the activity at the start of the high-to-low transition. The bottom trace is a “zoomed-in” view of that area. The button is actually transitioning multiple times before staying low. Internally the contacts are “bouncing.” This could result in false reads. In software, we can ignore these bounces by waiting some period of time after the first transition.<p><img src="https://www.baldengineer.com/wp-content/uploads/2013/12/MG_1763.jpg" alt="Arduino:  De-Bounce a Button with micros() or millis()"/></p></description></item><item><title>Arduino Millis() Examples</title><link>https://www.baldengineer.com/arduino-millis-examples.html</link><pubDate>Mon, 09 Dec 2013 23:00:51 +0000</pubDate><guid isPermaLink="false">https://www.baldengineer.com/?page_id=2055</guid><description>You’ve recently learned about millis() and can’t way to delete all of your references to delay(). The problem is that you don’t know quite how to convert your code into millis()-compatible code. Here is a (running) list of millis() examples I’ve put together to help.&#10;Baldengineer’s Arduino millis() Examples Arduino Multitasking - Step by step examples of how to convert delay() code into millis() based code, to simulate multitasking. Police Lights - Flash two LEDs like strobing police lights Control ON and OFF time for a flashing LED. - More control than “blink without delay” Stopwatch Example - Calculate how much time code takes to execute Chasing LEDs - Larson-scanner style chasing pattern De-bounch a button (or switch) - No need for de-bouncing capacitor Delayed events after a button push - Timed events (button push is just example.) analogWrite() PWM Fading - No delay() and a simple function to keep a LED fading with PWM Detect Short and Long Button Press - Give one button multiple functions You might also want to check out my “Blink Without Delay - Line by Line Tutorial.” It is a much more in-depth explanation than the comments provided with the Arduino IDE example.</description></item><item><title>The hidden Arduino Macro F() fixes random lock ups</title><link>https://www.baldengineer.com/arduino-f-macro.html</link><pubDate>Wed, 20 Nov 2013 12:25:33 +0000</pubDate><guid isPermaLink="false">https://www.baldengineer.com/?p=1995</guid><description>It’s 3am but you are not going to bed until you squash this last bug. You sprinkle Serial.print() statements everywhere you can think of, and then that’s when all hell breaks loose: Your code randomly locks up, the LEDs go crazy, and you’ve had it. What’s going on? You’ve run out of RAM!&#10;All of those Serial.print() statements are composed of c-style strings. Which are “constant character arrays.” In order for those arrays to work properly, they get loaded into the ATmega’s RAM before your code starts running. Which can be a problem because 2,048 bytes of RAM doesn’t allow for much.</description></item><item><title>Const vs #define - When do you use them and why?</title><link>https://www.baldengineer.com/const-vs-define-when-do-you-them-and-why.html</link><pubDate>Thu, 14 Nov 2013 11:13:40 +0000</pubDate><guid isPermaLink="false">https://www.baldengineer.com/?p=1944</guid><description>Arduino boards have microcontrollers with notoriously small amounts of RAM. The Uno only has 2,048 bytes of static RAM available for your program to store variables. So when you need to keep non-changing variables out of RAM which is best to use const or #define?&#10;What is #define #define is often misunderstood because it isn’t a programming statement. Instead, it sets up a macro which causes a text replace to occur before code is compiled. For example in this code:</description></item><item><title>Arduino: Sending and Receiving Multi-Digit Integers</title><link>https://www.baldengineer.com/arduino-multi-digit-integers.html</link><pubDate>Mon, 30 Jul 2012 09:18:32 +0000</pubDate><guid isPermaLink="false">https://www.baldengineer.com/?p=1105</guid><description>When Serial data is transmitted to an Arduino, it is sent one byte at a time. Even though you might type “123” in the Serial Monitor, that’s not quite what is sent. Instead the bytes “1” then “2” then “3” are sent. Once received into a buffer on the Arduino, these individual bytes need to be reassembled into something useful. The Arduino IDE provides Serial.readBytes() as one option. However, if you aren’t sure how many digits you are going to receive, this may not work. So another option is to use Serial.readBytesUntil() like so:<p><img src="https://www.baldengineer.com/wp-content/uploads/2012/07/Serial-Monitor-123.png" alt="Arduino:  Sending and Receiving Multi-Digit Integers"/></p></description></item><item><title>Arduino: Toggling Outputs</title><link>https://www.baldengineer.com/arduino-toggling-outputs.html</link><pubDate>Mon, 23 Jul 2012 09:00:13 +0000</pubDate><guid isPermaLink="false">https://www.baldengineer.com/?p=1072</guid><description>Did you know it is possible to toggle the state of a Arduino OUTPUT pin using a single line of code? It’s true! It’s also possible to use digitalRead() on an OUTPUT pin. Assuming pin 13 was set to output, this single line of code will cause the LED to change state (or flash) each time it is called:&#10;digitalWrite(13, !digitalRead(13)); Keep reading to understand how these two tricks work.<p><img src="https://www.baldengineer.com/wp-content/uploads/2012/07/picasion.com_59157f597d88f4db5519afff49288894.gif" alt="Arduino:  Toggling Outputs"/></p></description></item><item><title>Arduino: How do you reset millis() ?</title><link>https://www.baldengineer.com/arduino-how-do-you-reset-millis.html</link><pubDate>Mon, 16 Jul 2012 14:00:42 +0000</pubDate><guid isPermaLink="false">https://www.baldengineer.com/?p=861</guid><description>The quick answer to “How do you reset millis()” is: You Don’t! And here’s why: if you did, it would potentially break most libraries and functions that rely on it. Generally the reason people want to reset it, is that they are concerned about rollover. Instead of focusing on resetting millis(), here is how to use it correctly.&#10;Need to brush up on how millis() works? I’ve got a tutorial on how to effectively multi-task with millis() and another line-by-line tutorial on blink Without delay Avoiding rollover and checking how much time as passed is done in a single line:<p><img src="https://www.baldengineer.com/wp-content/uploads/2012/07/stopwatch_000018042439.jpg" alt="Arduino:  How do you reset millis() ?"/></p></description></item><item><title>Arduino Internal Pull-Up Resistor Tutorial</title><link>https://www.baldengineer.com/arduino-internal-pull-up-resistor-tutorial.html</link><pubDate>Fri, 30 Dec 2011 07:31:32 +0000</pubDate><guid isPermaLink="false">https://www.baldengineer.com/?page_id=704</guid><description>When hooking up switches or buttons to an Arduino I/O pin, sometimes the results might appear completely random. Sometimes it will appear as though there is a delay from when the button is pressed until the state of the pin actually changes. Other times the pin’s value will seem to randomly fluctuate from HIGH to LOW. Even more maddening might be as your finger gets closer to the switch, the pin’s state changes! The fix to these problems is simple: use the Arduino Internal Pull-up Resistor. Here’s how they can fix this problem and how you can use them with an Arduino board.<p><img src="https://www.baldengineer.com/wp-content/uploads/2011/12/youtube-jJnD6LdGmUo.jpg" alt="Arduino Internal Pull-Up Resistor Tutorial"/></p></description></item></channel></rss>