I wrote the code to study about bit operation as following:
int count;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
count = 0;
}
void print_number(byte m)
{
Serial.print("0x");
Serial.print(m, HEX);
Serial.print(", 0b");
for(int k=7;k >=0; k--)
{
if( m & (0x01 << k))
{
Serial.print('1');
}
else
{
Serial.print('0');
}
}
Serial.print("\n");
}
void loop() {
// put your main code here, to run repeatedly:
if (count == 0)
{
byte n = 0xAA, m;
Serial.print("\t\t: ");
print_number(n);
Serial.print("Bit AND\t: ");
m = n & 0xF0;
print_number(m);
count++;
}
}
There was no problem and succeed to upload it.
But this code was not worked in Arduino IDE 2.3.2 and Uno R4 WIFI.
And this is different issue related to write down something once.
I wrote the code as the following:
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
byte n = 0xAA, m;
Serial.print("\t\t: ");
print_number(n);
Serial.print("Bit AND\t: ");
m = n & 0xF0;
print_number(m);
}
void print_number(byte m)
{
Serial.print("0x");
Serial.print(m, HEX);
Serial.print(", 0b");
for(int k=7;k >=0; k--)
{
if( m & (0x01 << k))
{
Serial.print('1');
}
else
{
Serial.print('0');
}
}
Serial.print("\n");
}
And this was not worked again.
(It means the result was not displayed at Serial Monitor of Arduino IDE 2.3.2)
I'm trying to find the solution, but i can't find a clue to solve it.
could you share your experience to face or to solve it?
Please explain what "not worked" means.
What did you expect the program to do, and what did it actually do?
xfpd
June 7, 2024, 3:08am
3
Your second code does not have the required function void loop()
The error you saw:
C:\Users\AppData\Local\Temp\cc62OXjT.ltrans0.ltrans.o: In function `main':
C:\Users\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6\cores\arduino/main.cpp:46: undefined reference to `loop'
collect2.exe: error: ld returned 1 exit status
exit status 1
Compilation error: exit status 1
1 Like
kyc7334d:
this code was not worked
The code in #1 , abbreviated:
void loop() {
if (count == 0)
{
//...
count++;
}
}
We see that your loop stuff will execute once. Why is that what you want?
a7
Thanks for replying my question.
I would like to display once the result of bit operation at Serial Monitor of Arduino IDE (2.3.2)
And I expected that the result would be displayed at Serial Monitor. But it didn't.
I made this code to identify "the way of doing something once" for any kinds of method.
Simply say "do something once".
Yes, you are right.
The loop would be operated once.
I would like to make some code which operate something once.
So i wrote something in the setup method, but it didn't worked.
And then i changed my code like you said, but it also didin't worked.
(I mean the result wasn't displayed at Serial Monitor)
The code works fine on my computer. Did you set the serial monitor to the correct Baud rate?
Yes I did.
I set it as 9600 in the code and serial monitor.
Do not post pictures of code. Please edit your post and insert the code, using code tags.
Here is the output I get, after adding the loop() function:
: 0xAA, 0b10101010
Bit AND : 0xA0, 0b10100000
Yes I see.
I just wanted to show the state of my IDE.
Thanks.
And I also checked the result as writing the code in loop() function.
But I really wonder why the code written in the setup() function.
The code using setup() to print the result is also missing the loop() function, so it will not compile and run.
It is a good idea to add a delay to make sure that the serial connection is made, before trying to print:
void setup() {
Serial.begin(9600);
delay(2000); //pause for connection to be made
...
Thanks for notifying me.
I added the loop() method.
And deley() method works for me.
I write down my code like below.
void setup() {
Serial.begin(9600);
delay(800);
byte n = 0xAA, m;
Serial.print("\t\t: ");
print_number(n);
Serial.print("Bit AND\t: ");
m = n & 0xF0;
print_number(m);
}
void print_number(byte m)
{
Serial.print("0x");
Serial.print(m, HEX);
Serial.print(", 0b");
for(int k=7;k >=0; k--)
{
if( m & (0x01 << k))
{
Serial.print('1');
}
else
{
Serial.print('0');
}
}
Serial.print("\n");
}
void loop()
{
}
I test which delay time is effective on my system.
The delay time is below than 800, than the above code is not operated as i expected.
And i get the lesson that communication is affected on the hardware system.
(It is my guess)
Thank for solving this problem together.
The code is operating as expected.
However, the serial connection to the PC did not have time to become stable, so the serial output did not appear on the serial monitor.
I think i made you confusing because of my speaking.
The delay time is below than 800, than the above code is not operated as "i expected".
The word expectation in above sentence means that
"I expect the result will be displayed even baud rate is lower than some value,
But it doesn't."
Have a nice day!!
system
Closed
December 5, 2024, 10:42pm
19
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.