подключаю кнопку через пин11 ардуины - всё работает подключаю через MCP - нихрена неработает. что не так? микруха видится Код (C++): // Reads a button attached to a MCP23XXX pin. // ok to include only the one needed // both included here to make things simple for example //#include <Adafruit_MCP23X08.h> #include <Adafruit_MCP23X17.h> #define BUTTON_PIN 8 // MCP23XXX pin button is attached to #define BUTTON_PIN2 11 // uncomment appropriate line //Adafruit_MCP23X08 mcp; Adafruit_MCP23X17 mcp; void setup() { Serial.begin(9600); //while (!Serial); Serial.println("MCP23xxx Button Test!"); // uncomment appropriate mcp.begin if (!mcp.begin_I2C(0x22)) { Serial.println("Error."); while (1); } // configure pin for input with pull up mcp.pinMode(BUTTON_PIN, INPUT_PULLUP); //mcp.digitalWrite(BUTTON_PIN, LOW); pinMode(BUTTON_PIN2, INPUT_PULLUP); Serial.println("Looping..."); } void loop() { // LOW = pressed, HIGH = not pressed if (!mcp.digitalRead(BUTTON_PIN)) { Serial.println("Button Pressed!"); delay(250); } /*if (!digitalRead(BUTTON_PIN2)) { Serial.println("Button Pressed!"); delay(250); }*/ }
загрузил другой скетч, поигрался... загрузил опять этот - заработало. Потом - зависло.... И после этого опять не работает Хрень какая то
... теперь постоянно Код (C++): 02:29:03.625 -> Button Pressed! 02:29:03.902 -> Button Pressed! 02:29:04.132 -> Button Pressed! 02:29:04.409 -> Button Pressed! 02:29:04.640 -> Button Pressed! 02:29:04.870 -> Button Pressed!
если раскомментировать оба блока Код (C++): /*if (!mcp.digitalRead(BUTTON_PIN)) { Serial.println("Button1 Pressed!"); digitalWrite(led, HIGH); delay(250); } else digitalWrite(led, LOW);*/ if (!digitalRead(BUTTON_PIN2)) { Serial.println("Button2 Pressed!"); digitalWrite(led, HIGH); delay(250); } else digitalWrite(led, LOW); то ничего не работает, а если закомментировать верхний блок, то кнопка ардуины начинает работать нормально. библиотека MCP перехватывает криво функционал?
сделал так: Код (C++): #include <Wire.h> #include "Adafruit_MCP23017.h" Adafruit_MCP23017 mcp; void setup() { Wire.begin(); Serial.begin(9600); Serial.println(""); Serial.println("Scanning..."); byte error, address, address2; byte addr = 0; int nDevices = 0; for(address = 8; address < 127; address++ ){ Wire.beginTransmission(address); error = Wire.endTransmission(); if (error == 0){ Serial.print("I2C device found at address 0x"); if (address<16) { Serial.print("0"); } Serial.print(address,HEX); Serial.println(" !"); addr = address; nDevices++; } else if (error==4) { Serial.print("Unknow error at address 0x"); if (address<16) Serial.print("0"); Serial.println(address,HEX); } } if (nDevices == 0) Serial.println("No I2C devices found\n"); else Serial.println("done\n"); mcp.begin(0x22); // use default address 0 mcp.pinMode(8, INPUT); mcp.pullUp(8, HIGH); // turn on a 100K pullup internally pinMode(13, OUTPUT); // use the p13 LED as debugging } void loop() { // The LED will 'echo' the button digitalWrite(13, mcp.digitalRead(8)); if (!mcp.digitalRead(8)) { Serial.println("pressed"); delay(250); } }
реакция такая: 21:57:39.270 -> 21:57:39.316 -> Scanning... 21:57:39.316 -> I2C device found at address 0x22 ! 21:57:39.362 -> I2C device found at address 0x3C ! 21:57:39.362 -> I2C device found at address 0x3D ! 21:57:39.408 -> done 21:57:39.408 ->
немного поменял. обычная кнопка отрабатывает, а на кнопке MCP нет вольтов. Код (C++): pinMode(13, OUTPUT); // use the p13 LED as debugging pinMode(11, INPUT_PULLUP); } void loop() { // The LED will 'echo' the button digitalWrite(13, mcp.digitalRead(8)); if (!mcp.digitalRead(8)) { Serial.println("pressed"); delay(250); } if (!digitalRead(11)) { Serial.println("pressed2"); delay(250); } }
Потому что нужно делать подтяжку INPUT_PULLUP. Возможно, MCP некорректно работает с подтяжкой. Попробуйте внешний резистор 4.7-15 кОм.
так, вроде, так и делал Код (C++): mcp.begin(0x22); // use default address 0 mcp.pinMode(8, INPUT); mcp.pullUp(8, LOW); // turn on a 100K pullup internally