Ultrasonic sensor not working with my board.

Hi recently I built a project that can measure distance and it worked to make the project smaller I built the project with an Arduino Nano and now when I upload the code to the board and open the serial port the serial port says:

??sta?

What does that mean? What can I do to fix this? This is my code:

int trigPin = 12;
int echoPin = 13;

float pingTime;
float targetDistance;
float speedOfSound = 771.18;


void setup() {
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);


}

void loop() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2000);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(15);
  digitalWrite(trigPin, LOW);

  pingTime = pulseIn(echoPin, HIGH);
  pingTime = pingTime / 1000000.;
  pingTime = pingTime / 3600.;

  targetDistance = speedOfSound * pingTime; // calculates distance in miles
  targetDistance = targetDistance / 2;
  targetDistance = targetDistance * 63360;

  Serial.print("distance to target is ");
  Serial.print(targetDistance);
  Serial.println(" inches");
  Serial.begin(9600);
}

Thanks so much.

Remove Serial.begin(9600); from loop().

Make sure your serial monitor is set to 9600

Just curious: why remove the Serial.begin(9600); but set the Serial Monitor to just that baudrate? Is the Nano special with Serial.begin()?

DrDiettrich: Just curious: why remove the Serial.begin(9600); but set the Serial Monitor to just that baudrate? Is the Nano special with Serial.begin()?

Serial.begin(9600) in in setup() and loop() remove from loop() set the serial monitor to the same value as Serial.begin(xxxx) so you do not get weird characters.

Ah, I missed that it also was in loop(). Thanks :-)

Oh my gosh thank you s much guys now it works.

Thanks so much.