D
D
Dmitry Ivanov2016-03-16 01:15:00
Arduino
Dmitry Ivanov, 2016-03-16 01:15:00

Why is the remainder of division not calculated in Arduino?

We have code written in Arduino IDE:

void setup() { 
  Serial.begin(4800); 
} 

void loop() { 
  Serial.write((7 % 5) + 65);
}

Based on the rules of mathematics in the expression (7% 5) + 65, we should get 67 , but in reality the Arduino will write to port 72 .
As I did not try, but the "%" operator (remainder from division) does not work for me.
What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
D
Dmitry Ivanov, 2016-03-17
@IDma88

The question can be considered closed, because. the problem turned out to be in Proteus, which I used for simulation.

A
Alexander Volkov, 2016-03-16
@a_volkov1987

arduino.ru/Reference/Serial/Write
arduino.ru/Reference/Serial/Print
See the difference?
In the first case, you will send the result as a binary code. And the port monitor will display something completely different from what you expect.
In the second case, you will send the result as ASCII text. That is, what you send, you will see in the monitor.
Try just for comparison:
Serial.write(65);
Serial.write("65");
Serial.print(65);
Conclusion: the problem is not in mathematics, the problem is in the format of outputting the result to the port.
UPD: TS uses Proteus for verification, I use Arduino Leonardo

A
Anton Ulanov, 2016-03-16
@antonsr98

division is through / :)
read arduino.ru/Reference/Arithmetic is useful :)

V
Vadim Aliev, 2016-03-17
@Redfern89

Try this
float a = (7% 5) + 65;
Serial print(a);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question