Library math neededor not? Too much simplification?

Hello,
I've been working with Arduino with beginners for years, I've been developing Blockly@rduino (Blockly@rduino) for beginners, but I'm afraid there are too much simplification in the Arduino language.

It seems that "pinMode" isn't useful anymore, but when a student makes a mistake by forgetting this line (which seems to me indispensable to understand the principle of microcontrollers) it compiles anyway.

Same for the math.h library because the functions compile even if the library is not defined.

Thank you for giving me your opinions and for deciding so that the language and the documentation are synchronized.

scanet:
It seems that "pinMode" isn't useful anymore,

Why do you think that?

The default mode is INPUT so omitting a pinMode(pin, INPUT); will not cause a problem. AFAIK it is still needed for INPUT_PULLUP (which is very frequently used) and OUTPUT

...R

No, I was suprised but if you test:

void setup() {
  //pinMode(4, OUTPUT);
}

void loop() {
  digitalWrite(4, HIGH);

}

it works. The same with that:

void setup() {
  //pinMode(4, INPUT);
}

void loop() {
  digitalRead(4);
}

So the pb s to decide if this setup line is useful anymore.

What Arduino board are you using?

On an Uno, Mega etc the line

digitalWrite(4, HIGH);

will change the default INPUT mode to INPUT_PULLUP. This has always been a standard feature of the microprocessor.

That is very different from using OUTPUT mode.

…R

The board does not matter, whatever you chosse the test is made just by compiling. This is what my students do, and there's noo error in console.

scanet:
The board does not matter, whatever you chosse the test is made just by compiling. This is what my students do, and there's noo error in console.

I don't mean to offend, but that sounds like very poor teaching to me. The compiler will only identify syntax errors in the code. It makes no attempt to check that the code is sensible.

The only way to be sure that a program is correct is to run the program on real hardware and check that it is doing what it is supposed to do.

...R

Yeah, that's what I prefer too, but it exists, so they do... And it's faster for big system, when you just want to track grammar langage error.

Anyway, it does not answer my questioning: why coding "digitalWrite(4, HIGH);" should change the default INPUT mode?
And why 'asin' function works even when you do not add math.h library?
Thanks.

Consider:

pinMode (pinX, INPUT);
sets pin as an input. Input is also the default after a Reset per the datasheet.

If that is later followed by
digitalWrite (pinX, HIGH);
that enables the weak internal pullup resistor.
The output may appear high when measured with a meter, but the resistor will limit output current to
~ 5V/40K ohm = 0.125mA
level = digitalRead(pinX);
will then return HIGH (or 1).

If that is later followed by
digitalWrite (pinX, LOW);
the internal pullup is disabled.
level = digitalRead(pinX);
may then return HIGH or LOW, depending on what electric field influence is in the area (like a hand), otherwise continuously will return a random mix of the two. When the input goes below 01.5V (with VCC = 5V; 0.3 x VCC), LOW will result; above 3V (0.6 x VCC), HIGH will result. In between, it is anybody’s guess.

scanet:
Yeah, that's what I prefer too, but it exists, so they do... And it's faster for big system, when you just want to track grammar langage error.

The situation you describe with the code in Reply #2 is not a grammar language error - it is perfectly legitimate code. The compiler has no way of knowing what you want your program to do.

If your students do not understand that a successful compile does not guarantee a working program then they are missing a very important lesson.

Anyway, it does not answer my questioning: why coding "digitalWrite(4, HIGH);" should change the default INPUT mode?

That is explained in (for example) the Atmega 328 datasheet - it is the standard behaviour for the microprocessor. The Arduino pinMode(pin, INPUT_PULLUP); actually does a digitalWrite(pin, HIGH); behind the scenes

I have not used the math.h library myself so I am not commenting on it.

...R

Hello,
Thank you for all these explanations, I understand well, but I have the impression that I am not clear in my requests.
You explain to me how it works, I understand better, but I think at: "why make does it work this way?"

In my mind, the Arduino environment is a simplified microcontroller envrionnment for DIY and beginners, so choices have been made about the language used and what happens in the background. My problem is that in some cases the compiler lets it pass that there is a digitalWrite or digitalRead when there is no pinMode declared, that's what bothers me.
The INPUT default mode seems to generate more confusion than just having to declare a pinMode whatever you need, for beginners I don't like 'default' mode idea.

The problem is the same for the Maths functions, I had understood that the Arduino Reference is the universal basis for the boards (as for example for 'abs(x)' abs() - Arduino Reference), and that for the rest it is necessary to use libraries. But I can use the 'asin' function because 'math.h' is included in 'arduino.h', so it should be part of Arduino Reference, shouldn't it? Am I wrong?

Thank you very much.

scanet:
Hello,
Thank you for all these explanations, I understand well, but I have the impression that I am not clear in my requests.
You explain to me how it works, I understand better, but I think at: "why make does it work this way?"

The INPUT default mode seems to generate more confusion than just having to declare a pinMode whatever you need, for beginners I don't like 'default' mode idea.

That is a question and a comment that you need to address to the designers of the Atmega chips - but it's a bit late in the day for that, the chips were designed a long time ago. All the Arduino system is doing is correctly implementing the requirements of the microchip designers.

The reason the pins default to INPUT is because that is the safest state for the microprocessor.

My problem is that in some cases the compiler lets it pass that there is a digitalWrite or digitalRead when there is no pinMode declared, that's what bothers me.

I suspect you will find that it always lets digitalWrite() and digitalRead() pass even though there is no pinMode statement.

You seem to be under a misapprehension about the role of the compiler. Its only purpose is to convert valid C++ code into valid machine code. It is not designed to protect the programmer from silly mistakes that nevertheless are expressed in valid C++ code. For example myVal = digitalRead(3); would be perfectly valid code even though nothing is connected to pin3 and the programmer should have written myVal = digitalRead(5);. And keep in mind that the GCC compiler is not specific to Arduinos.

...R