Created
May 20, 2018 13:23
-
-
Save naokirin/f3463ff93e5e27727198d3e35a3cc1c2 to your computer and use it in GitHub Desktop.
This file contains 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
int BUTTON_PIN = 0; | |
int MAX_NUM_PIN = A0; | |
int OUTPUT_PIN = 5; | |
int button = LOW; | |
bool started = false; | |
bool end = false; | |
int max = 6; | |
int count = 0; | |
int value = 0; | |
void setup() { | |
pinMode(BUTTON_PIN, INPUT); | |
// ③ A1ピンの入力を乱数のシード値に利用する | |
randomSeed(A1); | |
} | |
void decideMax() { | |
// ① [0, 1023] の範囲の入力を [0, 99] の範囲にマッピングする | |
max = map(analogRead(MAX_NUM_PIN), 0, 1023, 0, 99); | |
// ② [0, 255] の範囲で出力されるようにする | |
analogWrite(OUTPUT_PIN, map(max, 0, 99, 0, 255)); | |
} | |
void draw() { | |
if (count <= 100) { | |
value = random(1, max + 1); | |
// ② [0, 255] の範囲で出力されるようにする | |
int output = map(value, 0, 99, 0, 255); | |
analogWrite(OUTPUT_PIN, output); | |
delay(10); | |
++count; | |
} | |
else { | |
// ② [0, 255] の範囲で出力されるようにする | |
int output = map(value, 0, 99, 0, 255); | |
analogWrite(OUTPUT_PIN, output); | |
count = 0; | |
end = true; | |
} | |
} | |
void startLot() { | |
started = true; | |
} | |
void finishLot() { | |
end = false; | |
started = false; | |
delay(600); | |
} | |
void loop() { | |
button = digitalRead(BUTTON_PIN); | |
// 最大値を決める | |
if (!started) { | |
decideMax(); | |
} | |
// 抽選を開始する | |
if (!started && button == HIGH) { | |
startLot(); | |
} | |
// 抽選する | |
if (started && !end) { | |
draw(); | |
} | |
// 抽選を終了する | |
if (end && button == HIGH) { | |
finishLot(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment