Arduino ESP32 TwoWire "method" doesn't work - arduino

I have trouble using two sensors on two I2C interfaces.
The problem is, that I when I try to replace the Wire.begin() with something like this: I2C.begin(21, 22), my sensor QMC5883L returns for x, y and z 0. I included the Wire library manually and created a TwoWire object:
#include <Wire.h>
TwoWire I2C = TwoWire(0);
When I tried this, I commented the _wire->begin() in the library, It worked with the commented line and with Wire.begin() in the sketch, but when I try to replace Wire.begin() with I2C.begin(21, 22) it doesn't work anymore.
The reason, why this should work is that I want to create 2 interfaces like that and then give them to the compass.begin(&I2C) method. In the library I have this:
void QMC5883LCompass::init(TwoWire *theWire){
_wire = theWire;
_wire->begin(); // this is the line I commented, when getting the zeros...
_writeReg(0x0B,0x01);
setMode(0x01,0x0C,0x10,0X00);
}
Did I forget something?

Problem is that when calling _wire->begin() is actually calling the default definition (from Wire.h):
bool begin(int sda=-1, int scl=-1, uint32_t frequency=0);
Default parameters -1 for SDA/SCL means default pins, wich overwrites the ones you added in the first begin(...)
Presuming you are using https://github.com/mprograms/QMC5883LCompass, the solution is to subclass QMC5883LCompass and override the void QMC5883LCompass::init() or create a new init(...) with your specific needs.

Related

How do I rotate 2 steppers at the same time?

Is there somethimg like theading in arduino?
I'm using Stepper.h library to use steppers and I want to rotate two of them at the same time, is that possible?
Additional info:
I'm using two drivers ULN2003 and Arduino Nano to controll steppers.
Code for one of them:
void AStep(){
AStepper.step(i1.toInt());
display.println("Turned A stepper;");
display.display();
}
You can make them appeir as they are moving at the some time by making the stepper do smaller "steps".
The only thing is that this method DOESN'T make them turn in actual sync.
Snippet by PaulS from the Arduino forums.
for(int s=0; s<step_360; s++)
{
AStepper.step(1);
BStepper.step(1);
}

Optimize library Arduino : undefined reference

The development for embedded system impose an other way to code.
In the goal to minimize size of my library named RF24Wave, I would adapt the structure of my main class.
The idea is to declare some functions only with the presence of certain #define during the inclusion of my library in main program.
The popular library, like MySensor use this way to minimize memory footprint.
So, I have two files for my library :
RF24Wave.h : header file which contain function declarations
#ifndef __RF24WAVE_H
#define __RF24WAVE_H
#include <arduino.h>
...
class RF24Wave
{
public:
/****** Common functions ******/
void begin();
#if !defined(WAVE_MASTER)
/****** Node functions ********/
void connect();
#else
/****** Master functions ******/
void printNetwork();
#endif
private:
bool synchronized = false;
}
RF24Wave.cpp: source file which contain function definitions
#include "RF24Wave.h"
void begin()
{
//Body of my function
}
#if !defined(WAVE_MASTER)
void connect()
{
//Body of my function
}
#else
void printNetwork()
{
//Body of my function
}
#endif
Secondly, I include this library in my main sketch named master.cpp, with #define WAVE_MASTER:
master.cpp: main sketch
#include <RF24.h>
#include <RF24Network.h>
#include <RF24Mesh.h>
#include <SPI.h>
#define WAVE_MASTER
#include <RF24Wave.h>
#include "master.h"
// Configure the chosen CE, CSN pins
RF24 radio(CE_PIN, CSN_PIN);
RF24Network network(radio);
RF24Mesh mesh(radio,network);
RF24Wave wave(network, mesh);
uint32_t displayTimer = 0;
void setup()
{
Serial.begin(115200);
wave.begin();
}
void loop()
{
wave.listen();
wave.printNetwork();
}
The goal is to include only master functions when #define WAVE_MASTER is defined in the main sketch.
However, during the compilation of my main sketch, I have a linking error
In function `main':
undefined reference to `RF24Wave::printNetwork()'
collect2: error: ld returned 1 exit status
*** [.pioenvs/uno/firmware.elf] Error 1
I compiled with PlaformIO Core 1.7.1 /Atom 1.13.0
Finally, the reason of this problem is the scope of #define.
The solution of this problem is added build flags to gcc-compiler !
If you use combo PlatformIO/Atom, you can add this line into this
config file platformio.ini :
build_flags = -D$NAME_DEFINE
In our case :
build_flags = -DWAVE_MASTER
After adding this line, the building seems work fine !
In addition, with this selective #define, my library has reduced footprint memory more than 10% !
Many thanks for your help.
If you add your library in the arduino IDE as it is described here it just consists in linking another project to your library functions. It's not a static library (see static and dynamic libraries). Then I think it's not necessary to worry about its size since the compiler will embed your library functions only if you use them.
Try opening any example (AnalogReadSerial), compile it. Then Sketch->Add a library->SPI. Compile it again, the size does not change. Try to call SPI.begin() in the setup function, the size increases. Add a call to SPI.setBitOrder(MSBFIRST);, the size increases again. Add another call to SPI.setBitOrder(MSBFIRST);, the size increases again, but not by the same amount since it contains only one setBitOrder definition and two calls to the setBitOrder function.
That's not exactly true for all libraries since some constructs could force the compiler to embed some code or allocate memory even if the variable is not used (see for instance volatile variables).
So regarding your size issue, you'd probably only need to use one #define MASTER, write the master code in setup and loop functions surrounded by #ifdef MASTER and the slave code surrounded by #else...#endif. The compiler will include the function definitions that both master or slave use.
So, with your answer, I found the solution !
The problem is the scope of #define. During the building, the first source code compiled is the main sketch ! So, it includes the correct #define WAVE_MASTER in RF24Wave.h of master.cpp. But, the compiler don't take this define into account of other source files. So, during the compilation of RF24Wave.cpp, #define WAVE_MASTER wasn't defined for this source file.
The solution consist to add an extra gcc-flag to define WAVE_MASTER for all source and header files.
With PlatformIO/Atom, it's possible in adding this line in config file platformio.ini
build_flags = -DWAVE_MASTER
Finally, your config file should look like this :
[env:uno]
platform = atmelavr
board = uno
framework = arduino
build_flags = -DWAVE_MASTER
With this solution, it is no longer necessary to add #define WAVE_MASTER in your header file, because this is the compiler which add it for you.
In addition, this optimization has been reduced more than 10% memory usage, because like this is a big class object, the compiler builded all functions. With selective define, the compiler will build only useful functions.

Coding errors between the Arduino Uno and SDI-12 interface on Decagon Devices

I have been working on a project recently using a sensor for electrical conductivity in soil (A 5TE sensor from Decagon Devices) with my Arduino Uno. I am ready to code, and found this example code on GitHub (the example code is there when you scroll down the page). When trying to run it on the latest version of Arduino, it gave me these compilation errors:
sketch_dec15a:7: error: expected initializer before 'void'
sketch_dec15a:4: error: 'SDISerial' does not name a type
sketch_dec15a:9: error: expected initializer before 'void'
sketch_dec15a.ino: In function 'void loop()':
sketch_dec15a:22: error: 'connection' was not declared in this scope
NOTE: I believe I installed the library correctly, but am not 100% certain...more like 85%.
What's wrong with the code and how can it be made to work?
The example code is wrong. Look at the compilation errors. The first thing it says is:
sketch_dec15a:7: error: expected initializer before 'void'
So what it's saying is that it found something that said void and expected to see something else first. void occurs only twice in your code, so we can't be far. Let's take a look at the code immediately surrounding it the first void:
char tmp_buffer[4];
char sensor_info[]
//initialize variables
void setup(){
connection.begin();
Serial.begin(9600);//so we can print to standard uart
//small delay to let the sensor do its startup stuff
delay(3000);//3 seconds should be more than enough
}
Right before the void setup(){ is //initialize variables. That's just a comment, and not code, so it doesn't relly count. Looking back one more line we see:
char sensor_info[]
Something is wrong with that line. Work on it and see if you can figure it out (check the other lines for "hints"). If you can't figure it out, the answer is right below (put your mouse over it to see it):
It needs a semicolon ";" at the end to complete the statement. Because the semicolon is missing, it thinks "void setup(){" is part of the previous statement.

Arduino Serial Output Dropping Characters

I have a bizarre one here with the serial output when trying to write some code for my Arduino Uno.
I have this proto-code:
MyClass myclass;
void setup()
{
Serial.Begin(9600);
Serial.println("Starting...");
}
void loop()
{
int status = myclass.DoWork();
Serial.println("Status: " + status);
}
class MyClass
{
int DoWork()
{
Serial.println("Doing some work...");
return 1;
}
}
Now when this runs I get the following output:
Starting...
Doing some work...
atus: 1
So the strange part is the "Status: 1" missing the first few characters. Is this because I am using serial in an object improperly or something?
I have noticed when I reference another library that also uses serial like MyClass does that I get other strange output behavior... so I assume that I am doing something wrong.
EDIT: In the end this turned out to actually be a memory issue. A library I was including was quite large and it was consuming the available memory. I found this by adding a few more debugging statements and found the corruption shifted around based on the string lengths and positions. By using the F() function I moved the strings to flash memory (e.g. I now run Serial.println(F("Starting...")); and it has corrected the strange output.
You cannot add strings and integers in C++. It would have been better for you if this failed to compile:
Serial.println("Status: " + status);
Instead the compiler guessed at something. It guessed wrong. Use this:
Serial.print("Status :");
Serial.println(status);
or for complete control of outputting numbers and strings learn to use C string formatting, sprintf()
In the end this turned out to actually be a memory issue. A library I was including was quite large and it was consuming the available memory. I found this by adding a few more debugging statements and found the corruption shifted around based on the string lengths and positions. By using the F() function I moved the strings to flash memory (e.g. I now run Serial.println(F("Starting...")); and it has corrected the strange output.
One more possible explanation.
I was running minicom to monitor, and I usually like that it auto-reconnects after resetting my device.
Well I minimized a terminal running minicom last night, and today I started a new instance that somehow also got connected to the same serial port (if that is even possible).
I think the two instances of minicom were each reading ~50% of the serial characters roughly at random, leaving me with quite a mess of text.

How to include an already written library to a custom library in Arduino

I'm creating a new library to control the keypad and LCD together. Most of the code seems to compile, but when it reaches the line where I define the LiquidCristal variable, it says:
'LiquidCrystal' does not name a type when creating a custom library
This is the extract of content of my LCDKeypad.h
// Include types & constants of Wiring core API
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#include "WConstants.h"
#endif
// Include LCD library
#include <LiquidCrystal.h>
The error is in this line:
private:
LiquidCrystal lcd( 8, 9, 4, 5, 6, 7 ); // <<-- Error
Alright, I was reading a lot and I found a very interesting article about this subject:
Including multiple libraries
It says that the compiler does not search for libraries that are not included in the sketch file. The way to hack this is to force the compiler to link them before loading your libraries, including, in my case LiquidCrystal.h in the sketch.
Let's say my library "LCDkeypad" requires LiquidCrystal.
My main program (sketch) needs to include LiquidCrystal in order to load it for my library "LCDKeypad".
Now, one interesting thing is using forward declaration, so in my LCDKeypad.h I will declare
"Class LiquidCrystal" but not include the library. I will do it in LiquidCrystal.cpp and in the sketch.
I hope this is clear.
There are two ways of doing it
If you are writing your own code Just create header file .h extension and relevant c code as name_c.While adding into main program, you need to specify header file name in double quotes.
code:
#ifndef LCD_H
#define LCD_H
//Declaration of variable /functions
#endif
If you are trying to download from link. Then you need paste the code into D:\arduino\arduino\libraries
Error problem:
overlapping of multiple declaration of variable.
overlapping of library functions

Resources