2020年2月1日 星期六

三個按鈕練習





























const int ledPin1 =  2;
const int ledPin2 =  3;
const int ledPin3 = 4;
const int buttonPin1 = 8;
const int buttonPin2 = 9;
const int buttonPin3 = 10;
int bz = 11;
int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;

void setup()
{
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
}
void loop()
{
  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);
  buttonState3 = digitalRead(buttonPin3);
  if (buttonState1 == LOW)
  {
    digitalWrite(ledPin1, HIGH);
    tone(bz, 100, 1000);
  }
  else if (buttonState2 == LOW)
  {
    digitalWrite(ledPin2, HIGH);
    tone(bz, 500, 1000);
  }
  else if (buttonState3 == LOW)
  {
    digitalWrite(ledPin3, HIGH);
    tone(bz, 1000, 1000);
  }
  else
  {
    // turn LED off:
    digitalWrite(ledPin1, LOW);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, LOW);
    noTone(bz);
  }
}

沒有留言:

張貼留言

algorithm

 #include <iostream> #include <string.h> using namespace std; int main(int argc, char** argv)  { for(int j=2;j<=100;j++)//j...