2018年10月10日

bpi-bit arduino 數位類比pwm

使用bpi-bit 在arduino ide上使用數位角位輸出-P2

參考網址:https://www.cnblogs.com/lulipro/p/8046248.html
void setup() {
  // put your setup code here, to run once:
  pinMode(P2, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(P2, HIGH);
  delay(500);
  digitalWrite(P2, LOW);
  delay(500);
}

要使用pwm來做呼吸燈


int freq = 5000;
int ledChannel = 0;
int resolution = 8;

void setup() {
   ledcSetup(ledChannel, freq, resolution);
  ledcAttachPin(P1, ledChannel);
 }
void loop()
{
  for (int dutyCycle = 0; dutyCycle <= 255; dutyCycle++) {
    ledcWrite(ledChannel, dutyCycle);
    delay(7);
  }

  for (int dutyCycle = 255; dutyCycle >= 0; dutyCycle--) {
    ledcWrite(ledChannel, dutyCycle);
    delay(7);
  }

}

 

P1類比輸入 

把類比感應器接在P1,使用serial monitor秀出。

void setup() {
  // put your setup code here, to run once:
    Serial.begin(115200); //Serial Port Config 115200-8-N-1
    pinMode(P1, INPUT_PULLUP);
}

void loop() {
  // put your main code here, to run repeatedly:
      Serial.println();
      Serial.println(analogRead(P1));
      delay(500);;

}

P1數位輸入

把數位感應器接在P1(按鈕),使用serial monitor秀出
void setup() {
  // put your setup code here, to run once:
    Serial.begin(115200); //Serial Port Config 115200-8-N-1
    pinMode(P1, INPUT);//Botton B Pin Mode :Digital Input
}

void loop() {
  // put your main code here, to run repeatedly:
  if (digitalRead(P1) == LOW)
    {
      delay(100);
   if (digitalRead(P1) == LOW)
    {
      Serial.println();
      Serial.println(LOW);
    }else{
      Serial.println();
      Serial.println(HIGH);
    }
   }
 
}

沒有留言: