Ultraschallsensor MB1033

Hallo,

ich versuch es nun mal auf deutsch, da ich mein Problem auf Englisch nicht so wirklich erklärt bekomme. Ich habe einen Arduino Mega 2560 und einen MB1033 Ultraschallsensor von MaxBotix. Er kommuniziert auch mit dem Arduino (Problem hab ich im englischsprachigen Teil gelöst bekommen), aber nun hab ich ein Problem an dem ich schon 2 Tage sitze und nicht weiß wie ich ran gehen soll. Der Sensor sendet auf dem Seriellen Bus 6 Bytes. Das erste ist immer ein 'R' und das letzte ein '^CR'. Was dazwischen steht ist die Entfernung in mm. Soweit sogut.

Der Sensor soll aber Teil eines Gesamtprojektes sein. Zeil ist damit den Füllstand eines Tanks zu überwachen. Dazu übernimmt der Arduino auch noch andere Funktionen wie Temperaturmessungen etc. Ich will jetzt also, dass der Sensor z.b. alle 10 Sekunden abgefragt wird. Das ist ja kein Problem z.b. mit

if(millis()%10000==0) { code }

Aber in diesem if-clause soll ein kompletter serieller Datenstrom aufgenommen und ausgewertet werden. Und das ist nun mein Problem. Wie kann ich den Strom komplett aufnehmen, so das ich auch weiß, das es ein gültiger ist?

Vielen Dank schon mal,

Verirrtes Schaf

Laut Datenblatt HRLV-MaxSonar-EZ Datasheet – MaxBotix kann der Sensor getriggert werden:

Pin 4- Ranging Start/Stop: This pin is internally pulled high. If this pin is left unconnected or held high, the sensor will
continually measure and output the range data. If held low, the HRLV-MaxSonar-EZ will stop ranging. Bring high for
20uS or longer to command a range reading.
Real-time Range Data: When pin 4 is low and then brought high, the sensor will operate in real time and the first reading
output will be the range measured from this first commanded range reading. When the sensor tracks that the RX pin is low
after each range reading, and then the RX pin is brought high, unfiltered real time range information can be obtained as
quickly as every 100mS.
...
Pin 5-Serial Output: By default, the serial output is RS232 format (0 to Vcc) with a 1-mm resolution. If TTL
output is desired, solder the TTL jumper pads on the back side of the PCB as shown in the photo to the right.
For volume orders, the TTL option is available as no-cost factory installed jumper. The serial output is the
most accurate of the range outputs. Serial data sent is 9600 baud, with 8 data bits, no parity, and one stop bit.

Also das pin 4 für 20µS bis 98mS auf H bringen udn Du startest eine Messung. Nach 100mS bekommst Du auf pin 5 die Antwort. Du wartest also ca 150mS und liest die 6 bytes von der seriellen Schnittstelle in ein array. Dann kontrollierst Du ob Element 0 des array R ist und Element 5 den wert 13 hat (^CR). Die elemente 1 bis 4 lädst Du in ein anderesArray und schreibst in Element 4 dieses arrays eine null. Mit der Funktion atoi wandelst du den string in die Zahl um.

abstannd = atoi(anderesArray)

Das einzige was ich nicht verstanden habe ist der Unterschied von "RS232 format (0 to Vcc)" und TTL outout.

Grüße Uwe

Danke Uwe, hab das gerade mal ausprobiert. Hab hier mal den Code:

void setup() {
  Serial.begin(9600);
  Serial1.begin(9600);
  pinMode(21, OUTPUT);
}

int i=1;
int empfang[6];

void loop() {
  if(millis()%10000 == 0) {
    digitalWrite(21, HIGH);
    delay(150);
    i=0;
    while (i<6) {
      if(Serial1.available()) {
        empfang[i] = Serial1.read();
       i++; 
      }
      else {}      
    }
    
    digitalWrite(21, LOW);    
    for(i=0; i<6; i++) {
      Serial.write  (empfang[i]);
    }
  }
}

Soweit erst mal, es funktioniert, das er alle 10 Sekunden was brauchbares einliest, allerdings immer das gleiche......egal wie der Sensor liegt oder steht.

Hab ich da irgend wo einen Denkfehler?

Verirrtes Schaf

Allerdings würde ich das so machen:

void loop() {
  if(millis()%10000 == 0) {
    digitalWrite(21, HIGH);
    delay (1);
    digitalWrite(21, LOW);
    delay(150);
    i=0;
    while (i<6) {
      if(Serial1.available()) {
        empfang[i] = Serial1.read();
       i++; 
      }
      for(i=0; i<6; i++) {
      Serial.write  (empfang[i]);
    }
    }
  }

Wenn Du andauernd die Werte ausgibst dann kommt natürlich für 10 Sekunden immer der gleiche Wert.
Grüße Uwe

Danke Uwe, funktioniert!