Hi I made a test breadboard with an Arduino Nano + MCP23S17 IC
I read @wolfv's topic, about the I/O expander problem. And I used his code, but I have a problem.
I put buttons into the port A and B, the A ports works well, but I get 11111111 from B ports continuously.
I used this wire schematic: link
I put 10K resitors between the pins and the buttons!
Can you help me, what is the problem:
// this constant won't change:
#include <SPI.h> // We use this library, so it must be called here.
#include <Wire.h>
const int buttonPin = 7; // the pin that the pushbutton is attached to
const int buttonPin2 = 6; // the pin that the pushbutton is attached to
const uint8_t ADDR = 0x20; //MCP23S17 address, all ADDR pins are grounded
const uint8_t OPCODE_WRITE = (ADDR << 1 | 0x00); //MCP23S17 opcode write has LSB clear
const uint8_t OPCODE_READ = (ADDR << 1 | 0x01); //MCP23S17 opcode read has LSB set
const uint8_t IODIRA = 0x00; // I/O direction A
const uint8_t IODIRB = 0x01; //buttons are on port B
const uint8_t GPIOA = 0x12; // port A
const uint8_t GPIOB = 0x13;
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonPushCounter2 = 0; // counter for the number of button presses
int buttonPushCounter3 = 0; // counter for the number of button presses
int buttonPushCounter4 = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int buttonState2 = 0; // current state of the button
uint8_t buttonState3 = 0; //bit wise
uint8_t buttonState4 = 0; //bit wise
int lastButtonState = 0; // previous state of the button
int lastButtonState2 = 0; // previous state of the button
int lastButtonState3 = 0; // previous state of the button
int lastButtonState4 = 0; // previous state of the button
uint8_t IOERead(const uint8_t REGISTER_ADDR)
{
uint8_t data;
SPI.beginTransaction(SPISettings (SPI_CLOCK_DIV8, MSBFIRST, SPI_MODE0)); //gain control of SPI bus
digitalWrite(SS, LOW); //enable Slave Select
SPI.transfer(OPCODE_READ); //read command
SPI.transfer(REGISTER_ADDR); //register address to read data from
data = SPI.transfer(0); //save the data (0 is dummy data to send)
digitalWrite(SS, HIGH); //disable Slave Select
SPI.endTransaction(); //release the SPI bus
return data;
}
void setup() {
pinMode(buttonPin, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(SS, INPUT); //configure controller's Slave Select pin to output
digitalWrite(SS, LOW); //disable Slave Select
SPI.begin();
// initialize serial communication:
Serial.begin(9600);
}
void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
buttonState2 = digitalRead(buttonPin2);
buttonState3 = IOERead(GPIOB);
buttonState4 = IOERead(GPIOA);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter++;
Serial.println("BE");
Serial.print("Első VELOSTAT: ");
Serial.println(buttonPushCounter);
} else {
// if the current state is LOW then the button
// wend from on to off:
Serial.println("KI");
}
// Delay a little bit to avoid bouncing
delay(50);
}
if (buttonState2 != lastButtonState2) {
// if the state has changed, increment the counter
if (buttonState2 == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter2++;
Serial.println("BE");
Serial.print("Második VELOSTAT: ");
Serial.println(buttonPushCounter2);
} else {
// if the current state is LOW then the button
// wend from on to off:
Serial.println("KI");
}
// Delay a little bit to avoid bouncing
delay(50);
}
if (buttonState3 > lastButtonState3) //if a button was pressed
{
Serial.println("BE");
Serial.print("PIN: ");
Serial.println(buttonState3, BIN);
Serial.print("B");
buttonPushCounter3++;
Serial.println(buttonPushCounter3);
delay(200);
}
if (buttonState4 > 0) //if a button was pressed
{
Serial.println("BE");
Serial.print("PIN: ");
Serial.println(buttonState4, BIN);
Serial.print("A");
buttonPushCounter4++;
Serial.println(buttonPushCounter4);
delay(200);
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
lastButtonState2 = buttonState2;
lastButtonState3 = buttonState3;
lastButtonState4 = buttonState4;
}