I'm trying to read an ADC (12 bit which is 0 - 4095) input from my AIN0 channel, and using that as an "int" so I could use it in a math function. Is this possible?
The directory I'm referring to is "sys/bus/iio/devices/iio:device0/in_voltage0_raw" on the Beaglebone Black Debian Wheezy.
Currently, I have a C file that reads the user's input (through terminal) and does the math function I need it to do, but I'm having a hard time wrapping my head around this active/constantly changing ADC value. I've looked into using "fopen" functions as well. Using the code below, I'm able to get the ADC value on the terminal, and it'll change based on how much volts go in. Is there a way to "grab" the input from the ADC and use it in a math function, even if the ADC value constantly changes?
#define SYSFS_ADC_DIR "/sys/bus/iio/devices/iio:device0/in_voltage0_raw"
#define MAX_BUFF 64
int main(){
int fd;
char buf[MAX_BUFF];
char ch[5]; //Update
ch[4] = 0; //Update
int i;
for(i = 0; i < 30; i++)
{
snprintf(buf, sizeof(buf), SYSFS_ADC_DIR);
fd = open(buf, O_RDONLY);
read(fd,ch,4);
printf("%s\n", ch);
close(fd);
usleep(1000);
}
}
Updated Code
I've made the changes to char ch[5], I've also gotten a little farther in the code putting the math functions I wanted.
int AIN0_low = 0; //lowest input of adc
int AIN0_high = 4095; //highest input of adc
int motor_low = 0; //lowest speed value for motor
int motor_high = 3200; //highest speed value for motor
double output = 0;
int main(){
double fd;
char buf[MAX_BUF];
char ch[4] = 0;
int i;
for(i = 0; i < 30; i++)
{
snprintf(buf, sizeof(buf), SYSFS_ADC_DIR);
fd = open(buf, O_RDONLY);
read(fd, ch, 4);
double slope = 1.0 * (motor_high - motor_low) / (AIN0_high - AIN0_low);
output = motor_low + slope * (ch - AIN0_low);
printf("%f\n", output);
close(fd);
usleep(1000);
}
}