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.

沒有留言:

張貼留言

只要點兩下兩次,就能依照設定拆分指定PDF頁數並合併成設定好的一個PDF

        最近遇到一個問題,想要對四個不同的PDF,分別擷取1~2頁、3~4頁、5~6頁、7~8頁,合併成一個總共8頁的PDF。那該怎麼寫程式來處理這件事呢?那如果是用線上拆分PDF網頁與線上合併PDF網頁,該怎麼做呢?則是需要將四個不同PDF分別上傳到拆分網頁後再 一頁一...