// Tugas Mikroprosesor 2 Pasca UTS
// Azfar Azdi Arfakhsyad
// Ariq Naufal Fakri Wiratno
// Diamond Azzukhrif Muzayyin
// Hannan Nur Muhammad
// Muhammad Anwari Syukur
// Rizki Alfiansyah
#define outputA 6
#define outputB 7
int counter = 0;
int aState;
int aLastState;
float jarak;
int speed;
unsigned long newtime;
unsigned long oldtime = 0;
int ldr=A0;// sensor LDR di pin A0
int nilaildr=0;// nilai awal sensor LDR=0
int relaycuttoffmotor=5;//pin relay untuk cutoffmotor di pin 5
void setup() {
pinMode (outputA,INPUT);//output clk encoder
pinMode (outputB,INPUT);//output dt encoder
pinMode(5, OUTPUT);//relay sebagai output
Serial.begin (9600);
// Reads the initial state of the outputA
aLastState = digitalRead(outputA);
}
void loop() {
newtime= millis();
aState = digitalRead(outputA); // Reads the "current" state of the outputA
// If the previous and the current state of the outputA are different, that means a Pulse has occured
if (aState != aLastState){
// If the outputB state is different to the outputA state, that means the encoder is rotating clockwise
if (digitalRead(outputB) != aState) {
counter ++;
} else {
counter --;
}
Serial.print("Jarak: ");
jarak=map(counter,0,40,0,31.41);
Serial.print(jarak*-1);
Serial.println(" cm");
speed=jarak*1000/(newtime-oldtime);
Serial.print("Speed: ");
Serial.print(speed*-1);
Serial.println(" cm/s");
oldtime = newtime;
}
aLastState = aState; // Updates the previous state of the outputA with the current state
nilaildr=analogRead(ldr);
Serial.print("nilai ldr: ");
Serial.println(nilaildr);
if(nilaildr<700){ //Asumsi nilai 700 cairan kimia sudah keruh
digitalWrite(5,HIGH); //turn relay ON
Serial.print("Motor ON //");
}
else {
digitalWrite(5,LOW); //turn relay OFF*/
Serial.print("Motor OFF //");
}
}