Arduino UNO와 GP2Y0A21YK(거리 센서)로 6cm에서 80cm를 구한다.

목적
adc 테스트용

왜 이런 일이 있었는지는 6-10,10-20,20-80을 참고

void setup() {
  Serial.begin(9600);
}

int sensorValue; //センサーの読み取り値
float Voltage;     //電圧
float ir_length;   //長さ

void loop() {

  sensorValue = analogRead(A0);
  Voltage = (float)sensorValue * ( 5.0 / 1024.0 );
  if (Voltage >= 0 && Voltage <= 0.4)   {Voltage = 0.4;}
  if (Voltage >= 3.12 && Voltage <= 5.0) {Voltage = 3.12;}

 if         (Voltage >= 0.4 && Voltage <= 1.3 ) { //80-20

 ir_length = 1.0/ ( 0.0125 + ( ( Voltage - 0.4) * (0.0375/0.9) ) );

  } else if (Voltage >= 1.3 && Voltage <= 2.3 ) { //20-10

ir_length = 1/ ( 0.05 + ( ( Voltage - 1.3) * 0.05 ) );

  } else if (Voltage >= 2.3 && Voltage <= 2.75)  { //10-8

    ir_length = 1.0/ ( 0.1 + ( ( Voltage - 2.3) * (0.025/0.45) ) );

  } else if (Voltage >= 2.75 && Voltage <= 2.98) { //8-7

    ir_length = 1.0/ ( 0.125 + ( ( Voltage - 2.75) * (0.0179/0.23) ) );

  } else if (Voltage >= 2.98 && Voltage <= 3.12) { //7-6

    ir_length = 1.0/ ( 0.1429 + ( ( Voltage - 2.98) * (0.0238/0.14) ) );

  }

  //Serial.println(sensorValue);
  Serial.print("800");
  Serial.print(",");
  Serial.print( ir_length*10 );
  Serial.print(",");  
  Serial.println("0");

  delay(10);
}



좋은 웹페이지 즐겨찾기