I am new to Arduino, and I am starting small and trying to modify basic programs.
Recently I have been blinking 2 leds with the intention of turning on one of the leds with an if statement. It is working, just that in a way that I think it is not supposed to.
Here is my doubt, when I use the if statement I was expecting to get the second led on when the first led turns on and shut down the second led when the first led turns off. Something like if led 12 bigger than zero, turn led 5 on, if led 12 is equal to zero turn led 5 off. Then only logic that I have got to execute well is if led 12 is bigger than zero turn led 5 on, and if led 12 is bigger than zero turn led 5 off, and it doesn't make sense to me. I tried the code in a second board and it is the same behaviour. Could you help me find out whats going on? The full code is below.
Here is the program
int led5 = 5;
int led12 = 12;
void setup() {
// put your setup code here, to run once:
pinMode(led12, OUTPUT);
pinMode(led5, OUTPUT);
}
void loop() {
digitalWrite(led12, HIGH);
if (led12 > 0) {digitalWrite(led5, HIGH);}
delay(1000);
digitalWrite(led12, LOW);
if (led12 > 0) {digitalWrite(led5, LOW);}
delay(1000);
Please read the post at the start of any forum , entitled "How to use this Forum".
This will help with advice on how to present your code and problems.
If you remove both if statements, you will find your code will still do as you have intended.
led12 in your code is a constant, infact it is a pin name/value, and has no real use like you have it in an if statement.
Try this;
int led5 = 5;
int led12 = 12;
void setup()
{
// put your setup code here, to run once:
pinMode(led12, OUTPUT);
pinMode(led5, OUTPUT);
}
void loop()
{
digitalWrite(led12, HIGH);
digitalWrite(led5, HIGH);
delay(1000);
digitalWrite(led12, LOW);
digitalWrite(led5, LOW);
delay(1000);
}
So it doesn't drive anything - its high impedance (floating). Outputs are always driven hard to 0V or to 5V (hard means with about 40 ohms of internal resistance).
An input has an impedance somewhere up in the 1000000 megaohms region which is not the same thing at all, meaning the pin is basically a voltage sniffer.