Arduino have unstable behavior

Hi

I think following function with Arduino have unstable behavior so please advice to have a solution

digitalRead(0);

digitalWrite(1,HIGH);

pinMode(2,OUTPUT):

Thanks in advance

Hi,

That's all the code you have?? Please show your whole sketch, using

CODE  FORMAT like this

To start with:

digitalWrite(1,HIGH);

Is a problem. 0 and 1 are the USB communications channel!

You think, we like to make sure :slight_smile:

Please post a good description of the "unstable behaviour".
Please post your complete code (or a representative example).

My first guess is, since the code fragment is messing with pins 0 and 1, it’s doing something weird to the USB serial connection.

Solution: don't do that, it messes with USB serial.

Pre-solution to many future problems: learn the hardware before evaluating it.

You'd be amazed at all the info right on the Arduino site through that Getting Started link. Like the Foundations Page full of links to knowledge guarded from many new members only by lack of patience, lots of words and even some numbers.

GoForSmoke:
Solution: don't do that, it messes with USB serial.

Problem: we don't know if that is the problem :slight_smile:

GoForSmoke:
guarded from many new members only by lack of patience, lots of words and even some numbers.

PA3040 is hardly a new member; registered in 2013. Not very active, I admit, with 8 questions in the last year.

Hi all

Above code only for example and the pin 0 and 1 not for what I used but above function can be nature of unexpected behavior

Hoped will explain

PA3040:
Above code only for example and the pin 0 and 1 not for what I used but above function can be nature of unexpected behavior

No

PA3040:
Hoped will explain

If your tried to explain your question, I think you unfortunately failed. If you want us to explain, you will have to give a proper description of your observations and post a working example that resulted in thos observations.

Please, a sketch that replicates this error or is this a guessing game?

Also, describe what you mean by "unstable" behavior.

The behavior of the code you described, as a snippet, is impossible to predict without knowing what board and what the rest of the sketch does, and whether there's anything external connected to those pins.

This is why you never post snippets - always a complete sketch, and always post which board you are using, and what, if anything, is connected to it. Otherwise, all you'll get is requests for that additional information, which is necessary to answer any question you might have asked.

PA3040:
Hi all

Above code only for example and the pin 0 and 1 not for what I used but above function can be nature of unexpected behavior

Hoped will explain

Don't feed the troll.

Hi All
Sorry for delay replied

You can see the source code for the Arduino digitalRead, digitalWrite, and pinMode functions in wiring_digital.c in the official Arduino AVR core. Each of those functions contains lines like the following:

#define digitalPinToPort(P) ( pgm_read_byte( digital_pin_to_port_PGM + (P) ) )
#define digitalPinToBitMask(P) ( pgm_read_byte( digital_pin_to_bit_mask_PGM + (P) ) )
#define digitalPinToTimer(P) ( pgm_read_byte( digital_pin_to_timer_PGM + (P) ) )

The digitalPinTo* macros, which are defined in Arduino.h, use the pin number as the array index without doing any bounds checking

Please advice

If P is an enumerated value or const....

it is representing pin number

Hear i think it is acting index number of array
You can see it is defined in pins_arduino.h or the pin map for the specific board being used.
Link
[pins_arduino.h](http://is defined in pins_arduino.h or the pin map for the specific board being used.)

If the programmer passes an invalid pin number for the processor, he/she will get garbage back.

If this relates to your original question regarding the use of pins 0 and 1, it's not the cause of your problem.

Yes
But

uint8_t timer = digitalPinToTimer(pin);
uint8_t bit = digitalPinToBitMask(pin);
uint8_t port = digitalPinToPort(pin);

The digitalPinTo* macros, which are defined in Arduino.h 1, use the pin number as the array index without doing any bounds checking:

PA3040:
The digitalPinTo* macros, which are defined in Arduino.h 1, use the pin number as the array index without doing any bounds checking:

As said, garbage in is garbage out.

I did not see a reply to the question if this relates to your original question. Or I did not understand your reply.

YES It is correct
garbage in is garbage out. lovely answer and well understood

But in this case it reads unrelated flash memory space in the flash which can case operate unpredictable pins that did not specify .

If you want bounds checked then write a function that checks the bounds but with a little experience you may see code where pin numbers are held in const arrays and named in enum statements that make coding pins easier and safer. When you do that, you don't get nonexisting pin numbers being used.

Approach your code so that it stay within bounds by how it works. Filter input as necessary but make it time spent once only.

Another disadvantage i when through PWM should off every time we called digitalWrite and pinMode