Show Posts
Pages: [1] 2 3 ... 5
1  Using Arduino / Audio / Re: Got a VLSI VS1000 Audio Module working with serial control. on: December 17, 2013, 03:07:16 am
I sent data between 2 UNO's using software serial on both. Some characters corrupted. Yes, I tried more than once and did change jumpers. Soldered, it may be okay. 57600 worked solid as rock. End of story.

Ah! I wonder if you can reduce the VS1000 to 57600...hmmm, might have to look into that!
Still losing 2 pins though. On the other hand, I could pass off some of the "pin work" like LCD driving, to the VS1000.
Swings and roundabouts!

I used a MEGA to get the VS1000 playing right.

For a one off, to test, it might be worth investing. But the difference between an Uno-compatible Pro Mini at £2 and the cheapest Mega 2560 clone at £16 is an 8x increase in price for that part, making the project unviable. So I'd rather avoid that route if possible!

Thanks for the info though.
2  Using Arduino / Audio / Re: Got a VLSI VS1000 Audio Module working with serial control. on: December 16, 2013, 12:48:06 pm
I can't get reliable 115200 using software serial.
I've been looking at a fair few projects connecting Arduino UNO and VS1000 and they all seem to use SoftwareSerial. The guide says:

"HardwareSerial - Best performance. Always use this first, if available!".


" The ATmega168 (328) provides UART TTL serial communication, which is available on digital pins 0 (RX) and 1 (TX). The Arduino software includes a serial monitor which allows simple textual data to be sent to and from the Arduino board via a USB connection."

But... what if I was using a Pro Mini (schematic)? I thought for a moment this might work, as the board doesn't direct pins 0 and 1 through the USB converted chip  as there isn't one

Then I saw a comment on the Pro Mini spec page which said:
digital pins 0 and 1 go to rx and tx for serial communication. they can't be used if you use the bootloader, but I would assume you could use them if you get rid of the bootloader.

I don't even understand that - why would the bootloader disable hardware serial? Does it? Or is that nonsense?

But then again, I saw this which appears to suggest you can even use the Uno R3 for hardware serial:

I can't think of a reason why your peripheral UART and the USB-UART would interfere. You probably want to disconnect you UART peripheral during programming, as it may interfere with programming.

I realise this is in danger of drifting off into a more general serial hardware question, but basically:

Arduino Pro Mini + VS1000 + hardwareSerial: Yes or no?

BTW, yes, I'm aware of the AltSoft serial library, but that still only goes to 57600 and besides, "Consumes a 16 bit timer (and will not work with any libraries which need that timer)" - which I need.
3  General Category / General Discussion / Re: Arduino - Coding without delays using program Ticks on: December 09, 2013, 02:54:55 pm
Let's start with the symptom...
Why would "a couple glow very dimly"?

Ah, slight red herring there. The two (very, very faintly) glowing pins are on 11 and 12, MOSI/MISO, breadboarded to an Adafruit level shifter for the SPI. Remove that link and they go out.

What if I/O pin zero was configured as an output that was turned on?  That line of code, meant to toggle an LED on I/O pin 5, would also turn off the output on I/O pin zero!  Not good.
Does that help?

Sort of. Well, yes. And no. In an explanation theory way, yes. In practice, well....

Taking what you said, 0x21 = 0b00100001, with the 1's representing pins 8(PB0) and 13(PB5) ?

So do you mean something along the lines of adding

pinMode(8, OUTPUT);
 digitalWrite(8, HIGH);

to the top of my sketch?

If I understand what you're saying, pin8(PB0) should be affected - I couldn't replicate that. The light on pin8 (PB0) just stays on.

In fact, just for good measure, I even changed it to    

wired them all up, all came on and stayed on except pins 13 and 12 which happily blinked away in their 3/2 time.

Possible I might have misunderstood you there. All I can say is - I most definitely didn't come up with the 2-byte-saving technique myself! I just noted it under "top tips".

I've been searching my history for where I got that "  PINB |= _BV (5) is better than PINB = _BV (5) " technique from. All I can say is it wasn't a Nick Gammon tip (he can breath a sigh of relief!) but that around the same time on that evening, I visited various sites including and and  and

And fortunately, a copy-and-paste of "the secret lies in using PINB instead of PORTB" led me to too which is similar, but not the same.

If I DO find the exact link I got it from, I'll post back here.
4  General Category / General Discussion / Re: Arduino - Coding without delays using program Ticks on: December 09, 2013, 11:01:20 am
You have done an excellent job investigating.  You have even identified the symptom.  Try slowly carefully rereading your last post.  Compare what happened with what you expected to happen.  If the light bulb doesn't come on I will put you out of your misery and explain.

OK, I give up! Step by step, here's each stage:

The OP posted a technique for "how to add delays to your program without actually delaying your program".

As others noted, this might be susceptible to other things going on in the loop.
I thought it might also consume a few cycles checking for a count every time the loop runs.

So, having followed some of Nick Gammon's tutorials on on both PWM timers and direct port manipulation, the Crossroad's post about bit-flipping (which also led me to a tip about saving a couple of bytes by "OR-ing") and then being guided toward the TimerOne library, I put all three together and offered an alternative in Timer1.attachInterrupt() function.
I'd been using this library and function for a few days with great success as I needed to fire off an SPI byte to another 328 not only quickly, but regularly too.

What I expected the code to do was to switch the LED on and off once a second. What it did was switch the LED on and off once a second.

You then wrote:

void blink() {
  PINB |= _BV (5);  // digitalWrite(13, !digitalRead(13));
Almost works as expected.  You're off by one character.

And westfw wrote:

(also, consider putting some LEDs on the other PORTB pins and watch what they do. )

I explained that that also behaved in the way I expected.

In fact, to put it all together, this will pulse the LED on and off completely unaffected by the delay in blinking an LED on pin 12, and I've even made the PIN12 LED flash at a different rate so you have a sort of "3/2 time beat" so you can see that neither LED is affected when the other changes state.

#include <TimerOne.h>

void setup() {
  DDRB |= _BV (5); // pinMode (13, OUTPUT);
  pinMode(12, OUTPUT);
  Timer1.attachInterrupt(blink, 500000);

void loop() {
  digitalWrite(12, HIGH);
  digitalWrite(12, LOW);

void blink() {
  PINB |= _BV (5); // digitalRead (13);

All is behaving exactly as I'd expect. So going back to your quote:

Compare what happened with what you expected to happen.  If the light bulb doesn't come on I will put you out of your misery and explain.

OK, the light bulb is coming on, and in the way I expect. As it also did in response to westfw's request.

I did wonder, for a short moment, if it was because I was using "blink()", even though I was redefining it, but changing it to anything else worked just the same, so it wasn't that either.

So, "put me out of my misery" and explain how it "almost works as expected" and which character I am "off by one of"?

I'm genuinely intrigued!
5  General Category / General Discussion / Re: Arduino - Coding without delays using program Ticks on: December 07, 2013, 05:02:44 am
Try expanding the expression.

I'm not sure I follow. I'm "OR"-ing bit 5 of PORTB with whatever bit 5 of PORTB currently is.

This will cause it to flip. Well, it will according to CrossRoads and Nick Gammon....

writing a logic one to a bit in the PINx Register, will result in a toggle in the corresponding bit in the Data Register

And it seems to do so. The only thing I could do differently is to put
 PINB = _BV (5);
instead of
 PINB |= _BV (5);

but while the end result is the same, it uses 2 more bytes. So it wouldn't be that, either.
I also just tried & and ^ in case there was some big byte-saving technique I wasn't aware of, but, as I expected, that doesn't work either.

(also, consider putting some LEDs on the other PORTB pins and watch what they do. )

OK, a couple glow very dimly, the rest are all off. As I'd expect, as I've not set them to be or do anything. Not sure why I would for this exercise, but if I make them output and low, then all go out and stay out.
If I make them outputs and high, they stay bright while pin 13 flashes.
As I expect, as I'm only addressing pin13. Should they be doing anything different?

OK, I give up. Hands in the air - I know your point is meant to make me think it out for myself, but I've spent an hour puzzling over this since you told me my code "almost works", but was "off by one character". I'm missing the knowledge gap that's allowing me to fill in the blanks here.

This (now thoroughly confused!) newbie is throwing in the towel and shamelessly begging for a clear explanation smiley
6  General Category / General Discussion / Re: Arduino - Coding without delays using program Ticks on: December 06, 2013, 09:02:49 pm
void blink() {
  PINB |= _BV (5);  // digitalWrite(13, !digitalRead(13));

Almost works as expected.  You're off by one character.
Huh??!? I'm looking at the board flashing away right in front of me - been running like that with that code since I posted it.

What am I missing? Which character? What happens when your run it? What do you mean by "almost" as expected?

EDIT: Just copied that code directly from the forum into  a new sketch, flashed it again just in case there was a forum character encoding glitch, and the LED blinks on and of once per second. This is what I expected. Clearly you're not getting the same result. What's happening to your LED? Which board are you using? I'm on the Atmega328-based Uno.
7  General Category / General Discussion / Re: Arduino - Coding without delays using program Ticks on: December 06, 2013, 06:57:15 pm
You might also be interested in

attachInterrupt(function, period)
Calls a function at the specified interval in microseconds. Be careful about trying to execute too complicated of an interrupt at too high of a frequency, or the CPU may never enter the main loop and your program will 'lock up'. Note that you can optionally set the period with this function if you include a value in microseconds as the last parameter when you call it.

Completely removes the need for anything in the loop.

Give it a go:

#include <TimerOne.h>

void setup() {
  DDRB |= _BV (5); //pinMode(13, OUTPUT);
  Timer1.attachInterrupt(blink, 500000);

void loop() {

void blink() {
  PINB |= _BV (5);  // digitalWrite(13, !digitalRead(13));

Also has the advantage of operating completely independently of "the loop", leaving it free for other things.

Not saying it's a better way, but another different way that might serve a different purpose.

Incidentally, I used this opportunity to "big up" that nice Mr Nick Gammon's post listing the direct port numbers. I have it pinned on my wall for permanent reference!

Simply replacing:

pinMode(13, OUTPUT);
DDRB |= _BV (5);

digitalWrite(13, !digitalRead(13));
PINB |= _BV (5); 

takes it from 1,478 to 904 bytes saving 574 bytes. Reasonably significant.  (Your example is 980 bytes, so actually smaller code without "fiddling"!).
8  General Category / General Discussion / Any Google Chrome wizards fancy making a forum-helping app? on: December 06, 2013, 06:09:46 pm
Any Google Chrome wizards fancy making a forum-helping app? Let me explain!

A couple of forums I'm sure we all use - avrfreaks and this one - are a bit tricky to get working if found via a Google search.

Google is indexing the wrong version of the forum (not Google's fault, it's a server-side issue).

Here are two examples - the first is what Google has indexed, the next is what it should be.

In this Arduino forum case, all we need to do is remove ;wap2 off the end of the URL.;wap2

In the avr-freaks case, printview has to be changed to viewtopic

The other thing here is that the code block renderer in the playground is STILL broken (eg:  - "Uncaught ReferenceError: $ is not defined").
I know everyone's busy and it doesn't look like anyone's got the time to fix the one line serverside, so I thought, why not fix it client-side?

Apparently, the client-side URL-rewrites can be done with this:

And the jQuery error could probably be fixed along the lines of the jQuerify or jQuery debugger.

But I really don't have a clue where to start! I'm not requesting or demanding anyone write one, just saying if someone happens to be good with extensions and happens to know how to do one quickly and easily, I'm sure I'm not the only one who'd appreciate it!
9  Using Arduino / Project Guidance / Re: Play mp3 from usb flash drive on: December 05, 2013, 05:07:47 pm
This topic caught my eye as I want to do something similar with the VS1000 module*.

It says:

Default firmware has four functionalities:
usbmass - makes SPI flash memory visible as USB Mass Storage when USB is connected to PC
spiplayer - plays music files from SPI FLASH
sdplayer - plays music (Ogg Vorbis or WAV) from SD card or makes SD card visible as USB Mass Storage when USB is connected to PC
sdupdate program - updates firmware or content of the SPI FLASH from the SD card

Now, before you get too excited, like I did, I checked and that doesn't mean you can play music from a USB stick.
It means you can mount the device to appear as a USB device, then copy music onto it, then unmount USB and play the music.
Besides, it doesn't have a full size USB socket anyway.

But I thought, there must be some easy combination of USB breakout board, Arduino and VS1000 module that will allow something along the following lines:

Insert USB with audio tracks on.
Breakout board detects stick and signals arduino which then switches VS1000 into mass storage mode and initiates a copy/overwrite of everything from the stick to the microSD (assuming enough space of course).
When finished, Arduino signals VS1000 to switch back to playback mode, and plays track 1.

So, I've been looking around at USB breakout boards. For example:

The price is right, all under £10, but as you'd expect from "that place", documentation and support is zero, and by the looks of it, the connections are going to require pretty much all the Arduino pins even if I could find some documentation.

EDIT: Just found this - $25, or $20 for the Pro Mini version. Sadly only 3.3v available though. Might be worth checking out!

I'd be willing to spend a little more if there was some kind of USB host to SPI breakout/converter thing, but hours on ebay and electronics forums and other places really hasn't turned up anything that seems suitable. Any ideas?

*Yes, I know it's ogg-only, and it works out at €20+tax and shipping each for 1 (not worth it) or €5+Tax and shipping for 100.
Which is then very good value, but I'm not quite that enthusiastic to need 100, but I got given 2, so why not use 'em?!
10  Community / Website and Forum / Port Manipulation page needs correcting or maybe clarifying WRT A6 and A7 on: December 05, 2013, 09:04:49 am
Been having a bit of a fight with pins A6 and A7 on my Pro Mini. Started searching and at first found various conflicting posts and information: says:
PORTC maps to Arduino analog pins 0 to 5. Pins 6 & 7 are only accessible on the Arduino Mini

DDRC - The Port C Data Direction Register - read/write
PORTC - The Port C Data Register - read/write
PINC - The Port C Input Pins Register - read only

Unless everyone I've pasted below saying 6 and 7 are purely analog input only and have no digital buffer, are wrong, then it would probably help to clarify the line above and rewrite it to:

Pins 6 & 7 are only accessible on the Arduino Mini and are analog inputs only and therefore can only be read with analogRead()

Here's why I was getting confused:

ADC6 and ADC7 can be used if you read and write directly to the registers.
They can also be used with the Arduino libraries if you use a Arduino Pro Mini compatible board.

So you can use them?

This has already been answered above, port C bit 6 is the reset pin, port C bit 7 is
not used at all.  Standard Arduinos do not configure port C bit 6 for use so that
the pin can be used as RESET.

Analog inputs 6 and 7 have nothing at all to do with port C or any other port.

They don't? But the Arduino guide says they do!

You can always use analogRead (6) and analogRead (7), which should just work.

And no, you can't _control_ ADC6 or 7, they are only wired to the analog multiplexer, so have no output
capability.  In fact port C bit 6 can be mapped to the RESET pin if the chip is appropriated fused, and port C
bit 7 is unassigned.  The standard bootloader keeps the reset pin as a reset signal so bits 6 and 7 of port
C are not used.

The analog inputs 0..5 share pins with digital port C bits 0..5, hence they can be set as digital I/O.

Ah, that seems a bit more definitive.

So the two extra SMD pins can be read with AnalogRead(6) for ADC6 and AnalogRead(7) for ADC7?  Sounds great - will give it a shot!

Make sure you review boards.txt and select a board type that uses "eightanaloginputs"
Such as:

A6 and A7 are 100% analog only pins and they are only available onboards with the surface mount version of the ATmega328.
Don't try to treat them as digital inputs.

OK, starting to get a consensus among the old-timers  here:)  I'll go with that.

And the docs says ( P266)

Note that ADC pins ADC7 and ADC6 do not have digital input buffers, and therefore do not
require Digital Input Disable bits.

And finally, joy of joys:

Except for A6, A7 on surface mount '328P equipped boards when they are broken out.  Those are analog input only.
Even those work well for reading switches.
The other day I connected a rotary encoder *and* two push-buttons to A6/A7 (with suitable resistors to give different voltage levels).

Fantastic! In which case, assuming that's right, I can move the rotary encoder to these two analogue read-only pins, freeing up two digital output pins, meaning I have exactly the right amount of pins needed and no extra shift registers or port expanders  required.

The bottom line is:

ADC6 and ADC7 are not reserved for any other purpose, they are exactly like the other analog pins, except that they are only analog inputs (cannot also be used as digital pins)

Therefore, unless anyone disagrees, might it be an idea to clarify that port manipulation page?
11  Using Arduino / Project Guidance / Re: Arduino UNO Pinout Diagram on: December 05, 2013, 06:42:39 am
Can't find anyone else mentioning this, and as I've just ordered the book, I'm hoping that the version I've just downloaded off the website has been corrected. But this is what I just downloaded 5 minutes ago from the link sent.... very confusing!

12  Using Arduino / Programming Questions / Re: Use of timer2 and SPI at the same time on Arduino Uno on: December 02, 2013, 05:41:59 pm
In my case, I want to use pin 11 as MISO pin for the SPI communication.
Which does not involve PWM.
Glad I saw this post - I was about to abandon thoughts  of using the rather neat looking timer1 library because I saw this issue:
The TimerOne library can't be used together with either the Servo or the SPI library.
In the case of Servo, TimerOne wins, whereas in the case of SPI, SPI wins and TimerOne won't function.

EDIT/UPDATE:I can answer my own question and save anyone else confusion - it DOES work.
OK, to clarify, using the attachInterrupt function of this library appears to work fine with SPI. Try this code...

And the reason it didn't work first time? Turned out the slave select wire to the slave was loose! As soon as I stuck the logic analyzer on it, I saw the problem, and everything fine!

I also found this forum post:

SPI in loop() working fine, but not in ISR TIMER 1 interrupt

"I might be wrong, but does the spi library rely on interrupts ? If so it will not work inside an interrupt service routine because interrupts are disabled temporarily while an isr runs".

Is he wrong? and

If you use SPI both in an ISR and in the loop() function then you must disable interrupts around all uses of SPI not in the ISR

Is that correct?

The Timer1 is triggering the interrupt at just the right times, and everything is looking good.

Except, I am seeing corruption on the SPI. I can only assume that it is due to the interrupt for Timer1 firing during the SPI transfer. If I disable the timer and its interrupt I get no problems with the SPI transfer at all.

In my case, I'm not trying to use any of the PWM pins or functions, simply:

attachInterrupt(function, period)
Calls a function at the specified interval in microseconds.

I just want to use attachInterrupt to trigger SPI to poll for some data on a slave Arduino ten times a second (ie: 10Hz). As you say, there's no PWM involved, so this should be fairly safe to use, yes?

Before anyone points out the obvious: Yes, I could do it in the loop.

Or I could use a library like Metro:
"Because Metro does not use interrupts, you have to "check" the Metro regularly (at least every millisecond)."

Or SimpleTimer.
The base goal is to be able to execute a particular piece of code every n milliseconds, without using interrupts.

The algorithm looks like this:

lastMillis = 0
forever do:
    if (millis() - lastMillis > n)
        call the particular piece of code
        lastMillis = millis()

Or Simon Monk's Timer library.
All of which use "the loop".

But I don't want to block a dynamically updating LCD display (which isn't time sensitive and uses no delays - only "read busy flag" before anyone asks!). And I also need to be sending and receiving UART serial data. Possibly even a little RTC module. Yeah, pushing those pins to the limit. Again, before anyone asks - I'm using a Pro Mini so got two extra pins A6 and A7 to play with.

In fact, do I even need a whole library? I'd be happy using timer0 if it could help me do the following:

In the background, there's a 100x32 OLED scrolling information. To scroll, I need to send it a full display-worth of 400 bytes about 30 times a second.
As I'm using direct port manipulation, I can get the whole display written in a few milliseconds.
While that's going on, I need to poll another Arduino somewhere around 10 to 25 times a second. I'm doing that at 2Mhz SPI setting to get it done quickly, and when the byte arrives, a simple lookup array converts that byte into one of a selection of predefined messages, then updates the OLED with the new message after grabbing the current time from the RTC.

Working as fast as it'll go with no delays, my OLED is scrolling slightly faster than I need it, so I figure using something efficient like an interrupt will naturally induce a delay just enough to make it readable, once I've taken my lookup array function into account (which includes a "if 0x00 then do nothing" part).

But I thought that if had a "runEvery..." type function being polled every single loop, that would be enough to visibly slow the display down again. As I say, I had to get right into port manipulation for every single pin otherwise the display was "tearing", so don't want to undo that if there's a nice timer that'll take the load off the CPU checking millis() all the time.

Am I on the right track here, or on a hiding to nothing? Incidentally, in case anyone asks if I've done my homework, here's where I've been over the last couple of days.

I'm pasting this here because although some of it isn't directly relevant to this subject, it all relates to timers and PWM and some cool related stuff I only found out about by stumbling on it. Here we go...

Nick Gammons pages on ... Interrupts , SPI and Timers and counters , in which I found, as a nice bonus... Tone library for timers
The small library below simplifies generating tones with the hardware timers. Unlike the "Tone" library that comes with the Arduino this one directly uses the outputting capability of the hardware timers, and thus does not use interrupts.

More things I found and read:
Timer1- the library I hope to use.
Arduino 101: Timers and Interrupts
Secrets of Arduino PWM
If you used a 16 bit timer (e.g. timer 1, or timers 3,4,5 on '1280), you could generate "tones" down to 1/8 Hz (one cycle every 8 seconds), although the library only accepts integers for frequency.

Overriding Arduino interrupt handlers
Adjusting PWM frequencies
Surprisingly, the polling function outperforms the interrupt request by almost a factor of 2!  In hindsight this result might not be so surprising. SPI in this case works at half the CPU frequency, which means that the CPU can only execute less than 16 instructions per byte sent. The polling loop wastes less time per transfer than an overhead of an interrupt call.

Varying the pwm frequency for timer 0 or timer 2?

If you change TCCR0B, it affects millis() and delay().  They will count time faster or slower than normal if you change the TCCR0B settings.  Below is the adjustment factor to maintain consistent behavior of these functions:

Default: delay(1000) or 1000 millis() ~ 1 second

0x01: delay(64000) or 64000 millis() ~ 1 second
0x02: delay(8000) or 8000 millis() ~ 1 second
0x03: is the default
0x04: delay(250) or 250 millis() ~ 1 second
0x05: delay(62) or 62 millis() ~ 1 second
(Or 63 if you need to round up.  The number is actually 62.5)

Also, the default settings for the other timers are:
TCCR1B: 0x03
TCCR2B: 0x04

Hope these notes help other wanderers on this long journey!
13  Using Arduino / Project Guidance / Re: State Machine - recomendattions / opinions ? on: November 30, 2013, 08:16:58 pm
I have written a statemachine library
Ah, thank you for that - good timing. I had come here to look for one! I very much enjoyed your writeup and explanation.

In fact, all the links provided are good - and Nick Gammon's links are always bursting with useful info.

Looking at my requirement, I'm not now sure whether a state machine is actually what I want. Here's a sort of mock-up in javascript:

If I could find an easy way to translate that to Arduino/C/C++, then I'd be very happy!

(In case anyone's wondering - no, I haven't found a magical way of making an Arduino play a podcast - it'll be controlling something else, but needs to keep track of where it is in the menu).

But that's some reading up I need to do. But just to say thanks for the good links, even though it wasn't my thread it was perfect timing!
14  General Category / General Discussion / What do we all reckon to this BE BOARD starter kit on Indiegogo? (ends 31/Nov) on: November 29, 2013, 02:42:32 pm
I'm very suprised I've not seen any chatter about this on here - perhaps the person behind it is persona-non-grata (Arduino politics gets confusing sometimes!).

Anyway, I just saw something on twitter about this:

Basically, there's various levels of Leondardo clone-based kits - for example, it claims that for the $29 pack you get $85-$112 worth of kit. There's also a "perk" of 5 Leonardo-style boards for $50, although nothing gets delivered 'til spring 2014.

Looks interesting; the first thing that bothers me is the import duty (which kicks in at items over £15 imported into the UK).
Going to have to figure out how much it would be if I get "intercepted".

As it all seems to end in about 30 hours (from now), and there seems to be a fair amount to read, has anyone else formed any opinions on these packs?
15  Using Arduino / Programming Questions / Re: Single pin Port Manipulation on: November 29, 2013, 05:24:06 am
hmm, I thought that direct port manipulation is the fastest way. Why is the digitalWriteFast faster?
Hmmm, same question here (by way of bumping thread smiley )

Moderator edit: quote corrected
Pages: [1] 2 3 ... 5