Hello to all,
I tested the following slightly modified sketch from the examples of Arduino_AdvancedAnalog and it works fine.
But up to now I didn't succeed in packing the used function into a class in order to automate the whole analog-reading procedure in a project.
AnalogIn.ino:
#include <Arduino_AdvancedAnalog.h>
char tmp[64] = "";
AdvancedADC adc(A0, A1, A2);
uint64_t last_millis = 0;
void setup() {
Serial.begin(9600);
// Resolution, sample rate, number of samples per channel, queue depth.
if (!adc.begin(AN_RESOLUTION_12, 16000, 32, 96)) { // ok
Serial.println("Fehler Start adc");
while (1);
}
}
void loop() {
if (adc.available()) {
SampleBuffer buf = adc.read();
// Process the buffer.
if (millis() - last_millis > 333) {
sprintf(tmp, "Wert A0: %d", buf[0]);
Serial.println(tmp); // Wert von Kanal 1
sprintf(tmp, "Wert A1: %d", buf[1]);
Serial.println(tmp); // Wert von Kanal 1
sprintf(tmp, "Wert A2: %d", buf[2]);
Serial.println(tmp); // Wert von Kanal 1
last_millis = millis();
}
// Direct access to the underlying buffer.
// buf.data()
// Release the buffer to return it to the pool.
buf.release();
}
}
I tried to do the following schematic:
analogIn.h:
class z;
extern z Z;
class z {
advancedADC *adc;
bool StartADC();
int getVal(int n);
...}
analogIn.cpp:
z Z;
AdvancedADC adc(A0, A1, A2);
...
}
z::StartADC() {
return adc->begin(AN_RESOLUTION_12, 16000, 32, 96);
}
z::getVal(int n) {
if (adc->available()) {
SampleBuffer buf = adc->read();
return buf[n];
} else {
return -1;
}
buf.release();
}
So analogIn.h is included in the main program, usb started, StartADC() called and then getVal(i). When calling getVal(i), the program crashes. What I'm doing wrong? Your help is appreciated!
Regards
Gjg_P320
P.S.: I tried also this and it doesn't work:
analogIn.h:
class z;
extern z Z;
class z {
bool StartADC();
int getVal(int n);
...}
analogIn.cpp:
z Z;
AdvancedADC adc(A0, A1, A2);
z::StartADC() {
return adc.begin(AN_RESOLUTION_12, 16000, 32, 96);
}
z::getVal(int n) {
if (adc.available()) {
SampleBuffer buf = adc.read();
return buf[n];
} else {
return -1;
}
buf.release();
}