Read-Sensor-Resistances/nano_c_mk1/nano_c_mk1.ino

75 lines
3.1 KiB
Arduino
Raw Normal View History

2023-03-04 05:37:01 +00:00
/**
* @file nano_c_mk1.ino
* @author SEAL Ammonia subdivision
* @brief Firmware to allow a Arduino board to read the analog values from the ADS1115 and send them to the main board
* @version 0.1
* @date 2023-03-03
*
* @copyright Copyright (c) 2023
*
*/
2023-02-01 09:54:27 +00:00
#include <Adafruit_ADS1X15.h> // https://github.com/adafruit/Adafruit_ADS1X15, with partially its "continuous" example code integrated in ths
2023-01-31 09:22:12 +00:00
2023-03-04 05:37:01 +00:00
#define WAIT_TIME 1000UL //
2023-01-31 09:22:12 +00:00
#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
2023-02-02 20:54:10 +00:00
#define ADC1_ADDR 0x48 // Address (identifyer) for the first ADS1115 chip. We might gonna have up to four of those connected in one I2C line
2023-02-01 09:54:27 +00:00
#ifndef IRAM_ATTR
#define IRAM_ATTR
#endif
2023-01-31 09:22:12 +00:00
Adafruit_ADS1115 adc;
2023-02-01 09:54:27 +00:00
unsigned long currTime; // non-blocking timer
int channel = 0; // mux selector for which channel (AIN0-AIN3) to read the analog value from. See ADS1115 datasheet for details
2023-02-01 06:45:45 +00:00
float adcReadings[ADS_MAX]; // buffer to hold all values read from an adc
2023-02-01 09:54:27 +00:00
constexpr int READY_PIN = 3; // pin # that connects the "ALRT" pin of the ADS1115 to the board
volatile bool new_data = false; // "ready" flag for the ADS1115
2023-03-04 05:37:01 +00:00
/**
* @brief parse data from each channel (AIN0-AIN3) of ADS1115 to read the analog value from. See ADS1115 datasheet for details
*
* @acknowledgement https://github.com/RobTillaart/ADS1X15/blob/master/examples/ADS_continuous_4_channel/ADS_continuous_4_channel.ino
*/
2023-02-01 09:54:27 +00:00
void readData() {
// we only wanna grab/set data if the ADS is ready to
2023-02-02 20:54:10 +00:00
if (new_data) { // TODO: for some reason some board get the channel index off
2023-02-01 09:54:27 +00:00
// save the value
2023-02-02 20:54:10 +00:00
adcReadings[channel] = adc.getLastConversionResults();
2023-02-01 09:54:27 +00:00
channel = (channel + 1) % ADS_MAX; // request next channel #. Cycle back to zero if the number is larger than ADS_MAX (maybe must multiple of 2)
adc.startADCReading(MUX_BY_CHANNEL[channel], true);
new_data = false; // toggle back the flag
}
}
void IRAM_ATTR NewDataReadyISR() { // interrupt to make it read data
new_data = true;
}
2023-01-31 09:22:12 +00:00
void setup() {
Serial.begin(19200);
2023-02-02 20:54:10 +00:00
adc.begin(ADC1_ADDR, &Wire); // initialize one ADS1115 on approprate address
2023-02-01 09:54:27 +00:00
pinMode(READY_PIN, INPUT);
// "Ready" when on falling signal edge (negedge)
attachInterrupt(digitalPinToInterrupt(READY_PIN), NewDataReadyISR, FALLING);
adc.startADCReading(MUX_BY_CHANNEL[channel], true); // read once
currTime = millis(); // record the current time
2023-01-31 09:22:12 +00:00
}
void loop() {
2023-02-01 09:54:27 +00:00
readData(); // always read data from the ads1115
if (millis() - currTime >= WAIT_TIME) { // non-blocking time delay, giving ADC and everything else enough time to update
// print the array, as we don't have internal method for it.
2023-01-31 10:18:13 +00:00
Serial.print("[");
2023-02-01 09:54:27 +00:00
for (int i = 0; i < ADS_MAX; i++)
{
Serial.print(adcReadings[i] * VOLTAGE_RANGE / CONST_I2C);
if (i != ADS_MAX - 1) { // fence-post the array print
2023-01-31 10:18:13 +00:00
Serial.print(", ");
}
}
Serial.println("]");
2023-02-01 09:54:27 +00:00
currTime = millis(); // reset the current time
}
2023-01-31 09:22:55 +00:00
}