Hola gente, por este código eh hecho varias consultas a lo largo del desarrollo, tengo 6 encoders en el, y cada cosa nueva que agregaba me traía problemas con la lectura de los mismos, fui resolviendo y el código funcionaba completo.
Lo ultimo que agrege al codigo y ahora no encuentro solucion a la falla de lectura de los enconders es una matriz de botones de 6 x 7 de la cual aca dejo el link:
https://forum.arduino.cc/t/matriz-de-botones/663252/19
Repito el problema: los encoders no muestran datos, como si el código fuese muy lento para leer el cambio de posición
aca les comparto el codigo completo a lo mejor a los ojos de algún experto la solución es simple:
#include <Arduino.h>
// Pertenece a la botonera de linea 3 a 7
#define BUTTON_COUNT 42
#define KEYPAD_OUTPUT_BEGIN 18
#define KEYPAD_OUTPUT_END 23
#define KEYPAD_INPUT_BEGIN 24
#define KEYPAD_INPUT_END 30
// Variables Globales Encodres
unsigned long time;
char buffer_temporal[10];
// encoder "PGM1" - Palanca de Gases Motor 1
int PGM1_PCLK = 48;//"PCLK" Pin CLK.
int PGM1_PDT = 49;//_"PDT" Pin DT.
int PGM1_RE_P = 1;//_"P" Posicion.
unsigned long PGM1_RE_T;
bool PGM1_RE_A = true;
bool PGM1_RE_B = true;
// encoder "PGM2" - Palanca de Gases Motor 2
int PGM2_PCLK = 52;// "PCLK" Pin CLK.
int PGM2_PDT = 53;// _"PDT" Pin DT.
int PGM2_RE_P = 1;// _"P" Posicion.
unsigned long PGM2_RE_T;
bool PGM2_RE_A = true;
bool PGM2_RE_B = true;
// encoder "PHM1" - Paso de Helice Motor 1
int PHM1_PCLK = 50;//"PCLK" Pin CLK.
int PHM1_PDT = 51;//_"PDT" Pin DT.
int PHM1_RE_P = 1;//_"P" Posicion.
unsigned long PHM1_RE_T;
bool PHM1_RE_A = true;
bool PHM1_RE_B = true;
// encoder "PHM2" - Paso de Helice Motor 2
int PHM2_PCLK = 45;// "PCLK" Pin CLK.
int PHM2_PDT = 44;// _"PDT" Pin DT.
int PHM2_RE_P = 1;// _"P" Posicion.
unsigned long PHM2_RE_T;
bool PHM2_RE_A = true;
bool PHM2_RE_B = true;
// encoder "PMM1" - Palanca de Mezcla Motor 1
int PMM1_PCLK = 42;//"PCLK" Pin CLK.
int PMM1_PDT = 43;//_"PDT" Pin DT.
int PMM1_RE_P = 9;//_"P" Posicion.
unsigned long PMM1_RE_T;
bool PMM1_RE_A = true;
bool PMM1_RE_B = true;
// encoder "PMM2" - Palanca de Mezcla Motor 2
int PMM2_PCLK = 47;// "PCLK" Pin CLK.
int PMM2_PDT = 46;// _"PDT" Pin DT.
int PMM2_RE_P = 9;// _"P" Posicion.
unsigned long PMM2_RE_T;
bool PMM2_RE_A = true;
bool PMM2_RE_B = true;
//Variables Tren de Aterrizaje
int TALN = 41; // _ _ _ _ _ _ _ _ "L" Led, "N" Nariz.
int TALI = 40; // _ _ _ _ _ _ _ _ "L" Led, "I" Izquierda.
int TALD = 38; // _ _ _ _ _ _ _ _ "L" Led, "D" Derecha.
int TALP = 39; // _ _ _ _ _ _ _ _ "L" Led, "P" Peligro.
int TAI = 36; //_ _ _ _ _ _ _ _ _ "I" Interruptor.
int TAPT = 37; // _ _ _ _ _ _ _ _ "P" Pulsador, "T" Test ok.
int TACR; //_ _ _ _ _ _ _ _ _ _ _ "C" Codigo, "R" Recibido: Almacena codigo de Link2FS para TA.
int TAVEI = digitalRead(TAI);// _ "V" Variable, "E" Estado, "I" Interruptor.
int TAVEAI = 0;// _ _ _ _ _ _ _ _ "V" Variable, "E" Estado, "A" Anterior , "I" Interruptor.
//Variables Flaps
int FAL = 33; //_ _ _ _ _ "L" Led.
const int FPD = 34; //_ _ "P" Pulsador, "D" Down.
const int FPU = 35; //_ _ "P" Pulsador, "U" Up.
int FPUV = 0; //_ _ _ _ _ "V" Variable.
int FPUVA = 0; // _ _ _ _ "V" Variable, "A" Anterior.
int FPDV = 0; //_ _ _ _ _ "V" Variable.
int FPDVA = 0; // _ _ _ _ "V" Variable, "A" Anterior.
int FP = 0; // _ _ _ _ _ _"P" Posicion.
//Variables Freno de Estacionamiento
int FELP = 32; // _ _ _ _ _ _ _ _ "L" Led, "P" Peligro.
int FEI = 31; //_ _ _ _ _ _ _ _ _ "I" Interruptor.
int FEVEI = digitalRead(FEI);// _ "V" Variable, "E" Estado, "I" Interruptor.
int FEVEAI = 0;// _ _ _ _ _ _ _ _ "V" Variable, "E" Estado, "A" Anterior , "I" Interruptor.
//Subcodigo Botonera
uint8_t keypad_button_pressed[BUTTON_COUNT];
volatile uint32_t ticks;
uint32_t lastTrigger;
ISR(TIMER0_COMPA_vect, ISR_BLOCK) {
ticks++;
}
void init_systicks() {
TCCR0B = _BV(CS02) | _BV(CS00);
TCCR0A = _BV(WGM01);
TIMSK0 = _BV(OCIE0A);
// 8000000/1024/78 == 100HZ -> 10 ms
OCR0A = 38; // !!! must be set last or it will not work!
}
uint32_t math_calc_diff(uint32_t value1, uint32_t value2) {
if (value1 == value2) {
return 0;
}
if (value1 > value2) {
return (value1 - value2);
}
else {
// check for overflow
return (0xffffffff - value2 + value1);
}
}
void keypad_reset_output() {
// configure pull ups
digitalWrite(18, HIGH);
digitalWrite(19, HIGH);
digitalWrite(20, HIGH);
digitalWrite(21, HIGH);
digitalWrite(22, HIGH);
digitalWrite(23, HIGH);
}
void clear_buttons() {
for(int i=0; i < BUTTON_COUNT; i++) {
keypad_button_pressed[i] = 0;
}
}
void keypad_setup() {
// initialize the digital pin as an output:
pinMode(18, OUTPUT);
pinMode(19, OUTPUT);
pinMode(20, OUTPUT);
pinMode(21, OUTPUT);
pinMode(22, OUTPUT);
pinMode(23, OUTPUT);
keypad_reset_output();
pinMode(24, INPUT);
pinMode(25, INPUT);
pinMode(26, INPUT);
pinMode(27, INPUT);
pinMode(28, INPUT);
pinMode(29, INPUT);
pinMode(30, INPUT);
// configure pull ups
digitalWrite(24, HIGH);
digitalWrite(25, HIGH);
digitalWrite(26, HIGH);
digitalWrite(27, HIGH);
digitalWrite(28, HIGH);
digitalWrite(29, HIGH);
digitalWrite(30, HIGH);
}
void keypad_read_buttons() {
clear_buttons();
uint8_t y=0;
for(int i=KEYPAD_OUTPUT_BEGIN; i <= KEYPAD_OUTPUT_END; i++) {
keypad_reset_output();
digitalWrite(i, LOW);
uint8_t x=0;
for(int j=KEYPAD_INPUT_BEGIN; j <= KEYPAD_INPUT_END; j++) {
if (digitalRead(j) == LOW) {
uint8_t index = x+7*y; //OJO el numero de la ecuacion es la cantidad de columnas
keypad_button_pressed[index] = 1;
}
x++;
}
y++;
}
}
uint8_t keypad_button_is_pressed() {
for (int i=0; i < BUTTON_COUNT; i++) {
if (keypad_button_pressed[i]) {
return 1;
}
}
return 0; // no button pressed
}
String datos[42] = {"H01", "H02","H03","H04","H05","H06","H07","H08","H09","H10","H11","H12","H13","H14", "H15","H16","H17","H18","H19","H20",
"H21", "H22","H23","H24","H25","H26","H27","H28","H29","H30","H31","H32","H33","H34", "H35","H36","H37","H38","H39","H40",
"H41", "H42"};
//Fin Subcodigo botonera
void setup() {
Serial.begin (115200);
// SETUP de Encoders
pinMode (PGM1_PCLK,INPUT);
pinMode (PGM1_PDT,INPUT);
pinMode (PGM2_PCLK,INPUT);
pinMode (PGM2_PDT,INPUT);
pinMode (PHM1_PCLK,INPUT);
pinMode (PHM1_PDT,INPUT);
pinMode (PHM2_PCLK,INPUT);
pinMode (PHM2_PDT,INPUT);
pinMode (PMM1_PCLK,INPUT);
pinMode (PMM1_PDT,INPUT);
pinMode (PMM2_PCLK,INPUT);
pinMode (PMM2_PDT,INPUT);
//Setup Tren de aterrizaje
pinMode(TALN, OUTPUT);
pinMode(TALI, OUTPUT);
pinMode(TALD, OUTPUT);
pinMode(TALP, OUTPUT);
pinMode(TAI, INPUT_PULLUP);
pinMode(TAPT, INPUT_PULLUP);
//Setup Flaps
pinMode(FPU, INPUT_PULLUP);
pinMode(FPD, INPUT_PULLUP);
pinMode(FAL, OUTPUT);
//Setup Freno de Estacionamiento
pinMode(FEI, INPUT_PULLUP);
pinMode(FELP, OUTPUT);
//Setup Botonera
init_systicks();
keypad_setup();
}
void loop() {
//_________ LOOP Encoders______________
time = millis();
//------Palanca de Gases Motor 1
if (digitalRead(PGM1_PCLK) == LOW){
PGM1_RE_T = time;
if (PGM1_RE_A == true){
PGM1_RE_P ++;
PGM1_RE_A = false;
PGM1_RE_B = false;
PGM1_RE_P = min(10, max(0, PGM1_RE_P));
sprintf(buffer_temporal, "C56%03u\r\n", PGM1_RE_P*99/10);
Serial.print(buffer_temporal);
}
}
if (digitalRead(PGM1_PDT)== LOW){
PGM1_RE_T = time;
if(PGM1_RE_B == true){
PGM1_RE_P --;
PGM1_RE_A = false;
PGM1_RE_B = false;
PGM1_RE_P = min(10, max(0, PGM1_RE_P));
sprintf(buffer_temporal, "C56%03u\r\n", PGM1_RE_P*99/10);
Serial.print(buffer_temporal); }
}
if (time - PGM1_RE_T>10){
PGM1_RE_A = true;
PGM1_RE_B = true;
}
//------Palanca de Gases Motor 2
if (digitalRead(PGM2_PCLK) == LOW){
PGM2_RE_T = time;
if (PGM2_RE_A == true){
PGM2_RE_P ++;
PGM2_RE_A = false;
PGM2_RE_B = false;
PGM2_RE_P = min(10, max(0, PGM2_RE_P));
sprintf(buffer_temporal, "C57%03u\r\n", PGM2_RE_P*99/10);
Serial.print(buffer_temporal);
}
}
if (digitalRead(PGM2_PDT)== LOW){
PGM2_RE_T = time;
if(PGM2_RE_B == true){
PGM2_RE_P --;
PGM2_RE_B = false;
PGM2_RE_A = false;
PGM2_RE_P = min(10, max(0, PGM2_RE_P));
sprintf(buffer_temporal, "C57%03u\r\n", PGM2_RE_P*99/10);
Serial.print(buffer_temporal);
}
}
if (time - PGM2_RE_T>10){
PGM2_RE_A = true;
PGM2_RE_B = true;
}
//------Paso Helice Motor 1
if (digitalRead(PHM1_PCLK) == LOW){
PHM1_RE_T = time;
if (PHM1_RE_A == true){
PHM1_RE_P ++;
PHM1_RE_A = false;
PHM1_RE_B = false;
PHM1_RE_P = min(10, max(0, PHM1_RE_P));
sprintf(buffer_temporal, "C60%03u\r\n", PHM1_RE_P*99/10);
Serial.print(buffer_temporal);
}
}
if (digitalRead(PHM1_PDT)== LOW){
PHM1_RE_T = time;
if(PHM1_RE_B == true){
PHM1_RE_P --;
PHM1_RE_A = false;
PHM1_RE_B = false;
PHM1_RE_P = min(10, max(0, PHM1_RE_P));
sprintf(buffer_temporal, "C60%03u\r\n", PHM1_RE_P*99/10);
Serial.print(buffer_temporal);
}
}
if (time - PHM1_RE_T>10){
PHM1_RE_A = true;
PHM1_RE_B = true;
}
//------Paso Helice Motor 2
if (digitalRead(PHM2_PCLK) == LOW){
PHM2_RE_T = time;
if (PHM2_RE_A == true){
PHM2_RE_P ++;
PHM2_RE_A = false;
PHM2_RE_B = false;
PHM2_RE_P = min(10, max(0, PHM2_RE_P));
sprintf(buffer_temporal, "C61%03u\r\n", PHM2_RE_P*99/10);
Serial.print(buffer_temporal);
}
}
if (digitalRead(PHM2_PDT)== LOW){
PHM2_RE_T = time;
if(PHM2_RE_B == true){
PHM2_RE_P --;
PHM2_RE_B = false;
PHM2_RE_A = false;
PHM2_RE_P = min(10, max(0, PHM2_RE_P));
sprintf(buffer_temporal, "C61%03u\r\n", PHM2_RE_P*99/10);
Serial.print(buffer_temporal);
}
}
if (time - PHM2_RE_T>10){
PHM2_RE_A = true;
PHM2_RE_B = true;
}
//------Palanca de Mezcla Motor 1
if (digitalRead(PMM1_PCLK) == LOW){
PMM1_RE_T = time;
if (PMM1_RE_A == true){
PMM1_RE_P ++;
PMM1_RE_A = false;
PMM1_RE_B = false;
PMM1_RE_P = min(10, max(0, PMM1_RE_P));
sprintf(buffer_temporal, "C58%03u\r\n", PMM1_RE_P*99/10);
Serial.print(buffer_temporal);
}
}
if (digitalRead(PMM1_PDT)== LOW){
PMM1_RE_T = time;
if(PMM1_RE_B == true){
PMM1_RE_P --;
PMM1_RE_A = false;
PMM1_RE_B = false;
PMM1_RE_P = min(10, max(0, PMM1_RE_P));
sprintf(buffer_temporal, "C58%03u\r\n", PMM1_RE_P*99/10);
Serial.print(buffer_temporal);
}
}
if (time - PMM1_RE_T>10){
PMM1_RE_A = true;
PMM1_RE_B = true;
}
//------Palanca de Mezcla Motor 2
if (digitalRead(PMM2_PCLK) == LOW){
PMM2_RE_T = time;
if (PMM2_RE_A == true){
PMM2_RE_P ++;
PMM2_RE_A = false;
PMM2_RE_B = false;
PMM2_RE_P = min(10, max(0, PMM2_RE_P));
sprintf(buffer_temporal, "C59%03u\r\n", PMM2_RE_P*99/10);
Serial.print(buffer_temporal);
}
}
if (digitalRead(PMM2_PDT)== LOW){
PMM2_RE_T = time;
if(PMM2_RE_B == true){
PMM2_RE_P --;
PMM2_RE_B = false;
PMM2_RE_A = false;
PMM2_RE_P = min(10, max(0, PMM2_RE_P));
sprintf(buffer_temporal, "C59%03u\r\n", PMM2_RE_P*99/10);
Serial.print(buffer_temporal);
}
}
if (time - PMM2_RE_T>10){
PMM2_RE_A = true;
PMM2_RE_B = true;
}
// Control TA - Tren de Aterrizaje
if(Serial.available()){
TACR = getChar(); // _ _ Lectura codigo Link2FS para pocision del TA (ASCII) ? -> 63 Y -> 89
if(TACR == 63){
TACR = getChar();
if(TACR == 89){
int TAVN = getChar()-48; // _ _ "V" Variable, "N" Nariz. (Se resta 48 pq el 2 en ASCII se representa con el codigo 50)
int TAVI = getChar()-48; // _ _ "V" Variable, "I" Izquierda. (Se resta 48 pq el 2 en ASCII se representa con el codigo 50)
int TAVD = getChar()-48; // _ _ "V" Variable, "N" Derecha. (Se resta 48 pq el 2 en ASCII se representa con el codigo 50)
if(TAVN == 2){digitalWrite(TALN, HIGH);}else {digitalWrite(TALN, LOW);}
if(TAVI == 2){digitalWrite(TALI, HIGH);} else {digitalWrite(TALI, LOW);}
if(TAVD == 2){digitalWrite(TALD, HIGH);} else {digitalWrite(TALD, LOW);}
if(TAVN+TAVI+TAVD<6 && TAVN+TAVI+TAVD>0){digitalWrite(TALP, HIGH);} else {digitalWrite(TALP, LOW);}
}
}
}
// Envio de codigo a Link2FS acorde a la posicion del interruptor de TA
TAVEI = digitalRead(TAI);
if(TAVEI == 0 && digitalRead(TALN)== LOW && TAVEI != TAVEAI){Serial.println("C02");}
if(TAVEI != 0 && digitalRead(TALN) == HIGH && TAVEI != TAVEAI){Serial.println("C01");}
TAVEAI = TAVEI;
int TAVPT = digitalRead(TAPT);//"V" Variable, "P" Pulsador, "T" Test.
if(TAVPT == 0){
digitalWrite(TALN, HIGH); digitalWrite(TALI, HIGH); digitalWrite(TALD, HIGH);digitalWrite(TALP, HIGH);delay(500);
digitalWrite(TALN, LOW); digitalWrite(TALI, LOW); digitalWrite(TALD, LOW);digitalWrite(TALP, LOW);}
// Control F - Flaps
FPUV = digitalRead(FPU);
if (FPUV == HIGH && FPUVA != HIGH) {
Serial.println("C15");
FP --;
FP = min(4, max(0, FP));
}
delay(10);
FPUVA = FPUV;
FPDV = digitalRead(FPD);
if (FPDV == HIGH && FPDVA != HIGH) {
Serial.println("C14");
FP ++;
FP = min(4, max(0, FP));
}
FPDVA = FPDV;
if(FP == 0){digitalWrite(FAL, LOW);} else {digitalWrite(FAL, HIGH);}
// Control FE - Freno de Estacionamiento
FEVEI = digitalRead(FEI);
if(FEVEI == 0 && digitalRead(TALN)== HIGH && FEVEI != FEVEAI){Serial.println("C040"); digitalWrite(FELP, LOW);}
if(FEVEI != 0 && digitalRead(TALN) == HIGH && FEVEI != FEVEAI){Serial.println("C041");digitalWrite(FELP, HIGH);}
FEVEAI = FEVEI;
// Matriz de Botonera
keypad_read_buttons();
// allow button processing only every 300ms (30 systicks)
if (keypad_button_is_pressed() && (math_calc_diff(ticks, lastTrigger) > 50)) {
lastTrigger = ticks;
for(int i=0; i < BUTTON_COUNT; i++) {
if (keypad_button_pressed[i]) {
Serial.println(datos[i]);
}
}
}
} // FINAL void loop OJO!!!!
//FUNCION getChar() Pertenece al Tren de Aterrizaje
char getChar(){
while(Serial.available()==0);
return((char)Serial.read());
}
aca les dejo el codigo de la botonera que esta repartido dentro del de arriba:
#include <Arduino.h>
#define BUTTON_COUNT 42
#define KEYPAD_OUTPUT_BEGIN 2
#define KEYPAD_OUTPUT_END 7
#define KEYPAD_INPUT_BEGIN 8
#define KEYPAD_INPUT_END 14
uint8_t keypad_button_pressed[BUTTON_COUNT];
volatile uint32_t ticks;
uint32_t lastTrigger;
ISR(TIMER0_COMPA_vect, ISR_BLOCK) {
ticks++;
}
void init_systicks() {
TCCR0B = _BV(CS02) | _BV(CS00);
TCCR0A = _BV(WGM01);
TIMSK0 = _BV(OCIE0A);
// 8000000/1024/78 == 100HZ -> 10 ms
OCR0A = 77; // !!! must me set last or it will not work!
}
uint32_t math_calc_diff(uint32_t value1, uint32_t value2) {
if (value1 == value2) {
return 0;
}
if (value1 > value2) {
return (value1 - value2);
}
else {
// check for overflow
return (0xffffffff - value2 + value1);
}
}
void keypad_reset_output() {
// configure pull ups
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
}
void clear_buttons() {
for(int i=0; i < BUTTON_COUNT; i++) {
keypad_button_pressed[i] = 0;
}
}
void keypad_setup() {
// initialize the digital pin as an output:
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
keypad_reset_output();
pinMode(8, INPUT);
pinMode(9, INPUT);
pinMode(10, INPUT);
pinMode(11, INPUT);
pinMode(12, INPUT);
pinMode(13, INPUT);
pinMode(14, INPUT);
// configure pull ups
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
digitalWrite(11, HIGH);
digitalWrite(12, HIGH);
digitalWrite(13, HIGH);
digitalWrite(14, HIGH);
}
// the loop() method runs over and over again,
// as long as the Arduino has power
void keypad_read_buttons() {
clear_buttons();
uint8_t y=0;
for(int i=KEYPAD_OUTPUT_BEGIN; i <= KEYPAD_OUTPUT_END; i++) {
keypad_reset_output();
digitalWrite(i, LOW);
uint8_t x=0;
for(int j=KEYPAD_INPUT_BEGIN; j <= KEYPAD_INPUT_END; j++) {
if (digitalRead(j) == LOW) {
uint8_t index = x+7*y; //OJO el numero de la ecuacion es la cantidad de columnas
keypad_button_pressed[index] = 1;
}
x++;
}
y++;
}
}
uint8_t keypad_button_is_pressed() {
for (int i=0; i < BUTTON_COUNT; i++) {
if (keypad_button_pressed[i]) {
return 1;
}
}
return 0; // no button pressed
}
String datos[42] = {"H01", "H02","H03","H04","H05","H06","H07","H08","H09","H10","H11","H12","H13","H14", "H15","H16","H17","H18","H19","H20",
"H21", "H22","H23","H24","H25","H26","H27","H28","H29","H30","H31","H32","H33","H34", "H35","H36","H37","H38","H39","H40",
"H41", "H42"};
// The setup() method runs once, when the sketch starts
void setup() {
init_systicks();
keypad_setup();
Serial.begin(9600);
}
void loop() {
keypad_read_buttons();
// allow button processing only every 300ms (30 systicks)
if (keypad_button_is_pressed() && (math_calc_diff(ticks, lastTrigger) > 50)) {
lastTrigger = ticks;
for(int i=0; i < BUTTON_COUNT; i++) {
if (keypad_button_pressed[i]) {
Serial.println(datos[i]);
}
}
}
delay(1);
}
desde ya gracias ya con esto casi tengo terminada mi cabina de simulador