Need help to some code bits

I have a question about for the arduino coding, im used to code in ladder.
I have a picture of the ladder program i want in c++.

And some code, almost as i want, but i can’t figure out how to make a bit, which is not set as an in- or output.

int pin2 = 2;
int pin3 = 3;
int pin4 = 4;
int pin5 = 5;
int led8 = 8;
int led9 = 9;
int led10 = 10;

int button2 = 0;
int button3 = 0;
int button4 = 0;
int light8 = 0;
int light9 = 0;

void setup()
{
pinMode(pin2, INPUT);
pinMode(pin3, INPUT);
pinMode(pin4, INPUT);
pinMode(led8, OUTPUT);
pinMode(led9, OUTPUT);
}

void loop()
{
button2 = digitalRead(pin2);
button3 = digitalRead(pin3);
button4 = digitalRead(pin4);
light8 = digitalRead(led8);
light9 = digitalRead(led9);

if(button2 == HIGH || light8 == HIGH && button3 == LOW)
{
digitalWrite(led8, HIGH);
}
else if (button3 == HIGH)
{
digitalWrite(led8, LOW);
}

if(light8 == HIGH && button4 == HIGH)
{
digitalWrite(led9, HIGH);
}
else if(light8 == LOW)
{
digitalWrite(led9, LOW);
}
}

i can't figure out how to make a bit, which is not set as an in- or output.

Which bit of what are you having a problem with ? Why don't you just set the light variables HIGH or LOW when you turn the associated LED on or off rather than reading the state of the LED ?

button2 = digitalRead(pin2);

Just about the dumbest name for a variable I've seen today. The variable holds a state. The name should reflect that.

 if(button2 == HIGH || light8 == HIGH && button3 == LOW)

Do you mean:

 if((button2 == HIGH || light8 == HIGH)&& button3 == LOW)

or:

 if(button2 == HIGH || (light8 == HIGH && button3 == LOW))

?

Instead of using the led state, i want to use the value k1 in the picture, a bit i can set high and low without its connectet to a led or button. Is it possible??

So the light8 is not used, but instead a other bit which is internally.

So the light8 is not used, but instead a other bit which is internally.

Do you want to use a single bit of the light8 variable, which you can do, or something else ? The drawing makes no sense to me. Can you please explain in more detail what you want to do ?

In the code i uploadet i want a internal bit, instead of the light8. So when the button2 is pushed it sets ex. "K1"

if(button2 == HIGH || K1 == HIGH && button3 == LOW) { digitalWrite(K1, HIGH);

So i don't have to use a actually output, but just a bit in the program. If you know what i mean.

I know this is a crappy code, but this is my idea with it, and i know it doesn't work.

int pin2 = 2; int pin3 = 3; int pin4 = 4; int pin5 = 5; int led9 = 9; int Help1;

int button2 = 0; int button3 = 0; int button4 = 0; int light9 = 0; int K1 = 0;

void setup() { pinMode(pin2, INPUT); pinMode(pin3, INPUT); pinMode(pin4, INPUT); pinMode(led9, OUTPUT); pinMode(Help1, INTERNAL); }

void loop() { button2 = digitalRead(pin2); button3 = digitalRead(pin3); button4 = digitalRead(pin4); light9 = digitalRead(led9); K1 = digitalRead(Help1);

if(button2 == HIGH || K1 == HIGH && button3 == LOW) { digitalWrite(Help1, HIGH); } else if (button3 == HIGH) { digitalWrite(Help1, LOW); }

if(K1 == HIGH && button4 == HIGH) { digitalWrite(led9, HIGH); } else if(K1 == LOW) { digitalWrite(led9, LOW); } }

a bit i can set high and low without its connectet to a led or button.

Look at the bitSet() and bitRead() functions for a simple way to manipulate individual bits of a variable.

Thank you, i will try it out.

PaulS: button2 = digitalRead(pin2);

Just about the dumbest name for a variable I've seen today. The variable holds a state. The name should reflect that.

So helpful. I sure learned a lot.

Krupski: So helpful. I sure learned a lot.

Paul may not have the style of a diplomat but his comments are almost always accurate and sensible when you extract them from his style. Do not ignore him because you don't like the style.

Giving variables meaningful names makes code much easier to understand and to maintain. It also makes silly logical mistakes easier to spot.

...R

Still not changed:

if(button2 == HIGH || K1 == HIGH && button3 == LOW)

I see. Do you want help or are you just wasting out time?

Robin2: Paul may not have the style of a diplomat but his comments are almost always accurate and sensible when you extract them from his style. Do not ignore him because you don't like the style.

I agree: ignore PaulS at your peril. Well put Robin2: filter the noise and you have a good signal.

Giving variables meaningful names makes code much easier to understand and to maintain. It also makes silly logical mistakes easier to spot.

Don't forget people on a forum do not have an intimate knowledge of your sketch. If we spend hours on our own sketches, we know what "button1" or "ledA" are: but a passer-by trying to help (and let's face it, that what other members are) is likely to understand your sketch better and more top the point, be more inclined to help, if "ledA" has a name like "overTemperatureWarning".

Henry_Best: The way i want is, is like this

if((button2 == HIGH || K1 == HIGH)&& button3 == LOW)

And yes i want help. I'm used to work in ladder, thats why my arduino logic is not that well.

JimboZA: I know i need to put more detailed names on the variables, but it was to understanding the use of internal bits.

The way i want is, is like this

if((button2 == HIGH || K1 == HIGH)&& button3 == LOW)

But that is not how you have written it in the latest code that you posted in reply #8

As an aside, how are your inputs wired ? Do they have pullup or pulldown resistors or are they just floating in the breeze at an uncertain voltage when the buttons are not pressed.

At the start you said

i can't figure out how to make a bit, which is not set as an in- or output

I still don't understand what that means.