我使用Serial.print()函数通过arduino的串口传输flex传感器值,但是我无法在接收端读取它

时间:2016-05-09 06:06:25

标签: serial-port arduino-uno

实际上在我的代码中我传输加速度计值以及4个弹性传感器值。我已将加速度计值转换为4状态,如F(前进),B(后退),R(右),L(左)。我能够收到这4个状态,并且我能够为这些状态编写代码。在这些状态下,我还发送了柔性传感器值来远程控制伺服电机。但是我无法读取这些弹性值,因为它是一个变化的整数。请帮我看一下flex值。我尝试过Serial.read(),Serial.parseInt()。

我的发射器代码是:

int xpin = A0;
int x;
int ypin = A1;
int y;
int vcc=13;

const int flexpin1 = 2;
const int flexpin2 = 3;
const int flexpin3 = 4;
const int flexpin4 = 5;

int flex1[20];
int flex2[20];
int flex3[20];
int flex4[20];

int flexsum1=0;
int flexsum2=0;
int flexsum3=0;
int flexsum4=0;

 char state1 = 'S';
void setup() {

  // put your setup code here, to run once:
  Serial.begin(115200);

  pinMode(flexpin1, INPUT);
  pinMode(flexpin2, INPUT);
  pinMode(flexpin3, INPUT);
  pinMode(flexpin4, INPUT);
  pinMode(xpin, INPUT);
  pinMode(ypin, INPUT);
  pinMode(vcc, OUTPUT);
  digitalWrite(vcc, HIGH);
}

void loop() {


  x = analogRead(xpin);
  Serial.print("xpin=");
  Serial.print(x);
  y = analogRead(ypin);
  Serial.print("\t ypin=");
  Serial.print(y);
  if ( y > 415)
  {
    state1 = 'F';
  }
  else if (y < 315)
  {
    state1 = 'B';
  }
  else if (x > 410)
  {
    state1 = 'R';
  }

  else if (x < 310)
  {
    state1 = 'L';
  }

  else {
    state1 = 'S';
  }

 // flex readings stabelized//


for(int x=0; x<25; x++)
{
flex1[x]=analogRead(flexpin1);
flex2[x]=analogRead(flexpin2);
flex3[x]=analogRead(flexpin3);
flex4[x]=analogRead(flexpin4);
flexsum1=flexsum1+flex1[x];
flexsum2=flexsum2+flex2[x];
flexsum3=flexsum3+flex3[x];
flexsum4=flexsum4+flex4[x];

delayMicroseconds(20);
}
flexsum1=flexsum1/25;
flexsum2=flexsum2/25;
flexsum3=flexsum3/25;
flexsum4=flexsum4/25;



  Serial.print("\t\t\tflexsum1= ");
  Serial.print(flexsum1);

  Serial.print("\tflexsum2= ");
  Serial.print(flexsum2);

  Serial.print("\tflexsum3= ");
  Serial.print(flexsum3);

  Serial.print("\tflexsum4= ");
  Serial.print(flexsum4);

  Serial.print("\t state1=");
  Serial.println(state1);

delay(200);
  }

我收到的代码:

#include<Servo.h>

  int IN1 = 13;
  int IN2 = 12;
  int IN3 = 7;
  int IN4 = 6;
  char state1 = 'S';
  Servo servo1, servo2, servo3, servo4;
  int flex1, flex2, flex3, flex4;
  int angle1, angle2, angle3, angle4;
  void setup() {
    // put your setup code here, to run once:
    Serial.begin(115200);
    pinMode(IN1, OUTPUT);
    pinMode(IN2, OUTPUT);
    pinMode(IN3, OUTPUT);
    pinMode(IN4, OUTPUT);
    servo1.attach(9);
    servo2.attach(8);
    servo3.attach(10);
    servo4.attach(11);
  }

  void loop() {
    // put your main code here, to run repeatedly:
    while (Serial.available()) {
      delay(10);
      state1 = Serial.read();
      Serial.print(state1);

      switch (state1)
      {
        case 'F' : digitalWrite(IN1 , HIGH);
          digitalWrite(IN2 , LOW);
          digitalWrite(IN3 , HIGH);
          digitalWrite(IN4 , LOW);
          Serial.print("\tcar is moving forward ");
          delay(100);
          break;
        case 'B' : digitalWrite(IN1 , LOW);
          digitalWrite(IN2 , HIGH);
          digitalWrite(IN3 , LOW);
          digitalWrite(IN4 , HIGH);
          Serial.print("\tcar is moving backward ");
          delay(100);
          break;
        case 'L' : digitalWrite(IN1 , HIGH);
          digitalWrite(IN2 , LOW);
          digitalWrite(IN3 , LOW);
          digitalWrite(IN4 , HIGH);
          Serial.print("\tcar is turning left ");
          delay(100);
          break;
        case 'R' : digitalWrite(IN1 , LOW);
          digitalWrite(IN2 , HIGH);
          digitalWrite(IN3 , HIGH);
          digitalWrite(IN4 , LOW);
          Serial.print("\tcar is turning right");
          delay(100);
          break;
        case 'S' : digitalWrite(IN1 , LOW);
          digitalWrite(IN2 , LOW);
          digitalWrite(IN3 , LOW);
          digitalWrite(IN4 , LOW);
          delay(100);
          break;

      }

      if (flex1 >= 845)
      {
        flex1 = Serial.read();
        angle1 = map(flex1, 848, 1000, 0, 180);
        angle1 = constrain(angle1, 0, 90);
        servo1.write(angle1);
        Serial.print("\tangle1=");
        Serial.print(angle1);
        delay(200);
      }
      if (flex2 >= 820)
      {
        flex2 = Serial.read();
        angle2 = map(flex2, 825, 1000, 0, 180);
        angle2 = constrain(angle2, 0, 90);
        servo2.write(angle2);
        Serial.print("\tangle2=");
        Serial.print(angle2);
        delay(200);
      }
      if (flex3 >= 770)
      {
        flex3 = Serial.read();
        angle3 = map(flex3, 770, 930, 0, 180);
        angle3 = constrain(angle3, 90, 180);
        servo3.write(angle3);
        Serial.print("\tangle3=");
        Serial.print(angle3);
        delay(200);
      }
      if (flex4 >= 870)
      {
        flex4 = Serial.read();
        angle4 = map(flex4, 875, 1020, 0, 180);
        angle4 = constrain(angle4, 90, 180);
        servo4.write(angle4);
        Serial.print("\tangle4=");
        Serial.print(angle4);
        delay(200);
      }


    }
  }

0 个答案:

没有答案