Advertisement
Timmy_Time

GH - Ino

May 7th, 2024 (edited)
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <AddicoreRFID.h>
  2. #include <Servo.h>
  3. #include <SPI.h>
  4.  
  5. #define uchar unsigned char
  6. #define uint unsigned int
  7. #define MAX_LEN 16
  8.  
  9. uchar serNumA[5];
  10. uchar fifobytes;
  11. uchar fifoValue;
  12.  
  13. const int SOIL_PIN = A0;
  14. const int PHOTORESISTOR_PIN = A1;
  15. const int DOOR_SERVO_PIN = A5;
  16. const int WATER_SERVO_PIN = A3;
  17.  
  18. const int NRSTPD = 5;
  19. const int BUZZER_PIN = 2;
  20. const int CHIP_SELECT_PIN = 10;
  21.  
  22. const int SENSOR_INTERVAL = 1000;
  23. const int SERIAL_INTERVAL = 3000;
  24. const int RFID_INTERVAL = 2000;
  25.  
  26. int soilState;
  27. int photoresistorState;
  28.  
  29. unsigned long currentMillis = 0;
  30. unsigned long previousSensorMillis = 0;
  31. unsigned long previousSerialMillis = 0;
  32. unsigned long previousRFIDMillis = 0;
  33.  
  34. AddicoreRFID myRFID;
  35. Servo doorServo;
  36. Servo waterServo;
  37.  
  38. const uchar allowedID[5] = {160, 205, 147, 91, 165};  // ID RFID yang diizinkan
  39.  
  40. void setup() {
  41.   Serial.begin(9600);
  42.   SPI.begin();  // mulai library SPI
  43.  
  44.   pinMode(SOIL_PIN, INPUT);
  45.   pinMode(PHOTORESISTOR_PIN, INPUT);
  46.  
  47.   pinMode(NRSTPD, OUTPUT);
  48.   digitalWrite(NRSTPD, HIGH);
  49.   pinMode(BUZZER_PIN, OUTPUT);
  50.   pinMode(CHIP_SELECT_PIN, OUTPUT);
  51.   digitalWrite(CHIP_SELECT_PIN, LOW);
  52.  
  53.   myRFID.AddicoreRFID_Init();
  54.  
  55.   doorServo.attach(DOOR_SERVO_PIN);
  56.   doorServo.write(0);
  57.   waterServo.attach(WATER_SERVO_PIN);
  58.   waterServo.write(0);
  59. }
  60.  
  61.  
  62. void loop() {
  63.   currentMillis = millis();
  64.   readSensor();
  65.   readSerial();
  66.   readRFID();
  67. }
  68.  
  69.  
  70. void readSensor() {
  71.   if ((currentMillis - previousSensorMillis) >= SENSOR_INTERVAL) {
  72.     soilState = analogRead(SOIL_PIN);
  73.     Serial.println(String("s" + String(soilState)));
  74.     photoresistorState = analogRead(PHOTORESISTOR_PIN);
  75.     Serial.println(String("p" + String(photoresistorState)));
  76.     previousSensorMillis += SENSOR_INTERVAL;
  77.   }
  78. }
  79.  
  80.  
  81. void readSerial() {
  82.   if ((currentMillis - previousSerialMillis) >= SERIAL_INTERVAL) {
  83.     if (Serial.available() > 0) {
  84.       String incomingMessage = Serial.readStringUntil('\n');
  85.       if (incomingMessage == "L") {  // water content: LOW
  86.         tone(BUZZER_PIN, 100, 1000);  // nyalakan buzzer
  87.         waterServo.write(90);  // buka keran air
  88.       }
  89.       else if (incomingMessage == "H") {  // water content: HIGH
  90.         waterServo.write(0);  // tutup keran air
  91.       }
  92.     }
  93.  
  94.     previousSerialMillis += SERIAL_INTERVAL;
  95.   }
  96. }
  97.  
  98.  
  99. void readRFID() {
  100.   if ((currentMillis - previousRFIDMillis) >= RFID_INTERVAL) {
  101.     uchar i, tmp, checksum1;
  102.     uchar status;
  103.     uchar str[MAX_LEN];
  104.     uchar RC_size;
  105.     uchar blockAddr;
  106.     String mynum = "";
  107.     str[1] = 0x4400;
  108.  
  109.     status = myRFID.AddicoreRFID_Request(PICC_REQIDL, str);
  110.     if (status == MI_OK) {  // RFID tag detected
  111.     }
  112.  
  113.     // Anti-collision, return tag serial number 4 bytes
  114.     status = myRFID.AddicoreRFID_Anticoll(str);
  115.     if (status == MI_OK) {
  116.       checksum1 = str[0] ^ str[1] ^ str[2] ^ str[3];
  117.  
  118.       if (str[0] == allowedID[0] && str[1] == allowedID[1] && str[2] == allowedID[2] && str[3] == allowedID[3] && str[4] == allowedID[4]) {
  119.         // KTM Timothy
  120.         doorServo.write(90);
  121.         delay(3000);
  122.         doorServo.write(0);
  123.       }
  124.     }
  125.    
  126.     myRFID.AddicoreRFID_Halt();
  127.   }
  128. }
  129.  
  130.  
  131. void rotateServo() {
  132.   doorServo.write(90);  // putar servo ke posisi 90 derajat
  133.   delay(1000);
  134.   doorServo.write(0);  // kembalikan servo ke posisi awal
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement