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
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.
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
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.
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.
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
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
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.
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.
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
}
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);
... 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.