Last active
May 12, 2018 04:34
-
-
Save scriptpapi/81ac557d11bf28c58ab031ebdb7ba4f5 to your computer and use it in GitHub Desktop.
C code that reads input from analog pins on Beaglebone Black
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Reference: Exploring BeagleBone - Derek Molloy | |
#include<iostream> | |
#include<fstream> | |
#include<string> | |
#include<sstream> | |
#include<unistd.h> | |
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[]){ | |
int AINnum; | |
cout << "number of AIN pin (0-6) > " << endl; | |
cin >> AINnum; | |
if(AINnum < 0 || AINnum > 6){ | |
cout << "Invalid pin number!" << endl; | |
exit(EXIT_FAILURE); | |
} | |
int value = readAnalog(AINnum); | |
while (1) { | |
value = readAnalog(AINnum); | |
cout << "Input Value > " << value << " out of 4095." << " [Voltage: "<< ((float)value/(float)4095)*1.8 << "V]" << endl; | |
usleep(1000000); | |
} | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment