با سلام
کد ذیل را تست کنید
const int led1=5;
const int led2=6;
const int led3=7;
const int led4=8;
const int led5=9;
const int key1=A0;
const int key2=15;
const int key3=14;
const int key4=16;
const int key5=10;
String outputString = "";
String inputString = "";
char temp;
char inputString_index=0;
bool send10=true,send11=false,send20=true,send21=false,send30=true,send31=false,send40=true,send41=false,send50=true,send51=false;
void setup() {
// put your setup code here, to run once:
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT);
pinMode(key1,INPUT_PULLUP);
pinMode(key2,INPUT_PULLUP);
pinMode(key3,INPUT_PULLUP);
pinMode(key4,INPUT_PULLUP);
pinMode(key5,INPUT_PULLUP);
outputString.reserve(10);
inputString.reserve(40);
Serial.begin(9600);
while (!Serial) {
;
}
ersal();
}
void loop() {
if (send11==false && digitalRead(key1)==HIGH){ersal();send11=true;send10=false;}
if (send10==false && digitalRead(key1)==LOW){ersal();send10=true;send11=false;}
if (send21==false && digitalRead(key2)==HIGH){ersal();send21=true;send20=false;}
if (send20==false && digitalRead(key2)==LOW){ersal();send20=true;send21=false;}
if (send31==false && digitalRead(key3)==HIGH){ersal();send31=true;send30=false;}
if (send30==false && digitalRead(key3)==LOW){ersal();send30=true;send31=false;}
if (send41==false && digitalRead(key4)==HIGH){ersal();send41=true;send40=false;}
if (send40==false && digitalRead(key4)==LOW){ersal();send40=true;send41=false;}
if (send51==false && digitalRead(key5)==HIGH){ersal();send51=true;send50=false;}
if (send50==false && digitalRead(key5)==LOW){ersal();send50=true;send51=false;}
serialEvent();
}
void ersal(){
outputString.concat(digitalRead(key1));
outputString.concat(",");
outputString.concat(digitalRead(key2));
outputString.concat(",");
outputString.concat(digitalRead(key3));
outputString.concat(",");
outputString.concat(digitalRead(key4));
outputString.concat(",");
outputString.concat(digitalRead(key5));
outputString.concat(",");
outputString.concat('*');
Serial.println(outputString);
outputString="";
}
void serialEvent() {
while (Serial.available()) {
char inChar = (char)Serial.readStringUntil('\n');
inputString += inChar;
inputString_index++;
if(inChar=='*'){
temp=inputString[inputString_index-3];
if(temp=='1') digitalWrite(led5,HIGH);
else digitalWrite(led5,LOW);
temp=inputString[inputString_index-5];
if(temp=='1') digitalWrite(led4,HIGH);
else digitalWrite(led4,LOW);
temp=inputString[inputString_index-7];
if(temp=='1') digitalWrite(led3,HIGH);
else digitalWrite(led3,LOW);
temp=inputString[inputString_index-9];
if(temp=='1') digitalWrite(led2,HIGH);
else digitalWrite(led2,LOW);
temp=inputString[inputString_index-11];
if(temp=='1') digitalWrite(led1,HIGH);
else digitalWrite(led1,LOW);
inputString_index=0;
inputString="";
}
}
}