Hi
I have a project in mind that will require more pins than the 328 can provide.Having done a bit of research i discovered the PCF8574 module could be the answer,however the code aspect once again is my problem.I can address all the pins P0-P7 no probs as outputs,my question is in my example code below how would i change "sensor pin 8" arduino pin to be an input pin on the PCF.I know my code is not great but it works and this is my first venture into using I2C so any advice will be very much appreciated.
Thank You
#include "Wire.h"
#define PCF 0x20
int sensor = 8;
int sensorState = 0;
void setup()
{
Wire.begin();
pinMode (sensor, INPUT);
}
void OPTION()
{
Wire.beginTransmission(PCF);
//Wire.write(0);
Wire.write(B11110111);
Wire.endTransmission();
delay(1000);
Wire.beginTransmission(PCF);
Wire.write(B11111111);
Wire.endTransmission();
delay(1000);
}
void OPTION1()
{
Wire.beginTransmission(PCF);
//Wire.write(0);
Wire.write(B11110111);
Wire.endTransmission();
delay(200);
Wire.beginTransmission(PCF);
Wire.write(B11111111);
Wire.endTransmission();
delay(50);
Wire.beginTransmission(PCF);
//Wire.write(0);
Wire.write(B11110111);
Wire.endTransmission();
delay(200);
Wire.beginTransmission(PCF);
Wire.write(B11111111);
Wire.endTransmission();
delay(50);
}
void loop()
{
sensorState = digitalRead(sensor);
if (sensorState == LOW)
OPTION();
else
OPTION1();
}
Did you try a PCF8574_Byte = wire.read() ?
Here is the reference page ...
WireRead
After your Read the BYTE then
check the appropriate BIT in that byte
which corresponds to the "SENSOR" Input.
You can check my library - Arduino/libraries/PCF8574 at master · RobTillaart/Arduino · GitHub
either to use as is, or to see how it could be done.
Thank You both for your reply's.I am out of town at the moment and will try both options when i return.
mrsummitville, Thank you for your advise i have tried your suggestion and have had some success.With my code posted below the Switch input is recognised and the Switchcounter increments as expected.However the Switch input is only recognised at the end of each "OPTION();" function.I have tried using the While statement to correct this but with no success,any advise would be appreciated.
Thank You
#include "Wire.h"
byte PCF8574 = 0x20;
byte Switch;
int Switchcounter = 0;
void setup(){
Wire.begin();
}
void OPTION(){
Wire.beginTransmission(PCF8574);
Wire.write(B11111111);
Wire.endTransmission();
}
void OPTION_1(){
Wire.beginTransmission(PCF8574);
Wire.write(B01111101);
Wire.endTransmission();
delay(200);
Wire.beginTransmission(PCF8574);
Wire.write(B10111011);
Wire.endTransmission();
delay(200);
Wire.beginTransmission(PCF8574);
Wire.write(B11010111);
Wire.endTransmission();
delay(200);
Wire.beginTransmission(PCF8574);
Wire.write(B11101111);
Wire.endTransmission();
delay(200);
}
void OPTION_2(){
Wire.beginTransmission(PCF8574);
Wire.write(B11101111);
Wire.endTransmission();
delay(200);
Wire.beginTransmission(PCF8574);
Wire.write(B11010111);
Wire.endTransmission();
delay(200);
Wire.beginTransmission(PCF8574);
Wire.write(B10111011);
Wire.endTransmission();
delay(200);
Wire.beginTransmission(PCF8574);
Wire.write(B01111101);
Wire.endTransmission();
delay(200);
}
void OPTION_3(){
Wire.beginTransmission(PCF8574);
Wire.write(B11111101);
Wire.endTransmission();
delay(250);
Wire.beginTransmission(PCF8574);
Wire.write(B11111011);
Wire.endTransmission();
delay(250);
Wire.beginTransmission(PCF8574);
Wire.write(B11110111);
Wire.endTransmission();
delay(250);
Wire.beginTransmission(PCF8574);
Wire.write(B11101111);
Wire.endTransmission();
delay(250);
Wire.beginTransmission(PCF8574);
Wire.write(B11011111);
Wire.endTransmission();
delay(250);
Wire.beginTransmission(PCF8574);
Wire.write(B10111111);
Wire.endTransmission();
delay(250);
Wire.beginTransmission(PCF8574);
Wire.write(B01111111);
Wire.endTransmission();
delay(250);
}
void loop() {
Wire.requestFrom(0x20, 1);
Switch = Wire.read();
if (bitRead(Switch, 0) == LOW){
Switchcounter ++;
delay(200);
}
if (Switchcounter == 4)
Switchcounter = 0;
if (Switchcounter == 0 )
OPTION();
else if (Switchcounter == 1 )
OPTION_1();
else if (Switchcounter == 2 )
OPTION_2();
else if (Switchcounter == 3 )
OPTION_3();
}
Thank for reply,the code is just a simple test sketch that flashes some led's in different sequences for each function depending on the value of the Switchcounter.My reasoning for the While statement was exactly as you have stated as i would have to read the pin inside the different options to exit the while loop.The code works exactly as intended but the Switch input is only recognised if i press it,let's say when the last led is lit for a particular sequence.Hope that makes sense to you.
O.K will do thanks for your help.