pin 3 Input high - pin 5 output low ~

Hey,

I'm completely new to the Arduino and programming. I wonder if someone would be able to help me out?

It's a simple project but I'm struggling.

When pin 3, HIGH - pin 5, LOW
When Pin 3, LOW -pin 5 , HIGH

Here is my rubbish code:

// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin 3 as an output.
pinMode(3, OUTPUT);
pinMode (5,OUTPUT); }

// the loop function runs over and over again forever
void loop() {

digitalRead (3); // Read state of pin 3
digitalWrite(3, HIGH ); // turn the LED on (HIGH is the voltage level)
delay(2000); // wait for a 2 seconds
if (3 ,HIGH)
digitalWrite (5,LOW);
digitalRead (3); // Read state of pin 3
if (3, LOW);
digitalWrite (5, HIGH);
digitalWrite(3, LOW); // turn the LED off by making the voltage LOW
delay(2000); // wait for a second

}

Thanks.

You are on the right track, but your first point of confusion is that you set pin 3 as an output, but then read it: one reads input pins, not outputs. Then you do a write to pin 3 to set an led.

So first you need to clarify:

  • is pin 3 in input that you want to read FROM, like a switch?
  • is pin 3 an output that you want to write TO, like an led?

If as I suspect, pin 3 is an input and pin 5 is an output, then here's the pseudo-code for that. (ie it's not real code, just an outline....

make pin3 an input
make pin5 an output


read pin3...
    if pin 3 is high, make pin 5 low
       otherwise (ie pin 3 is low), make pin 5 high.

What you're trying to do is basically this standard example but with the logic reversed.

edit: you need to read up on the if-else.

Further to that though, if pin 3 really is meant to be an output, ie it has an led not a switch, then the idea of reading it is meaningless: if it's an output you have control over it so you already know what it is, since you set it that way. So, if you just want the two leds on 3 and 5 to be opposite, then your pseudo code is just this, with no pin reading involved.

make pin 3 an output
make pin 5 an output

make pin 3 high and pin 5 low
wait a while
make pin 3 low and pin 5 high

Hi, thanks for the reply!

I appreciate what you've said but its not what I'm trying to do! :0)

What I'm trying to do is have a blinking LED and dependent on the state of that blinking LED I want the other LED to do the oposite...

LED1 Goes High, Make LED2 go LOW

LED1 goes Low, Make Led2 go HIGH

Thanks again.

Oops, sorry I didn't see you had sent that other reply!

Yes, that's exactly what I'm trying to do...you're right, I can see why there is no point in using digitalRead now!

Just for argument sake, say if I didn't have control over it, can you make LED2 rely on the state of LED1 or is there just no need?

Well the problem there would be that you can't read an output pin..... there would have to be some basis for setting one of the leds in the first place, then you could use that fact to control the other one.

That said.... let's say you literally had no knowledge of one led from an electrical standpoint but you could see with your eye that it's on or off... it could be part of another, independent system and all you can do is see the led.

Well then you could use a phototransistor to "read" the led's status as light or dark, and then use the transistor to switch the second led. But maybe that's a discussion for another day?

But what you might like to look into, is the idea of the ! operator, which means "not". You could have a Boolean variable (which means it's just on or off) to hold the state of one of your leds, maybe call it ledState for example. Then you set that variable to be 1 or 0 and write ledState to one led and !ledState (ie its opposite) to the other.

That's awesome.

The idea of the phototransistor is something I could achieve.

The boolean blows my mind... hopefully it'l be something I'll get the hang of after a while of learning and understanding code!

Thanks a lot for all your help!

Detectors like this are infact just an led and a phototransistor in one plastic housing, although usually in the IR spectrum not visible. IR is less susceptible to ambient light.

The led shines out of its "window" and the light is either reflected back or not, into the phototransistor's "window". The incoming light (or lack of) then activates (or doesn't) the base of the transistor whose output is then used by the program's logic to do other stuff.

Those are commonly used in line following robots to "see" the line which is usually black on white.

That's great! Really helpful too,

Thanks again for all your help 8)

JimboZA:
Well the problem there would be that you can't read an output pin.....

I know i am not expert here But What does exactly digitalRead() do?

It’s also possible to use digitalRead() on an OUTPUT pin

please find the link..http://www.baldengineer.com/blog/2012/07/23/arduino-toggling-outputs/

I don't can I ask here.. But

Do I have to use digitalRead() for pins 8-13 only ?
or Can I use for all Pins?

you are reading the status of the a register called “PORTB” inside the ATmega microcontroller. PORTB contains the current state of the pin for pins 8-13. So when you call digitalWrite() you are actually writing to this register

Interesting, I stand corrected thanks.

Fact remains though, that most of the time you would know the state of an output pin since you would be the one that set it in the first place. But that technique means you don't have to remember the status.

anilkunchalaece:
Do I have to use digitalRead() for pins 8-13 only ?
or Can I use for all Pins?

Now it's my turn to answer :wink:

You van use digitalRead on all pins, 0-13 as well as the analog pins, since the analog pins act as full digital pins as well.

PORTB is for pins 8-13 but as you can see here, the other pins have ports as well.

Hello. digitalRead() reads a pin assigned to input and returns true if input pin is high or false if input pin is low.

goodinventor:
Hello. digitalRead() reads a pin assigned to input

No that's not actually the case, according to the link 2 posts above: it reads the port associated with the pin regardless of pinMode. The pin's impedence is set either high or low by pinMode.

ledState = HIGH // (or LOW) from your LED blink code

digitalWrite(3, ledState);  //if 3 is HIGH
digitalWrite(5, !ledState);  //5 is LOW and vice versa

Just for Curiosity

digitalWrite(5,!digitalRead(3)); 
digitalWrite(3,!digitalRead(5));

Does It Work?

anilkunchalaece:
Just for Curiosity

digitalWrite(5,!digitalRead(3)); 

digitalWrite(3,!digitalRead(5));



Does It Work?

It'll take 2 minutes to punch that in and find out.

This code (modified Blink) reads an output pin and prints the status. As the pic shows, the status of an output pin can indeed be read with digitalRead.

/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.
 
  This example code is in the public domain.
 */

void setup() {                
  // initialize the digital pin as an output.
  // Pin 13 has an LED connected on most Arduino boards:
  pinMode(13, OUTPUT);  
  Serial.begin(9600);  
  Serial.println("Starting.....");
}

void loop() {
  digitalWrite(13, HIGH);   // set the LED on
  Serial.println(digitalRead(13));  //<<<<<<<<<<<<<<<<<<<<<<<<
  delay(1000);              // wait for a second
  digitalWrite(13, LOW);    // set the LED off
  Serial.println(digitalRead(13)); //<<<<<<<<<<<<<<<<<<<<<<<
  delay(1000);              // wait for a second
}

read an output pin.png

Ok so after reading all the information on these posts I've got some code to work... It does what I want it to do. check it out

// the setup function runs once when you press reset or power the board
void setup() {
// initialize the digital pins as an output.
// Pin 3 and 5 as output
pinMode(3, OUTPUT);
pinMode(5,OUTPUT);
Serial.begin(9600);
Serial.println("Starting.....");
}

// the loop function runs over and over again forever
void loop() {

digitalWrite(3, HIGH ); // turn the LED on (HIGH is the voltage level)
Serial.println(digitalRead(3)); //<<<<<<<<
delay(2000); // wait for a 2 seconds
digitalRead (3); // Read state of pin 3
if (3 ,true); // if pin 3 is high....
digitalWrite (5,HIGH); // ....turn 5 high
digitalWrite(3, LOW); // turn the LED off by making the voltage LOW
Serial.println(digitalRead(3)); //<<<<<<<
delay(2000); // wait for 2 seconds
digitalWrite (5, LOW);

}

I'm not sure why this compiled:

if (3 ,true);

... and I don't think it does what you expect. Is shouldn't have the ; there on the end either.

You probably want this:

if (digitalRead(3) == HIGH))
    {
       //do stuff
   }

But anyway, it's pointless checking if pin 3 is high there, since you set it high just a few lines earlier and there's nothing inbetween that could have changed it, so it can only be high at that point.

You could use just one output to toggle 2 leds like this:

Note:
You can't turn them both fully ON, you can't turn them both OFF. Toggle only.
If D3 is set as input, they both illuminate dimly.