Afstand programforslag

Fra HTX Arduino
Spring til navigation Spring til søgning

På denne side angives to forskellige løsninger til hvordan man kan få en afstandsmåler til at fungere.

Løsningerne tager udgangspunkt i Ping eksemplet[1]. Dette eksempel arbejder med et modul, hvor triggerbenet og echobenet fungerer på samme ben. Det kræver at man vender input og output hele tiden i målingen. De fleste af de moduler til afstandsmåling man kan købe i dag har de to ben delt op på hver sin pin.

Løsning med Seriel port

Koden herunder tager udgangspunkt i en afstandsmåler med 4 ben. VCC der skal til +5V og GND til GND. Echo-benet forbindes til pin 8 og trig-benet forbindes til ben 9.

/*
  Ping))) Sensor

  This sketch reads a PING))) ultrasonic rangefinder and returns the distance
  to the closest object in range. To do this, it sends a pulse to the sensor to
  initiate a reading, then listens for a pulse to return. The length of the
  returning pulse is proportional to the distance of the object from the sensor.

  The circuit:
	- +V connection of the PING))) attached to +5V
	- GND connection of the PING))) attached to ground
	- ECHO connection of the PING))) attached to digital pin 8
  - TRIG connection of the PING))) attached to digital pin 9

  created 3 Nov 2008
  by David A. Mellis
  modified 30 Aug 2011
  by Tom Igoe
  modified 28 Nov 2018 by Bent Arnoldsen

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/Ping
*/

// this constant won't change. It's the pin number of the sensor's output:
const int trigPin = 9;
const int echoPin = 8;

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() {
  // 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:
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);

  Serial.print(inches);
  Serial.print("in \t");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();

  delay(100);
}

long microsecondsToInches(long microseconds) {
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds) {
  return microseconds / 29 / 2;
}

Hardware til Serielport-løsningen

For at få afstandsmåleren til at fungere på Serial Monitor anvendes følgende hardware:
Afstandsmåler til Serial Monitor
Figur 2. Afstandsmåler til Serial Monitor

Løsning med Display og Seriel port

Hvis man gerne vil have visningen på et display, så er der selvfølgelig mere hardware der skal kobles til, men visningen bliver mere brugervenlig, og Arduinoen kan fungere uden en computer tilsluttet.

Det er følgende kode der skal anvendes:

/*
  Ping))) Sensor

  This sketch reads a PING))) ultrasonic rangefinder and returns the distance
  to the closest object in range. To do this, it sends a pulse to the sensor to
  initiate a reading, then listens for a pulse to return. The length of the
  returning pulse is proportional to the distance of the object from the sensor.

  The circuit:
	- +V connection of the PING))) attached to +5V
	- GND connection of the PING))) attached to ground
	- ECHO connection of the PING))) attached to digital pin 8
  - TRIG connection of the PING))) attached to digital pin 9

  created 3 Nov 2008
  by David A. Mellis
  modified 30 Aug 2011
  by Tom Igoe
  modified 28 Nov 2018 by Bent Arnoldsen

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/Ping
*/
// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

// this constant won't change. It's the pin number of the sensor's output:
const int trigPin = 9;
const int echoPin = 8;

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("Afstandsmåler");
  delay(2000);
  lcd.clear();
}

void loop() {
  // 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:
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);

  Serial.print(inches);
  Serial.print("in \t");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();

  lcd.setCursor(0,0);
  lcd.print("Afst. cm = ");
  lcd.print(cm);
  lcd.print("    ");
  lcd.setCursor(0,1);
  lcd.print("Tommer = ");
  lcd.print(inches);
  lcd.print("    ");
  
  delay(100);
}

long microsecondsToInches(long microseconds) {
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds) {
  return microseconds / 29 / 2;
}

Hardware til Display-løsningen

For at få displayet til at fungere anvendes følgende hardware:
Afstandsmåler med display
Figur 2. Afstandsmåler med display

Referencer

  1. Ping Tutorial på www.arduino.cc
Kom i gang med Arduino
Grundlæggende forståelse for Arduino Arduino Hardware - Arduino Prototype Print - Blink Eksempel - Overblik - Serial Monitor - Simple Komponenter - Software og Udviklingsmiljø
Programmering af Arduino Anvendelse af Eksempler - Klasser og Objekter - Programafvikling i Arduino - Test af Programmer
Dokumentationsformer Dokumentation med Kode og Flowchart - Dokumentation med State-machines - Flowchart - Pseudokode - Program-kommentarer - Systemdokumentation - Syntaksfarvning - Blokdiagram - Pulsplaner - UML
Opkoblinger til Arduino Moduler og Biblioteker - Driver - Opkobling af Hardware - Simple Komponenter - Tips til anvendelse af ben på Arduino UNO
Kompliceret Programmering Arduino kombineret med Processing - Kommunikation fra Arduino til Processing - Kommunikation fra Processing til Arduino - CopyThread Multitasking - Dokumentation med State-machines - Tid og Samtidighed i Software - Arduino Memory
Kompliceret Hardware I2C - Andre Processorboards - Internet of Things (IoT)
Oprindelige Dokumenter PDF-Version - Forfattere
Software Udviklingsteknikker Agile metoder - Brugertest - SCRUM

Fasemodellen - Kravspecifikation - Databasedesign - Struktur eller Arkitektur - Softwaretest

Projekter Afstand programforslag - Lysmåling - Projektforslag - Prototyper
Undervisningsforløb 4t Valgfag til Folkeskolen - Læsevejledning 4t Valgfag - Materialer til 4t Valgfag - Undervisningsnoter 4t Valgfag - Undervisningsplan 4t Valgfag - Slides til Undervisning

Kort Valgfag til Folkeskolen - Læsevejledning Kort Valgfag - Materialer til Kort Valgfag - Undervisningsnoter Kort Valgfag - Undervisningsplan Kort Valgfag