Hi friends,
I am trying to voltage values from 2 analog sensors which are connected to pins AIN0 and AIN1 using C++. I have taken Derek Molloy’s code for reading one voltage value and edited it to add a second pin to read values from. The code is as follows:
#include
#include
#include
#include
#include
using namespace std;
#define LDR_PATH “/sys/bus/iio/devices/iio:device0/in_voltage”
int readAnalog(int number){
stringstream ss;
ss << LDR_PATH << number << “_raw”;
fstream fs;
fs.open(ss.str().c_str(), fstream::in);
fs >> number;
fs.close();
return number;
}
int main(int argc, char* argv[]){
cout << “Starting the IR distance sensor program:” << endl;
while(1) {
int value0 = readAnalog(0);
int value1 = readAnalog(1);
float V0 = ((float)value0 / 4096) * 1.8;
float V1 = ((float)value1 / 4096) * 1.8;
cout << "Sensor0 is: " << V0 << " Sensor1 is: " << V1 << ‘\r’ << flush;
usleep(100000);
}
return 0;
}
However, when I run this code, after 10-15 successful iterations, I receive this error message:
terminate called after throwing an instance of ‘std::ios_base::failure’
what(): basic_filebuf::underflow error reading the file
Aborted
I am not sure as to why I am receiving this error, or really what this error message even means. My guess it is some access error when trying to read the pin value.
The Adafruit BBIO python library actually found a work around this issue in C, but I’m not sure how I can extrapolate this to C++ (see: https://github.com/adafruit/adafruit-beaglebone-io-python/blob/master/source/c_adc.c)
If anyone has any insight or help they could offer, that would be very helpful!
Thanks