One thing that i cannot figure out, is how to use MCP23017 in a class. I tried to make a class, so i didn't have to do all these definitions, and so i could switch input and output on command. Unfortunately, the class doesn't work. In fact, i don't get anything over the serial port, like the coded "hello", or "goodbye". Any idea what is going on?
#include <Adafruit_MCP23017.h>
#include <String.h>
Adafruit_MCP23017 mcp1;
Adafruit_MCP23017 mcp2;
Adafruit_MCP23017 mcp3;
#include "Wire.h"
//Definiation
#define addr1 0 //addr 1=A2 low, A1 low, A0 low = "000"
#define addr2 1 //addr 2 = A2 low, A1 low, A0 high = "001"
#define addr3 2 //addr 3= A2 low , A1 high, A0 low= "010"
int lower_pin_bound = 0; //lower bound for set of digital pins being read, on GPIO of MCP23017
int upper_pin_bound = 15; //upper bound for set of digital pins being read
const byte GPIOA = 0x12; //Register address of port A
const byte GPIOB = 0x13; //Register address of port B
byte inputs=0;
//************************************************** class process logic ********************************************************************** ********************************************//
bool arry[100];
class processLogic
{
int myPin; //class object, myPin, will be used to process a particular integer pin number
bool myState; //class object, myState, will be used to store the digital reading of myPin, as a true/false, high/low
//constructor
public:
processLogic( int pin)
{
myPin = pin; //assigned passed integer, "pin", to class object "myPin"
mcp1.pinMode(myPin, INPUT); //setting the pinMode of class object, "myPin" as a input. This feature can be changed throughout the code.
//pinMode (myPin, OUTPUT);
myState = false; //iniatlizing class object, "myState", as a false
}
bool scan(int myPin) //declaring class function "scan" which will scan the pin number passed to the class, read as "myPin"
{
mcp1.begin(addr1);
mcp1.pinMode(myPin, INPUT);
myState = mcp1.digitalRead(myPin);//assigned the "myState" to hold the memory of the read pin
return myState;
}
void detect(int pin) //class function "detect" which will compared the stored value of the read pin, "myState". if it is true, it will report.
{
myPin = pin;
myState = this->scan(pin); //this calls the function "scan", in the class that is local the "detect"
if (myState == true)
{
arry[pin] = true; //Writing the the master section of the array, denoted by [0][pin]
}
else
{
arry[pin] = false; //Writing the the master section of the array, denoted by [0][pin]
}
}
void source (int pin) //Sourcing function in the class, this needs works to get working
{
myPin = pin;
pinMode (myPin, OUTPUT);
digitalWrite (myPin, HIGH);
}
};
processLogic myLogic(0); //delcaring a class member, myLogic
void setup()
{
Serial.begin(9600);
mcp2.begin(addr2);
mcp3.begin(addr3);
// Wire.begin(); // wake up I2C bus
}
void loop()
{
Serial.println("hello");
for(int i = lower_pin_bound; i < upper_pin_bound; i++) //this while looper will cycle through the lower and upper pins, scanning and reporting the state of each pin, printing across the serial communication port.
{
myLogic.detect(i); //"myLogic" is a declared object of class "processLogic", which then is calls class function "detect", passing it integer "i". detect will call scan inside of itself.
}
Serial.print("goodbye");
for (int j = 0; j < 100; j++)
{
Serial.print(j);
Serial.print(" : ");
Serial.println(arry[j]);
}
if(mcp1.digitalRead(0) == HIGH)
{
Serial.print ("1");
Serial.print(":");
Serial.println ("HIGH");
//delay(1000);
}
delay(200);
if(mcp2.digitalRead(0) == HIGH)
{
Serial.print ("2");
Serial.print(":");
Serial.println ("HIGH");
//delay(1000);
}
delay(200);
if (mcp3.digitalRead(0) == HIGH)
{
Serial.print ("3");
Serial.print(":");
Serial.println ("HIGH");
}
delay(200);
}