Skip to content

Instantly share code, notes, and snippets.

@likersacademia
Last active October 15, 2018 04:49
Show Gist options
  • Select an option

  • Save likersacademia/92e8100f5ae893ef33f3fe7cf4c20d48 to your computer and use it in GitHub Desktop.

Select an option

Save likersacademia/92e8100f5ae893ef33f3fe7cf4c20d48 to your computer and use it in GitHub Desktop.
// 電気が消え、かつスイッチが押されるとモーターがまわる
#define cdsPin 1 //光センサーが1番ピン
#define buttonPin 12 //スイッチが12番ピン
#include <Servo.h> //サーボモーターを動かす関数を用意します
#define moterPin 3 //モーターが3番ピン
bool buttonState = 0;
Servo myservo;
void setup() {
Serial.begin(9600);
pinMode(cdsPin, INPUT);
pinMode(buttonPin, INPUT);
myservo.attach(moterPin,700,2300);
}
void loop() {
// R1のAD値を取得
float cds_ad = analogRead(cdsPin);
// AD値を電圧値に変換
float cds_v = cds_ad * 5 / 1023;
// 電圧値より、Lux計算
float lux = 10000 * cds_v / (5 - cds_v) / 1000;
Serial.print(lux);
Serial.println(" Lux ");
if (lux > 20) {
Serial.println("High lux");
delay(2000);
} else {
Serial.println("Low lux");
delay(2000);
}
buttonState = digitalRead(buttonPin);
if (lux < 20 && buttonState == HIGH) { //lux < 20(20luxより小さい)→<、>と数値の変更ができます。buttonState == HIGH(スイッチが押される)
myservo.write(700); //700はモーターが一番早く回る数値です。時計回り(when 1450~700 µsec) 逆時計回り(when 1550~2300 µsec) 1500に近い方が回転がおそくなる
delay(200);
} else { //上以外のときは、下のことが起こる
myservo.write(1500); //1500はモーターが止まる数値です。変更可能です。
delay(200);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment