記録

頑張って勉強したこと書くよ

1つのボタンと3つのLED

const int LED0 = 2;
const int LED1=7;
const int LED2=12;
const int BUTTON0 = 3; 

int val = 0;
void setup() {
  pinMode(LED0, OUTPUT); 
  pinMode(LED1, OUTPUT); 
  pinMode(LED2,OUTPUT); 
  pinMode(BUTTON0, INPUT);
  Serial.begin(9600);
}
void loop() {
  
  val = digitalRead(BUTTON0); 
  Serial.println(val);
  if (val == HIGH) {
    digitalWrite(LED0, HIGH);
    delay(1000);
    digitalWrite(LED0,LOW);
    delay(1000);
    digitalWrite(LED0,HIGH);
    delay(1000);
    digitalWrite(LED0,LOW);
    digitalWrite(LED1,HIGH);
    digitalWrite(LED2,HIGH);
    delay(1000);
    digitalWrite(LED1,HIGH);
    digitalWrite(LED2,LOW);
    delay(random(1,1000));
    digitalWrite(LED1,LOW);
    digitalWrite(LED2,HIGH);
    delay(random(1,1000));
    digitalWrite(LED1,HIGH);
    digitalWrite(LED2,LOW);
    delay(random(1,1000));
    digitalWrite(LED1,LOW);
    digitalWrite(LED2,HIGH);
    delay(random(1,1000));
    digitalWrite(LED1,HIGH);
    digitalWrite(LED2,LOW);
    delay(random(1,1000));
    digitalWrite(LED1,LOW);
    digitalWrite(LED2,HIGH);
    delay(random(1,1000));
    digitalWrite(LED1,HIGH);
    digitalWrite(LED2,LOW);
    delay(random(1,1000));
    digitalWrite(LED1,LOW);
    digitalWrite(LED2,HIGH);
    delay(random(1,1000));
     digitalWrite(LED1,HIGH);
    digitalWrite(LED2,LOW);
    delay(random(1,1000));
    digitalWrite(LED1,LOW);
    digitalWrite(LED2,HIGH);
    delay(random(1,1000));
    digitalWrite(LED1,HIGH);
    digitalWrite(LED2,LOW);
    delay(random(1,1000));
    digitalWrite(LED1,LOW);
    digitalWrite(LED2,HIGH);
    delay(random(1,1000));
    digitalWrite(LED1,HIGH);
    digitalWrite(LED2,LOW);
    delay(random(1,1000));
    digitalWrite(LED1,LOW);
    digitalWrite(LED2,HIGH);
    delay(random(1,1000));
    digitalWrite(LED1,HIGH);
    digitalWrite(LED2,LOW);
    delay(random(1,1000));
    digitalWrite(LED1,LOW);
    digitalWrite(LED2,HIGH);
    delay(random(1,1000));
    digitalWrite(LED1,HIGH);
    digitalWrite(LED2,LOW);
    delay(random(1,1000));
    digitalWrite(LED1,LOW);
    digitalWrite(LED2,HIGH);
    delay(random(1,1000));
    digitalWrite(LED1,HIGH);
    digitalWrite(LED2,LOW);
    delay(random(1,1000));
    digitalWrite(LED1,LOW);
    digitalWrite(LED2,HIGH);
    delay(random(1,1000));
    digitalWrite(LED1,HIGH);
    digitalWrite(LED2,LOW);
    delay(random(1,1000));
    digitalWrite(LED1,LOW);
    digitalWrite(LED2,HIGH);
    delay(random(1,1000));
    digitalWrite(LED1,HIGH);
    digitalWrite(LED2,LOW);
    delay(random(1,1000));
    digitalWrite(LED1,LOW);
    digitalWrite(LED2,HIGH);
    delay(random(1,1000));
   } else {
    digitalWrite(LED0, LOW);
}

前作った回路はそのままにコードだけ書き換えてみた。

button0を押す

LED0が2回光る(点灯1秒→消灯1秒→点灯1秒)

LED1と2が同時に光る(1秒)

LED1と2が交互に光る。
光る時間は0.001秒~1秒の間でランダムに光る。

とりあえず考えてみたのはこれだけ。
もう2つはまた別の書き方で動かしたい。