Snippets and Sketches
A collection of contributed code snippets and sketches. You can add your own code here. For contributed Libraries see the LibraryList page here.
- DCF77 Clock - a binary DCF77 clock with good noise tolerance.
- The Clock - a DCF77 clock with extraordinary noise tolerance.
- Code.NTPclient - Low-memory footprint, scheduler-friendly NTP client
- Code.webclient - Low-memory footprint, scheduler-friendly web time client
Concurrency, Timing & Schedulers
- heartbeat sketch - use an array to create an irregular blink pattern
- Stopwatch Sketch: doing multiple things at the same time.
- A Delay Sketch: An easy to use "blink without delay" style sketch written to be used with the ease of "delay()".
- How to write reliable repeating and one off timers: Timers that don't use delay() but can cope with software that does delay the main loop().
- Multi Blink: Demonstrates a table driven approach to concurrency by blinking lots of LEDs at different speeds. The technique is extensible to many other situations.
- Timer/Interrupt Scheduler: Shows how to create a basic task scheduler using timers and interrupts.
- Simple Timer This is a simple way to execute functions at different intervals.
- Object Timeslicer: Uses object-oriented coding to create a network of encapsulated objects, each of which is given a time slice by loop(). The demo does some blink without delay, controlled by an analog input.
- Task Scheduler - by DL1DUZ: yet another simple Task Scheduler which does not use any extra timer (It can handle all types of tasks that are of practical interest, which are cyclical, single, conditional repetitive (repeat task only if ...) or non-linear (1st run -> repeat after 10s, 2nd run -> repeat after 25s, ...))
- SmartDelay - by nw-wind: A simple delay() replacement class which can allow non-blocking delays, pseudo-parallel execution etc. It is an arduino library, so just include & use.
- ZsuTT 2.5: This code provides the functionality of a remotely managed timer switch (eitchalthr) that can be set to switch on and off at time intervals occurring daily, hourly, or repeating every minute. This code is known to work with the Arduino UNO WiFi. As written it controls pins 4, 5, 6, 7 and is thus suitable for use with Seeedstudio's Relay Shield v3.0. It provides status and log information via MQTT.
Input / Output
- Full_Alpha-numeric_keyboard_Input using only 2 push buttons
- Hardware Patching - Change hardware usual function with software
- PWM Techniques
- PWMallPins - a sketch to get more PWM pins
- atmega_i2c_pwm_expander - sketch to use an ATmega as a PWM expander, controlled by I²C
- 8-bit PCM audio (on a PC speaker)
- Creating Variable frequencies for PWM @ anything from a 15Hz to 1MHz
- Create PWMs in-sync and out of sync Create PWMs in-sync and out of sync
- 8/9/10-bit compressed audio output @ 8k, 10k, 12.5k, 16k, 20k, 25k, 40k sample rate.
- Using the TLC5940 to run PWM at different frequencies
- Adjusting the TLC5940 libraries to give different PWM frequencies
- Using TLC5940 to give phase-shifted PWM Signals
- Arduino PWM on all pins - Software PWM example sketch that moves any pin between any 2 states (low, high, high impedance, or pull up)
- PCA9685 test code - Simple test code interfacing I2C with PCA9685 PWM chip.
- AnalogAndDigitalFireflies - a sketch to randomly turn LED's on/off using PWM for fading with both Analog and Digital PWM (Digital PWM implemented in software).
- WillOTheWisp - a sketch to fade 10 LED's on, make them pulse, then flicker, then fade off using PWM to simulate the Will o' the Wisp's from the movie "Brave" by Disney. (Digital PWM implemented in software).
- Data Handling
- Digital Potentiometers
- MAX5481 10-bit Digital Potentiometer Arduino Code A MAX5481 10-bit (1024-step) Digital Potentiometer example code showing how to drive the digital pot over SPI. Running the example and opening the serial monitor will print out a list of commands you can send the device. Enter the desired command into the serial monitor to set the pot to the desired output via SPI.
- Touchscreen Piano: Arduino IDE ver. 1.0.1 sketch for using a 2.8" Touch Shield on an Arduino Uno or Mega to make a little "piano".
- Adafruit Keyboard: Standard keyboard on a Adafruit TFT. (code is messy, but I will work on it)
- RickRoll: sketch that rickrolls people by playing the song "Never Gonna Give You Up" by Rick Astley on a piezo buzzer.
- Touchscreen Painting: Since 2012 some TFT-touch shields don't support the IRQ-pin anymore. Here's a little sketch for using one of these (ITeadstudio 2.4" TFT LCD Touch shield) without any library.
- Using multiple devices (including Arduinos!) on an I2C bus
- Getting multiple Serial channels with an Arduino
- ReadReceiver a sketch that uses Timer1 and PinChangeInt to read PPM output from a standard RC car Receiver
- EEPROM Load and Save - A sketch to Load and Save settings from/to the EEPROM
- EEPROM Wear Leveling An example for extending EEPROM endurance by wear leveling
- EEPROMutility - EEPROM management via the serial monitor. Allows reading and writing of any data type and testing.
- FindText: A simple function to search for some text within a string.
- tempToAscii - Convert a float to ASCII string with 3 decimal places
- printf() simple shell around vsnprintf() letting you use send printf style format strings over serial.
- findStrLen: a simple function to determine the length of any string by incrementing an index and moving down the string until the null terminator ("0", or "0x00"), is found.
- utf8ascii: a string conversion function for remapping utf8-encoding to extended ASCII characters. Necessary to show special characters on the serial display
- Parsing an HTTP request Using the in-place method.
- Prime number generation sketch
- smooth(data, filterVal, smoothedVal) - a low-pass filter smoothing function, useful for removing sensor jitter
- digitalSmooth(data, smoothArrayName) - a digital low-pass filter smoothing function, useful for removing sensor jitter, and glitches
- autoscale(int originalMin, int originalMax, int newBegin, int newEnd, int inputValue) - scale a variable between two ranges using linear interpolation
- fscale(float originalMin, float originalMax, float newBegin, float newEnd, float inputValue, float curve) - a function to map a variable between two floating point ranges. Can logarithmically favor either end of the output range.
- smoothstep - a function to ease from one value to the next with a smooth "s" shaped logarithmic curve.
- nonlinear mapping - reMap() allows to adapt a nonlinear function with line segments.
- multiMap - multiMap() allows to adapt functions with interpolation - similar as reMap() above - but this one is 100% integer to improve speed, and a bit more hardened.
- Random Card Draw - Example sketch that demonstrates how to select a number in a range in a random order, where all numbers in the range appear once. Kind of like drawing a card from a shuffled deck.
- randomHat() - choose every number from a "hat" before repeating
- A true random seed value - Example sketch that demonstrates how to seed the libraries pseudo-random number generator with a different random value every time the sketch runs. Very 'lightweight' technique.
- randomWalk(stepSize) - wander between endpoints at a specified rate
- freqout(freq, duration) - a simple frequency generator
- Musical Algo Fun - Sketch for making music with old PC speakers
- Measuring frequency with Arduino - A small and simple code snippet for 0 - 100 kHz range
- FFT tutorial: Learn how to implement Fast Fourier Transform in Arduino
- FIR filters tutorial: Learn how to implement FIR (Finite Impulse Response) filters in Arduino
- PID tutorial: learn how to use PID for temperature control
- Intersection of circles by the law of cosines. A common problem when triangulating distances.
- Logistical Curve soft motion Using the logistical function to produce a soft start and end for a servo's motion.