Help for a novis

Hi,

I have just started to learn to code the arduino for a youth project i am helping with and I need some help.

When I try to write an if statement i cant get it to work and the if command doesn’t turn orange like all the other commands. I have enclosed a screenshot for illustration. Any help would be great.

Thanks

You have the syntax for digitalRead() wrong or at least have the closing brackets in the wrong place.

You have

if (digitalRead(led1 == HIGH))

It should be

if (digitalRead(led1) == HIGH)

When you next post code please post it into a message and use code tags (</>) top/left above the editor to make it look like my examples.

You are reading from a pin that you have set as an output. This is probably not what you want.

Jobi-Wan:
You are reading from a pin that you have set as an output. This is probably not what you want.

No, it's a standard trick to avoid using an output status variable.

aarg:
it's a standard trick

Oooh.. nice, thanks. Is this documented in the reference section?

Jobi-Wan:
Oooh.. nice, thanks. Is this documented in the reference section?

No. It is not. But if you look at the datasheet you'll see why it works.

Hi
Many thanks for the help, have t working now .
Thanks again.

also theres a caveat with digitalRead/Write you should know about. Each function while easy to use, take around 40 clocks to complete, so if your writing a fast, tight loop they will fail you and you may not know why your simple fast loop won't work like you think.

DigitalRead() for instance while it's going through its 40 lines or so, checks to see if the PWM is active on a pin you may be trying to read (like activate a tight sampling loop while the PWM pin is high), if digitalRead detects PWM is active on that pin, it will disable PWM, then perform the read which will always be LOW..

I wish they would have at least put notes in the language REFERENCE about these quirks when trying to use digitalRead/Write. Fortunately, direct port manipulation is available and is much faster, but not noob proof.

Also, a side note, Divide "/" is emulated, no MCU carries hardware divide, thus using divides in your code even on integer math is very slow when compared to a shift left. There are also work-arounds for this, a few examples of how to divide by 5 and 10 using addition and bit shifting.

I wish Arduino would make the Arduino language more robust by including caveats like these at the bottom of the individual language elements and how about an average clock count for their functions? This will assist the advanced noobs with planning out their programs instead of DAYS of googling and reading through forum posts to find out these caveats!!

I wish they would have at least put notes in the language REFERENCE about these quirks when trying to use digitalRead/Write.

I think that misses the point that the Arduino environment was designed for ease of use rather than efficiency or speed, hence Arduino specific functions rather than the need to use direct port manipulation, for instance.

For those that need/want to know the information is all out there, or here, of course.

Is there such a thing as an "advanced noob" or is that an oxymoron ?

Not meaning to hijack, but what's the BEST way to read an output pin:

digitalRead(3); // or
bitRead(PORTD,3); // or
bitRead(PIND,3);

?

Define what you mean by BEST

Fastest ?
Most reliable ?
Most convenient ?
Easiest to understand ?
What ?

rinkrides:
also theres a caveat with digitalRead/Write you should know about. Each function while easy to use, take around 40 clocks to complete, so if your writing a fast, tight loop they will fail you and you may not know why your simple fast loop won’t work like you think.

See: https://code.google.com/p/digitalwritefast/downloads/list

This will assist the advanced noobs

No such thing. :slight_smile:

outsider:
Not meaning to hijack, but what's the BEST way to read an output pin:

digitalRead(3); // or

bitRead(PORTD,3); // or
bitRead(PIND,3);




?
if(bitRead(DDRD,3)) {
   // Then the last two are pretty much equivalent
   //  They're just getting the same information from different registers
}