Hola a todos! Voy a explicar un poco la situación.
Tengo un sensor infrarrojo, 2 motores DC y un led RGB conectado a mi Arduino Nano, todos funcionan correctamente, el problema está a la hora de controlar las funciones, específicamente una que es podes_vernos(), esta función se encarga de activar los pines pwm para activar dos motores durante un lapso de tiempo, y luego debe hacer que el led rgb haga un fade-in hacia el color amarillo; el código se ejecuta correctamente, si presiono los botones correspondientes a cualquier otra función se ejecutan sin importar el orden ni la cantidad de veces.
Despues de realizar la función podes_vernos(); no ejecuta ninguna otra función que esté asociada a ningun boton.
Es como si el programa se colgara ahi, el led queda encendido, y ya no pasa nada más. Espero haber sido lo más claro posible, dejo comentado código que no tiene que ver con las funciones, muchas gracias! Saludos.
//Inclusion de librerías
#include "I2Cdev.h"
//#include "MPU6050_6Axis_MotionApps20.h"
#include <IRremote.h>
#include <SoftwareSerial.h>
#include <DFPlayer_Mini_Mp3.h>
/*#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
#include "Wire.h"
#endif*/
SoftwareSerial mySerial(4, 7);
//MPU6050 mpu;
//Definiciones
/*#define OUTPUT_READABLE_YAWPITCHROLL
#define INTERRUPT_PIN 2 // Pin de interrupción (INT de Giroscopio como D2 de arduino)
bool blinkState = false;
// MPU control/status vars
bool dmpReady = false; // Si la conexión es correcta, se vuelve verdadero.
uint8_t mpuIntStatus; // Guarda el estado actual del pin de interrupción
uint8_t devStatus; // Retorna Success (0) o !0 (error)
uint16_t packetSize; // Tamaño predeterminado de (paquete 42 bytes)
uint16_t fifoCount; // Cuenta de los bytes de FIFO
uint8_t fifoBuffer[64]; // FIFO buffer de almacenamiento
// Variables de orientación
Quaternion q; // [w, x, y, z] Contenedor de cuadrantes
VectorFloat gravity; // [x, y, z] Vector de gravedad
float ypr[3]; // [yaw, pitch, roll] yaw/pitch/roll y gravedad vector
//motores*/
int motorbase = 5; //nuev
int motormedio = 6; //nuev
int LedPinx = 9;
int LedPiny = 10;
int LedPinz = 11;
int count = 0;
int count1 = 0;
int count2 = 0;
int cont3 = 0;
int count4 = 0;
int cont5 = 0;
bool ledon = false;
bool podesoirnos = false;
bool gustanpiratas = false;
bool podesvernos = false;
bool ledfijo = false;
bool encenderverde = false;
int contador = 0;
bool amarillo = false;
int red=0;
int green=0;
int blue=0;
////////////////////////////////////////////////////////////
//----------------funciones de varita----------------------
///////////////////////////////////////////////////////////
void podes_oirnos() {
if (podesoirnos == true) {
analogWrite(motorbase, 0);
delay(300);
analogWrite(motorbase, 150);
delay(300);
analogWrite(motorbase, 0);
delay(300);
analogWrite(motorbase, 150);
delay(300);
analogWrite(motorbase, 0);
delay(300);
count++;
}
}
//////////////////////////////////////////////////
void gustan_piratas() {
if (gustanpiratas == true) {
analogWrite(motorbase, 0);
delay(300);
analogWrite(motorbase, 150);
delay(300);
analogWrite(motorbase, 0);
delay(300);
count1++;
}
}
////////////////////////////////////////////////
void podes_vernos() {
analogWrite(motorbase, 0);
analogWrite(motormedio, 0);
delay(300);
analogWrite(motorbase, 150);
analogWrite(motormedio, 150);
delay(300);
analogWrite(motorbase, 0);
analogWrite(motormedio, 0);
delay(300);
analogWrite(motorbase, 150);
analogWrite(motormedio, 150);
delay(300);
analogWrite(motorbase, 0);
analogWrite(motormedio, 0);
delay(300);
amarillo = true;
}
/* if (cont3 < 255)
{ analogWrite(LedPiny, cont3 + 5);
analogWrite(LedPinz, cont3 + 5);
delay(30);
cont3 = cont3 + 5;
}
if (cont3 >= 255) {
analogWrite(LedPiny, 255);
analogWrite(LedPinz, 255);
}*/
/////////////////////////////
void pelear_piratas() {
if (count4 < 1) {
analogWrite(motorbase, 150);
analogWrite(motormedio, 150);
delay(500);
analogWrite(motorbase, 0);
analogWrite(motormedio, 0);
delay(500);
count4++;
}
/*if (count4==1){
analogWrite(motorbase,0);
analogWrite(motormedio,0);
}*/
if (cont5 < 255)
{ analogWrite(LedPinz, cont5 + 5);
delay(30);
cont5 = cont5 + 5;
}
else {
ledon = true;
}
/*while (cont5 == 255) {
analogWrite(LedPinz, 255);
}*/
}
// ================================================================
// === Detección de interrupción ===
// ================================================================
/*volatile bool mpuInterrupt = false; // Indica cuando el pin de interrupción entra en high
void dmpDataReady() {
mpuInterrupt = true;
}
// ================================================================
// === Setup ===
// ================================================================
int pwmx;
int pwmy;
int pwmz;
*/
// Declaracion de variables para el Infrarrojo
int IRpin = 3;
IRrecv irrecv(IRpin);
decode_results results;
void setup() {
irrecv.enableIRIn();
pinMode(LedPinx, OUTPUT);
pinMode(LedPiny, OUTPUT);
pinMode(LedPinz, OUTPUT);
pinMode(motorbase, OUTPUT);
pinMode(motormedio, OUTPUT);
//
/*#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
Wire.begin();
Wire.setClock(400000); //
#elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
Fastwire::setup(400, true);
#endif*/
// Iniciar la comunicación serial
Serial.begin(38400);
mySerial.begin(9600);
mp3_set_serial (mySerial); //set Serial for DFPlayer-mini mp3 module
mp3_set_volume (20);
while (!Serial); //
// Inicializar MPU
/* mpu.initialize();
pinMode(INTERRUPT_PIN, INPUT);
// Cargar DMP
Serial.println(F("Inicializando DMP..."));
devStatus = mpu.dmpInitialize();
// Ajustes de offsets
mpu.setXGyroOffset(-12); // X giroscopio offset
mpu.setYGyroOffset(42); // Y giroscopio offset
mpu.setZGyroOffset(73); // Z giroscopio offset
// Controles
if (devStatus == 0) {
// Encender dmp
mpu.setDMPEnabled(true);
// Habilitar la detección de interrupción en arduino
attachInterrupt(digitalPinToInterrupt(INTERRUPT_PIN), dmpDataReady, RISING);
mpuIntStatus = mpu.getIntStatus();
// DMP Listo para usar
dmpReady = true;
// Obtener paquete
packetSize = mpu.dmpGetFIFOPacketSize();
} else {
// ERROR!
// 1 = Carga de memoria inicial fallida
// 2 = DMP falla al actualizar config.
Serial.print(F("DMP fallo con codigo (code "));
Serial.print(devStatus);
Serial.println(F(")"));
}
// Configurar salida de leds
pinMode(LedPinx, OUTPUT);
pinMode(LedPiny, OUTPUT);
pinMode(LedPinz, OUTPUT);*/
}
bool gled = false;
bool fin = false;
// ======================Main==========================================
void loop() {
Serial.begin(9600);
marca1:;
if (irrecv.decode(&results))
{
if (results.value == 16753245) {
podesoirnos = true;
gled = true;
}
/* Button codes
* 1-> 16753245
2->16736925
3->16769565
4->16720605
5->16712445
6->16761405
7->16769055
8->16754775
9->16748655
0->16738455
#->16750695
Up->16756815
left->16718055
Ok->16716015
Right->16726215
down->16734885
*/
//boton 3
if (results.value == 16769565) {
podesoirnos = true;
}
if (results.value == 16720605) {
gustanpiratas = true;
}
if (results.value == 16712445) {
podesvernos = true;
}
if (results.value == 16761405 && ledon == false) {
pelear_piratas();
analogWrite(LedPinz, 255);
}
//////////////////////////////////////Llamado a funciones//////////////////////////
if (podesoirnos == true) {
podes_oirnos();
podesoirnos = false;
}
if (gustanpiratas == true) {
gustan_piratas();
gustanpiratas = false;
}
if (podesvernos == true) {
podes_vernos();
for (int i=0; i<=255;i=i+5){
delay(70);
analogWrite(LedPinz,i);
analogWrite(LedPiny,i);
}
podesvernos = false;
results.value=0;
irrecv.resume();
goto marca1;
encenderverde = true;
}
irrecv.resume(); // Receive the next value
}
}