PCF8574

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();
      }

You don't have any code to read that switch in the OPTION functions. The only place where you try to read the switch is at the beginning of the loop function. I'm not sure what your OPTION functions do or why you would want to read the switch in them. Can you explain what this program is supposed to do? How it works now and how that differs from what you expect?

I don't see how a while loop would fit into fixing it. A while loop would repeat a section of code without doing anything else until a given condition is met. How were you thinking you'd use that?

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.

But if you read the switch input in a while loop you wouldn't be blinking the leds.

What you need to do is get rid of all the calls to delay in your program. During a delay the board is blind and can't do anything else. Look at the "Blink Without Delay" example and the famous "Demonstration code for several things at the same time" for some inspiration on how to restructure your program.

O.K will do thanks for your help.

To understand better why a while loop won't help, understand that a while loop only checks its condition at the beginning of the while loop. All the code inside it will run, and then it will check the condition before it runs all the way through again. So you'd have the exact same problem there.