mDot with ADS1115
Fork of mbed-os-example-mbed5-blinky by
Diff: main.cpp
- Revision:
- 40:1c33564c68c7
- Parent:
- 29:0b58d21e87d6
--- a/main.cpp Thu Jun 22 09:00:02 2017 +0100
+++ b/main.cpp Mon Jul 31 13:37:07 2017 +0000
@@ -1,12 +1,131 @@
-#include "mbed.h"
-
-DigitalOut led1(LED1);
-
-// main() runs in its own thread in the OS
-int main() {
- while (true) {
- led1 = !led1;
- wait(0.5);
- }
-}
-
+#include "mbed.h"
+#include "Adafruit_ADS1015.h"
+
+//Pin definition
+DigitalOut led1(PA_0); //External Red LED
+DigitalOut led2(PB_1); //Second LED (Green)
+DigitalOut reset(PA_7); //Pin to reset the ESP
+DigitalIn User_Button(PC_13); //User button
+DigitalOut Wire1(PA_4); //Sensor wire 1
+DigitalOut Wire2(PA_1); //Sensor wire 2
+DigitalOut Wire3(PC_1); //Sensor wire 3
+DigitalIn Sensor_Switch(PA_11); //Pin to switch the peripherals on/off
+DigitalOut SPI_cs(PB_0); // SPI chip select pin
+#define D_SDA PC_9 //I2C SDA pin
+#define D_SCL PA_8 //I2C SCL pin
+#define Serial2_tx PA_2 //Serial2 tx pin
+#define Serial2_rx PA_3 //Serial2 rx pin
+
+
+
+//Interfaces definition
+int BaudRate =115200;
+Serial pc(USBTX, USBRX,BaudRate); //tx, rx : PA_9,PA_10 : pc Serial
+Serial Serial2 (Serial2_tx,Serial2_rx,BaudRate); //tx, rx : Serial2 Serial
+I2C i2c(D_SDA, D_SCL); //I2C interface, pins define in "Pin definition"
+SPI spi(NC, PA_6, PA_5); //PI interface : mosi, miso, sclk
+Adafruit_ADS1115 ads(&i2c);//ADS1115 library though I2C
+
+//Function definition
+void ADSReadinmg();
+void SerialPrintData();
+
+//Global variable definition
+int Reading_Number=0;
+int data[12]= {0,3,0,0,0,0,0,0,0,0,0,32}; //Reading_Number, V_bat, Wire1_1, Wire1_2, Wire1_3, Wire2_1, Wire2_2, Wire2_3, Wire3_1, Wire3_2, Wire3_3, Temp
+
+
+int main()
+{
+ wait(3);
+ ads.setGain(GAIN_SIXTEEN); // set range to +/-0.256V
+ pc.printf("\r\n"); //New carage return + new line
+ pc.printf("App Started \r\n"); //New carage return + new line
+ SerialPrintData();
+ while (1) {
+ Reading_Number++;
+ data[0]=Reading_Number;
+ ADSReadinmg();
+ SerialPrintData();
+ led1 = !led1;
+ wait(1);
+ }
+}
+
+
+void ADSReadinmg()
+{
+ Wire1=0;
+ Wire2=0;
+ Wire3=0;
+ wait(1);
+
+ Wire1=1;
+ wait(1);
+ for(int i=1; i<4; i++) {
+ pc.printf("Wire1_X: %d\r\n",ads.readADC_SingleEnded(i)); //Wire 1
+ data[i+1]=ads.readADC_SingleEnded(i);
+ wait(1);
+ }
+ Wire1=0;
+ wait(1);
+
+ Wire2=1;
+ wait(1);
+ for(int i=1; i<4; i++) {
+ pc.printf("Wire2_?: %d\r\n",ads.readADC_SingleEnded(i)); //Wire 1
+ data[i+4]=ads.readADC_SingleEnded(i);
+ wait(1);
+ }
+ Wire2=0;
+ wait(1);
+
+ Wire3=1;
+ wait(1);
+ for(int i=1; i<4; i++) {
+ pc.printf("Wire3_?: %d\r\n",ads.readADC_SingleEnded(i)); //Wire 1
+ data[i+7]=ads.readADC_SingleEnded(i);
+ wait(1);
+ }
+
+ pc.printf("\r\n"); //Wire 1
+ Wire3=0;
+ wait(1);
+
+
+ /*
+ for(int i=0; i<4; i++) {
+ data[i+1]=ads.readADC_SingleEnded(i);
+ }
+ */
+}
+
+
+void SerialPrintData()
+{
+ pc.printf("\r\n"); //New carage return + new line
+ pc.printf("Reading #: %d\r\n",data[0]); //Reading #
+ pc.printf("V_Bar: %d\r\n",data[1]); //Baterry voltage
+
+ pc.printf("Wire1_1: %d\r\n",data[2]); //Wire 1
+ pc.printf("Wire1_2: %d\r\n",data[3]); //Wire2
+ pc.printf("Wire1_3: %d\r\n",data[4]); //Wire3
+
+ pc.printf("Wire2_1: %d\r\n",data[5]); //Wire 1
+ pc.printf("Wire2_2: %d\r\n",data[6]); //Wire2
+ pc.printf("Wire2_3: %d\r\n",data[7]); //Wire3
+
+ pc.printf("Wire3_1: %d\r\n",data[8]); //Wire 1
+ pc.printf("Wire3_2: %d\r\n",data[9]); //Wire2
+ pc.printf("Wire3_3: %d\r\n",data[10]); //Wire3
+
+ pc.printf("Temp: %d\r\n",data[11]); //Temperature
+ pc.printf("\r\n"); //New carage return + new line
+
+ /*
+ int DataElements=sizeof(data)/sizeof(data[0]);
+ for(int i=0; i<DataElements; i++) {
+ pc.printf("Ch%d: %d\r\n",i,data[i+1]);
+ }
+ */
+}
\ No newline at end of file
Jorge Troncoso
