Bug using sensor DHT11 on Arduino Uno

Hello guys,

I need to use DHT11 without the void loop() and void setup() functions, but i’m having problems when i just change the name of functions…
It’s only a part of code, because the other one is working fine.
Note that when you rename functions to void setup() and void loop(), the code works and does not hang, but when i try to use it without the function it stops for no reason.

Please, help me!

Thanks for your time, anyway.

dht11.ino (3.13 KB)

When you define a function named main() in your sketch it overrides the default main() that's defined in the core libraries. For example, here's the default main() for Arduino AVR Boards: https://github.com/arduino/ArduinoCore-avr/blob/3f63f2975e7183c3254b6794bfcc8f19ca0301c9/cores/arduino/main.cpp#L33-L51

There's some important initialization code that happens in that function that you don't want to get rid of unless you really know what you're doing. This is almost certainly the cause of your "hang".

Why do you want to define main() in your sketch?

I need to program the code by registers, but i can use the dht11 function to make it easier to read this sensor. Looking for errors in the code I discovered that it is the 'dht.begin ()' function that is causing my code to stop, when I comment it the code flows normal, but does not read the sensor which is what this function should do.

You still didn't answer my question:

pert: Why do you want to define main() in your sketch?

What happens if you add this line to the top of your main():

init();

Assuming you're using one of the Arduino AVR Boards, that's the most important thing you're missing.

Only certain boards use initVariant() and unfortunately I haven't found a way to call that function from the sketch.

Only boards with native USB need USBDevice.attach();.

The serialEventRun() call is for the idiotic serialEvent() "feature", which is the main reason I wanted to define main() in my own sketches in the past but I found that by adding an empty definition for serialEventRun() you can eliminate the overhead for that call and so there is no longer any benefit for me to define my own main(). I find the setup(), loop() convention quite reasonable. If you're using a recent version of the IDE/Arduino AVR Boards there is no overhead for those function calls because they're inlined.

I need to create my main because I am programming for ATMEGA and I can’t use the ready-made arduino functions.

Looking for I found a discussion that said that the arduino main() is defined that way:

int main(void)
{
 init();

 initVariant();

#if defined(USBCON)
 USBDevice.attach();
#endif
 
 setup();
    
 for (;;) {
 loop();
 if (serialEventRun) serialEventRun();
 }
        
 return 0;
}

I tried to put this code on my own and got this error:

main.cpp.o (symbol from plugin): In function `atexit’:

(.text+0x0): multiple definition of `main’

My full code is linked above. Some parts are in Portuguese, but I think it’s understandable.

Projeto_SEMB_Ket_Rod.ino (6.02 KB)

rodrigorpo: I need to create my main because I am programming for ATMEGA and I can't use the ready-made arduino functions.

That makes no sense. You posted a .ino file. That means you're using the Arduino IDE. That means you can use the ready-made Arduino functions.

Please provide exact details of what you're doing. "programming for ATMEGA" makes no sense. All the most popular Arduino boards have ATmega microcontrollers so that's pretty much what we're all doing. "I need to program the code by registers" also makes no sense.

rodrigorpo: Looking for I found a discussion that said that the arduino main() is defined that way:

You didn't need to go looking, I posted a link to the Arduino AVR Boards definition of main() in my previous reply:

pert: For example, here's the default main() for Arduino AVR Boards: https://github.com/arduino/ArduinoCore-avr/blob/3f63f2975e7183c3254b6794bfcc8f19ca0301c9/cores/arduino/main.cpp#L33-L51

Yeah, I know that's confuse, but i need to write this code in c. I used Serial.print only for test, but I will send by serial communication. It is a final project of a college discipline. The only rule is not being able to use the arduino libraries .

OK, now I understand the reason for the limitation. I don't know why you got the "multiple definition of `main'" error. Does it go away if you restart the Arduino IDE to clear the cache?

It worked using some parts of main Arduino function.

int main(void)
{  
  init();

  Serial.begin(9600);
    
  uint8_t i = 0, j = 0; // Loop variables
  Ad_init(); // inicializar o ad
  Uart_init(); // Initialize USART0
  Dht_init();

  //initVariant();

  #if defined(USBCON)
    USBDevice.attach();
  #endif

    
  while(1) 
  {
    Solo_leitura();
    Dht_leitura();    
    printf("Temperatura: %d ºC Umidade: %d%%\n",temperatura,umidade);
    printf("\n\n");
    _delay_ms(1000);
    if (serialEventRun) serialEventRun();
  }


        
 return 0;
}

I had to comment 'initVariant();' and used part of the main code. Thank you man, really!