#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <NeuralNetwork.h>
NeuralNetwork *NN;
const unsigned int layers[] = {3, 4, 1}; // 3 layers: 3 input neurons (color values), 4 hidden neurons, 1 output neuron
float *output; // Output layer's value
// Training data for fruit ripeness detection
const float inputs[4][3] = {
{0.1, 0.2, 0.3}, // Unripe
{0.4, 0.5, 0.6}, // Unripe
{0.7, 0.8, 0.9}, // Ripe
{0.2, 0.6, 0.4} // Ripe
};
const float expectedOutput[4][1] = {
{0}, // Unripe
{0}, // Unripe
{1}, // Ripe
{1} // Ripe
};
int buttonPin = 2; // Pin untuk tombol push
bool newDataAvailable = false; // Status data baru yang tersedia
LiquidCrystal_I2C lcd(0x27, 16, 2); // Inisialisasi objek LCD I2C dengan alamat 0x27 dan ukuran 16x2
void setup() {
Serial.begin(9600);
lcd.init(); // Inisialisasi LCD I2C dengan ukuran 16x2
lcd.backlight(); // Aktifkan backlight LCD
pinMode(buttonPin, INPUT_PULLUP); // Set pin tombol sebagai INPUT_PULLUP
NN = new NeuralNetwork(layers, sizeof(layers) / sizeof(layers[0])); // Initialize NeuralNetwork object
lcd.setCursor(0,0);
lcd.print("Training...");
// Train the neural network
for (int epoch = 0; epoch < 10000; epoch++) {
for (int i = 0; i < sizeof(inputs) / sizeof(inputs[0]); i++) {
NN->FeedForward(inputs[i]); // Feed forward the input
NN->BackProp(expectedOutput[i]); // Backpropagation with expected output
}
}
NN->print();
lcd.clear();
lcd.print("tekan tombol");
}
void loop() {
if (digitalRead(buttonPin) == LOW) { // Saat tombol ditekan
// Generate random color values
//lcd.setCursor(0,0);
lcd.clear();
lcd.print("Proses.....");
delay(1000);
lcd.clear();
float red = random(0, 255) / 255.0;
float green = random(0, 255) / 255.0;
float blue = random(0, 255) / 255.0;
// Create input array
float input[3] = {red, green, blue};
// Feed forward the input to get the output
output = NN->FeedForward(input);
// Determine the predicted fruit ripeness based on the output value
int predictedRipeness = output[0] >= 0.5 ? 1 : 0;
// Print the predicted fruit ripeness
Serial.print("Red: ");
Serial.print(red);
Serial.print(" - Green: ");
Serial.print(green);
Serial.print(" - Blue: ");
Serial.print(blue);
Serial.print(" - Predicted Ripeness: ");
if (predictedRipeness == 0) {
Serial.println("Mentah");
} else {
Serial.println("Matang");
}
// Display the result on
// Display the result on LCD
lcd.clear(); // Clear the LCD display
lcd.setCursor(0, 0); // Set the cursor to the first row
lcd.print("Red: ");
lcd.print(red);
lcd.setCursor(0, 1); // Set the cursor to the second row
lcd.print("Green: ");
lcd.print(green);
delay(2000); // Wait for 2 seconds to display the color values
lcd.clear(); // Clear the LCD display
lcd.setCursor(0, 0); // Set the cursor to the first row
lcd.print("Blue: ");
lcd.print(blue);
lcd.setCursor(0, 1); // Set the cursor to the second row
lcd.print("Ripeness: ");
if (predictedRipeness == 0) {
lcd.print("Mentah");
} else {
lcd.print("Matang");
}
newDataAvailable = true; // Set status data baru tersedia
}
// Reset status data baru tersedia setelah diproses
if (newDataAvailable) {
newDataAvailable = false;
delay(1000);
lcd.clear();
lcd.setCursor(2,0);
lcd.print("tekan tombol");
lcd.setCursor(3,1);
lcd.print("kembali..");
}
}