Arduino (interrupt) frustration

164 views
Skip to first unread message

Oliver

unread,
Jul 31, 2016, 4:12:06 AM7/31/16
to Reading Hackspace
Quite new to Arduino, and I am currently trying to replicate a project created on a Raspberry Pi on a Arduino Uno...
The basics of the project are using distance measurement (NewPing) and GPS (TinyGPS/SoftwareSerial).

I can get both the distance measurement working and the GPS working independently (separate sketches), however, as soon as I bring them both into the same sketch, the GPS refresh rate/new location seems to be interfering with the NewPing library...

Has anyone else had similar issues?

Jeremy Poulter

unread,
Jul 31, 2016, 5:33:34 AM7/31/16
to rLab List

The libraries are probably fighting over the same resources. Can you give some more details? How are things wired up? I presume by the title you suspect an interrupt issue, what interrupts are being used?

Jeremy


--
You received this message because you are subscribed to the Google Groups "Reading Hackspace" group.
To unsubscribe from this group and stop receiving emails from it, send an email to reading-hacksp...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Andrew Jacobs

unread,
Jul 31, 2016, 5:38:29 AM7/31/16
to Reading Hackspace
Can you change to hardware serial? NewPing's use of timer interrupts is probably breaking the delays used in the SoftwareSerial library to sync with serial data bits. Hardware based I/O would be more forgiving.

Oliver

unread,
Jul 31, 2016, 5:52:13 AM7/31/16
to Reading Hackspace
Jeremy

This is the code that i'm using (a mixture of the NewPing demo and the TinyGPS demo): http://pastebin.com/nXDTVbrk

I have the proximity sensors wired to pins 8,7,13,12 and the GPS using 3,2 

Oliver

Oliver

unread,
Jul 31, 2016, 5:53:01 AM7/31/16
to Reading Hackspace
Andrew,

Not familiar with hardware serial (as mentioned, sill learning the ropes), I will have a look into it!

Oliver

Richard Ibbotson

unread,
Jul 31, 2016, 7:27:03 AM7/31/16
to reading-...@googlegroups.com

I haven't used the software serial library for a while, but it is a serious resource hog. I think it waits for a pin interrupt on the serial rx pin, then disables interrupts, and decodes the whole character with interrupts disabled. So running your ss at 9600 will give (10 * 100us) 1ms of zz time for each character. The characters may be contiguous, and I guess there are about 50 in any GPS string. I guess your GPS is sending messages once per second, and we don't need to send any data to the GPS. With some GPS you can change the interval between messages, and reduce the number of message types sent.

 

As Andrew suggests, using hardware serial is better, but many arduino only have one serial port. I try to use Leonardo rather than uno, so as to have two serial ports, one hardware and one USB.

 

I note you are using the ping_timer() function, which should be better than the base ping() in newping. It depends on which arduino you use, but this uses timer 2 on most ATMega Arduino. I don't think Timer 2 is used by anything else in your code. I would have thought, that the software serial should not spoil the distance readings.

 

You should probably play around with the overall timing on a more simple sketch. Your code and the newping library use other timer functions, which will be distorted by the software serial. Scheduling the pings seems a bit of a faf on timing , maybe just do the pings one after each other.

 

Stiil, looks like you are making good progress,

Cheers,

Richard

--

Oliver

unread,
Jul 31, 2016, 8:09:22 AM7/31/16
to Reading Hackspace
I do have a SAMD21 dev board that I could use (it was already in the back of my mind that I might need something beefier than at Mega328).. I'll see if I have enough breakaway header left to solder it up!

I'll try and simplify the sketch a little too! 

Thanks,

Oliver

Oliver

unread,
Jul 31, 2016, 11:24:13 AM7/31/16
to Reading Hackspace
Managed to get Hardware Serial running on the SAMD21 chip! Thanks a ton for that

Now to play around with the NewPing code to get that working

Yossi

unread,
Jul 31, 2016, 12:19:47 PM7/31/16
to Reading Hackspace
Depending on how critical is your application, you may want considering adding an Arduino chip to read the GPS data and make it available on an I2C bus. This way the date will be ready for the master whenever it's needed and improve the timing in your program. You can get an ATMega 328, crystal and 2 capacitors on ebay for 2.99.

Check out:

Oliver

unread,
Aug 3, 2016, 1:49:50 AM8/3/16
to Reading Hackspace
Small update. Following Richard's advice I picked up a SAMD21 Dev Board and used Hardware Serial for the GPS feed, works a charm!
Also stripped back the proximity sensor code to remove most of the interrupt requirement, adding some stabilization now to account for cheap sensors and misreads.

Next up, battery management, then try and design a PCB to send off to print
Reply all
Reply to author
Forward
0 new messages