스마트화분 간단소스코드

스마트화분 간단소스코드

Makerist 0 796

// 토양 수분 센서의 핀
const int soilMoisturePin = A0;


// L9110s 모터 드라이버의 핀
const int motorAPin1 = 2; // A_1A
const int motorAPin2 = 3; // A_1B


// 모터 상태
const int MOTOR_STOP = 0;
const int MOTOR_FORWARD = 1;


// 수분 센서의 임계값 (수정 가능)
const int moistureThreshold = 500;


// 모터 회전 간격 (1초마다 회전)
const unsigned long motorRotationInterval = 1000;


// 마지막으로 모터를 제어한 시간
unsigned long lastMotorRotationTime = 0;


void setup() {
  // 토양 수분 센서의 핀을 입력으로 설정
  pinMode(soilMoisturePin, INPUT);


  // 모터 드라이버의 핀을 출력으로 설정
  pinMode(motorAPin1, OUTPUT);
  pinMode(motorAPin2, OUTPUT);
}


void loop() {
  // 현재 시간 저장
  unsigned long currentTime = millis();


  // 일정 간격마다 모터 제어
  if (currentTime - lastMotorRotationTime >= motorRotationInterval) {
    int moistureLevel = analogRead(soilMoisturePin);


    // 수분이 모자라면 모터를 회전
    if (moistureLevel < moistureThreshold) {
      controlMotor(MOTOR_FORWARD);
    }
    // 수분이 충분하면 모터를 멈춤
    else {
      controlMotor(MOTOR_STOP);
    }


    // 모터를 제어한 시간 갱신
    lastMotorRotationTime = currentTime;
  }
}


// 모터를 제어하는 함수
void controlMotor(int direction) {
  switch (direction) {
    case MOTOR_STOP:
      digitalWrite(motorAPin1, LOW);
      digitalWrite(motorAPin2, LOW);
      break;
    case MOTOR_FORWARD:
      digitalWrite(motorAPin1, HIGH);
      digitalWrite(motorAPin2, LOW);
      break;
  }
}

0 Comments
Category
반응형 구글광고 등
State
  • 현재 접속자 73 명
  • 오늘 방문자 1,341 명
  • 어제 방문자 3,368 명
  • 최대 방문자 4,586 명
  • 전체 방문자 676,601 명
  • 전체 게시물 26,447 개
  • 전체 댓글수 36 개
  • 전체 회원수 22 명
Facebook Twitter GooglePlus KakaoStory KakaoTalk NaverBand