int led = 13;
const int bouton = 2;
boolean state;
boolean laststate;
void setup() {
// put your setup code here, to run once:
pinMode(led, OUTPUT);
Serial.begin(9600);
pinMode(bouton, INPUT);
digitalWrite(led, LOW);
}
void loop() {
// put your main code here, to run repeatedly:
state = digitalRead(bouton);
digitalWrite(led, state);
if (state == HIGH) {
Serial.println("on");
setRemoteState(5);
delay(5000);
} else {
Serial.println("off");
setRemoteState(4);
delay(5000);
}
}
void setRemoteState(char value){
Serial.write(0x7E); // start byte
Serial.write((byte)0x0);
Serial.write(0x10);
Serial.write(0x17);
Serial.write((byte)0x0);
// id of recipient or use 0xFFFF for broadcast
Serial.write((byte)00);
Serial.write((byte)00);
Serial.write((byte)00);
Serial.write((byte)00);
Serial.write((byte)00);
Serial.write((byte)00);
Serial.write(0xFF);
Serial.write(0xFF);
// 16 bit of reciepent
Serial.write(0xFF);
Serial.write(0xFE);
Serial.write(0x02);
Serial.write('D');
Serial.write('2');
Serial.write(value);
long sum = 0x17 + 0xFF + 0xFF + 0xFF + 0xFE + 0x02 + 'D' + '2' + value;
Serial.write(0xFF - ( sum & 0xFF) );
Serial.println(sum,HEX);
}

答案 0 :(得分:0)
看起来问题在于延迟(5000) 微控制器将在按钮状态的样本之间等待5秒。如果你删除延迟声明,它应该立即打开和关闭。
你应该尝试仅在状态改变时触发setRemoteState,因此它不会不断发送它。
之类的东西$body = @{
project_id = $project_id
task_token = $task_token
}
$action = {Invoke-RestMethod -Uri http://localhost/temp.php -Method Post -Body $body}
Register-ObjectEvent -InputObject $timer -EventName Elapsed -SourceIdentifier MyTimer -Action $action
答案 1 :(得分:-1)
我想到像这样的剧本!
String inputString;
int led = 13;
boolean state;
boolean laststate;
void setup() {
// put your setup code here, to run once:
pinMode(led, OUTPUT);
Serial.begin(9600);
Serial1.begin(9600);
}
void loop() {
while (Serial.available() ) {
// get the new byte:
delay(3);
char inChar = Serial.read();
// add it to the inputString:
inputString += inChar;
}
if (inputString.length() >0) {
Serial.println(inputString);
if (inputString == "on"){
digitalWrite(led,HIGH);
setRemoteState(5);
}
if (inputString == "off"){
digitalWrite(led,LOW);
setRemoteState(4);
}
inputString="";
}
}
void setRemoteState(char value){
Serial1.write(0x7E); // start byte
Serial1.write((byte)0x0);
Serial1.write(0x10);
Serial1.write(0x17);
Serial1.write((byte)0x0);
// id of recipient or use 0xFFFF for broadcast
Serial1.write((byte)00);
Serial1.write((byte)00);
Serial1.write((byte)00);
Serial1.write((byte)00);
Serial1.write((byte)00);
Serial1.write((byte)00);
Serial1.write(0xFF);
Serial1.write(0xFF);
// 16 bit of reciepent
Serial1.write(0xFF);
Serial1.write(0xFE);
Serial1.write(0x02);
Serial1.write('D');
Serial1.write('2');
Serial1.write(value);
long sum = 0x17 + 0xFF + 0xFF + 0xFF + 0xFE + 0x02 + 'D' + '2' + value;
Serial1.write(0xFF - ( sum & 0xFF) );
Serial1.print(sum,HEX);
}