Arduino code check, please help me

I have a program, which should actually be correct, but I'm sure there are some mistakes in there, cause otherwise it would work. I will write programm in here.

int pinOut1 = 4; //LED1
int pinOut2 = 5; //LED2
int pinOut3 = 6; //LED3
int pinOut4 = 9; //This is an output which leads to input pinIn2 so that code works only if input2 is active (see more in code below)

int pinIn1 = 7; //PullUP Input for the button, which (must) activate random number down below
int pinIn2 = 8; //Input that gets Voltage signal form Output pinOut4

int ran_num; //A random number from 20-22 (20,23)

void setup() 
{ 
  Serial.begin(9600);
  pinMode(pinOut1, OUTPUT);
  pinMode(pinOut2, OUTPUT);
  pinMode(pinOut3, OUTPUT);
  pinMode(pinOut4, OUTPUT);

  pinMode(pinIn1, INPUT);
  pinMode(pinIn2, INPUT);
}

void loop() 
{
                 
    if(digitalRead(pinIn1 = true)) {(digitalWrite(pinOut4, true));}  //Whenever button (input1) is pressed -> the outpu4 (must) activate and leads Voltage signal to input2 (somehow output4 always has 5V, no matter button is pressed or no)
     else if (digitalRead(pinIn1 = false)&&digitalRead(pinIn2 = true)) {ran_num = random(20,23); Serial.println(ran_num); delay(50); (digitalWrite)(pinOut4, false);}
      //When button (input1) is no longer pressed & input2 is still getting Voltage signal -> Serial.print random number 20-22 (20,23) and deactivate outpu4
    
 
    if(ran_num == 20) {
     digitalWrite(pinOut1, true);
     digitalWrite(pinOut2, false);
     digitalWrite(pinOut3, false);
     }
     
     
    
    if(ran_num == 21) {
     digitalWrite(pinOut1, false); 
     digitalWrite(pinOut2, true);
     digitalWrite(pinOut3, false);
     }
     
    
    
    if(ran_num == 22) {
     digitalWrite(pinOut1, false); 
     digitalWrite(pinOut2, false);
     digitalWrite(pinOut3, true);
     }
     

  
}

Welcome to the forum.

It would help if you said what the program is supposed to do that it does not, or what it does not do that it should.

As in… not gonna try to guess what you up to and what constitutes screwing that up.

a7

sorry im new in this site, gotta learn abit how to provide my thoughts correctly.

The programm:

whenever the Button is pressed, code must generate a random number form 20 to 22 and serial print it and when the number is for example 20 than 1 of 3 leds must be true while others must be false. and the random number must be generated every time i press button. Thats the whole idea of program. if you want more info about it i wrote some more information how exactly it must work in program itselfe.

but whenever i press button it doesnt do anything, and if i delete the buttons form program, they blink randomly forever.

Thank you.

I cant scroll horizontally that long to read your code im not on 80” screen.

Thank you

Haha, sry, I noticed that later.

I may not be the only one who routinely ignores all comments in most code posted here, especially if I get the idea that the writer is new-ish.

Comments lie, mislead or confuse more than they help, so I am pretty sure that I did not even see them, my eyes ahead of my brain filtering them out.

THX for the prose additions.

How are your switches wired? Have you tried any simo,e program to simply see if your switches can be digitally read digitaRead()?

Perhaps you need to draw schematic - more likely, INPUT_PULLUP might help.

There are simple examples in the IDE, start small and make sure you can read a switch and turn on an off an LED with code.'

a7

Review the difference between assignment and test for equality.

'=' is not the same as '==''…

Review the syntax of the if statement. And where those parenthesis might need to be more exactly placed in you test.

a7

Thank you for answer, i will try to switch all = with == and see if it does work.

1 more thing, i have tested the voltage with multimeter and it shows 5V for the input. I think i have to check it somehow differently if my pins actually read them or not.

Thank you.

BTW, what is a simo,e programm and where do i get it, or should i make it myselfe with serial screen?

The IDE has examples, I do not recall and can't check just now that they start out at the kind of extreme simplicity I have in mind.

So take a look at those.

I just googled

turn on off led with switch Arduino

and look at this:

https://create.arduino.cc/projecthub/hrsajjad844/control-an-led-with-switch-using-arduino-9872d9

Google is truly your friend. Often adding "Arduino" will focus results and turn up stuff that'll keep you busy for awhile.

HTH

a7

Plan

a small schematics plan for my project.

The code addresses two inputs, the schematic only has one input.

You have explained that you have connected pinIn1 to output4 (in order to make the code work). Why did you do that? Both input and output values are known to the processor.

Nice.

While I am right here, I will say that a more common switch arrangement is to wire the switch bewteen the input pin and ground, and place the resistor between the input pin and 5 volts.

This is called pulled-up, and means your switch will read LOW when pressed, and HIGH when not pressed.

For reasons.

Your switch will work and be HIGH when pressed.

a7

the 8 is the 2nd input just like in code

9 is directly connected with 8 with red color

As asked in reply #12, what is the reason for that bizarre arrangement of feeding an output back into an input?

If the schematic requires any words to explain it, it is flawed. Please correct any mistakes or omissions and post it again.

The least you can do, is explain it in a reply. Your last one was too terse to really understand.

that is something i usualy do, because i dont have like in SPS any Noticer function so i do it so that only if the pin is active... (meanwhile in SPS i write only if the Noticer is active...)

i tried also writing

bool Merker1; //Translated from German Merker is the Noticer

Thank you for that small tutorial, it actually made my stuff work at least for now,

Vrezh.

If you are using a pin output to store a boolean value, it is an extreme "kludge".

SPS is a completely unknown acronym to me, what is it? Perhaps "noticer" is just a bad translation? It's also impossible to understand.

SPS is a big industrial factory programming device with a lot of pins.
siemens SPS, I am master programmer of SPS, but Arduino is harder, so it is difficult for me to program it. Please don't scream at me for not knowing everything in it, otherwise I wouldn't ask here for some help.

Up to now I hadn't read your code too closely or worried about what you are up to.

It seems the program you present is some testing for a larger idea.

As @anon57585045 says, it makes no sense to use an output to control an input - all that can stay inside, so to speak.

Which is why that are called inputs and outputs. Iam sure the same thing obtains in SAS.

Perhaps it makes sense in some way with exactly what you are aiming for, but I think you would find the same trick odd if it was done on your more familiar territory.

a7