#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
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("Ujicoba Nilai Sensor");
lcd.setCursor(0, 1);
lcd.print("Entry your data via ");
lcd.setCursor(0, 2);
lcd.print("Serial Terminal IDE ");
lcd.setCursor(0, 3);
lcd.print(" ex.1000,2000,3000 ");
Serial.println("Input your testing value: ");
delay(3000);
}
void loop() {
float y1[1];
if(Serial.available()>0) {
Serial.readBytesUntil('\n', rawIinput, 40);
char* pch = strtok(rawIinput,"[],\r\n");
while(pch != NULL){
sensorDataArray[counter] = atoi(pch);
pch = strtok(NULL, "[],\r\n");
counter += 1;
mVsensor1 = sensorDataArray[0];
mVsensor2 = sensorDataArray[1];
mVsensor3 = sensorDataArray[2];
float x1[3] = {mVsensor1, mVsensor2, mVsensor3};
myNeuralNetworkFunction(x1, y1, 3);
lcd.setCursor(0, 0);
lcd.print("Ujicoba Nilai Sensor");
lcd.setCursor(0, 1);
lcd.print("Nilai:");lcd.print(mVsensor1);lcd.print(",");lcd.print(mVsensor2);lcd.print(",");lcd.print(mVsensor3);lcd.print(" ");
lcd.setCursor(0, 2);
lcd.print("Output:");lcd.print(y1[0],10);lcd.print("");
if(y1[0]<0.5){
kondisiBuah = "Busuk";
}
if(y1[0]>0.8){
kondisiBuah = "Segar";
}
lcd.setCursor(0, 3);
lcd.print("Kondisi Buah: ");lcd.print(kondisiBuah);lcd.print("");
}
counter = 0;
Serial.print ("[");
Serial.print(mVsensor1);
Serial.print (",");
Serial.print(mVsensor2);
Serial.print (",");
Serial.print(mVsensor3);
Serial.print ("]");
Serial.println();
Serial.print("Output Nilai:");
Serial.println(y1[0],10);
Serial.print("Kondisi Buah: ");Serial.println(kondisiBuah);
}
delay(1000);
}