2023-01-31 09:22:55 +00:00
# include <Adafruit_ADS1X15.h> // https://github.com/adafruit/Adafruit_ADS1X15
2023-01-31 09:22:12 +00:00
# define WAIT_TIME 1000UL // default wait time each value read
# define ADS_MAX 4 // # of analog input pins of an ADS1115
2023-02-01 06:45:45 +00:00
# define VOLTAGE_RANGE 6.144f // ADS1115 default gain (+/- 6.144V). Change this number smaller will reduced its actual magnutude, and vice versa
2023-01-31 09:22:12 +00:00
# define CONST_I2C 32768 // a constant value
unsigned long currTime ; // non-blocking timer
Adafruit_ADS1115 adc ;
int i ; // loop iterators
2023-02-01 06:45:45 +00:00
float adcReadings [ ADS_MAX ] ; // buffer to hold all values read from an adc
2023-01-31 09:22:12 +00:00
void setup ( ) {
Wire . begin ( ) ;
Serial . begin ( 19200 ) ;
adc . begin ( ) ;
currTime = millis ( ) ;
}
void loop ( ) {
2023-02-01 06:45:45 +00:00
// TODO: The current reading is too slow beacuse `readADC_SingleEnded` is a blocking function. Not ideal on real-application.
2023-01-31 09:22:12 +00:00
if ( millis ( ) - currTime > = WAIT_TIME ) { // non-blocking time delay
2023-01-31 10:18:13 +00:00
for ( i = 0 ; i < ADS_MAX ; i + + ) {
2023-02-01 06:45:45 +00:00
adcReadings [ i ] = adc . readADC_SingleEnded ( i ) * VOLTAGE_RANGE / CONST_I2C ; // this part can just read the adc. The conversion part could done in the Python code for flexibility
2023-01-31 09:22:12 +00:00
}
2023-01-31 10:18:13 +00:00
Serial . print ( " [ " ) ;
2023-02-01 06:45:45 +00:00
// two seperate for loops for read adc and serial write to make it seems less laggy. again, this is completly wrong and need to replace it to something more non-blocking
2023-01-31 10:18:13 +00:00
for ( i = 0 ; i < ADS_MAX ; i + + ) {
Serial . print ( adcReadings [ i ] ) ;
if ( i ! = ADS_MAX - 1 ) {
Serial . print ( " , " ) ;
}
}
Serial . println ( " ] " ) ;
currTime = millis ( ) ;
2023-01-31 09:22:12 +00:00
}
2023-01-31 09:22:55 +00:00
}