When do you use the Arduino’s Serial.flush()?

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.

Does Serial.flush() affect the Transmit Buffer or the Receive Buffer and when do you need to use it?

Continue Reading »

AddOhms #7 – Comparing Arduino and Raspberry Pi

The seventh AddOhms TutorialCast has gone “live”. (Gone “uploaded” sounds wrong.) Being able to understand difference between an Arduino and a Pi is a critical point for many new electronics hobbyist. The boards seem so similar, but they are so different.  AddOhms #7: Comparing the Arduino and Raspberry Pi

Continue Reading »

Arduino: Chasing LEDs with millis()

A popular LED project is the “Larson Scanner.”  This scanner emulates the effect seen on KIT from Knight Rider and the Cylons in Battlestar Galactica.  The code is usually written using “delay()” which means you can’t combine it with anything else.  The following code could be put into a function, called periodically and allow your code to scan while doing other things.

Continue Reading »

Arduino: Timing Code with Millis() as a Stopwatch

Since most Arduino boards do not have debug capability, this limits the programmer to using Serial.prints. A useful piece of information might be knowing how long certain parts of code are running. Here’s a simple example that demonstrations:

  1. How to properly use Serial.flush() (hint: it’s for TRANSMIT, not RECEIVE!)
  2. How long Serial.print()s can “tie up” the Arduino.

Continue Reading »

Arduino: Independent On-Off Times with Millis()

When using delay() to flash a LED there is a time for the LED to be on and then off. This makes it easy to have independent control of the “on” and “off” times. The standard blink without delay example doesn’t give you this flexibility.

millis-cookbook-two-duty-cycles

This example code gives you complete independant control of how long a LED (or any OUTPUT pin) stays “ON” or “OFF”. This also demonstrates a very simple two-state state machine.

The variable “LED13state” is used to track what should happen each time the millis() event fires.

Continue Reading »

Arduino Millis() Examples

Example code using millis()

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.  If you want to know when more are added make sure you subscribe to the RSS feed or email notification list.

Baldengineer’s Arduino millis() Examples

  1. Arduino Multitasking – Step by step examples of how to convert delay() code into millis() based code, to simulate multitasking.
  2. Police Lights – Flash two LEDs like strobing police lights
  3. Control ON and OFF time for a flashing LED. – More control than “blink without delay”
  4. Stopwatch Example – Calculate how much time code takes to execute
  5. Chasing LEDs – Larson-scanner style chasing pattern
  6. De-bounch a button (or switch) – No need for de-bouncing capacitor
  7. Delayed events after a button push – Timed events (button push is just example.)
  8. analogWrite() PWM Fading – No delay() and a simple function to keep a LED fading with PWM
  9. 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.