How to edit library to use with Arduino DUE? - arduino

I want to add Arduino DUE in this code.
// Arduino Uno, Duemilanove, LilyPad, etc
//
#elif defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__)
#define ALTSS_USE_TIMER1
#define INPUT_CAPTURE_PIN 8 // receive
#define OUTPUT_COMPARE_A_PIN 9 // transmit
#define OUTPUT_COMPARE_B_PIN 10 // unusable PWM
Code from library

The preprocessor for the Arduino Due is __SAM3X8E__. For example:
// Arduino Uno, Duemilanove, LilyPad, etc
//
#elif defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__)
#define ALTSS_USE_TIMER1
#define INPUT_CAPTURE_PIN 8 // receive
#define OUTPUT_COMPARE_A_PIN 9 // transmit
#define OUTPUT_COMPARE_B_PIN 10 // unusable PWM
//
// Arduino Due
//
#elif defined(__SAM3X8E__)
// define or do your stuff

Related

Arduino Nano communication with ST7735 over SPI-Bus

I use an Arduino Nano with a ST7735 display and a CAN controller MCP2515. Via SPI bus I want to communicate with the display and the CAN controller. The communication via CAN controller works smoothly. With the display I have the problem that it only shows a white screen.
This is my current setup:
CAN-Setup as picture
I use this kind of code to communicate with the CAN-Controller and with the display:
#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library for ST7735
#include <SPI.h>
#include <mcp2515.h>
#define MCP2515_CS 10 // Chip Select CAN-Controller
#define TFT_CS 7 // Chip Select TFT-Display
#define TFT_RST 8 // Reset
#define TFT_DC 9
#define TFT_MOSI 11 // Data out
#define TFT_SCLK 13 // Clock out
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST);
struct can_frame canMsg;
MCP2515 mcp2515(MCP2515_CS);
Can anyone spot a bug?
In Adafruit libraries the constructors where you enter the SPI pins use software SPI (bit banged). It conflicts with hardware SPI for the CAN controller on the same pins. Use the constructor
Adafruit_ST7735(int8_t cs, int8_t dc, int8_t rst);
so
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);

ESP8266 (Nodemcu) + PN532 (RFID) + ST7735 (Display) in one setup possible?

I am trying to get an RFID-Reader (PN532) to work with a display, so it is shown there, who has scanned his RFID-Card.
The problem I ran into was, that 2 pins (D7 HMOSI) and (D5 HSLCK) are used by both devices. Thus I simply put both connections on those. (wrong?)
Now when initializing either of both devices, the other one gets disabled.
I use Adafruit to initialize both devices.
In addition to this, the ESP8266 does not start when the RFID-Reader is connected. Removing the Pin from 3.3Volt VCC and waiting for init, then Adding the Pin, only then the RFID-Reader gets recognized and the ESP8266 runs. (bad case for crashes, as it would never reboot)
This is my cable setup:
Also here is my code:
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_PN532.h>
#define PN532_SCK (14)
#define PN532_MOSI (13)
#define PN532_SS (15)
#define PN532_MISO (12)
#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library for ST7735
#include <Adafruit_ST7789.h> // Hardware-specific library for ST7789
#define TFT_CS 5
#define TFT_RST 16
#define TFT_DC 4
Adafruit_PN532 nfc(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS);
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
void testdrawtext(char *text, uint16_t color) {
tft.setCursor(0, 0);
tft.setTextColor(color);
tft.setTextWrap(true);
tft.print(text);
}
void setup(void) {
Serial.begin(9600);
Serial.print(F("Hello! ST7735 TFT Init"));
tft.initR(INITR_BLACKTAB); // Init ST7735 chip, black tab
Serial.println(F("Initialized"));
tft.fillScreen(ST77XX_BLACK);
while (!Serial) delay(10);
Serial.println("Hello! PN532 RFID Init");
nfc.begin(); // Init PN532 chip
uint32_t versiondata = nfc.getFirmwareVersion();
if (! versiondata) {
Serial.print("Didn't find PN532 board");
while (1); // halt
}
// Got ok data, print it out!
Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX);
Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC);
Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);
nfc.setPassiveActivationRetries(0xFF);
nfc.SAMConfig();
Serial.println("Waiting for a Card");
}
The constructor
Adafruit_PN532 nfc(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS);
creates a 'driver' which uses software SPI. But you supply pins of hardware SPI as parameters. Hardware SPI is used by the Adafruit_ST7735 library over the SPI library to access the display so hardware SPI conflicts with the software SPI of the PN532 library.
Use
Adafruit_PN532 nfc(PN532_SS);
constructor which uses the hardware SPI over the SPI library. The SPI library 'knows' the pin numbers of the SPI pins. (SPI library is part of the boards package. It can't be installed separately.)
And don't use SS (io 15) as CS. Use a different pin. io 15 is a boot configuration pin and must be LOW at boot.

Arduino communication with TMC2209

I'm trying to communicate with TMC2209 (stepper drive) with an Arduino nano Every.
I connected pin RX on pin D2 and TX on pin D3.
I placed a 1K resistor between TX and RX.
It seems I can write parameters (even I'm testing this deeply, I'm not so sure now..) but I'm not able
to read nothing from driver.
Picture added 15/04/2021 related to datasheet of TMC2209
In my test, I tried with only one driver with address 0, means MS1_AD0 and MS2_AD1 connected to GND.
driver1.microsteps(MICROSTEPS);
SerialPort.print("Counter1=");
SerialPort.print(driver1.IFCNT());
SerialPort.print(", Status=");
SerialPort.println(driver1.DRV_STATUS());
I tried reading IFCNT and DRV_STATUS, always ZERO.
The initialization is done in this way:
#include <Wire.h>
#include <AG_AS5600.h>
#include <TMCStepper.h>
#include <AccelStepper.h>
#define ENCODER false
#define FEEDBACK_I2C true
#define GEARBOX 139 //51
#define MICROSTEPS 16
#define MIN_SPEED 20
#define MAX_SPEED 3000
double offsetHome = 77.0;
#ifdef ARDUINO_SAMD_VARIANT_COMPLIANCE
#define SerialPort SerialUSB
#define SYS_VOL 3.3
#else
#define SerialPort Serial
#define SYS_VOL 5
#endif
#define SW_RX 2 // TMC2208/TMC2224 SoftwareSerial receive pin
#define SW_TX 3 // TMC2208/TMC2224 SoftwareSerial transmit pin
#define EN_PIN 7 // Enable
#define DIR_PIN 8 // Direction
#define STEP_PIN 9 // Step
#define SERIAL_PORT Serial1 // TMC2208/TMC2224 HardwareSerial port
#define DRIVER_ADDRESS1 0b00 // TMC2209 Driver address according to MS1 and MS2
#define R_SENSE 0.11f // Match to your driver
// SilentStepStick series use 0.11
// UltiMachine Einsy and Archim2 boards use 0.2
// Panucatt BSD2660 uses 0.1
// Watterott TMC5160 uses 0.075
AG_AMS_5600 ams5600;
TMC2209Stepper driver1(SW_RX, SW_TX, R_SENSE, DRIVER_ADDRESS1);
AccelStepper stepper = AccelStepper(stepper.DRIVER, STEP_PIN, DIR_PIN);
String cmd = {};
void setup()
{
SerialPort.begin(115200);
Wire.begin();
Wire.setTimeout(10);
SerialPort.println(">>>>>>>>>> Program started <<<<<<<<<<");
pinMode(EN_PIN, OUTPUT);
pinMode(STEP_PIN, OUTPUT);
pinMode(DIR_PIN, OUTPUT);
digitalWrite(EN_PIN, HIGH); // Enable driver in hardware
// Enable one according to your setup
SPI.begin(); // SPI drivers
driver1.beginSerial(115200); // SW UART drivers
driver1.begin(); // SPI: Init CS pins and possible SW SPI pins
// UART: Init SW UART (if selected) with default 115200 baudrate
driver1.toff(5); // Enables driver in software
driver1.rms_current(2000); // Set stepper current to 600mA. The command is the same as command TMC2130.setCurrent(600, 0.11, 0.5);
driver1.microsteps(MICROSTEPS);
driver1.pwm_autoscale(true); // Needed for stealthChop
stepper.setMaxSpeed(degToSteps(500.0)); // steps/s
stepper.setAcceleration(degToSteps(1.0)); // steps/s^2
stepper.setEnablePin(EN_PIN);
stepper.setPinsInverted(false, false, true);
stepper.disableOutputs();
}
void loop()
{
unsigned long t0 = micros();
switch (step) {
case 0:
cmd = SerialPort.readString();
if (cmd.charAt(0)=='a' && cmd.charAt(1)=='=') {
acc = degToSteps(cmd.substring(2).toDouble());
SerialPort.print("Acceleration=");
SerialPort.println(acc);
cmd = "";
}
else if (cmd.charAt(0)=='d' && cmd.charAt(1)=='=') {
dec = cmd.substring(2).toInt();
SerialPort.print("Deceleration=");
SerialPort.println(dec);
cmd = "";
}
else if (cmd.charAt(0)=='c' && cmd.charAt(1)=='?') {
driver1.microsteps(MICROSTEPS);
driver2.microsteps(MICROSTEPS);
SerialPort.print("Counter1=");
SerialPort.print(driver1.IFCNT());
SerialPort.print(", Counter2=");
SerialPort.print(driver2.IFCNT());
SerialPort.print(", Status=");
SerialPort.println(driver1.DRV_STATUS());
cmd = "";
}
}
The hardware should be ok because the jumper for UART is configured as default on pin4.
Can someone help me to understand why?
Thanks
Andrea
I found!
The issue was the handling of "software" serial port.
Unfortunately I was confused because many examples of "TMCstepper.h" library are shown using two pins as TX and RX, so I was convinced internally pins were handled to send and receive.. but it's not.
So solution I found is:
#include <SoftwareSerial.h>
SoftwareSerial SerialDriver1(SW_RX, SW_TX);
TMC2209Stepper driver1(&SerialDriver1, R_SENSE, DRIVER_ADDRESS1);
void setup()
{
SerialDriver1.begin(57600);
SerialDriver1.listen();
...
and was really important to comment:
//driver1.beginSerial(115200); // SW UART drivers
this is important I guess because the port was already opened in the setup by me.
and it's necessary to specify the software port where to listen, not done by TMC library.
Last hint, with Arduino Nano Every I found out that Software Serial seems to work with max baudrate of 57600, 115200 was not working.

Can't connect Arduino to RFID

I am using an A-Star 32U4 Micro Arduino and I'm trying to connect the RDM6300 - 125KHz Cardreader Mini-Module.
I'm using this sketch at the moment:
#include <SoftwareSerial.h>
// RFID | Nano
// Pin 1 | D2
// Pin 2 | D3
SoftwareSerial Rfid = SoftwareSerial(2,3);
void setup() {
// Serial Monitor to see results on the computer
Serial.begin(9600);
// Communication to the RFID reader
Rfid.begin(9600);
}
void loop() {
// check, if any data is available
if(Rfid.available() > 0 ){
// as long as there is data available...
while(Rfid.available() > 0 ){
// read a byte
int r = Rfid.read();
// print it to the serial monitor
Serial.print(r, DEC);
Serial.print(" ");
}
// linebreak
Serial.println();
}
}
With this circuit:
module TX --- Arduino pin 2
module VCC ----- 5v
module ground ---- ground
antenna pins ---- antenna
When I put the card in the sensor nothing shows up on serial port. I tried this setup and the exact same sensors on an Arduino Uno (same sketch) and it worked perfectly, but I cant get this working on the Micro.
Arduino UNO and Micro uses different processors, though they work fairly similarly, they are not totaly identical.
It seams that
not all pins on the Leonardo and Micro support change interrupts, so only the following can be used for RX: 8, 9, 10, 11, 14 (MISO), 15 (SCK), 16 (MOSI).
From the SoftwareSerial Library description ( https://www.arduino.cc/en/Reference/softwareSerial )
Change the module TX from pin 2 to pin 8. and you should be good. :-)

error in 'QTR_NO_EMITTER_PIN' was not declared in this scope in arduino?

here is the part of code that i have problem in it, i add the library and i include it, also it appear in (sketch-->import library)
#include <PololuQTRSensors.h>
#include <Servo.h>
// Change the values below to suit your robot's motors, weight, wheel type, etc.
#define KP 0.5
#define KD 1.7
#define M1_DEFAULT_SPEED 100
#define M2_DEFAULT_SPEED 100
#define rightMaxSpeed 200
#define leftMaxSpeed 200
#define NUM_SENSORS 6 // number of sensors used
#define TIMEOUT 2500 // waits for 2500 us for sensor outputs to go low
#define leftPWM 3
#define rightPWM 5
//#define leftEnable 2
//#define rightEnable 4
//#define leftGND 2//12 //connected Directly to gnd
//#define rightGND 4//13
#define echoPin 2 // Echo Pin
#define trigPin 4// Trigger Pin
#define LEDpin 13 //used for testing and calibration
#define DEBUG 0
#define LINEFOLLOWER 0
Servo svoTilt;
Servo svoClaw;
/* Create instance of sensors. Sensors connected to pins 6 to 11
*/
PololuQTRSensorsRC qtrrc((unsigned char[]) {6,7,8,9,10,11}, NUM_SENSORS, TIMEOUT, QTR_NO_EMITTER_PIN);
but i have the following error in 'QTR_NO_EMITTER_PIN' was not declared in this scope,
i do not know why.
you need to include the following in your sketch
#include <QTRSensors.h>
after your above the
#include <PololuQTRSensors.h>
#include <Servo.h>
After downloading the library, I was able to compile the following successfully.
#include <PololuQTRSensors.h>
#include <QTRSensors.h>
#include <Servo.h>
#define NUM_SENSORS 6 // number of sensors used
#define TIMEOUT 2500 // waits for 2500 us for sensor outputs to go low
#define DEBUG 0
#define LINEFOLLOWER 0
Servo svoTilt;
Servo svoClaw;
QTRSensorsRC qtrrc((unsigned char[]) {6,7,8,9,10,11}, NUM_SENSORS, TIMEOUT, QTR_NO_EMITTER_PIN);
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
with the following successful result.
C:\Users\mflaga\AppData\Local\Temp\build5914291637870389663.tmp/QTR_NO_EMITTER_PIN.cpp.hex
Sketch uses 2,264 bytes (7%) of program storage space. Maximum is 32,256 bytes.
Global variables use 84 bytes (4%) of dynamic memory, leaving 1,964 bytes for local variables. Maximum is 2,048 bytes.
Where I get the same error as reported above when I comment out the "#include "
Please note you may need to restart the IDE. to have it cache the presence of the library files.

Resources