NOOB - Help with Arduino code, simple pin state = pin active

I am VERY NEW to programming and using Arduino so looking for assistance.

It is a simple project (in my mind) with a simple operation table. Basically, based on the status input (either active lo [ground] or active hi [more than 0]) on pins 3 & 4 will trigger active lo on pins 11 & 12 based on the following table:

#1) pin 3 - hi / pin 4 - hi = pin 11 - lo / pin 12 - lo *this will be the most common regular one
#2) pin 3 - lo / pin 4 - lo = pin 11 - hi / pin 12 - lo
#3) pin 3 - hi / pin 4 - lo = pin 11 - hi / pin 12 - hi

I also want to see about adding a 3 second delay after the pin state has changed for # 2 and # 3. For example pin 3/pin 4 are both HI and then once pin 3 or pin 4 (or both) go LO and then the pin change stops the output of pin 11 or pin 12 (or both) will hold the output for 5 seconds and then change to the state based on the input of pins 3 & 4.

So far I have the following code written:
void setup()
{
pinMode(3, INPUT);
pinMode(4, INPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
}

void loop()
{
if (3 == HIGH)
if (4 == HIGH)
{
digitalWrite(11, LOW);
digitalWrite(12, LOW);
}

if (3 == LOW)
if (4 == LOW)
{
digitalWrite(11, HIGH);
delay(3000);
digitalWrite(12, LOW);
delay(3000);
}
else
{
digitalWrite(11, LOW);
digitalWrite(12, LOW);
}

if (3 == HIGH)
if (4 == LOW)
{
digitalWrite(11, HIGH);
delay(3000);
digitalWrite(12, HIGH);
delay(3000);
}
else
{
digitalWrite(11, LOW);
digitalWrite(12, LOW);
}
}

When I try to run it on Tinkercad I do not get any error in the code but it does not seem to produce any output on pins # 11 & 12 based on changing the input (running a wire from GRouND pin). As I said this is the first time for me doing something like this and I do need some help or guidance. Am I making sense and heading in the right direction??

Statements like if (3 == HIGH) don't do anything useful. That's like writing if (3 == 1), which is also never true.

You might want to read up on digitalRead.

Also, use code tags. And indentation. See the difference it makes?

void setup()
{
   pinMode(3, INPUT);
   pinMode(4, INPUT);
   pinMode(11, OUTPUT);
   pinMode(12, OUTPUT);
}

void loop()
{
   if (3 == HIGH)
      if (4 == HIGH)
      {
         digitalWrite(11, LOW);
         digitalWrite(12, LOW);
      }

   if (3 == LOW)
      if (4 == LOW)
      {
         digitalWrite(11, HIGH);
         delay(3000);
         digitalWrite(12, LOW);
         delay(3000);
      }
      else
      {
         digitalWrite(11, LOW);
         digitalWrite(12, LOW);
      }

   if (3 == HIGH)
      if (4 == LOW)
      {
         digitalWrite(11, HIGH);
         delay(3000);
         digitalWrite(12, HIGH);
         delay(3000);
      }
      else
      {
         digitalWrite(11, LOW);
         digitalWrite(12, LOW);
      }
}

This is a confusing description

You have two inputs
io-pin 3 io-pin 4
these two inputs determine what shall happen so a very short still easy to understand description of these two io-pins is to use two bits represented by two digits beeing "1" or "0"

So your case

can be written as

pin3 pin 4
  1   1
  
in short 
11  

same for your output pins

pin11 pin 12
  0   0
  
in short 
00  

your case #1): 11 => 00
your case #2): 00 => 10
your case #3): 10 => 11

what is with
case #4): 01 => ??

draw a timing diagram that shows what shall happen over time.
This timing diagram has four lines each with a height representing LOW and a height for representing HIGH

  • input pin 3
  • input pin 4
  • output pin 11
  • outpin pin 12

A timing diagram will define very precise what you mean.
Without a timing-diagram you would need hundreds of words to achieve the same precision

Additionally you should give an overview about your whole project.
This will help very much to understand what you want to do.
And in 80% of all cases makes it possible to find different and easier to realise solutions of the final purpose of switching two IO-pins LOW/HIGH in a certain pattern

additionally:

You are using the function delay()
This function should better have the name

stop_code_execution_freeze_microcontroller_completely_until_freezing_time_is_over()

As long as a "delay()" is active your microcontroller is unable to read in any IO-pin
which means if a rather short change of an IO-pin occurs in the delay-time your microcontroller will be unable to detect it.

This is another reason to know what the final purpose of all this is.

Take a look into this tutorial:

Arduino Programming Course

It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.

Besides the programming problems, the states that you explain can be put in the form of 'truth table'. It will help to clarify the logic. And it shows that you missed one state:

PIN3    PIN4 |  PIN11   PIN12
-----------------------------
high    high    low     low
low     low     high    low
high    low     high    high
low     high    ?       ?

First, you have to define the last state.
Then, applying some boolean logic, or just looking at the table, you can see that PIN11 depends only on PIN4 state (it's the opposite), not matter what happens with the other pin. At least until you define the missing state.

So the boolean calculation of the output pins could be reduced to this, without any IF/ELSE.:

PIN11 = !PIN4
PIN12 = !(PIN3 == PIN4)

With IF/ELSE it would work also. But would get more complex if you have to deal with more pins and states.

Then, you need to solve the programming problems. As recommended, learn more about Arduino programming. Reproduce some working examples, etc.

1 Like

Hi, @n9upc
Welcome to the forum.

This link might help with your basic table interpretation to Arduino code.

Tom... :grinning: :+1: :coffee: :australia:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.