Hello All,
I am new to this forum so hopefully I am posting at the right place.
I am trying to control remotely control sockets from Pi-Mote using Arduino Nano over the I2C using chip MCP23017. How the Pi mote works is on this link Pi mote & Python.
Before I have managed to control the Pi mote using just the pins on the Arduino UNO using this code Arduino Pimote . It worked with no problem. But when I came to the point trying to expand it using the Arduino Nano and the chip MPC23017 it doesn't work.
Thank you for help..
#include <Wire.h>
// Codes for switching on and off the sockets
// Socket: all 1 2 3 4
// ON = ['1011', '1111', '1110', '1101', '1100']
// OFF = ['0011', '0111', '0110', '0101', '0100']
// ----------------
// Functions
// ----------------
/*
*/
void setupMPC23017()
{
char* sides[]={"A", "B"};
int chipSide[] = {0x00, 0x01}; // [IODIRA, IODIRB] register
int chipSides = 2;
for (int i=0; i < chipSides; i++)
{
Wire.beginTransmission(0x20);
Wire.write(chipSide[i]); // register
Wire.write(0x00); // set all of port as outputs
Wire.endTransmission();
outputMPC23017(sides[i], 0);
}
}
void clearOutputSettings()
{
char* side[]={"A", "B"};
int chipSides = 2;
for (int i=0; i < chipSides; i++)
{
outputMPC23017(side[i], 0);
}
}
/*
*/
void outputMPC23017(String chipSide, int output)
{
Wire.beginTransmission(0x20);
// address port A
if(chipSide == "A")
Wire.write(0x12);
// address port B
if(chipSide == "B")
Wire.write(0x13);
Wire.write(output); // value to send
Wire.endTransmission();
}
/*
*/
void manageOutput(String chipSide, int output[])
{
// sending code to activate or deactivate socket
outputMPC23017(chipSide, output[0]);
// let it settle, encoder requires this
delay(100);
// Enable the modulator
outputMPC23017(chipSide, output[1]);
// keep enabled for a period
delay(250);
// Disable the modulator
outputMPC23017(chipSide, output[0]);
}
/*
*/
void switch_on(int socket, String chipSide)
{
if(socket == 0)
{
int output[] = {11, 27};
manageOutput(chipSide, output);
}
else if(socket == 1)
{
int output[] = {15, 31};
manageOutput(chipSide, output);
}
else if(socket == 2)
{
int output[] = {14, 30};
manageOutput(chipSide, output);
}
else
{
int output[] = {0, 0};
manageOutput(chipSide, output);
}
}
/*
*/
void switch_off(int socket, String chipSide)
{
if(socket == 0)
{
int output[] = {3, 19};
manageOutput(chipSide, output);
}
else if(socket == 1)
{
int output[] = {7, 23};
manageOutput(chipSide, output);
}
else if(socket == 2)
{
int output[] = {6, 22};
manageOutput(chipSide, output);
}
else
{
int output[] = {0, 0};
manageOutput(chipSide, output);
}
}
/*
* Begin
*/
void setup() {
Serial.begin(9600); // Serial Communication
Wire.begin(); // wake up I2C bus
setupMPC23017(); // wake up MPC23017
}
void loop() {
switch_on(2, "B");
clearOutputSettings();
delay(1000);
switch_off(2, "B");
clearOutputSettings();
delay(1000);
}