lehrkraefte:blc:math:formi:us

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
lehrkraefte:blc:math:formi:us [2017/02/02 14:45] Ivo Blöchligerlehrkraefte:blc:math:formi:us [2017/02/02 15:00] (current) Ivo Blöchliger
Line 1: Line 1:
 +===== Ultraschall Sensor HC-SR04 =====
 +Das Interweb ist voll von Tutorials dazu. 
  
 +==== Minimalsketch, ohne Kabel ;-) ====
 +<code C++>
 +// Kabelfreie Verbindung. Stromversorgung über GPIOs.
 +// Modul einfach bei folgenden Pins richtig herum einstecken...
 +//
 +// Tools -> Serial Monitor öffnen !
 +
 +#define VCC 8
 +#define TRIG 9
 +#define ECHO 10
 +#define GND 11
 +
 +void setup() {
 +  pinMode(GND, OUTPUT);
 +  digitalWrite(GND, LOW);
 +  pinMode(TRIG,OUTPUT);
 +  digitalWrite(TRIG, LOW);
 +  pinMode(ECHO, INPUT);
 +  pinMode(VCC,OUTPUT);
 +  digitalWrite(VCC,HIGH);
 +  Serial.begin(9600);
 +}
 +
 +void loop() {
 +  // Messung auslösen
 +  digitalWrite(TRIG, HIGH);
 +  delayMicroseconds(10);
 +  digitalWrite(TRIG, LOW);
 +  // Puls-Länge messen (in us). (Maximal 1m)
 +  long d = pulseIn(ECHO,HIGH,6000);
 +  // Distanz in cm
 +  float dist = d/2.0/29.1;
 +  Serial.println(dist);
 +  delay(20);
 +}
 +</code>
 +
 +Und wer seine Umgebung nerven möchte, steure mit der Distanz einen Passiv-Buzzer (z.B. auf Pin 3) mit der Zeile
 +<code C++>
 +  tone(3,400*pow(2,(dist-4)/20));
 +</code>
 +Oder etwas melodiöser:
 +<code C++>
 +  tone(3,400*pow(2,((int)((dist-4)/3))/12.0));
 +</code>
 +Oder mit asiatischem Einschlag:
 +<code C++>
 +  if (dist>4 && dist<64) {
 +    int penta[]={0,2,4,7,9,12,14,16,19,21,24};
 +    tone(3,400*pow(2,(penta[(int)((dist-4)*11.0/60.0)]/12.0)));
 +  } else {
 +    noTone(3);
 +  }
 +</code>
 +
 +Und wer es jazzig mag:
 +<code c++>
 +  if (dist>4 && dist<64) {
 +    int jazz[]={0,3,5,6,7,10,12,15,17,18,19,22,24};
 +    tone(3,400*pow(2,(jazz[(int)((dist-4)*13.0/60.0)]/12.0)));
 +  } else {
 +    noTone(3);
 +  }
 +</code>