Last active
October 17, 2024 05:57
-
-
Save RyoKosaka/662b329dc663f56cb89a1167783f819c to your computer and use it in GitHub Desktop.
プロダクトデザイン応用実習サンプルコード - オルタネイト動作でLEDをふんわり点灯/消灯させるサンプルコード
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//プロダクトデザイン応用実習サンプルコード - オルタネイト動作でLEDをふんわり点灯/消灯させるサンプルコード | |
int led = 13; | |
bool flag = false; //直前のセンサーの状態を記録しておくための変数 | |
int threshold = 500; //閾値 | |
void setup() { | |
Serial.begin(9600); //シリアルモニタを使いますという宣言 | |
pinMode(led, OUTPUT); | |
} | |
void loop() { | |
int sensorValue = analogRead(A0); //A0ピンで読んだ値に名前をつける | |
Serial.print(sensorValue); //シリアルモニタに値を表示 | |
Serial.print(","); | |
Serial.println(flag); | |
//直前までセンサーに反応がなく、sensorValueが500以下になったとき | |
if (sensorValue < threshold && flag == false) { | |
flag = true; //flagをtrueに センサーが反応している状態の保存 | |
if (digitalRead(led) == LOW) { | |
for (int i = 0; i <= 255; i++) { | |
analogWrite(led, i); | |
delay(3); | |
} | |
} else { | |
for (int i = 255; i >= 0; i--) { | |
analogWrite(led, i); | |
delay(3); | |
} | |
} | |
} | |
//直前までセンサーに反応があり、sensorValueが500より大きくなったとき | |
if (sensorValue >= threshold + 100 && flag == true) { | |
flag = false; //flagをfalseに センサーが反応していない状態の保存 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment