標籤

bat (52) 作品 (38) python (21) shell (17) windows (11) 虛擬機 (11) php (10) CPP (6) KMS (6) 程式設計 (6) docker (5) 使用教學 (5) xoops (4) 公文 (4) Apache2 (3) Excel (3) juniper (3) 資料庫 (3) mysql (2) 免動手 (2) 資料結構 (2) 軟體廣播 (2) 電腦維修 (2) Android Studio (1) Apple IPAD管理 (1) Arduino (1) CSS (1) LAMP (1) NAS (1) Ubuntu (1) VHD (1) Windows Server (1) 原因 (1) 程式應用 (1) 程式積木 (1) 編輯器 (1) 雲端硬碟 (1)

2021年3月4日 星期四

只要關燈,電腦自動關機(使用Arduino Leonardo)

 一、成果影片

二、應用情境:
1.夜深人靜,用完電腦。只想上床睡覺。只要關燈,電腦就關機。

三、使用技能:
1.使用Arduino Leonardo
2.光敏電阻-感應關燈
3.鍵盤-命令電腦關機

四、接線圖:

五、圖形化程式碼(ArduBlock):
六、Arduino 程式碼
檔名:ProjectNo0001
內容:
#include <Keyboard.h>

/*
 These core libraries allow the 32u4 and SAMD based boards 
 (Leonardo, Esplora, Zero, Due and MKR Family) 
 to appear as a native Mouse and/or Keyboard to a connected computer.
 */

void setup(){  // put your setup code here, to run once:
  Keyboard.begin(); // initialize control over the keyboard
  Keyboard.releaseAll();

}

void loop(){  // put your main code here, to run repeatedly:
  if ( analogRead( A0 ) < 600 ) {
    PcShutDown();
  }
}

void PcShutDown() { //customised function

    pinMode(4,INPUT_PULLUP); //make pin 4 an input and turn on the pullup resistor so it goes high unless connected to ground
  if(digitalRead(4)==LOW){  // do nothing until pin 4 goes low
    Keyboard.press(131); //the key to press (ASCII code)
  }
  pinMode(4,INPUT_PULLUP); //make pin 4 an input and turn on the pullup resistor so it goes high unless connected to ground
  if(digitalRead(4)==LOW){  // do nothing until pin 4 goes low
    Keyboard.press('r'); //the key to press (ASCII code)
  }
  Keyboard.releaseAll();

  pinMode(4,INPUT_PULLUP); //make pin 4 an input and turn on the pullup resistor so it goes high unless connected to ground
  if(digitalRead(4)==LOW){  // do nothing until pin 4 goes low
    Keyboard.print("shutdown /s /f /t 0"); //Send the message
  }
  Keyboard.releaseAll();

  pinMode(4,INPUT_PULLUP); //make pin 4 an input and turn on the pullup resistor so it goes high unless connected to ground
  if(digitalRead(4)==LOW){  // do nothing until pin 4 goes low
    Keyboard.press(176); //the key to press (ASCII code)
  }
  Keyboard.releaseAll();
  delay( 5000 ); // waits a few milliseconds 
}


資料來源:
1.【Arduino冷知識5】
3.

沒有留言:

張貼留言

只要點兩下,傳統右鍵選單改回Win11右鍵選單

系列文章: 1. 只要點兩下,就能將Win11 右鍵選單 回復 傳統右鍵選單 2. 只要點兩下,傳統右鍵選單改回Win11右鍵選單 上一篇提到只要點兩下,就能將Win11選單回到傳統選單。但是有沒有方法能夠回到Win11選單呢?                    ...