digitalWrite LED inverted output

Hi,

I'm using a servo motor to control the position of it via switch.

Goal is :
When switch is HIGH I want one LED to open and another close.
When switch is LOW I want one LED to close and the other open.

Here is my loop :

void loop() {

  • if (digitalRead(pin3) == HIGH) {*

  • servo1.write(180);*

  • digitalWrite(LedHi, HIGH);*

  • digitalWrite(LedLow, LOW);*

  • }*

  • else*

  • servo1.write(0);*

  • digitalWrite(LedHi,LOW);*

  • digitalWrite(LedLow, HIGH);*

  • delay(15);*

  • }*

Problems:

1)The 2nd LED (LedLow) is always "open" whether the switch is HIGH or not.
It just like ignoring the 2nd digitalWrite at " if " ...

  1. The 1st LED (LedHi) seems to work fine with the switch - but it has too low "power" (and I'm using it without any resistor)

Any help?
Should I use the "while" function maybe?

Thank you in advance
Jtel

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Please post your complete code.

Thanks.. Tom... :slight_smile:

Hi,

I'm terribly sorry about my previous post... :confused:

Well, attached you will find my connection to Arduino UNO.

Here is my code :

#include<Servo.h>
int pos =0 ;
int pin3 = 3;
int LedHi = 5;
int LedLow = 6;
Servo servo1;

void setup() {
  pinMode(LedHi, OUTPUT);
  pinMode(LedLow, OUTPUT);
  pinMode(pin3, INPUT);
  servo1.attach(9);
}

void loop() {
    if (digitalRead(pin3) == HIGH) {
       servo1.write(180);
      digitalWrite(LedHi,HIGH);
      digitalWrite(LedLow, LOW);
      
      
   
    }
    else
    servo1.write(0);
    digitalWrite(LedHi,LOW);
    digitalWrite(LedLow, HIGH);
  
    
    delay(15);
  }

Here is what I'm trying to achieve :
While servo motor is moving to "180" position (button HIGH), I want the LedHi to ON, while LedLow to be off.
The opposite when I release the button - (LedHi Off - LedLow On) while servo goes back to "0" position.

Problems with the above code and connections.

  1. The 2nd LED (LedLow) is always "ON" whether the button is HIGH or not.
    It just like ignoring the 2nd digitalWrite at " if " state.

  2. The 1st LED (LedHi) seems to work fine while the button is held - but it has too low "power" (and I'm using it without any resistor)

I hope that is more clear to anyone that reading this.

Any help or comment is acceptable

Thank you in advance,
Jtel

First thing I see is that while your if statement has braces around the statements that follow it, your else does not. That's the reason for your first problem.

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