Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentes Révision précédente Prochaine révision | Révision précédente | ||
projets:brutpix:telecommande [2021/06/24 15:46] – modification externe 127.0.0.1 | projets:brutpix:telecommande [2022/06/08 12:33] (Version actuelle) – guillaume | ||
---|---|---|---|
Ligne 160: | Ligne 160: | ||
Le code ci-dessous est une adaptation finalement assez simple de celui qui précède, le stockage des valeurs des 5 encodeurs se fait dans un tableau, qui sera réutilisé ensuite pour l' | Le code ci-dessous est une adaptation finalement assez simple de celui qui précède, le stockage des valeurs des 5 encodeurs se fait dans un tableau, qui sera réutilisé ensuite pour l' | ||
+ | |||
+ | |||
``` | ``` | ||
#include < | #include < | ||
#include < | #include < | ||
- | |||
Adafruit_MCP23017 mcp; | Adafruit_MCP23017 mcp; | ||
- | |||
byte arduinoIntPinA = 14; | byte arduinoIntPinA = 14; | ||
byte arduinoIntPinB = 12; | byte arduinoIntPinB = 12; | ||
- | |||
//Les broches de l' | //Les broches de l' | ||
//Rot1 | //Rot1 | ||
byte mcpPin1A = 6; | byte mcpPin1A = 6; | ||
byte mcpPin1B = 7; | byte mcpPin1B = 7; | ||
- | |||
//Rot2 | //Rot2 | ||
byte mcpPin2A = 4; | byte mcpPin2A = 4; | ||
byte mcpPin2B = 5; | byte mcpPin2B = 5; | ||
- | |||
//Rot3 | //Rot3 | ||
byte mcpPin3A = 2; | byte mcpPin3A = 2; | ||
byte mcpPin3B = 3; | byte mcpPin3B = 3; | ||
- | |||
//Rot4 | //Rot4 | ||
byte mcpPin4A = 0; | byte mcpPin4A = 0; | ||
byte mcpPin4B = 1; | byte mcpPin4B = 1; | ||
- | |||
//Rot5 | //Rot5 | ||
byte mcpPin5A = 14; | byte mcpPin5A = 14; | ||
byte mcpPin5B = 11; | byte mcpPin5B = 11; | ||
- | |||
// There is an issue with thin pin, don't use GPB7, microchip is aware of it !!!! | // There is an issue with thin pin, don't use GPB7, microchip is aware of it !!!! | ||
// | // | ||
// | // | ||
- | |||
byte mcpPins[10] = {mcpPin1A, mcpPin1B, mcpPin2A, mcpPin2B, mcpPin3A, mcpPin3B, mcpPin4A, mcpPin4B, mcpPin5A, mcpPin5B}; | byte mcpPins[10] = {mcpPin1A, mcpPin1B, mcpPin2A, mcpPin2B, mcpPin3A, mcpPin3B, mcpPin4A, mcpPin4B, mcpPin5A, mcpPin5B}; | ||
- | |||
int encoded[5]; | int encoded[5]; | ||
volatile int lastEncoded[5]; | volatile int lastEncoded[5]; | ||
volatile long encoderValue[5]; | volatile long encoderValue[5]; | ||
- | |||
long lastencoderValue[5]; | long lastencoderValue[5]; | ||
- | |||
int MSB[5]; | int MSB[5]; | ||
int LSB[5]; | int LSB[5]; | ||
- | |||
int lastMSB[5]; | int lastMSB[5]; | ||
int lastLSB[5]; | int lastLSB[5]; | ||
- | |||
uint16_t allGPIO; | uint16_t allGPIO; | ||
- | |||
// 2 SWITCHES | // 2 SWITCHES | ||
//Rot5 | //Rot5 | ||
Ligne 216: | Ligne 203: | ||
byte sw2 = 13; | byte sw2 = 13; | ||
boolean sw1Pressed, sw2Pressed; | boolean sw1Pressed, sw2Pressed; | ||
- | |||
long lastPrint = 0; | long lastPrint = 0; | ||
int delayPrint = 50; | int delayPrint = 50; | ||
- | |||
void setup() { | void setup() { | ||
- | |||
Serial.begin(115200); | Serial.begin(115200); | ||
Serial.println(" | Serial.println(" | ||
- | |||
pinMode(arduinoIntPinA, | pinMode(arduinoIntPinA, | ||
pinMode(arduinoIntPinB, | pinMode(arduinoIntPinB, | ||
- | |||
mcp.begin(); | mcp.begin(); | ||
- | |||
mcp.pinMode(mcpPin1A, | mcp.pinMode(mcpPin1A, | ||
mcp.pinMode(mcpPin1B, | mcp.pinMode(mcpPin1B, | ||
Ligne 240: | Ligne 221: | ||
mcp.pinMode(mcpPin5A, | mcp.pinMode(mcpPin5A, | ||
mcp.pinMode(mcpPin5A, | mcp.pinMode(mcpPin5A, | ||
- | |||
//SW | //SW | ||
mcp.pinMode(sw1, | mcp.pinMode(sw1, | ||
mcp.pinMode(sw2, | mcp.pinMode(sw2, | ||
- | |||
//other pins | //other pins | ||
mcp.pinMode(8, | mcp.pinMode(8, | ||
mcp.pinMode(9, | mcp.pinMode(9, | ||
mcp.pinMode(10, | mcp.pinMode(10, | ||
- | |||
mcp.pinMode(15, | mcp.pinMode(15, | ||
- | |||
mcp.pullUp(8, | mcp.pullUp(8, | ||
mcp.pullUp(9, | mcp.pullUp(9, | ||
mcp.pullUp(10, | mcp.pullUp(10, | ||
mcp.pullUp(15, | mcp.pullUp(15, | ||
- | |||
mcp.setupInterrupts(false, | mcp.setupInterrupts(false, | ||
// Mirroring (first param) must be set to false, may cause crashes when turning multiples buttons | // Mirroring (first param) must be set to false, may cause crashes when turning multiples buttons | ||
- | |||
//SW1 | //SW1 | ||
mcp.setupInterruptPin(mcpPin1A, | mcp.setupInterruptPin(mcpPin1A, | ||
Ligne 275: | Ligne 250: | ||
mcp.setupInterruptPin(mcpPin5A, | mcp.setupInterruptPin(mcpPin5A, | ||
mcp.setupInterruptPin(mcpPin5B, | mcp.setupInterruptPin(mcpPin5B, | ||
- | |||
//BTN1/2 | //BTN1/2 | ||
mcp.setupInterruptPin(sw1, | mcp.setupInterruptPin(sw1, | ||
mcp.setupInterruptPin(sw2, | mcp.setupInterruptPin(sw2, | ||
- | |||
mcp.readGPIOAB(); | mcp.readGPIOAB(); | ||
- | |||
attachInterrupt(digitalPinToInterrupt(arduinoIntPinA), | attachInterrupt(digitalPinToInterrupt(arduinoIntPinA), | ||
attachInterrupt(digitalPinToInterrupt(arduinoIntPinB), | attachInterrupt(digitalPinToInterrupt(arduinoIntPinB), | ||
- | |||
- | |||
} | } | ||
- | |||
ICACHE_RAM_ATTR void updateEncoderA() { | ICACHE_RAM_ATTR void updateEncoderA() { | ||
| | ||
} | } | ||
- | |||
ICACHE_RAM_ATTR void updateEncoderB() { | ICACHE_RAM_ATTR void updateEncoderB() { | ||
readGPIO_MCP(); | readGPIO_MCP(); | ||
} | } | ||
- | |||
ICACHE_RAM_ATTR void readGPIO_MCP() | ICACHE_RAM_ATTR void readGPIO_MCP() | ||
{ | { | ||
- | |||
// on lit toutes les entrées d'un coup, | // on lit toutes les entrées d'un coup, | ||
// Reads all 16 pins (port A and B) into a single 16 bits variable. | // Reads all 16 pins (port A and B) into a single 16 bits variable. | ||
allGPIO = mcp.readGPIOAB(); | allGPIO = mcp.readGPIOAB(); | ||
- | |||
// On utilise bitRead pour récupérer la valeur pour chaque bouton | // On utilise bitRead pour récupérer la valeur pour chaque bouton | ||
//SW5 | //SW5 | ||
MSB[4] = bitRead(allGPIO, | MSB[4] = bitRead(allGPIO, | ||
- | LSB[4] = bitRead(allGPIO, | + | LSB[4] = bitRead(allGPIO, |
- | | + | |
//SW4 | //SW4 | ||
MSB[3] = bitRead(allGPIO, | MSB[3] = bitRead(allGPIO, | ||
LSB[3] = bitRead(allGPIO, | LSB[3] = bitRead(allGPIO, | ||
- | |||
//SW3 | //SW3 | ||
MSB[2] = bitRead(allGPIO, | MSB[2] = bitRead(allGPIO, | ||
LSB[2] = bitRead(allGPIO, | LSB[2] = bitRead(allGPIO, | ||
- | |||
//SW2 | //SW2 | ||
MSB[1] = bitRead(allGPIO, | MSB[1] = bitRead(allGPIO, | ||
LSB[1] = bitRead(allGPIO, | LSB[1] = bitRead(allGPIO, | ||
- | |||
//SW1 | //SW1 | ||
MSB[0] = bitRead(allGPIO, | MSB[0] = bitRead(allGPIO, | ||
LSB[0] = bitRead(allGPIO, | LSB[0] = bitRead(allGPIO, | ||
- | |||
//btn | //btn | ||
sw1Pressed = bitRead(allGPIO, | sw1Pressed = bitRead(allGPIO, | ||
sw2Pressed = bitRead(allGPIO, | sw2Pressed = bitRead(allGPIO, | ||
- | |||
for (int i = 0; i < 5; i++) | for (int i = 0; i < 5; i++) | ||
{ | { | ||
Ligne 337: | Ligne 296: | ||
lastEncoded[i] = encoded[i]; //store this value for next time | lastEncoded[i] = encoded[i]; //store this value for next time | ||
} | } | ||
- | |||
} | } | ||
- | |||
- | |||
void loop() { | void loop() { | ||
- | |||
if (millis() - lastPrint > delayPrint) | if (millis() - lastPrint > delayPrint) | ||
{ | { | ||
Ligne 357: | Ligne 312: | ||
lastPrint = millis(); | lastPrint = millis(); | ||
} | } | ||
- | | ||
} | } | ||
``` | ``` | ||