(There is no warning if you are placing the result into bool, however that is not the version of the IDE that the OP is using)./* Created by ArduinoGetStarted. A test program written in C++ and compiled with the -Wconversion flag gives a suitable warning: warning: conversion to ‘uint8_t ’ from ‘int’ may alter its value I must admit I don't get a warning when compiling with the Arduino IDE, however there is certainly the potential for truncation. Whilst we know, by reading the code and reading the documentation, that digitalRead will return a 0 or 1, the fact that the return type is int means that the compiler can potentially see that you are stuffing a 16-bit return value into a 8-bit variable which will possibly lead to data truncation. Yes, for bool, but the question was about boolean. Oh, and assigning the return of digitalRead to a bool is perfectly valid. What this boils down to is that both byte and boolean will be written as "binary" bytes, not converted into decimal.Īs Majenko says, things are different today, and boolean is now equivalent to bool which is promoted to int.ĮlectroManiac did not respond to a question about what version of the IDE s/he is using, however my point is that for some versions of the IDE my explanation is true. So you can see that the overload for print and println treats an unsigned char (effectively uint8_t) to be printed with BYTE default treatment and not DEC treatment.Īnd the writing is done thus: void Print::print(unsigned long n, int base) Syntax Serial.println(val) Serial. This command takes the same forms as Serial.print(). Virtual void write(const uint8_t *buffer, size_t size) Serial.println() Description Prints data to the serial port as human-readable ASCII text followed by a carriage return character (ASCII 13, or '\r') and a newline character (ASCII 10, or ' '). Void printNumber(unsigned long, uint8_t) Now if you look at the Print class in that release: class Print So you can clearly see that boolean and byte are the same, and therefore that using boolean would be treated as byte. WiFi - client. Looking at, for example, Arduino pre-release version 0023 I see in the file wiring.h: typedef uint8_t boolean The Arduino programming language Reference, organized into Functions, Variable and Constant, and Structure keywords. What you mean by "is a typedef for bool" is in some (later) versions of the IDE. bool gets promoted to int, not byte - Majenko If you insist on using the boolean type, then a reasonable way of printing would be: Serial.println(buttonState ? "HIGH" : "LOW") īoolean is a typedef for bool. I raised the issue almost 4 years ago, and no-one is even assigned to this particular task. I have tried to get the Arduino developers to improve the documentation in this respect, something they have declined to do. If they had said it returns true or false you might reasonably put the result into a boolean field. BTW, neither of those values are, per se, booleans. Unfortunately the documentation for digitalRead on the Arduino site does not mention the datatype, it just mentions that it returns HIGH or LOW which isn't very helpful to help you work out what data type to use. What I am guessing is happening is that the version of the IDE you are using does not have an overloaded function for Serial.println that takes a boolean argument, and the compiler is casting it to byte, in which case it would print as 0x00 (LOW) or 0x01 (HIGH) which are not printable characters. Thus, the correct data type for buttonState in your code is int. The function prototype in Arduino.h is: int digitalRead(uint8_t) First, you are not supposed to use boolean with digitalRead.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |