Stap 8: Program de Arduino
Upload de volgende code: (aanpassingen voor de pennen die u gebruikt)
//////////////////////////////////////////////////////////////////////////////////// // PERIPHERAL MAINS POWER CONTROL THROUGH SENSING THE MOTHERBOARD POWER LED // - Oscar /////////////////////////////////////////////////////////////////////////////////// pin that connects to the positive of the opto-isolator // for the on button leads // (if these don't work then try switching around the switch // leads from the opto-isolator to the remote) int powerOnPin = 10; // same for the off button int powerOffPin = 9;// input pin from the Motherboard's power LED (5v) int inputPin = A0;int arduinoLedPin = 13;boolean isPcOn = false; boolean wasPcOn = false;void setup() { // set inputs and outputs pinMode(inputPin, INPUT); pinMode(powerOnPin, OUTPUT); pinMode(powerOffPin, OUTPUT); pinMode(arduinoLedPin, OUTPUT); }// in the loop, we check to see if the PC is on and set the power LED accordingly // we then see if the power status has changed and if so, turn the power on or off void loop() { // find out if the PC is on by reading the Motherboard LED pin isPcOn = digitalRead(inputPin); // show the power status with flashing (on) or solid (off) LED if (isPcOn) { ledFlash(); } else { ledOn(); }// if the power status is different to before, turn power on or off if (isPcOn != wasPcOn) { if (isPcOn) power(true); else power(false); }// this is important so that we can tell if power state has changed from last time wasPcOn = isPcOn; }// method to switch on or off power via the remote void power(boolean powerCommand) { // button (on or off) that will be pressed int buttonToPress; // set which button will be pressed if (powerCommand) { buttonToPress = powerOnPin; } else { buttonToPress = powerOffPin; } // press the button for 100ms digitalWrite(buttonToPress, HIGH); delay(100); digitalWrite(buttonToPress, LOW); }// solid LED to show arduino knows the PC is off void ledOn() { digitalWrite(arduinoLedPin, HIGH); delay(100); }// short flash of the LED to show arduino knows PC is on void ledFlash() { digitalWrite(arduinoLedPin, LOW); delay(500); digitalWrite(arduinoLedPin, HIGH); delay(500); }