#include <SoftwareSerial.h>
#include <LiquidCrystal_I2C.h>
#define SAMPLE_TIME 1500
#define MOTOR_TIME 2000
SoftwareSerial mySerial(2, 3); //RX,TX
LiquidCrystal_I2C lcd(0x27, 16, 2); //Adrress I2C, Baris, Kolom
const int enA = 9; // PIN D9 arduino -> PIN Enable A di modul L298D
const int in1 = 8; // PIN D8 arduino -> PIN IN1 di modul L298D
const int in2 = 7; // PIN D7 arduino -> PIN IN2 di modul L298D
const int buttonStart = 6; // PIN D6 Arduino -> output tombol start
const int buttonStop = 5; // PIN D6 Arduino -> output tombol start
const int buttonReset = 4; // PIN D4 Arduino -> output tombol reset
const int currentSensor = A0; // PIN A0 Arduino -> output current sensor
const int ldr = A1; // PIN A1 Arduino -> ouput LDR
const int dldr = 10; // PIN D10 Arduino -> ouput digital LDR
const int detector = 11;
int counter = 0;
bool pressed = false;
bool btnPressed = false;
uint8_t motorStatus = 0;
uint8_t stopStatus = 0;
char bufferData[40];
int detectorStatus = 0;
unsigned long previousMillis = 0;
unsigned long currentMillis = 0;
unsigned long previousMillisMotor = 0;
unsigned long currentMillisMotor = 0;
unsigned long previousMillisStop = 0;
unsigned long currentMillisStop = 0;
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
// Set pin for motor
pinMode(enA, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(buttonStart, INPUT);
pinMode(buttonStop, INPUT);
pinMode(buttonReset, INPUT);
pinMode(dldr, INPUT);
pinMode(detector, INPUT);
// Turn off motors - Initial state
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Paper Counter");
lcd.setCursor(0, 1);
lcd.print("IoT Based");
delay(2000);
lcd.clear();
}
void checkButton() {
detectorStatus = digitalRead(detector);
if ((digitalRead(buttonStop) == LOW) && (stopStatus == 0)) { //Prepare Turn Off Motor
currentMillisStop = millis();
previousMillisStop = millis();
stopStatus = 1;
digitalWrite(enA, HIGH);
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
motorStatus = 0;
}
else if (detectorStatus == 1) {
if ((digitalRead(buttonStart) == LOW) && (motorStatus == 0)) { //Turn On Motor
digitalWrite(enA, HIGH);
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
motorStatus = 1;
previousMillisMotor = millis();
currentMillisMotor = millis();
}
}
if ((digitalRead(buttonStop) == HIGH) && (stopStatus == 1)) {
stopStatus = 0;
}
currentMillisMotor = millis();
if (digitalRead(buttonReset) == LOW) {
currentMillisStop = millis();
previousMillisStop = millis();
stopStatus = 1;
digitalWrite(enA, HIGH);
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
counter = 0;
}
}
float readCurrent() {
unsigned int x=0;
float AcsValue=0.0,Samples=0.0,AvgAcs=0.0,AcsValueF=0.0;
for (int x = 0; x < 150; x++){ //Get 150 samples
AcsValue = analogRead(currentSensor); //Read current sensor values
Samples = Samples + AcsValue; //Add samples together
delay (3); // let ADC settle before next sample 3ms
}
AvgAcs=Samples/150.0;//Taking Average of Samples
AcsValueF = (2.5 - (AvgAcs * (5.0 / 1024.0)) )/0.185;
//Serial.println(AcsValueF);//Print the read current on Serial monitor
delay(50);
return AcsValueF;
}
void checkLDR() {
if (digitalRead(dldr) == HIGH && pressed == false) {
int readPaper = analogRead(ldr);
if ((150 < readPaper) && (readPaper <= 568)) {
counter = counter + 1;
}
else if ((568 < readPaper) && (readPaper <= 737)) {
counter = counter + 2;
}
else if ((737 < readPaper) && (readPaper <= 823)) {
counter = counter + 3;
}
else if ((823 < readPaper) && (readPaper <= 879)) {
counter = counter + 4;
}
pressed = true;
}
else if (digitalRead(dldr) == LOW && pressed == true) {
pressed = false;
}
}
void stopMotor() {
digitalWrite(enA, LOW);
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
motorStatus = 0;
}
void loop() {
checkButton();
if (stopStatus == 1) {
currentMillisStop = millis();
if ((currentMillisStop-previousMillisStop) > 2000) {
stopMotor();
}
}
checkLDR();
float current = readCurrent();
sprintf(bufferData, "#%s,%d,%d,%d",String(current).c_str(),motorStatus,counter,detectorStatus);
while (mySerial.available()){
char data = (char)mySerial.read();
if (data == '1') {
digitalWrite(enA, HIGH);
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
previousMillisMotor = millis();
currentMillisMotor = millis();
Serial.println(data);
motorStatus = 1;
}
else if (data == '0') {
digitalWrite(enA, LOW);
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(13, LOW);
motorStatus = 0;
Serial.println(data);
}
}
currentMillis = millis();
if ((currentMillis-previousMillis) > SAMPLE_TIME) {
previousMillis = currentMillis;
Serial.println(bufferData);
mySerial.println(bufferData);
}
if ((currentMillisMotor-previousMillisMotor) > MOTOR_TIME) {
previousMillisMotor = currentMillisMotor;
PORTB ^= (1 << PB0); // Toggle PIN in1 (D8)
PORTD ^= (1 << PD7); // Toggle PIN in2 (D7)
}
lcd.setCursor(0, 0);
lcd.print("Paper Count: ");
lcd.setCursor(0, 1);
lcd.print(counter);
lcd.print(" ");
delay(200);
}