#include <LiquidCrystal_I2C.h>
#include <WiFi.h>
#include "ThingSpeak.h"
#include <Wire.h>
#include <DFRobot_ADS1115.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
DFRobot_ADS1115 ads(&Wire);
int16_t Raw;
int16_t adc0, adc1, adc2, adc3;
float volts0, volts1, volts2, volts3;
int16_t mVsensor1, mVsensor2, mVsensor3;
String kondisiBuah;
char rawIinput[40];
uint16_t sensorDataArray[3] = {};
int counter = 0;
String inString = "";
String dataSensor1; //data String
String dataSensor2; //data String
String dataSensor3; //data String
// Neural Network Constants
const float x1_xoffset[3] = {204, 347, 498};
const float x1_gain[3] = {0.000769822940723634, 0.000485083676934271, 0.000507872016251904};
const float x1_ymin = -1;
// Layer 1
const float b1[3] = {-0.87085459544090348949, -1.8304478896182221348, -2.0406971720356703592};
const float IW1_1[3][3] = {
{3.2587232865308464191, 0.99038146993084930081, 1.5118115008354817697},
{3.3304866802176937846, 0.98307291099736404671, 4.2364332413093377383},
{-1.3090364286156148665, -0.58868635165202010118, 1.2396102658128185237}
};
// Layer 2
const float b2 = 1.1547200452001507021;
const float LW2_1[3] = {-5.0122745796862417933, -10.063040696826149656, 1.126059049957864211};
// Sigmoid Positive Transfer Function
float logsig_apply(float n) {
return 1.0 / (1.0 + exp(-n));
}
// Sigmoid Symmetric Transfer Function
float tansig_apply(float n) {
return 2.0 / (1.0 + exp(-2 * n)) - 1;
}
// Map Minimum and Maximum Input Processing Function
void mapminmax_apply(float x[], float y[], int size) {
for (int i = 0; i < size; i++) {
y[i] = (x[i] - x1_xoffset[i]) * x1_gain[i] + x1_ymin;
}
}
// Neural Network Simulation Function
void myNeuralNetworkFunction(float x1[], float y1[], int Q) {
float xp1[3];
mapminmax_apply(x1, xp1, 3);
float a1[3];
for (int i = 0; i < 3; i++) {
a1[i] = tansig_apply(b1[i] + IW1_1[i][0] * xp1[0] + IW1_1[i][1] * xp1[1] + IW1_1[i][2] * xp1[2]);
}
float a2 = logsig_apply(b2 + LW2_1[0] * a1[0] + LW2_1[1] * a1[1] + LW2_1[2] * a1[2]);
y1[0] = a2;
}
void setup() {
Serial.begin(115200);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Deteksi Buah Segar &");
lcd.setCursor(0, 1);
lcd.print(" Busuk berbasis JST ");
lcd.setCursor(0, 2);
lcd.print("TGS2600,2602&TGS822 ");
lcd.setCursor(0, 3);
lcd.print(" ============= ");
ads.setAddr_ADS1115(ADS1115_IIC_ADDRESS0); // 0x48
ads.setGain(eGAIN_TWOTHIRDS); // 2/3x gain
ads.setMode(eMODE_SINGLE); // single-shot mode
ads.setRate(eRATE_128); // 128SPS (default)
ads.setOSMode(eOSMODE_SINGLE); // Set to start a single-conversion
ads.init();
lcd.clear();
delay(100);
}
void loop() {
float y1[1];
mVsensor1 = ads.readVoltage(0);
mVsensor2 = ads.readVoltage(1);
mVsensor3 = ads.readVoltage(2);
float x1[3] = {mVsensor1, mVsensor2, mVsensor3};
myNeuralNetworkFunction(x1, y1, 3);
lcd.setCursor(0, 0);
lcd.print("Deteksi Buah Sgr&Bsk");
lcd.setCursor(0, 1);
lcd.print("S1:");lcd.print(mVsensor1);lcd.print(" ");
lcd.setCursor(0, 2);
lcd.print("S2:");lcd.print(mVsensor2);lcd.print(" ");
lcd.setCursor(0, 3);
lcd.print("S3:");lcd.print(mVsensor3);lcd.print(" ");
Serial.print(mVsensor1);Serial.print(",");Serial.print(mVsensor2);Serial.print(",");Serial.println(mVsensor3);
if(y1[0]<0.5){
kondisiBuah = "Busuk";
}
if(y1[0]>0.8){
kondisiBuah = "Segar";
}
lcd.setCursor(8, 1);
lcd.print(y1[0],9);
lcd.setCursor(8, 2);
lcd.print("Buah: ");lcd.print(kondisiBuah);
delay(1000);
}