男廁小便池自動感應沖手設備
第一次應女友所托,要在公司的男廁小便池安裝一個自動感應沖水設備一般我們在外的公廁,很少人會在小便後主動去沖水,尤其是用考克。
原本我的考量是買一個水閥用arduino nano控制,但仔細評估後,發現水管沒有合適的水閥可裝,想了幾天後,有個靈感告訴我,可以用伺服馬達做個連桿來推拉考克,於是我就從程式開始著手……
先上安裝好測試的視頻瞧瞧
材料
MG996R x1
Arduino nano x1
Sharp 紅外線測距模組 (GP2Y0A02YK0F) x1
pca9685 x1
3D列印外殼
盒-https://www.tinkercad.com/things/1khLTXx7P3h-irbox
蓋-https://www.tinkercad.com/things/keXzxpyY8vK-ircover
** 伺服馬達盒-https://www.tinkercad.com/things/7wSmGhsgi9x-servobracket-2**
杜邦線 若干
接線
**
測試紅外線測距模組
首先先測試紅外線模組的距離,量測現場環境要感應的範圍
套用在程式裡。
紅外線模組可參考此篇—Sharp紅外線模組測試
或是可參考這篇https://robojax.com/learn/arduino/?vid=robojax-sharp_IR
條件參數設好之後加入伺服馬達的動作角度
這裡必須配合要裝的環境,修正伺服馬達的運轉角度
首先要載入pca9685舵控板的庫,配合紅外測距模組的動
作區間值,使其轉向設定的角度。
程式碼
#include <SharpIR.h>
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
const int servomin=150;
const int servomax=600;
int sensor;
unsigned long time_old=0;
#define IR A2 // define signal pin
#define model 20150 // used 1080 because model GP2Y0A21YK0F is used
// Sharp IR code for Robojax.com
// ir: the pin where your sensor is attached
// model: an int that determines your sensor: 1080 for GP2Y0A21Y
// 20150 for GP2Y0A02Y
// 430 for GP2Y0A41SK
/*
2 to 15 cm GP2Y0A51SK0F use 1080
4 to 30 cm GP2Y0A41SK0F / GP2Y0AF30 series use 430
10 to 80 cm GP2Y0A21YK0F use 1080
10 to 150 cm GP2Y0A60SZLF use 10150
20 to 150 cm GP2Y0A02YK0F use 20150
100 to 550 cm GP2Y0A710K0F use 100550
*/
SharpIR SharpIR(IR, model);
void setup() {
Serial.begin(9600);
Serial.println("Robojax Sharp IR ");
pwm.begin();
pwm.setOscillatorFrequency(27000000);
pwm.setPWMFreq(60);
pwm.setPWM(14, 0, 590);
delay(10);
}
void loop() {
delay(500);
int dis=SharpIR.distance(); // this returns the distance to the object you're measuring
Serial.print("Mean distance: "); // returns it to the serial monitor
Serial.println(dis);
if (dis<20){
Serial.println("close sw");
}else if(dis>=20 && dis<=70){
unsigned long time_now=millis();
if (time_now-time_old>600){
Serial.println("open water");
pwm.setPWM(14, 0, 300);
delay(6000);
pwm.setPWM(14, 0, 590);
time_now=time_old;
}
}
}