你如何正确读取两个arduino pro minis之间传入的串行数据?

时间:2017-05-13 18:58:42

标签: serialization arduino arduino-ide

我有两个arduino pro minis,我试图使用模拟一个波纹加法器来进行学校演示。在这种情况下,输入是两组连接到arduino 1的八个开关,输出9由arduino 2驱动。 Arduino 1读取2x8pins,将两个二进制数转换为十进制,加上它们并通过串行传输到arduino 2. Arduino 2将数字转换回二进制并按照设置9个LED。两个arduinos在与USB连接到串行模块时自行工作,但不能一起工作。我花了5个小时进行研究和故障排除,但无济于事。 通过我的实验,arduino 2只能检测传入的串行通信,但由于某种原因不能检测其值。 Arduino 1:

 void setup() {
pinMode(2,INPUT);
pinMode(3,INPUT);
pinMode(4,INPUT);
pinMode(5,INPUT);
pinMode(6,INPUT);
pinMode(7,INPUT);
pinMode(8,INPUT);
pinMode(9,INPUT);

pinMode(10,INPUT);
pinMode(11,INPUT);
pinMode(12,INPUT);
pinMode(13,INPUT);
pinMode(14,INPUT);
pinMode(15,INPUT);
pinMode(16,INPUT);
pinMode(17,INPUT);
Serial.begin(9600);

}

void loop() {
int a=0;
if (digitalRead(2)==HIGH)a=a+128;
if (digitalRead(3)==HIGH)a=a+64;
if (digitalRead(4)==HIGH)a=a+32;
if (digitalRead(5)==HIGH)a=a+16;
if (digitalRead(6)==HIGH)a=a+8;
if (digitalRead(7)==HIGH)a=a+4;
if (digitalRead(8)==HIGH)a=a+2;
if (digitalRead(9)==HIGH)a=a+1;

int b=0;
if (digitalRead(10)==HIGH)b=b+128;
if (digitalRead(11)==HIGH)b=b+64;
if (digitalRead(12)==HIGH)b=b+32;
if (digitalRead(13)==HIGH)b=b+16;
if (digitalRead(14)==HIGH)b=b+8;
if (digitalRead(15)==HIGH)b=b+4;
if (digitalRead(16)==HIGH)b=b+2;
if (digitalRead(17)==HIGH)b=b+1;
a=a+b;
Serial.println(a);
delay(1000);
}

Arduino 2:

void setup() {
    Serial.begin(9600);
    pinMode(2, OUTPUT);
    pinMode(3, OUTPUT);
    pinMode(4, OUTPUT);
    pinMode(5, OUTPUT);
    pinMode(6, OUTPUT);
    pinMode(7, OUTPUT);
    pinMode(8, OUTPUT);
    pinMode(9, OUTPUT);
    pinMode(10,OUTPUT);
    pinMode(13, OUTPUT);



}

void loop() {
int i=0;
int a;
int b;
String x;
int xs;
if (Serial.available() > 0) {
  digitalWrite(13,HIGH);
  delay(100);
  digitalWrite(13,LOW);
  x = Serial.readString();
  i = x.toInt();

  a = i / 256;
  if (a >= 1) {
    digitalWrite(10, HIGH);
    i = i - 256;
    Serial.println("256t");
  }
  else  digitalWrite(10, LOW);


  a = i / 128;
  if (a >= 1) {
    digitalWrite(2, HIGH);
    i = i - 128;
    Serial.println("128t");
  }
  else  digitalWrite(2, LOW);

  a = i / 64;
  if (a >= 1) {
    digitalWrite(3, HIGH);
    i = i - 64;
    Serial.println("64t");
  }

  else  digitalWrite(3, LOW);

  a = i / 32;
  if (a >= 1) {
    digitalWrite(4, HIGH);
    Serial.println("32t");
    i = i - 32;
  }
  else  digitalWrite(4, LOW);



  a = i / 16;
  if (a >= 1) {
    digitalWrite(5, HIGH);
    i = i - 16;
    Serial.println("16t");
  }
  else  digitalWrite(5, LOW);



  a = i / 8;
  if (a >= 1) {
    digitalWrite(6, HIGH);
    i = i - 8;
    Serial.println("8t");
    }
  else  digitalWrite(6, LOW);



  a = i / 4;
  if (a >= 1) {
    digitalWrite(7, HIGH);
    i = i - 4;
    Serial.println("4t");
  }
  else  digitalWrite(7, LOW);



  a = i / 2;
  if (a >= 1) {
    digitalWrite(8, HIGH);
    i=i-2;
    Serial.println("2t");
  }
  else  digitalWrite(8, LOW);

  a = i / 1;
  if (a >= 1) {
    digitalWrite(9, HIGH);
    i = i - 1;
    Serial.println("1t");
  }
  else  digitalWrite(9, LOW);

}

}

非常感谢提前和其他建议,欢迎。

0 个答案:

没有答案