Author Topic: Fehler bei Sketch mit Ultraschallsensor  (Read 14267 times)

House

  • freakyfriday
  • Jr. Member
  • *
  • Posts: 95
  • Karma: +1/-0
    • Hausammann-Dekor
Fehler bei Sketch mit Ultraschallsensor
« on: September 03, 2015, 06:01:54 PM »
Hallo zusammen,

Ich hab im Internet einen netten Arduino Sketch mit der Möglichkeit die Daten grafisch in Processing auszugeben gefunden. Das Processingscript scheint zu funktionieren, bzw. gibt die Meldung, dass kein Serial verfügbar ist, jedoch ist dies kaum verwunderlich, da ich keinen Arduino am PC angeschlossen hatte. Leider kompilliert der Arduino Sketch nicht und die Fehlermeldung erscheint neun Zeilen vor dem Ende des Programms. Die Fehlermeldung lautet wie folgt: cannot convert 'int' to 'char*(const char*, int)' in assignment
Keine Ahnung was damit gemeint ist, bzw. wie ich den Fehler beheben kann. Weiss hier vielleicht jemand eine Lösung?

Unten poste ich mal den fehlerhaften Sketch für den Arduino:

#include <Servo.h>

/*
code for arduino bord ultrasonic radar system.
*/
             
Servo leftRightServo;         // set a variable to map the servo
int leftRightPos = 0;         // set a variable to store the servo position
const int numReadings = 10;   // set a variable for the number of readings to take
int index = 0;                // the index of the current reading
int total = 0;                // the total of all readings
int average = 0;              // the average
int echoPin = 6;              // the SRF05's echo pin
int initPin = 7;              // the SRF05's init pin
unsigned long pulseTime = 0;  // variable for reading the pulse
unsigned long distance = 0;   // variable for storing distance
 
/* setup the pins, servo and serial port */
void setup() {
  leftRightServo.attach(9);
  // make the init pin an output:
  pinMode(initPin, OUTPUT);
  // make the echo pin an input:
  pinMode(echoPin, INPUT);
  // initialize the serial port:
  Serial.begin(9600);
}
 
/* begin rotating the servo and getting sensor values */
void loop() {
  for(leftRightPos = 0; leftRightPos < 180; leftRightPos++) {  // going left to right.
    leftRightServo.write(leftRightPos);
      for (index = 0; index<=numReadings;index++) {            // take x number of readings from the sensor and average them
        digitalWrite(initPin, LOW);
        delayMicroseconds(50);
        digitalWrite(initPin, HIGH);                           // send signal
        delayMicroseconds(50);                                 // wait 50 microseconds for it to return
        digitalWrite(initPin, LOW);                            // close signal
        pulseTime = pulseIn(echoPin, HIGH);                    // calculate time for signal to return
        distance = pulseTime/58;                               // convert to centimetres
        total = total + distance;                              // update total
        delay(10);
      }
    average = total/numReadings;                               // create average reading
 
    if (index >= numReadings)  {                               // reset the counts when at the last item of the array
      index = 0;
      total = 0;
    }
    Serial.print("X");                                         // print leading X to mark the following value as degrees
    Serial.print(leftRightPos);                                // current servo position
    Serial.print("V");                                         // preceeding character to separate values
    Serial.println(average);                                   // average of sensor readings
  }
  /*
  start going right to left after we got to 180 degrees
  same code as above
  */
  for(leftRightPos = 180; leftRightPos > 0; leftRightPos--) {  // going right to left
    leftRightServo.write(leftRightPos);
    for (index = 0; index<=numReadings;index++) {
      digitalWrite(initPin, LOW);
      delayMicroseconds(50);
      digitalWrite(initPin, HIGH);
      delayMicroseconds(50);
      digitalWrite(initPin, LOW);
      pulseTime = pulseIn(echoPin, HIGH);
      distance = pulseTime/58;
      total = total + distance;
      delay(10);
    }
    average = total/numReadings;
    if (index >= numReadings)  {
      index = 0;  // Hier tritt der Fehler auf  :o
      total = 0;
    }
    Serial.print("X");
    Serial.print(leftRightPos);
    Serial.print("V");
    Serial.println(average);
   }
}

Gruss House

arduinopraxis

  • freakyfriday
  • Hero Member
  • *
  • Posts: 553
  • Karma: +11/-0
  • Arduino Praxiseinstieg (4.Auflage)
    • Arduino Praxiseinstieg, 4. Auflage
Re: Fehler bei Sketch mit Ultraschallsensor
« Reply #1 on: September 03, 2015, 06:58:28 PM »
Hallo House

ich habe dein Sketch geprüft. Bei mir kompiliert das Programm fehlerfrei mit IDE 1.6.2 und 1.6.5

Gruss
Thomas

PS:
Code kann man besser lesbar machen wenn man diesen mit der Funktion # einfügt  ;)

Code: [Select]
#include <Servo.h>

/*
code for arduino bord ultrasonic radar system.
*/
             
Servo leftRightServo;         // set a variable to map the servo
int leftRightPos = 0;         // set a variable to store the servo position
const int numReadings = 10;   // set a variable for the number of readings to take
int index = 0;                // the index of the current reading
int total = 0;                // the total of all readings
int average = 0;              // the average

knusi@gmx.ch

  • Newbie
  • *
  • Posts: 6
  • Karma: +1/-0
    • knusi.ch
Re: Fehler bei Sketch mit Ultraschallsensor
« Reply #2 on: September 03, 2015, 08:09:04 PM »
Hallo

Genau sehe ich hier keinen Fehler. Also muss ich raten.

Hast du den Code Direckt vom Internet in die Arduino IDE kopiert?

Es existieren spetzielle Schriftzeichen. Vor allem das Internet ist voll damit ;-)
Diese führen vor allem bei Codeinterpretern zu fehler.
Zum Beispiel währe da das "Geschützte Leerzeichen" ==> https://de.wikipedia.org/wiki/Gesch%C3%BCtztes_Leerzeichen
Oder „Hallo World“ ist nicht gleich "Hallo World"

Kopier den Text vom Internet zuerst in einen Texteditor (z.B. Notepad) und nachher
in die Arduino IDE. Und Teste es noch mal.




House

  • freakyfriday
  • Jr. Member
  • *
  • Posts: 95
  • Karma: +1/-0
    • Hausammann-Dekor
Re: Fehler bei Sketch mit Ultraschallsensor
« Reply #3 on: September 03, 2015, 11:29:21 PM »
Lieber  arduinopraxis, ,

Ich verwende die IDE 1.6.4, wegen meinen Teensys, die 1.6.5 Version wird noch nicht unterstützt. Ich denke jedoch nicht dass es daran liegt. Danke für den Hinweis betreffend Code mit #.

Gruss House
« Last Edit: September 04, 2015, 12:00:30 AM by House »

House

  • freakyfriday
  • Jr. Member
  • *
  • Posts: 95
  • Karma: +1/-0
    • Hausammann-Dekor
Re: Fehler bei Sketch mit Ultraschallsensor (gelöst)
« Reply #4 on: September 03, 2015, 11:55:10 PM »
Lieber knusi@gmx.ch,

Gut geraten!!! ;) Vielen Dank für den Hinweis und die schnelle Antwort!  :D Ja der Sketch war aus dem Netz. Ich habe Deinen Tip betreffend Editor verwendet, und Bingo. Der Sketch kompilliert jetzt richtig. Ich habe nun den Sketch für meinen Ultraschallsensor geändert, der Miniservo fährt von der Mitte nach rechts und von dort nach links, obwohl das ganze nach dem Sketch anders laufen sollte. Ebenso empfange ich keine Ausgabe am seriellen Monitor. Das ganze kommt mir komisch vor, meine Freaduino ist teilweise über COM nicht ansprechbar und auch im Gerätemanager ist er teilweise nicht sichtbar. Ich denke die Stromversorgung vom Board sollte es mit meinen Miniservo eher nicht sein, ob wohl nur am usb vom PC betrieben, könnte es das sein? Ich füge unten noch meinen modifizierten Code an.

Code: [Select]
#include <Servo.h>

/*
code for arduino bord ultrasonic radar system.
*/
             
Servo leftRightServo;         // set a variable to map the servo
int leftRightPos = 0;         // set a variable to store the servo position
const int numReadings = 10;   // set a variable for the number of readings to take
int index = 0;                // the index of the current reading
int total = 0;                // the total of all readings
int average = 0;              // the average
const int pingPin = 3;

 
/* setup the pins, servo and serial port */
void setup() {
  leftRightServo.attach(9);

  // initialize the serial port:
  Serial.begin(9600);
}
 
/* begin rotating the servo and getting sensor values */
void loop() {
  for(leftRightPos = 0; leftRightPos < 180; leftRightPos++) {  // going left to right.
    leftRightServo.write(leftRightPos);
      for (index = 0; index<=numReadings;index++) {            // take x number of readings from the sensor and average them
  // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
  long duration,  cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);
  cm = microsecondsToCentimeters(duration);
        total = total + cm;                              // update total
        delay(10);
      }
    average = total/numReadings;                               // create average reading
 
    if (index >= numReadings)  {                               // reset the counts when at the last item of the array
      index = 0;
      total = 0;
    }
    Serial.print("X");                                         // print leading X to mark the following value as degrees
    Serial.print(leftRightPos);                                // current servo position
    Serial.print("V");                                         // preceeding character to separate values
    Serial.println(average);                                   // average of sensor readings
  }
  /*
  start going right to left after we got to 180 degrees
  same code as above
  */
  for(leftRightPos = 180; leftRightPos > 0; leftRightPos--) {  // going right to left
    leftRightServo.write(leftRightPos);
    for (index = 0; index<=numReadings;index++) {
        // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
  long duration, inches, cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);
  cm = microsecondsToCentimeters(duration);
        total = total + cm;                              // update total
        delay(10);

    }
    average = total/numReadings;
    if (index >= numReadings)  {
      index = 0;
      total = 0;
    }
    Serial.print("X");
    Serial.print(leftRightPos);
    Serial.print("V");
    Serial.println(average);
   }
}
long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

Gruss House
« Last Edit: September 05, 2015, 11:10:03 PM by House »

pylon

  • freakyfriday
  • Full Member
  • *
  • Posts: 158
  • Karma: +16/-0
Re: Fehler bei Sketch mit Ultraschallsensor
« Reply #5 on: September 14, 2015, 02:08:23 PM »
Quote
Ich habe nun den Sketch für meinen Ultraschallsensor geändert, der Miniservo fährt von der Mitte nach rechts und von dort nach links, obwohl das ganze nach dem Sketch anders laufen sollte.

Wie sollte es denn Deiner Meinung nach laufen? Was erwartest Du, dass der Sketch macht?

Die Stellen:

Code: [Select]
    if (index >= numReadings)  {
      index = 0;
      total = 0;
    }

solltest Du einfach so schreiben:

Code: [Select]
    total = 0;

Die Variable "index" wird bei der nächsten Schlaufe sowieso zurückgesetzt und die if-Abfrage ist überflüssig, da die Schleife nur in diesem Fall beendet wird.

Quote
Ebenso empfange ich keine Ausgabe am seriellen Monitor.

Ist die Baudrate korrekt (9600) eingestellt?

Quote
Das ganze kommt mir komisch vor, meine Freaduino ist teilweise über COM nicht ansprechbar und auch im Gerätemanager ist er teilweise nicht sichtbar. Ich denke die Stromversorgung vom Board sollte es mit meinen Miniservo eher nicht sein, ob wohl nur am usb vom PC betrieben, könnte es das sein?

Ich würde auf eine Unterversorgung mit Strom tippen. Je nach USB-Port bekommst Du über USB nur 100mA, im besten Fall sind es 500mA. Ein Servo kann bis über 1A Anlaufstrom benötigen (hängt sehr vom Modell ab), somit kann es sein, dass Dein Freaduino ständig unterversorgt ist und sich deshalb etwas komisch benimmt. Notfalls hilft: Messen!

House

  • freakyfriday
  • Jr. Member
  • *
  • Posts: 95
  • Karma: +1/-0
    • Hausammann-Dekor
Re: Fehler bei Sketch mit Ultraschallsensor
« Reply #6 on: February 23, 2017, 10:15:56 AM »
Leider hat dieses Projekt ein wenig geruht. Danke für die Tips und Anregungen, so wie ich es jetzt sehe, lag es doch an der Stromversorgung über die USB Schnittstelle.  ::)

 

anything