Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save wesbasinger/0b171eb1125182814ebaf4992ddfcfab to your computer and use it in GitHub Desktop.
Save wesbasinger/0b171eb1125182814ebaf4992ddfcfab to your computer and use it in GitHub Desktop.
read uart sensor
#include <Adafruit_ADS1X15.h>
#include <SoftwareSerial.h>
Adafruit_ADS1115 ads; /* Use this for the 16-bit version */
//Adafruit_ADS1015 ads; /* Use this for the 12-bit version */
SoftwareSerial mySerial(2, 3); // RX, TX
bool selfTestEn = false;
void setup(void) {
pinMode(4, OUTPUT); // self test
pinMode(5, OUTPUT); // mipex en
Serial.begin(57600);
Serial.println("Hello!");
Serial.println("Getting single-ended readings from AIN0..3");
Serial.println("ADC Range: +/- 6.144V (1 bit = 3mV/ADS1015, 0.1875mV/ADS1115)");
if (!ads.begin()) {
Serial.println("Failed to initialize ADS.");
while (1);
}
// set the data rate for the SoftwareSerial port
mySerial.begin(57600);
}
void loop(void) {
Serial.println("ADC Portion");
int16_t adc0, adc1, adc2;
float volts0, volts1, volts2;
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
if (!selfTestEn) {
selfTestEn = true;
digitalWrite(4, HIGH);
Serial.println("Self test enabled");
} else {
selfTestEn = false;
digitalWrite(4, LOW);
Serial.println("Self test disabled");
}
adc0 = ads.readADC_SingleEnded(0);
adc1 = ads.readADC_SingleEnded(1);
adc2 = ads.readADC_SingleEnded(2);
volts0 = ads.computeVolts(adc0);
volts1 = ads.computeVolts(adc1);
volts2 = ads.computeVolts(adc2);
Serial.println("-----------------------------------------------------------");
Serial.print("AIN0: "); Serial.print(adc0); Serial.print(" "); Serial.print(volts0); Serial.println("V");
Serial.print("AIN1: "); Serial.print(adc1); Serial.print(" "); Serial.print(volts1); Serial.println("V");
Serial.print("AIN2: "); Serial.print(adc2); Serial.print(" "); Serial.print(volts2); Serial.println("V");
// Send command to the peripheral device
mySerial.print("DATAE2\n");
// Initialize response storage
String response = "";
unsigned long startTime = millis();
unsigned long timeout = 1000; // 1-second timeout
// Read response with a non-blocking approach
while (millis() - startTime < timeout) {
while (mySerial.available()) {
char incomingByte = mySerial.read();
response += incomingByte;
Serial.print(response);
}
}
Serial.print("\n");
// Add a delay before sending the next command
delay(4000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment