OK, so I downloaded your code and as expected it works perfectly. The three LEDs come on one after the other. So what ever is your problem it is not the code.
what i want to make is to have full control on each output port.
example:
i want to switch on some pin. this pin must stay on if the code say so.
in the mean time i want have some other pin switched on or off if it is asking in the code.
Yep sitting on my bench now with one light going on one after the other and after the third light goes off the first comes on again.
With the code directly copied from your post with a copy and paste.
what i want to make is to have full control on each output port.
You have it?
i want to switch on some pin. this pin must stay on if the code say so.
That is what your code does.
in the mean time i want have some other pin switched on or off if it is asking in the code.
Just change the number you send to the port. All the bits with a zero in them will turn on. If you want to change one pin without the other then just toggle that bit.
Hang on I will write you a demo.
To keep a relay or LED on you just do not change that bit in the variable you send to the expander.
To change that output you change the bit corresponding to the output you want to change.
You can change bits with the bit set and bit clear instructions or as grown up do use the bit wise logical AND and OR operations.
In the example I was using the bit wise EXCLUSIVE OR operation to toggle the bits. Notice that only one LED flashes at a time and the others not flashing do not change. This surely is what you want isn't it?
int Stereovalue = analogRead(A2); // Read the Stereo input on Analog pin 2:
float Stereo = Stereovalue * (4.5 / 1023.0); // Convert the Stereo value to a voltage
if (Stereo<1) // A Stereo signal wil be present Lower then 1 volts!(0,8v) Mono = 3,6v
{
Wire.beginTransmission(PCF2_output); // Begin the transmission to PCF8574AN
Wire.write(Yellow); // (P2) Stereo led (Yellow)
Wire.endTransmission(); // End the Transmission
digitalWrite(Stereoled, HIGH); // Turn Stereo LED "ON" when Tuner receive a Stereo signal
lcd.setCursor(21,0);
lcd.print("S"); // display Stereo signal received
}
else
{
Wire.beginTransmission(PCF2_output); // Begin the transmission to PCF8574AN
Wire.write(0, Yellow); // turn off LED (P2)
Wire.endTransmission(); // End the Transmission
digitalWrite(Stereoled, LOW); // Turn Stereo LED "OFF" when there is no Stereo signal
lcd.setCursor(21,0);
lcd.print("M");
the Relay PCF2 is compare as the "digitalWrite(Stereoled, HIGH)" or "digitalWrite(Stereoled, LOW)"
This mean that in this code, when i dont have a Stereo signal the relay (LED for now) is switched off and stay off
and if there is a signal, it swiched on and stay on
for testing the code i have put a LED color Yellow in the PCF
Forget about accessing the pins directly and just go through there two functions:-
byte outputData = 0xff;
void setOutptr(byte bitToChange){
Wire.beginTransmission(PCF2_output);
outputData |= 1<< bitToChange;
Wire.write(outputData); // set the bit and leave the others alone
Wire.endTransmission();
}
void clearOutptr(byte bitToChange){
Wire.beginTransmission(PCF2_output);
outputData &= ~(1<< bitToChange);
Wire.write(outputData); // clear the bit and leave the others alone
Wire.endTransmission();
}
So when you want to turn on say relay 4, that is the relay on bit 4 you just use
setOutput(4);
when you want to turn it off then use
clearOutput(4);
As long as you use those two calls nothing else will change only the one bit that you specify.
}
int Stereovalue = analogRead(A2); // Read the Stereo input on Analog pin 2:
float Stereo = Stereovalue * (4.5 / 1023.0); // Convert the Stereo value to a voltage
if (Stereo<1) // A Stereo signal wil be present Lower then 1 volts!(0,8v) Mono = 3,6v
{
void setOutptr(byte bitToChange)
{
Wire.beginTransmission(PCF2_output);
outputData |= 1;
Wire.write(outputData); // set the bit and leave the others alone
Wire.endTransmission();
digitalWrite(Stereoled, HIGH); // Turn Stereo LED "ON" when Tuner receive a Stereo signal
lcd.setCursor(21,0);
lcd.print("S"); // display Stereo signal received
}
else
{
void clearOutptr(byte bitToChange)
{
Wire.beginTransmission(PCF2_output);
outputData &= ~(1);
Wire.write(outputData); // clear the bit and leave the others alone
Wire.endTransmission();
digitalWrite(Stereoled, LOW); // Turn Stereo LED "OFF" when there is no Stereo signal
lcd.setCursor(21,0);
lcd.print("M"); // display Mono signal received
}
}
int Stereovalue = analogRead(A2); // Read the Stereo input on Analog pin 2:
float Stereo = Stereovalue * (4.5 / 1023.0); // Convert the Stereo value to a voltage
if (Stereo<1) // A Stereo signal wil be present Lower then 1 volts!(0,8v) Mono = 3,6v
{
void setOutptr(byte bitToChange)
{
Wire.beginTransmission(PCF2_output);
outputData |= 1 << bitToChange;
Wire.write(outputData); // set the bit and leave the others alone
Wire.endTransmission();
digitalWrite(Stereoled, HIGH); // Turn Stereo LED "ON" when Tuner receive a Stereo signal
lcd.setCursor(21,0);
lcd.print("S"); // display Stereo signal received
}
else
{
void clearOutptr(byte bitToChange)
{
Wire.beginTransmission(PCF2_output);
outputData &= ~(1<<bitToChange);
Wire.write(outputData); // clear the bit and leave the others alone
Wire.endTransmission();
digitalWrite(Stereoled, LOW); // Turn Stereo LED "OFF" when there is no Stereo signal
lcd.setCursor(21,0);
lcd.print("M"); // display Mono signal received
}
The statement should be
outputData |= 1 << bitToChange;
This sets the appropriate bit to a 1 without altering any of the other bits. The << is a shift to the left, you shift a one by as many bits to the left as the number of the bit you want to change.
Similarly when you want to clear a bit you create a number with a one in the bit you want to change, then you invert it, that is swap all the zeros and ones. Then you AND it with the number, this clears the bit.
What I gave you was two functions to turn on and off individual bits in your interface.
clearOutput(4);
also where to put those
You put them in your code when ever you want to turn something on or off. Specifically when you want to turn bit 4 off. If you want to turn bit 1 off you would put clearOutput(1) ; if you want to turn bit 6 off you would use clearOutput(6) ; if you want to turn bit 8 off then you understand even less because there is no bit 8 in your interface.
The code you just posted is a nonsense, it contains a function definition in the middle of a function, that does not make sense and the compiler no doubt tells you so. The functions I gave you must be put separately at the end of your code. Then in the code you call the functions when ever you want to turn something on or off. That will be turned on or off without affecting any other things attached to the same expander. Which is what I thought you wanted.
I don't think you know enough about programming to do your project. I would advise you learning some very basic fundamentals of programming. Like what is a function.