Maker Pro
Maker Pro

Basics of IF and Else statements (Arduino)

Stoneww

Apr 18, 2017
42
Joined
Apr 18, 2017
Messages
42
upload_2017-10-3_21-10-59.png

------------------------------------------------------------------------------------------------------------------------------------------------------------------------

void setup() {
pinMode(10, OUTPUT);
pinMode(2, OUTPUT);
pinMode(12, INPUT);
pinMode(A0, INPUT);
}

void loop() {
analogWrite(10, analogRead(A0)/4);
if (12==HIGH)
{
digitalWrite(2, HIGH);
}
else
{
digitalWrite(2, LOW);
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
So this is my code. I want 2 to be the same logic as pin 12. So if pin 12 is logic 1 then pin 2 should be logic 1 and if pin 12 is logic 0 then pin 2 should be logic 0.

This doesn't work... I'd appreciate if someone could tell me where I'm going wrong with the code, I can't seem to find any similar examples. Thank you
 

Attachments

  • upload_2017-10-3_21-10-42.png
    upload_2017-10-3_21-10-42.png
    390 KB · Views: 117

Harald Kapp

Moderator
Moderator
Nov 17, 2011
13,700
Joined
Nov 17, 2011
Messages
13,700
if (12==HIGH)
This statement doesn't look for the state of pin 12, as you expect. Rather it compares the value 12 with the logical value HIGH.
You need to read from the pin like this:
Code:
if (DigitalRead(12) == HIGH) then
 

Stoneww

Apr 18, 2017
42
Joined
Apr 18, 2017
Messages
42
This statement doesn't look for the state of pin 12, as you expect. Rather it compares the value 12 with the logical value HIGH.
You need to read from the pin like this:
Code:
if (DigitalRead(12) == HIGH) then


Thank you so much :):)
 
Top