0% found this document useful (0 votes)
14 views5 pages

Medidor Motor ESP32.Ino

Uploaded by

Brineth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views5 pages

Medidor Motor ESP32.Ino

Uploaded by

Brineth
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

#include "BluetoothSerial.

h"
#include <Ticker.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "ntc.h"

#define printByte(args) write(args);


//#define printByte(args) print(args,BYTE);

#define pwm_motor 18
#define in_rpm 12
#define out_giro2 13
#define out_giro1 14
#define in_ntc 32
#define in_pot 33
#define piloto 2

String device_name = "ESP32-BT-Motor";


////////////////////////////////////////
float diam_llanta = 10.0;//10.0 en cm
uint16_t diente_x_vuelta = 20;
float temp_set = 35.0;
////////////////////////////////////////
uint16_t count_tmr1 = 0;
volatile uint16_t NumPulsos = 0;
uint8_t _enable_med = 0;
uint16_t pulsos = 0;
uint8_t one_lap = 0;
float res = 0.0;
float _cm_seg = 0;
uint32_t _cm_hor = 0;
uint32_t _mtrs_hor = 0;
float _km_hor = 0.0;
uint8_t rpm_ = 0;
boolean muestra = false;
uint16_t time_wait = 0;
uint16_t rpm = 0;
float temp = 0.0;
const int ledChannel = 0;
const int frequency = 5000;
const int resolution = 8;
double adcMax = 4095.0; // ADC resolution 12-bit (0-4095)
double Vs = 3.3; // supply voltage
double adc = 0;
double Vout, Rt = 0;
double T, Tc, Tf = 0;
double R1 = 10000.0; // voltage divider resistor value
double Beta = 3950.0; // Beta value
double To = 298.15; // Temperature in Kelvin for 25 degree Celsius
double Ro = 10000.0; // Resistance of Thermistor at 25 degree Celsius
boolean encendido = false;
uint8_t grados[8] = {0x00,0x04,0x0A,0x0A,0x04,0x00,0x00};

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)


#error Bluetooth is not enabled! Please run `make menuconfig` to and
enable it
#endif
#if !defined(CONFIG_BT_SPP_ENABLED)
#error Serial Bluetooth not available or not enabled. It is only
available for the ESP32 chip.
#endif

Ticker Timmer;
BluetoothSerial SerialBT;
LiquidCrystal_I2C lcd(0x27,16,2);

void Leer_BT(){
if (SerialBT.available()) {
char caracter = SerialBT.read();
if(caracter == 'a'){
digitalWrite(piloto, HIGH);
encendido = true;
} else if(caracter == 'b'){
digitalWrite(piloto, LOW);
encendido = false;
temp = 0.0;
ledcWrite(ledChannel, 0);
digitalWrite(out_giro1, LOW);
digitalWrite(out_giro2, LOW);
} else if(caracter == 'c'){
if(encendido == true){
digitalWrite(out_giro2, LOW);
delay(100);
digitalWrite(out_giro1, HIGH);
}
} else if(caracter == 'd'){
if(encendido == true){
digitalWrite(out_giro1, LOW);
delay(100);
digitalWrite(out_giro2, HIGH);
}
}
}

void lee_pot(){
uint16_t result;
uint16_t sensorValue;
uint8_t veces;

veces = 10;
sensorValue = 0;
for(uint8_t i = 0; i < veces; i++){
sensorValue += analogRead(in_pot);
delay(1);
}

result = sensorValue / veces;

uint8_t value_pwm = map(result, 0, 4096, 0, 255);


if(Tc > temp_set){
ledcWrite(ledChannel, 0);
} else {
ledcWrite(ledChannel, value_pwm);
}
}

void lectura_ntc(){
adc = analogRead(in_ntc);
adc = ADC_LUT[(int)adc];

Vout = adc * Vs/adcMax;


Rt = R1 * Vout / (Vs - Vout);

T = 1/(1/To + log(Rt/Ro)/Beta); // Temperature in Kelvin


Tc = T - 273.15; // Celsius
Tf = Tc * 9 / 5 + 32; // Fahrenheit

temp = Tc;
if (Tc > 0) {
Serial.print("T: ");
Serial.println(Tc);
}

uint8_t Obtener_rpm() {
//float _rpm;
uint8_t _rpm;

res = pulsos / diente_x_vuelta;


_rpm = (uint8_t)(res);

/*res = diam_llanta / diente_x_vuelta;//9


_cm_seg = res * pulsos;//10
_cm_hor = (uint32_t)(_cm_seg * 3600);
_mtrs_hor = _cm_hor / 100;
_km_hor = _mtrs_hor / 1000.0;
_rpm = _km_hor;*/

return _rpm;
}

void muestra_pantalla(){
lcd.setCursor(0,0);
lcd.print("RPM: ");
//lcd.print("KM/H: ");
lcd.print(rpm);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print("T: ");
lcd.print(temp,1);
lcd.print(" ");
lcd.printByte(0);
lcd.print("C");
lcd.print(" ");
}

void IRAM_ATTR velocidad() {


if(encendido == true)
NumPulsos++;
else
NumPulsos = 0;
}

void Cambio_estado(){
//timmer.detach(); // detener el contador
if(encendido == true){
time_wait++;
if(time_wait == 20){ // 2 segundos
time_wait = 0;
muestra = true;
}

count_tmr1++;
if (count_tmr1 == 10) {
count_tmr1 = 0;
_enable_med = 1;
pulsos = NumPulsos;
NumPulsos = 0;
}

} else if(encendido == false){


time_wait = 0;
muestra = false;
}

void setup() {
SerialBT.begin(device_name);
#ifdef USE_PIN
SerialBT.setPin(pin);
Serial.println("Using PIN");
#endif

//pinMode(in_pot, INPUT);
//pinMode(in_ntc, INPUT);
pinMode(in_rpm, INPUT);
pinMode(piloto, OUTPUT);
pinMode(out_giro1, OUTPUT);
pinMode(out_giro2, OUTPUT);
pinMode(pwm_motor, OUTPUT);
attachInterrupt(digitalPinToInterrupt(in_rpm), velocidad, FALLING);
Timmer.attach_ms(100, Cambio_estado);
ledcSetup(ledChannel, frequency, resolution);
ledcAttachPin(pwm_motor, ledChannel);
ledcWrite(ledChannel, 0);

lcd.init();
lcd.init();
lcd.backlight();
lcd.createChar(0, grados);
lcd.home();
muestra_pantalla();
delay(500);
}

void loop() {
Leer_BT();
if(encendido == true){
lee_pot();
if(muestra == true){
lectura_ntc();
muestra = false;
}
if (_enable_med == 1) {
rpm = Obtener_rpm();
//rpm_ = (uint8_t)(rpm);
_enable_med = 0;
}
}
muestra_pantalla();
}

You might also like