----------------------ARDUINO CODE--------------------------
#include <Sensor_Ultrassom.h>
Sensor_Ultrassom ultrassom(3,4);
float dist_cm, dist_m;
void setup() {
}
void loop() {
}
--------------------SENSOR_ULTRASSOM.H----------------
#ifndef SENSOR_ULTRASSOM_H
#define SENSOR_ULTRASSOM_H
#include <Arduino.h>
class Sensor_Ultrassom
{
public:
Sensor_Ultrassom(char pt, char pe);
double distancia_cm();
private:
char pino_trigger;
char pino_echo;
}
#endif
--------------------SENSOR_ULTRASSOM.CPP----------------
#include <Sensor_Ultrassom.h>
Sensor_Ultrassom::Sensor_Ultrassom(char pt, char pe){
this->pino_trigger=pt;
this->pino_echo=pe;
pinMode(pino_trigger,OUTPUT);
pinMode(pino_echo, INPUT);
}
double Sensor_Ultrassom::distancia_cm(){
unsigned long microsegundos;
double distancia_cm;
digitalWrite(pino_trigger,LOW);
delayMicroseconds(2);
digitalWrite(pino_trigger, HIGH);
delayMicroseconds(10);
digitalWrite(pino_trigger, LOW);
microsegundos=pulseIn(pino_echo,HIGH);
distancia_cm=(microsegundos*0.0343)/2.0;
if(distancia_cm==0 || distancia>400)
{
return -1.0;
}
else
{
return distancia_cm;
}
}
-----------------------KEYWORDS.TXT----------------------
#Keyword para a classe
Sensor_Ultrassom KEYWORD1
#Keyword para métodos
distancia_cm KEYWORD2
I think you're missing a semicolon after the } in the .h file.
GypsumFantastic, you were right, the * .H file was missing a semicolon. It's working now, but the function's reserved words aren't getting highlighted. Can you tell me why?
neraildes:
It's working now, but the function's reserved words aren't getting highlighted. Can you tell me why?
Because they don't appear in a keywords file?
TheMemberFormerlyKnownAsAWOL:
Because they don't appear in a keywords file?
Yes, they do.
-----------------------KEYWORDS.TXT----------------------
#Keyword para a classe
Sensor_Ultrassom KEYWORD1
#Keyword para métodos
distancia_cm KEYWORD2
"Sensor_Ultrassom" and "distancia_cm" do not stand out.
The file name is keywords.txt.
Do you have to restart the IDE?
(I mostly ignore colouring - it isn't important)
you need a TAB between keyword and type not blanks ...
By the way, sparkfun has a nice tutorial How to wite a great Arduino library - follow the deep links!
per1234 here making the promised visit!
Here's some additional information on the format of the keywords.txt file:
https://arduino.github.io/arduino-cli/latest/library-specification/#keywordstxt-format
Thanks, there were really blanks between the words, I put TAB and it worked. Thanks for the link and the tip.