Servo.h: mudanças entre as edições
Ir para navegação
Ir para pesquisar
imported>Fargoud (Criou página com ' #ifndef SR04_H #define SR04_H #if defined(ARDUINO) && ARDUINO >= 100 #include "Arduino.h" #else #include "WProgram.h" #endif //#include "pins_arduino.h" #include...') |
imported>Fargoud Sem resumo de edição |
||
| (3 revisões intermediárias pelo mesmo usuário não estão sendo mostradas) | |||
| Linha 3: | Linha 3: | ||
#if defined(ARDUINO) && ARDUINO >= 100 | #if defined(ARDUINO) && ARDUINO >= 100 | ||
#include "Arduino.h" | |||
#else | #else | ||
#include "WProgram.h" | |||
#endif | #endif | ||
//#include "pins_arduino.h" | |||
#include <inttypes.h> | #include <inttypes.h> | ||
| Linha 16: | Linha 16: | ||
class SR04 { | class SR04 { | ||
public: | public: | ||
/** | |||
* Constructor | |||
* Ultrasonic sensor SR04, four connections pins | |||
* VCC, ECHO, TRIGGER, GND | |||
* <br> | |||
* \param echoPin digital INPUT-Pin for measuring distance | |||
* \param triggerPin if 10us high a trigger signal is generated from | |||
* SR04 | |||
* | |||
* \return void | |||
*/ | |||
SR04(int echoPin, int triggerPin); | |||
/** | |||
* Do a measurment for this sensor. Return distance as long | |||
* in centimenter | |||
* \return long distance in centimeter | |||
*/ | |||
long Distance(); | |||
/** | |||
* Do count measurents and calculate the average. | |||
* To avoid defilement from ow/high peaks, min/max values | |||
* are substracted from the average | |||
* | |||
* \param wait delay between measurements, default = DEFAULT_DELAY/ms | |||
* \param count number of measurements, default DEFAULT_PINGS | |||
* \return long distance in centimeter | |||
**/ | |||
long DistanceAvg(int wait=DEFAULT_DELAY, int count=DEFAULT_PINGS); | |||
/** | |||
* Do only a ping. Get result with methode getDistance() | |||
* | |||
* \param keine | |||
*/ | |||
void Ping() ; | |||
/** | |||
* return latest distance. Methode Ping() should be called before | |||
* \param keine | |||
* \return Distanz in Zentimeter | |||
*/ | |||
long getDistance(); | |||
private: | private: | ||
/** | |||
* Do the measurement calculation and return result in centimeter | |||
* SR04 measure echo time to obstacle and return way. | |||
* <br> | |||
* Sound travels with 340m/sec | |||
* <br> | |||
* Example: Obstace 100cm away from SR04. Measure time is 100cm to | |||
* obstacle and 100cm return = 200cm | |||
* <br> | |||
* 1sec = 1000ms = 1.000.000uS | |||
* 1.000.000 / 340 = Distance in microseconds for 100cm | |||
* 2941uS fuer 100cm = 5882 uS fuer 200cm | |||
* | |||
* duration / 5882 * 100 = distance in cm | |||
*/ | |||
long MicrosecondsToCentimeter(long duration); | |||
long _currentDistance; | |||
int _echoPin, _triggerPin; | |||
long _duration, _distance; | |||
bool _autoMode; | |||
}; | }; | ||
#endif | #endif | ||
Outro exemplo: | |||
#include "SR04.h" | |||
#define TRIG_PIN 12 | |||
#define ECHO_PIN 11 | |||
SR04 sr04 = SR04(ECHO_PIN,TRIG_PIN); | |||
long a; | |||
void setup() { | |||
Serial.begin(9600); | |||
delay(1000); | |||
} | |||
void loop() { | |||
a=sr04.Distance(); | |||
Serial.print(a); | |||
Serial.println("cm"); | |||
delay(1000); | |||
} | |||
####################################### | |||
# Syntax Coloring Map SR04 lib | |||
####################################### | |||
####################################### | |||
# Datatypes (KEYWORD1) | |||
####################################### | |||
####################################### | |||
# Methods and Functions (KEYWORD2) | |||
####################################### | |||
Distance KEYWORD2 Distance | |||
DistanceAvg KEYWORD2 Distance average | |||
Ping KEYWORD2 only a ping signal | |||
getDistance KEYWORD2 get distance after ping | |||
####################################### | |||
# Instances (KEYWORD2) | |||
####################################### | |||
SR04 KEYWORD2 ultrasonic library | |||
####################################### | |||
# Constants (LITERAL1) | |||
####################################### | |||
Edição atual tal como às 15h25min de 8 de maio de 2019
#ifndef SR04_H
#define SR04_H
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
//#include "pins_arduino.h"
#include <inttypes.h>
#define PULSE_TIMEOUT 150000L // 100ms
#define DEFAULT_DELAY 10
#define DEFAULT_PINGS 5
class SR04 {
public:
/**
* Constructor
* Ultrasonic sensor SR04, four connections pins
* VCC, ECHO, TRIGGER, GND
*
* \param echoPin digital INPUT-Pin for measuring distance
* \param triggerPin if 10us high a trigger signal is generated from
* SR04
*
* \return void
*/
SR04(int echoPin, int triggerPin);
/**
* Do a measurment for this sensor. Return distance as long
* in centimenter
* \return long distance in centimeter
*/
long Distance();
/**
* Do count measurents and calculate the average.
* To avoid defilement from ow/high peaks, min/max values
* are substracted from the average
*
* \param wait delay between measurements, default = DEFAULT_DELAY/ms
* \param count number of measurements, default DEFAULT_PINGS
* \return long distance in centimeter
**/
long DistanceAvg(int wait=DEFAULT_DELAY, int count=DEFAULT_PINGS);
/**
* Do only a ping. Get result with methode getDistance()
*
* \param keine
*/
void Ping() ;
/**
* return latest distance. Methode Ping() should be called before
* \param keine
* \return Distanz in Zentimeter
*/
long getDistance();
private:
/**
* Do the measurement calculation and return result in centimeter
* SR04 measure echo time to obstacle and return way.
*
* Sound travels with 340m/sec
*
* Example: Obstace 100cm away from SR04. Measure time is 100cm to
* obstacle and 100cm return = 200cm
*
* 1sec = 1000ms = 1.000.000uS
* 1.000.000 / 340 = Distance in microseconds for 100cm
* 2941uS fuer 100cm = 5882 uS fuer 200cm
*
* duration / 5882 * 100 = distance in cm
*/
long MicrosecondsToCentimeter(long duration);
long _currentDistance;
int _echoPin, _triggerPin;
long _duration, _distance;
bool _autoMode;
};
#endif
Outro exemplo:
#include "SR04.h"
#define TRIG_PIN 12
#define ECHO_PIN 11
SR04 sr04 = SR04(ECHO_PIN,TRIG_PIN);
long a;
void setup() {
Serial.begin(9600);
delay(1000);
}
void loop() {
a=sr04.Distance();
Serial.print(a);
Serial.println("cm");
delay(1000);
}
####################################### # Syntax Coloring Map SR04 lib ####################################### ####################################### # Datatypes (KEYWORD1) ####################################### ####################################### # Methods and Functions (KEYWORD2) ####################################### Distance KEYWORD2 Distance DistanceAvg KEYWORD2 Distance average Ping KEYWORD2 only a ping signal getDistance KEYWORD2 get distance after ping ####################################### # Instances (KEYWORD2) ####################################### SR04 KEYWORD2 ultrasonic library ####################################### # Constants (LITERAL1) #######################################