Moving from Arduino to Photon - where are the "differences" called out?

Blink on my new Photon is easy - but taking the next step leads to either forum overload (year-old threads about work in progress with “core”…) or google() misdirects (search for “particle” and “photon” and you get tons of physics and optical results and very few programming ones…)

(I’m not looking at the cloud stuff yet - this is all “on” the photon at this point…)

Some examples of what I’m looking for:

  • How do I use I2C? #include <Wire.h> gives “file not found” errors…
  • How do I use boolean types? #include <Arduino.h> fails…
  • What core libraries in 'duino land are available here?
  • How do I manage a particle.build hosted set of libraries for my own sketchbook?

Is there a documentation trove where these conventions and differences are collected and discussed?

-Thanks!
John

2 Likes

The API is documented here: http://docs.particle.io/photon/firmware/

This API is available to you out the box - no need to include any headers when building in the Web IDE.

The photon and the core have identical public APIs, so code that works for the core will work for the photon.

1 Like

Thanks for the pointer, which is the example that launched my initial post. (As must be obvious), I’m a bit overwhelmed by all the content aimed at IoT newcomers that is making it hard to find non-trivial examples to learn from…

As a concrete example, I have a library/C++ class that uses the boolean datatype. The firmware docs imply that “boolean” is a built in type; it must be, because I can’t #include <Arduino.h>:

  • I2Cextender.h:11:21: fatal error: Arduino.h: No such file or directory
  • #include <Arduino.h>

Yet, if I don’t include <something>, I get type related errors:

  • I2Cextender.h:18:59: error: ‘boolean’ has not been declared
  • void init(int address, int type, unsigned config, boolean debounce=false);
  • I2Cextender.h:30:5: error: ‘boolean’ does not name a type

which leads me back to my original question of where these things are discussed in detail. Searching the forums finds a lot of threads from early '14 about the initial stages of library support in the web IDE and the like, which leads me to suspect that this whole area is still a work in progress - which is fine as long as there is a pool of advice and workarounds somewhere.

After digging through random library source files, it seems that it’s vitally important to know that instead of including “Arduino.h” in library code, with Photon I need to include “application.h”; yet that advice is documented … where? …

As an experienced 'duino user trying to transition to a particle ecosystem, and not wanting to be a burden on the community as I learn, I’m still looking for a decoder ring.

-John

Hi @JohnP

The equivalent of #include <Arduino.h> is to do #include "application.h" but you usually don’t need to in your main sketch. That name is very likely to change in the near future so I don’t think it has been doc’ed. If you add a C++ library to the web IDE (there’s a little circle plus up near the top) then you should have the include in the C++ header file, but the Particle pre-processor does it for you in you sketch (.ino file). So users of libraries don’t need to know about the include, but authors of libraries do.

The application include brings a lot of the Wiring language you are familiar with already and that is listed in the firmware section of the doc.

The boolean datatype is called bool like C and C++. You can #define boolean bool I guess but fixing the library to use the standard C/C++ datatype is probably better.

boolean should be available - if it’s not, this might be a regression in the latest release for the photon. I’ll look into it.

EDIT: just checked, and boolean is available. Not sure why it isn’t being included for you. To be sure, please include “application.h”.

Notes for whoever comes next…

The Photon can be power hungry compared to the simpler 'duinos (just an observation, not necessarily a problem…) - while a 9v battery can power a 'duino for a 6-12 hour hack-as-you-travel experience, the Photon with active WiFi and an external 7805 vreg combine to suck it down to useless in less than an hour. Symptoms of “not enough juice” are failure to OTA upload sketches, not getting out of reset to run a sketch, and other flakey behaviors. I have an 8x8 MAX7219 LED matrix that runs fine on a Leonardo for hours, but won’t even initialize on a Photon with 9v battery power alone. Plugged into my bench supply instead and everyone is now happy!

Include “application.h” instead of “Arduino.h” in any self-provided library/class header files (but this may change in the future says @bko)

Don’t include Wiring.h, its effective content gets autoincluded in your main sketch, and by application.h elsewhere

You will probably need to clean up your old 'duino code:

  • int’s that you assumed were 16 bits need to be int_16’s instead,
  • unsigned’s become uint_16’s…
  • int’s are 32 bits now
  • Arduino’s boolean can be spelled ‘bool’ if the predefined type isn’t working for you (it worked for me after including application.h)

You need to self-define the bitRead/bitWrite routines (and see above for code cleanup if int-size assumptions may impact your code):

#ifndef bitRead
#define bitRead(value, bit) (((value) >> (bit)) & 0x01)
#define bitSet(value, bit) ((value) |= (1UL << (bit)))
#define bitClear(value, bit) ((value) &= ~(1UL << (bit)))
#define bitWrite(value, bit, bitvalue) (bitvalue ? bitSet(value, bit) : bitClear(value, bit))
#endif

The predefined names for the DAC and WKP pins are A6 and A7, not DAC1 and WKP as implied

EDIT: CLOCK_SPEED_100KHZ is not defined as claimed by the firmware docs:

  • Wire.setSpeed(clockSpeed);
  • clockSpeed: CLOCK_SPEED_100KHZ, CLOCK_SPEED_400KHZ or user specified speeds.
    (code verifies, but does not link:
    /spark/compile_service/shared/workspace/6_hal_12_0/firmware-privatestledmatrix.cpp: In function ‘void setup()’:
    /spark/compile_service/shared/workspace/6_hal_12_0/firmware-privatestledmatrix.cpp:91:19: error: ‘CLOCK_SPEED_100KHZ’ was not declared in this scope)

I’ll re-edit this post to add more info as I learn :smile: - thanks to all who jumped in to help!

-John

2 Likes

JohnP, when you want to take your code further and get connected, there is a great book, just released, from O’rielly Media. At the home page search on Simon Monk, Photon. That will take you to the book. You can order both paper and eBook and download the ebook right away. It is a clean, well organized, thorough presentation of several Core/Photon projects, connected through web pages.

@JohnP, the 'duino you refer to dosen’t have wifi so it uses less power by default. If you disable wifi on the Photon, it will use a lot less power. Remember that a typical Arduino runs at 16MHz while the Photon at 120MHz! Also the regulator you use with the 9v battery will create power loss so it all adds up. You can flash code to the Photon over USB (which also powers the Photon) using Particle CLI or DEV. If the 9V battery can power your LED matrix and Leonardo but not the Photon it is most likely because of the extra regulator you are using, not the Photon.

There is a lot to learn when moving from the Arduino world to the Particle devices. By investing time to read the documentation and asking questions in the community, things will go a lot faster. One thing for sure is we appreciate any comments on the documents being incorrect. :smile:

1 Like

Thank you!
I needed a definition of bitRead(n,x) for Photon.

1 Like

Very useful! I used all of these tips at once in order to adapt the Arduino HAL Button library. thanks!

1 Like