Playing with an arduino mega and a pcf8574 8-bit I/O expander on i2c bus. What i would like to do is switch on/off individual led's connected to seperate pins on the pcf8574. Strictly speaking i've accomplished that. Problem is that i can't do it without 'touching' the others. here's the code so far;
What you can do is define a global variable in your skecth that at all times holds a copy of the pin states on your port expander. For an 8 bit port expander this will conveniently fit in one byte. Then in order to change pin states you should do so in two steps:
Change the pins in your local copy (using bit operations)
Send the local copy to the port expander
If you follow this sequence then your local copy and the acual state of pins on the port expander will always be the same.
Perhaps you want to try this yourself rather than having someone write the code for you.
From experience.. no..
If you download the byte from the 8574 and change the one bits you need and reupload it only the bits that changed will change physically....
Thanks for all tips.
Just to make it clear; i was not looking for a 'complete code generation forum'. Sorry if i gave that impression.
Anwer to Coding Badly's post.
How many LEDs are connected to the PCF8574?
there are 8 led's connected.
expanderWrite(alloff); ...which LEDs are on and which are off?
All of the 8 led's are off or are switched off.
expanderWrite(led8on); ...which LEDs are on and which are off?
Only led 8 is on. All others are switched off/switching of if they were on.
The other answers are the same as the led8on answer except for the lednumber.
To BenF
Thanks for the strategy. I'll try this one. Once again, sorry for the wrong impression i made.
Here the lednumbers connected to portnumbers
Led1 is connected to port 0 of the expander.
led2 to port 7.
led3 to port 1.
led4 to port 6.
led5 to port 2.
led6 to port 5.
led7 to port 3
led8 to port 4.
#define expander B00100000 //expander address #define time 1000
byte ledStatus = B11111111; //all off
byte led1 = 0; //led 1 connected to port 0 of expander
byte led2 = 7; //led 2 connected to port 7 of expander
byte led3 = 1;
byte led4 = 6;
byte led5 = 2;
byte led6 = 5;
byte led7 = 3;
byte led8 = 4;
void setup() {
Wire.begin();
}
void loop() {
changeLedStatusAlloff(); //switch all led's off to be sure
delay(time);
changeLedStatusOn(led1); //switch on only led 1
delay(time);
changeLedStatusOn(led2); //switch on led 2 (and don't touch other led's)
delay(time);
changeLedStatusOn(led3);
delay(time);
changeLedStatusOn(led4);
delay(time);
changeLedStatusOn(led5);
delay(time);
changeLedStatusOn(led6);
delay(time);
changeLedStatusOn(led7);
delay(time);
changeLedStatusOn(led8); //switch on led 8. now all led's are on
delay(time);
changeLedStatusOff(led1); //switch off led 1. now led 1 is off and 2 to 8 are on
delay(time);
changeLedStatusOff(led2); //switch off led 2. now led's 1 and 2 are off and 3 to 8 are on.
delay(time);
changeLedStatusOff(led3);
delay(time);
changeLedStatusOff(led4);
delay(time);
changeLedStatusOff(led5);
delay(time);
changeLedStatusOff(led6);
delay(time);
changeLedStatusOff(led7);
delay(time);
changeLedStatusOff(led8); //switch off led 8. now all led's are off.
delay(time);
changeLedStatusAllon(); //switch on all led's
delay(time);
changeLedStatusAlloff(); //switch off all led's
delay(time);
}