hello, I got a code that the mcp23017 responds for blynk, but it is unstable, the led and the blynk button are on by themselves why is it?
#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <SPI.h>
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Wire.h>
#include "Adafruit_MCP23017.h"
Adafruit_MCP23017 mcp;
char auth[] = "";
char ssid[] = "";
char pass[] = "";
// Set your LED and physical button pins here
//const int ledPin = 2;
//const int btnPin1 = 3;
BlynkTimer timer;
void checkPhysicalButton();
int ledState1 = LOW;
int btnState1 = HIGH;
int ledState2 = LOW;
int btnState2 = HIGH;
// Every time we connect to the cloud...
BLYNK_CONNECTED() {
// Request the latest state from the server
Blynk.syncVirtual(V2);
Blynk.syncVirtual(V1);
// Alternatively, you could override server state using:
//Blynk.virtualWrite(V2, ledState);
}
// When App button is pushed - switch the state
BLYNK_WRITE(V1) {
ledState1 = param.asInt();
mcp.digitalWrite(0, ledState1);
}
BLYNK_WRITE(V2) {
ledState1 = param.asInt();
mcp.digitalWrite(2, ledState2);
}
void checkPhysicalButton()
{
if (mcp.digitalRead(9) == LOW) {
// btnState is used to avoid sequential toggles
if (btnState1 != LOW) {
// Toggle LED state
ledState1 = !ledState1;
mcp.digitalWrite(0, ledState1);
// Update Button Widget
Blynk.virtualWrite(V1, ledState1);
}
btnState1 = LOW;
} else {
btnState1 = HIGH;
}
}
void setup()
{
// Debug console
mcp.begin();
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
////////////saidas mcp//////////////
mcp.pinMode(0, OUTPUT);
mcp.pinMode(1, OUTPUT);
////////////entradas//////////////
mcp.pinMode(8, INPUT);
mcp.pinMode(9, INPUT);
//pinMode(ledPin, OUTPUT);
//pinMode(btnPin, INPUT_PULLUP);
mcp.digitalWrite(0, ledState1);
mcp.digitalWrite(1, ledState2);
// Setup a function to be called every 100 ms
timer.setInterval(100L, checkPhysicalButton);
}
void loop()
{
Blynk.run();
timer.run();
}