Yorug'lik sensori bo'lgan dasturga misol
Ushbu misolda pardalar yorug'likka qarab ochiladi va yopiladi. Bizga IR qabul
qilgich kerak emas, lekin yorug'lik sensori (Troyka moduli) va potansiyometrni
(Troyka moduli) qo'shishimiz kerak.
// Servolar bilan ishlash uchun kutubxonani ulang
#include
// ishlatiladigan pinlar sonini aniqlang
// yorug'lik sensori
#define LIGHT_PIN A0
// yorug'likka sezgirlik
#define POT_PIN A5
// servo pin
#define SERV_PIN 3
// tweeter pin
#define BUZZER_PIN 4
// "harakat" turini aniqlangenum Command
{
CMD_NONE = 0,
CMD_OPEN = 1,
CMD_CLOSE = 2
};
// dastur boshida biz panjurlar ochiq yoki ochiq emasligini bilmaymiz
int cmd = CMD_NONE;
// doimiy aylanish servosini boshqarish uchun ob'ekt yaratish
Servo srv;
void setup()
{
// foydalanuvchiga biz uch martalik cho'qqi bilan boshlaganimiz haqida xabar bering
tone(BUZZER_PIN, 500, 100);
delay(200);
tone(BUZZER_PIN, 500, 100);
delay(200);
tone(BUZZER_PIN, 500, 100);
delay(200);
}
void loop()
{
int light = analogRead(LIGHT_PIN);
int lightSens = analogRead(POT_PIN);
Serial.print(light);
Serial.print("\t\t");
Serial.print(lightSens);
Serial.print("\t\t");
Serial.println(cmd);
if (light > lightSens && (cmd == CMD_OPEN || cmd == CMD_NONE)) {
srv.attach(SERV_PIN);
srv.write(120);
// pardalarning belgilangan ochilish/yopilish vaqtini kuting
delay(2000);
// doimiy aylanish servosini to'xtatishning eng oson yo'li
// undan ajrating srv.detach();
cmd = CMD_CLOSE;
} else if (light < lightSens && (cmd == CMD_CLOSE || cmd == CMD_NONE)) {
srv.attach(SERV_PIN);
// pardalarning belgilangan ochilish/yopilish vaqtini kuting
srv.write(30);
delay(2000);
// doimiy aylanish servosini to'xtatishning eng oson yo'li
// undan ajrating srv.detach();
cmd = CMD_OPEN;
}
}
Do'stlaringiz bilan baham: |