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.

沒有留言:

張貼留言

只要點兩下,就可以將資料夾input內的所有Word通通轉成一個PDF

  系列文章: 1.  只要點兩下,就能將一堆的Doc與Docx 轉成 PDF 1. https://skjhcreator.blogspot.com/2023/05/docdocx-pdf.html 2. 只要點兩下,就能將一堆的JPG轉成一個PDF,並以JPG所在的資料夾名稱...