Sunday, February 21, 2016

Syncing Oscilloscope To Interrupt Activity

Debugging interrupt service routines can be very tricky.  It's not like you can print out variabes to the serial port as you might when debugging code outside the interrupt.  One way you can can get some idea of what your code is doing in the ISR is to pulse one of the GPIO pins while in the ISR.  On the 16Mhz Arduino Uno this takes about 4 microseconds of overhead.  Another reason you might want to generate a pusle like this is to synchronize your oscilloscope with another event.
In the case of this experiment I wanted to see the clocking out of data to a 74HC595 shift register. 

This all started when I wanted to modify a sample sketch from the Sparkfun inventor kit to allow me to dynamically select and change the pattern being displayed on the 8 leds.  The experiment was set up so that the student was expected to un comment only the function to be used then re-upload the sketch to the arduino/redboard.

We had an extra Adafruit RGB LED Matrix that had an I2C interface and 5 menu buttons.  This worked very nicely because it only requires 2 pins from the redboard.  However, no matter what method you use to interact with the redboard in this example, you have the following problem:  The sketch as written uses delays between updating the led patterns.  In fact, the majority of what the sketch does is wait in delays.  If you try and add code that checks for key presses in the main loop of the program then you will have times that you need to hold the button or press the key for over half a second in order for the sketch to see it.   (See sketch that uses polling here)

There are are a few ways this can be solved.  1 - Have the keyboard / buttons generate an external interrupt to the redboard which cases a keyboard read and stores the key pressed in a buffer to be processed soon as the current led pattern function call is finished. (this would require additional hardware to route the physical interrupt signal). 2 - Set up a timer interrupt that polls the key for a keypressed, if one if found, set a flag and store the keypress for the sketch to process soon as the current led pattern function has finished.

Note that both 1 and 2 will involve some delay in the response time but will reduce the need of having the user hold the key or button until the redboard has a chance to check it.

A third way to do this is set up a interrupt that occurs ever millisecond, in the interrupt service routine you will check if the required time has elapsed and if so update the leds with the next pattern.  The main program of your loop will be devoted to interacting with the user.  Every millisecond the program will take 15 microseconds to decide if it needs to update the led patter on the 74HC595.  99% of the time it has nothing to do and returns.  When it does need to update the display it will take about 135 microseconds to shift the pattern out.

This does require rewriting the code.  No longer will you have functions you call for each pattern, but will keep a global variable telling the interrupt service routine which pattern is active and will decide how to do that within the interrupt service routine.  The advantage to doing this is the updating the display is happening in the background and allowing the main program to be more interactive with the user.

Here is a video of the project along with the code:  






Wednesday, February 10, 2016

Matrix keypad with 7 segment display (Arduino mini project)


3x4 Matrix keypad connected to an arduino. Displaying results of key pressed on a 7 segment led display via 74HC595 shift register which reduces the required pins of the 7seg display from 8 to 3.



I will be holding Arduino classes at Northbranch Library on the 2nd and 4th Saturdays .  Click Here For More Info. 

Come learn both theory and trouble shooting techniques.  If you cannot make it I can also skype.

Here is the video followed by the code used in this min project:




Monday, January 11, 2016

Arduino GPS LCD Clock

I built the Arduino GPS Clock by Tony DiCola over at Adafruit.  The clock was very cool, setting it's time from the satellites, however, I soon started feeling like I was wasting my GPS shield by only using it to get time.

So I decided to use the RGB LCD Shield which would allow me to display more information and be more interactive with the device.

At the time of this writing I have four boards stacked.  1) The arduino, 2) An Adafruit PowerBoost Shield with a 2000ma Lipo Battery, 3) The Adafruit Ultimate GPS Logger Shield and 4) the Adafruit RGB LCD Shield.

Having four boards stacked does make it bulky but I will be reducing that by dropping the Arduino Uno R3 board and replacing it's functionality with an Arduino Pro Mini placed in the prototype section of the GPS Logger Shield.

On the software side I currently have it displaying the time and date on its initial screen.  Other screens show "Latitude and Longitude", "Speed and Angle", Altitude and Satellites, then the Select button displays the message "This is a clock NOT a bomb. :-)

I plan to add some code to support logging GPS coordinates to the SD card.  Then I would like to try and implement Ladyada's logger which is interrupt driven to log data.  I would hope to accomplish a feature where background logging could be turned on and off.  The user in the foreground could set flags that would mark spots as important for the back ground logger to flag so that it could be recognized later when importing the data to google maps or some other gps mapping system.

Here is the current code on codebender

Lots to learn and try on this project.

UPDATE 4/10/16

The Arduino Pro Mini was put in place eliminating the need of the UNO R3.








Saturday, January 2, 2016

Adafruit Wave Shield

Here is a shield for Arduino that makes adding quality sound to your products easy.

It can play up to 22KHz, 12bit uncompressed audio files of any length. 

It's low cost, available as an easy-to-make kit. 

It has an onboard DAC, filter and op-amp for high quality output. 

Audio files are read off of an SD/MMC card, which are available at nearly any store. 

Volume can be controlled with the onboard thumbwheel potentiometer.

The shield comes with an Arduino library for easy use; simply drag uncompressed wave files onto the SD card and plug it in. Then use the library to play audio when buttons are pressed, or when a sensor goes off, or when serial data is received, etc. Audio is played asynchronously as an interrupt, so the Arduino can perform tasks while the audio is playing.

Click on the play button to watch a demo of the wave shield playing assorting audio through a small speaker.


    The shield comes with an Arduino library for easy use; simply drag uncompressed wave files onto the SD card and plug it in. Then use the library to play audio when buttons are pressed, or when a sensor goes off, or when serial data is received, etc. Audio is played asynchronously as an interrupt, so the Arduino can perform tasks while the audio is playing.

  • Can play any uncompressed 22KHz, 12bit, mono Wave (.wav) files of any size. While it isnt CD quality, it is certainly good enough to play music, have spoken word, or audio effects.
  • Output is mono, into L and R channels, standard 3.5mm headphone jack and a connection for a speaker that is switched on when the headphones are unplugged.
  • Files are read off of FAT16 formatted SD/MMC card.
  • Included library makes playing audio easy.

While the shield has been tested and works well, here are some points to keep in mind:
  • The audio playback library uses 10K of flash - so if you want to use an NG arduino, you'll need to upgrade to an Atmega168 chip.
  • About 600 bytes of SRAM are used to buffer the audio and keep track of file data, so RAM-heavy projects may not work well.
  • The shield can't play MP3, WMA, Ogg or other compressed audio files. It can only play uncompressed PCM/WAV files. Converting audio to WAV format is very easy, and is often the default format for many audio programs.
  • Files are stored as 8.3 name format, and can only be placed in the root directory. That means you can only have ~512 files (but they can be any size).
Ideas for what you can use it for...
  • Make a portable audio player
  • Use the AT&T text-to-speech site to make snippets of speech that you string together for a talking project, like..
  • Talking temperature sensor
  • Talking clock
  • Interfaces for sight-impared people
  • Doorbell that plays a cool tune
  • Jukebox/music-box that plays a song when its opened, or a coin is inserted
  • Security system that warns the intruder
  • Audio looper for musical effects and performances
  • Synthesizer with different sounds
  • Really freaky halloween props that scream
  • Display (like a point-of-sale box) that you can plug into to hear the message

Wednesday, December 30, 2015

LCD Button Shield V2 for Arduino by Sparkfun

The LCD Button Shield from Sparkfun Electronics provides a 16x2 LCD screens along with a keypad consisting of 5 keys — select, up, right, down and left. With this shield you will be able to move through menus and make selections straight from one board attached to your Arduino without requiring a massive tower of shields.

The LCD Button Shield V2 works perfectly in 4-bit mode with the “LiquidCrystal” library found in the Arduino IDE.

 NOTE: The pin hook up used by this shield is different from the samples in the library.  
  The circuit used in Arduino library is usually wired as:
 * LCD RS pin to digital pin 12
 * LCD Enable pin to digital pin 11
 * LCD D4 pin to digital pin 5
 * LCD D5 pin to digital pin 4
 * LCD D6 pin to digital pin 3
 * LCD D7 pin to digital pin 2
 * LCD R/W pin to ground
 * 10K resistor:
 * ends to +5V and ground
 * wiper to LCD VO pin (pin 3)
   The circuit on the shield is  wired as:
 * LCD RS pin to digital pin 8
 * LCD Enable pin to digital pin 9
 * LCD D4 pin to digital pin 4
 * LCD D5 pin to digital pin 5
 * LCD D6 pin to digital pin 6
 * LCD D7 pin to digital pin 7
 * LCD R/W pin to ground
 * 10K resistor:
 * ends to +5V and ground
 * wiper to LCD VO pin (pin 3)

The keypad consisting of 5 keys are arranged in a voltage ladder connected to A0.  Each key press produces a unique value ranging from 0 to 1023.  Pressing multiple keys together will generate unique values as well.  When I wrote the demo program to test the shield I would use the serial monitor to see the analogue values read for each key then I adjusted my code to accept values ranging within +- 10 of the value read.

Here is a short video of the shield with the demo code below:




Here is a link to pick up one of these shields for your next project
https://www.sparkfun.com/products/13293

Monday, December 28, 2015

Adding Second Joystick to Joystick Shield for Arduino

Added a second joystick to the shield and use one for forward/back motion and the other for turning.  I find this setup gives better control.

Jeff Branson, from Sparkfun, saw my project on facebook and suggested I submit it to Sparkfun to be added to their project section.  I said I would if I could get some better quality joysticks, like the ones Sparkfun sells :-).   I should be receiving those this week and will build up a nicer version and also add some additonal control buttons.

Below is a video of me driving the redbot using the dual joystick conroller.


Code used in controller:

Code Running Redbot:


Thursday, December 17, 2015

Wireless Arduino Thumb Joystick Shield

This shield provides the necessary connections between the joystick and the Arduino so that it can read the joysticks X, Y and Switch readings on it's analog inputs.  I chose to mount the joystick on it's own proto shield instead of mounting it to the prototype area of my XBee shield,

Looking at the picture to the left you can see three yellow wires that connect to A0, A1 and A2.  These correspond to the VRx, VRy and SW outputs from the joystick.

In the video I mount the shield to an XBee Wireless shield then mount that to the Arduino that acts as the brain for this wireless joystick controller.  The XBee shield has it's serial switch set so that it looks for serial data D2 and D3, requiring the use of the Software Serial library, leaving the hardware UART on the arduino free for programming and debugging.

The Arduino program polls the status of the joystick every 40 ms (about 20 times a second) then transmits the X, Y and Switch readings over via the XBee wireless module to an XBee wireless module on a Sparkfun redbot.  The program on the Sparkfun Redbot receives the values then makes the necessary adjustment to the power and direction of it's left and right wheels causing the redbot to go forward, backwards or turn.

Software on controller:

Software on Redbot

Learn Arduino
http://goo.gl/5Uv71f





[UPDATE 3/19/2016]

Controlling with one joystick felt kinda squirelly so I added a second one then adjusted the programming so one joystick was used for forward/backwards and the other for left right. This gave it much more stability and made it more fun to drive.  Shortly after completing the build Jeff Branson with sparkfun sent me some better joysticks so I built a second shield.  Here is a video I shot part way into the second build:


Here is a video of the redbot in action using the new and improved dual joystick xbee controller!