Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <AddicoreRFID.h>
- #include <Servo.h>
- #include <SPI.h>
- #define uchar unsigned char
- #define uint unsigned int
- #define MAX_LEN 16
- uchar serNumA[5];
- uchar fifobytes;
- uchar fifoValue;
- const int SOIL_PIN = A0;
- const int PHOTORESISTOR_PIN = A1;
- const int DOOR_SERVO_PIN = A5;
- const int WATER_SERVO_PIN = A3;
- const int NRSTPD = 5;
- const int BUZZER_PIN = 2;
- const int CHIP_SELECT_PIN = 10;
- const int SENSOR_INTERVAL = 1000;
- const int SERIAL_INTERVAL = 3000;
- const int RFID_INTERVAL = 2000;
- int soilState;
- int photoresistorState;
- unsigned long currentMillis = 0;
- unsigned long previousSensorMillis = 0;
- unsigned long previousSerialMillis = 0;
- unsigned long previousRFIDMillis = 0;
- AddicoreRFID myRFID;
- Servo doorServo;
- Servo waterServo;
- const uchar allowedID[5] = {160, 205, 147, 91, 165}; // ID RFID yang diizinkan
- void setup() {
- Serial.begin(9600);
- SPI.begin(); // mulai library SPI
- pinMode(SOIL_PIN, INPUT);
- pinMode(PHOTORESISTOR_PIN, INPUT);
- pinMode(NRSTPD, OUTPUT);
- digitalWrite(NRSTPD, HIGH);
- pinMode(BUZZER_PIN, OUTPUT);
- pinMode(CHIP_SELECT_PIN, OUTPUT);
- digitalWrite(CHIP_SELECT_PIN, LOW);
- myRFID.AddicoreRFID_Init();
- doorServo.attach(DOOR_SERVO_PIN);
- doorServo.write(0);
- waterServo.attach(WATER_SERVO_PIN);
- waterServo.write(0);
- }
- void loop() {
- currentMillis = millis();
- readSensor();
- readSerial();
- readRFID();
- }
- void readSensor() {
- if ((currentMillis - previousSensorMillis) >= SENSOR_INTERVAL) {
- soilState = analogRead(SOIL_PIN);
- Serial.println(String("s" + String(soilState)));
- photoresistorState = analogRead(PHOTORESISTOR_PIN);
- Serial.println(String("p" + String(photoresistorState)));
- previousSensorMillis += SENSOR_INTERVAL;
- }
- }
- void readSerial() {
- if ((currentMillis - previousSerialMillis) >= SERIAL_INTERVAL) {
- if (Serial.available() > 0) {
- String incomingMessage = Serial.readStringUntil('\n');
- if (incomingMessage == "L") { // water content: LOW
- tone(BUZZER_PIN, 100, 1000); // nyalakan buzzer
- waterServo.write(90); // buka keran air
- }
- else if (incomingMessage == "H") { // water content: HIGH
- waterServo.write(0); // tutup keran air
- }
- }
- previousSerialMillis += SERIAL_INTERVAL;
- }
- }
- void readRFID() {
- if ((currentMillis - previousRFIDMillis) >= RFID_INTERVAL) {
- uchar i, tmp, checksum1;
- uchar status;
- uchar str[MAX_LEN];
- uchar RC_size;
- uchar blockAddr;
- String mynum = "";
- str[1] = 0x4400;
- status = myRFID.AddicoreRFID_Request(PICC_REQIDL, str);
- if (status == MI_OK) { // RFID tag detected
- }
- // Anti-collision, return tag serial number 4 bytes
- status = myRFID.AddicoreRFID_Anticoll(str);
- if (status == MI_OK) {
- checksum1 = str[0] ^ str[1] ^ str[2] ^ str[3];
- if (str[0] == allowedID[0] && str[1] == allowedID[1] && str[2] == allowedID[2] && str[3] == allowedID[3] && str[4] == allowedID[4]) {
- // KTM Timothy
- doorServo.write(90);
- delay(3000);
- doorServo.write(0);
- }
- }
- myRFID.AddicoreRFID_Halt();
- }
- }
- void rotateServo() {
- doorServo.write(90); // putar servo ke posisi 90 derajat
- delay(1000);
- doorServo.write(0); // kembalikan servo ke posisi awal
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement