升级了下之前的《厕所状态指示旋转牌》,更新了外观、还有电子结构和软件工作方式,
b站介绍新视频:卫生间竟然没装门?别慌,做个神器帮你搞定!【卫生间旋转指示牌】 #废柴发明# 第四弹 @hardihuang
这是第一次用单反录的第一版视频,里面有详细的制作过程:
视频里都有详细介绍,这里就不过多赘述了。
规划:
拍摄:
剪片子:
代码:
#include
Servo myservo; // create servo object to control a servo
int photocellPin = 1;
int photocellReading;
int lightOnValue = 350;
int lightOffValue = 100;
int peopleState = false;
void setup() {
pinMode(2, INPUT_PULLUP);
pinMode(4, OUTPUT);
digitalWrite(4, LOW);
myservo.attach(3); // attaches the servo on GIO2 to the servo object
//Serial.begin(9600);
myservo.write(0);
}
void loop() {
int pos;
photocellReading = analogRead(photocellPin);
//Serial.println(photocellReading);
delay(100);
if(photocellReading < lightOffValue && peopleState == true){//dark peopleState = false; digitalWrite(4, LOW); //servo to 0
myservo.attach(3);
for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
myservo.detach();
}else if(photocellReading > lightOnValue && peopleState == false){//light on
peopleState = true;
digitalWrite(4, HIGH);
//servo to 180
myservo.attach(3);
for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
myservo.detach();
}
}
遇到的问题:
- servo如果在静止状态,由于某些原因一直响,刚开始以为是旋转盘太沉的过,后来查了下发现很多人都有这个问题,一般处理方法是,等servo转到预定位置后,用servo.detach(),来断掉和servo的连接,等需要旋转时,再用servo.attach(servoPin)来重新链接,换上新的代码后,不再用奇怪的响声出现了