Notice: the content of the following post is outdated. Please see the instructions in the Macchina M2 documentation or the the README on GitHub for current information.
First off, a big thank you to @t_doust for putting together these files. Tony has been working on a library for the 6 GPIO drivers and started modifying the core Arduino “Variant” files to be more M2-specific.
The result is a much cleaner, easy to set up, and less confusing development experience. We don’t need to use the “SamNonDuePin” library anymore. The following 2 pieces of code do the exact same thing, but you’ll see that the second is simpler.
If you select “Arduino Due (Native USB Port)” from the Tools->Board menu in the Arduino IDE, you’d use this code:
[details=Code using “SamNonDuePin” library
]
/*
Demonstrates using "Non-Due" pins for Button inputs and LED outputs.
Also found here:
https://gist.github.com/macchina/d7b22db67d62e48583eb7530371e3c36
*/
#include "Arduino.h"
#include "SamNonDuePin.h"
// constants won't change. They're used here to
// set pin numbers:
const int SW1 = X1; // Pushbutton SW1
const int SW2 = PIN_EMAC_ERX1; // Pushbutton SW2
const int Yellow = X0; // the number of the LED pin
const int Red = 32; // the number of the LED pin
// others are: 32(RED), X0(YELLOW), 27(YELLOW), 24(YELLOW), 23(GREEN), 12(RGB_GREEN), 5(RGB_BLUE), 11(RGB_RED)
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
int buttonState2 = 0; // variable for reading the pushbutton status
void setup() {
pinModeNonDue(Yellow, OUTPUT);
pinMode(Red, OUTPUT);
pinModeNonDue(SW1, INPUT);
pinModeNonDue(SW2, INPUT);
digitalWriteNonDue(Yellow, LOW);
digitalWrite(Red, LOW);
}
void loop() {
buttonState = digitalReadNonDue(SW1);
if (buttonState == HIGH) { // NOT pressed
digitalWriteNonDue(Yellow, HIGH); // turn LED OFF:
}
else {
digitalWriteNonDue(Yellow, LOW); // turn LED ON:
}
buttonState2 = digitalReadNonDue(SW2);
if (buttonState2 == HIGH) {
digitalWrite(Red, HIGH); // turn LED OFF:
}
else {
digitalWrite(Red, LOW); // turn LED ON:
}
}
[/details]
Here is the code you can use if you select “M2” from the Tools->Board menu in the Arduino IDE.
[details=Code with M2 board definitions - notice it is a bit simpler.]```cpp
/*
Demonstrates using “Non-Due” pins for Button inputs and LED outputs using M2 board Defs.
*/
// constants won’t change. They’re used here to
// set pin numbers:
const int SW1 = Button1; // Pushbutton SW1
const int SW2 = Button2; // Pushbutton SW2
const int Yellow = DS3; // the number of the LED pin
const int Red = DS2; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
int buttonState2 = 0; // variable for reading the pushbutton status
void setup() {
pinMode(Yellow, OUTPUT);
pinMode(Red, OUTPUT);
pinMode(SW1, INPUT);
pinMode(SW2, INPUT);
digitalWrite(Yellow, LOW);
digitalWrite(Red, LOW);
}
void loop() {
buttonState = digitalRead(SW1);
if (buttonState == HIGH) { // NOT pressed
digitalWrite(Yellow, HIGH); // turn LED OFF:
}
else {
digitalWrite(Yellow, LOW); // turn LED ON:
}
buttonState2 = digitalRead(SW2);
if (buttonState2 == HIGH) {
digitalWrite(Red, HIGH); // turn LED OFF:
}
else {
digitalWrite(Red, LOW); // turn LED ON:
}
}
----------
**So how do you get "M2" to show up in your Tools->Boards menu in the Arduino IDE ?**
Just paste this:
`https://macchina.github.io/package_macchina_index.json`
into the "additional Boards Manager URLs" field under "Preferences", and then select "Board Manager" under Tools->Boards, and install "M2 by Macchina".
This [guide](https://www.macchina.cc/guide/m2/getting-started), although slightly outdated will walk you through that process.
**What are the pin names now?**
We'll be making a nice table for you to refer to in our guide, but for now, they can be seen in the source here:
[details=variant.h]https://github.com/macchina/macchina.github.io/blob/master/Macchina-2.0.1/variants/arduino_due_x/variant.h[/details]
Note that this is a work-in-progress, so keep that in mind.
**How can I report bugs, do pull requests, or generally help improve things?**
We are keeping the M2 board files in this [repo](https://github.com/macchina/macchina.github.io). Feel free to contribute.
We'll be periodically making "official" updates to the files as we go. [Here](https://forum.arduino.cc/index.php?topic=409715.0) are the instructions we followed to set up the files needed by the "Boards Manager" feature of the Arduino IDE.