Hi. When I build my code, Visual Studio report next error:
"lcd.ino: 18:1: error: 'veloRes' does not name a type veloRes velocimetro(int pin)
Error compiling project sources
Build failed for project 'lcd' "
Then I build my code in arduino IDE but there are not error.
Why doesn't run in VisualStudio? Know anyone how to fix this build error?
Thanks.
My code is (The comments are in spanish but it are irrelevant for the question):
#include <LiquidCrystal_I2C.h>
#include <LiquidCrystal.h>
#include <Wire.h>
//estructura de datos para la función velocímetro
struct veloRes
{
double velocidad;//almacena la velocidad
double frecuencia;//almacena la frecuencia
bool valido;//almacena si la medida ha sido válida o no
String msg;//si algo ha fallado se almacena aquí un mensaje descriptivo
};
//Crear el objeto lcd dirección 0x27 y 16 columnas x 2 filas
LiquidCrystal_I2C lcd(0x27,16,2);
veloRes datos = { 0,0,false,"" };
const int pinVel = 7;//pin para conectar el radar
const int nLect = 4;//número de lectiras para realizar una medida
//Función que devuelve la velocidad, la frecuencia y los errores si los hay
veloRes velocimetro(int pin) {
veloRes resultados;
long matAnchoPulso[nLect];
long pulso_tot = 0;
resultados.velocidad = 0;
resultados.frecuencia = 0;
resultados.valido = false;
resultados.msg = "";
noInterrupts();//Desactiva las interrupciones
pulseIn(pin, HIGH);
for (int i = 0; i < nLect; i++)//realiza las lecturas necesatias
{
long anchoPulso = 0;
anchoPulso = pulseIn(pin, HIGH);
anchoPulso += pulseIn(pin, LOW);
matAnchoPulso[i] = anchoPulso;
pulso_tot += anchoPulso;
if (anchoPulso == 0)//si no ha habido pulso esctibe un mensaje de error y devuelve los valores
{
resultados.valido = false;
resultados.msg = "pulso 0";
interrupts();
return resultados;
break;
}
}
interrupts();//activa las interrupciones
for (int i = 0; i < nLect - 1; i++)//comprueba que el ancho de los pulsos no varíe demasiado
{//si los pulsos no son más o menos del mismo tamaño saca un error como resultado
if (((matAnchoPulso[0] * 2) < matAnchoPulso[i + 1]) || ((matAnchoPulso[0] / 2) > matAnchoPulso[i + 1]))
{
resultados.valido = false;
resultados.msg = "pulso no válido";
return resultados;
break;
}
else//si no pone a true la variable
{
resultados.valido = true;
}
}
//si todoes correcto calcula la frecuencia y la velocidad y devuelve los resultados
resultados.frecuencia = double(1000000 / (pulso_tot / nLect));
resultados.velocidad = double(resultados.frecuencia / 19.49);
resultados.msg = "todo OK";
return resultados;
}
void setup() {
//se inicializa la pantalla
lcd.init();
lcd.backlight();
//se declara comoentrada el pin para el radar
pinMode(pinVel, INPUT);
}
void loop() {
//se mide la velocidad
datos = velocimetro(pinVel);
if (datos.valido)//si elcálculo ha salido bien, se sacan por la pantalla los resultados
{
pantalla(datos.velocidad, datos.frecuencia);
}
else//si no, se saca por la pantalla el mensaje de error
{
lcd.clear();
lcd.print(datos.msg);
}
delay(500);//tiempo de espera entre medidas
}
//función para sacar los datos por pantalla
void pantalla(double vel, double freq){
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Vel: ");
lcd.print(vel,1);
lcd.print("km/h");
lcd.setCursor(0, 1);
lcd.print("frec: ");
lcd.print(freq,1);
lcd.print("Hz");
}