Hello, I made an air simulation control with the Arduino Micro board, it works correctly, but it usually crashes, and I have to reconnect it to get it working again, does anyone know where the problem could be?
I use an AS5600 sensor and a 74hc4067 Multiplexer
(Olá, fiz um controle de simulação aérea com a placa arduino micro, funciona corretamente, mas geralmentre trava, e tenho que re conectar para voltar a funcionar, alguem sabe onde pode ser o problema?
Uso sensor AS5600 e Multiplexador 74hc4067)
Thanks
Follow the code:
(Begue o código)
#include "MUX74HC4067.h"
#include <AS5600.h>
#include <Joystick.h>
#include <Wire.h>
#include <Arduino_FreeRTOS.h>
#include <semphr.h>
// Sensor AS5600
AS5600 sensor;
// Mutex para acesso ao sensor/I2C
SemaphoreHandle_t i2cMutex;
// Cabeçalho tarefa analógica
TaskHandle_t taskAnalogicoHandle;
// Configurado pinos MUX74HC4067
MUX74HC4067 mux(7, 8, 14, 16, 15);
// SIG = 7
// S0 = 8
// S1 = 14
// S2 = 16
// S3 = 15
// EN = GND
// VCC = 5V
// GND = GND
//##################################################
//############ Configuração ########################
// Nativo do Arduino
int quantidadeBotoes = 9; // No máximo 11 botões BOTÕES DIGITAIS ==> os pinos 6, 4, 1, 0, 5, 10, 9, 19, 18, {21, 22}
int setBotoesAD[2] = {0, 0}; // 0 => Digital | 1 => Analógico ==> Sequencia dos pinos 21, 20
// Use os dois primeiros para analógico, pinos 21 | 20, sequência setRxAxis | setRyAxis
// Nativo do MUX74HC4067
int quantidadeBotoesMUX = 16; // No máximo 16 BOTÕES DIGITAIS ==> 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15
// Configurando os 16 BOTÕES DIGITAIS DO MUX74HC4067 !!!!A DEFINIR NA MONTAGEM!!!!
// Botão 12(?) | Botão 13(?) | Botão 14(?) | Botão 15(?) | Botão 16(?) | Botão 17(?) | Botão 18(?) | Botão 19(?)
// Botão 20(?) | Botão 21(?) | Botão 22(?) | Botão 23(?) | Botão 24(?) | Botão 25(?) | Botão 26(?) | Botão 27(?)
// X Axis(0) | Y Axis(1) | Z Axis(2) | Rx Axis(3) | Ry Axis(4) | Rz Axis(5) | Accelerator(6) | Brake(7)
bool sensores[8] = {true, true, true, true, true, true, true, false};
// Para desabilitar RX, RY mudar para false acima
// Se não funcionar mudar tambem nas linhas 64,65 acrescentar false e comentar ex.:false, //sensores[3], //Include Rx Axis
//############ Configuração ########################
//##################################################
// Configurando os 9 botões DIGITAIS NATIVOS
// Botão 1(6) | Botão 2(4) | Botão 3(1) | Botão 4(0) | Botão 5(5) | Botão 6(10) | Botão 7(9) | Botão 8(19) | Botão 9(18)
int pinBT[9] = {6, 4, 1, 0, 5, 10, 9, 19, 18};
// Configurando os 2 botões DIGITAIS | ANALÓGICOS
// Botão 10(21) | Botão 11(20)
int botoesAD[2] = {21, 20};
// Configuração do Joystick
Joystick_ Joystick(0x03, //Joystick HID ID: A Hex value identifier for HID Device Recognition (default: 0x03). DO NOT USE 0x01 or 0x02
JOYSTICK_TYPE_MULTI_AXIS,
quantidadeBotoes+quantidadeBotoesMUX+2, //Button Count: Number of Buttons shown to HID system (default: 32)
0, //Hat Switch Count: Number of Hat Switches, max 2. (default:2)
sensores[0], //Include X Axis
sensores[1], //Include Y Axis
sensores[2], //Include Z Axis
sensores[3], //Include Rx Axis
sensores[4], //Include Ry Axis
sensores[5], //Include Rz Axis
false, //Include Rudder
sensores[6], //Include Throttle
false, //Include Accelerator
sensores[7], //Include Brake
false); //Include Steering
// Seletor de portas do TCA
void TCA9548A(uint8_t bus)
{
if (bus > 7) return;
//if (xSemaphoreTake(i2cMutex, portMAX_DELAY) == pdTRUE) {
Wire.beginTransmission(0x70);
Wire.write(1 << bus);
Wire.endTransmission();
delay(10);
xSemaphoreGive(i2cMutex);
//}
}
void setup()
{
Serial.begin(115200);
// Cria o mutex
i2cMutex = xSemaphoreCreateMutex();
for(int a=0;a<quantidadeBotoes;a++){
pinMode(pinBT[a], INPUT_PULLUP);
}
for(int a=0;a<2;a++){
if(setBotoesAD[a] == 0){
pinMode(botoesAD[a], INPUT_PULLUP);
}else if(setBotoesAD[a] == 1){
pinMode(botoesAD[a], INPUT_PULLUP);
}
}
// Criando tarefa
xTaskCreate(TaskAnalogico, "Analogico", 128, NULL, 0, &taskAnalogicoHandle);
mux.signalPin(7, INPUT_PULLUP, DIGITAL);
Joystick.begin(true);
}
void loop()
{
// Dados Mux
byte data;
// Responsável pelos botões
int o=0;
int p=0;
for(int a=0;a<(quantidadeBotoes+quantidadeBotoesMUX+2);a++){
// 4 botões fixo DIGITAL | ANALOGICO
if(a < 2){
if(setBotoesAD[a] == 0){
if (digitalRead(botoesAD[a]) == LOW){
Joystick.setButton(a, HIGH);
}else if (digitalRead(botoesAD[a]) == HIGH){
Joystick.setButton(a, LOW);
}
}
}
// Botões vindo da Placa
if (a >= 2 and a < (quantidadeBotoes+2)){
if (digitalRead(pinBT[o]) == LOW){
Joystick.setButton(a, HIGH);
}else{
Joystick.setButton(a, LOW);
}
o++;
}
// Botões vindo do MUX
if (a >= (quantidadeBotoes+2) and a < (quantidadeBotoesMUX+quantidadeBotoes+2)){
data = mux.read(p);
if (data == HIGH ){
Joystick.setButton(a, LOW);
}else if ( data == LOW ){
Joystick.setButton(a, HIGH);
}
p++;
}
}
delay(10);
}
// X Axis | Y Axis | Z Axis | Rx Axis | Ry Axis | Rz Axis | Accelerator | Brake
void sensoresAxis(bool XAxis, bool YAxis, bool ZAxis, bool RxAxis, bool RyAxis, bool RzAxis, bool Accelerator, bool Brake){
if(XAxis){
int val = safeGetPosition(0);
Joystick.setXAxis(map(val,0,1023,0,255));
Serial.print("X Axis: ");
Serial.println(map(val,0,1023,0,255));
}
if(YAxis){
int val = safeGetPosition(1);
Joystick.setYAxis(map(val,0,1023,0,255));
Serial.print("Y Axis: ");
Serial.println(map(val,0,1023,0,255));
}
if(ZAxis){
int val = safeGetPosition(2);
Joystick.setZAxis(map(val,0,1023,0,255));
Serial.print("Z Axis: ");
Serial.println(map(val,0,1023,0,255));
}
if(RxAxis){
if(setBotoesAD[0] == 0){
int val = safeGetPosition(3);
Joystick.setRxAxis(map(val,0,1023,0,255));
Serial.print("Rx Axis: ");
Serial.println(map(val,0,1023,0,255));
}
if(setBotoesAD[0] == 1){
Joystick.setRxAxis(map(analogRead(botoesAD[0]),0,1023,0,255));
}
}
if(RyAxis){
if(setBotoesAD[1] == 0){
int val = safeGetPosition(4);
Joystick.setRyAxis(map(val,0,1023,255,0));
Serial.print("Ry Axis: ");
Serial.println(map(val,0,1023,255,0));
}
if(setBotoesAD[1] == 1){
Joystick.setRyAxis(map(analogRead(botoesAD[1]),0,1023,255,0));
}
}
if(RzAxis){
int val = safeGetPosition(5);
Joystick.setRzAxis(map(val,0,1024,0,255));
Serial.print("Rz Axis: ");
Serial.println(map(val,0,1024,0,255));
}
if(Accelerator){
int val = safeGetPosition(6);
Joystick.setThrottle(map(val,0,1023,-255,0));
Serial.print("Accelerator: ");
Serial.println(map(val,0,1023,-255,0));
}
if(Brake){
int val = safeGetPosition(7);
Joystick.setBrake(map(val,0,1023,0,255));
Serial.print("Brake: ");
Serial.println(map(val,0,1023,0,255));
}
}
void TaskAnalogico(void *pvParameters)
{
(void) pvParameters;
for (;;)
{
// Função dos sensores;
sensoresAxis(sensores[0], sensores[1], sensores[2], sensores[3], sensores[4], sensores[5], sensores[6], sensores[7]);
delay(5);
}
}
// Função protegida para acessar o sensor AS5600
int safeGetPosition(uint8_t bus) {
int pos = 0;
if (xSemaphoreTake(i2cMutex, portMAX_DELAY) == pdTRUE) {
TCA9548A(bus);
pos = sensor.getPosition();
xSemaphoreGive(i2cMutex);
}
return pos;
}