Static unsigned long problems

hi guys,
im relatively new to using Arduinos
I bought one of these starter kits from Amazon and after playing around I felt comftable enough to go on aliexpress buy a nano and a servo and start a project for my tortoise and her enclosure to open a hatch letting of some hot air..

up until now I just frankensteined together the code of the tutorials and it worked on the mega from the kid, now with my nano it only opens but doesnt close again / stops measuring entirely..


#include <SimpleDHT.h>




#include <dht_nonblocking.h>
#define DHT_SENSOR_TYPE DHT_TYPE_11


static const int DHT_SENSOR_PIN = 3;
DHT_nonblocking dht_sensor( DHT_SENSOR_PIN, DHT_SENSOR_TYPE );

Servo myservo;

/*
 * Initialize the serial port.
 */
void setup( )
{
  Serial.begin( 9600);
  myservo.attach(5);
  myservo.write(90);// move servos to center position -> 90°
}



/*
 * Poll for a measurement, keeping the state machine alive.  Returns
 * true if a measurement is available.
 */
static bool measure_environment( float *temperature, float *humidity )
{
  static unsigned long measurement_timestamp = millis( );

  /* Measure once every four seconds. change the 1000  */
  if( millis( ) - measurement_timestamp > 1000ul )
  {
    if( dht_sensor.measure( temperature, humidity ) == true )
    {
      measurement_timestamp = millis( );
      return( true );
    }
  }

  return( false );
}



/*
 * Main program loop.
 */
void loop( )
{
  float temperature;
  float humidity;

  /* Measure temperature and humidity.  If the functions returns
     true, then a measurement is available. */
  if( measure_environment( &temperature, &humidity ) == true )
  {
    Serial.print( "T = " );
    Serial.print( temperature, 1 );
    Serial.print( " deg. C, H = " );
    Serial.print( humidity, 1 );
    Serial.println( "%" );
  }
  /*change the 25*/
  if (temperature > 25 )
  {
   myservo.write(30);
  }
  else if (humidity > 80)
  {
    myservo.write(30);
  }
  else
  {
    myservo.write(90);
  }  
      
}
tippe oder füge den Code hier ein

I think it has to do with static unsigned long but couldnt find anything on her helping me.
Is there maybe a way of using delay or something similar?
btw Im planning to use it battery powered so it would be nice to keep as energy low as possible :slight_smile:

Thanks in advance (from me and my tortoise ;))

Does not compile,
possibly missing the Servo.h library

It includes the Servo.h library and worked correctly.

As the simulator doesn't have DHT11, I used DHT22 which is an improved DHT11.

" Servo e DHT11 - Wokwi ESP32, STM32, Arduino Simulator

#include <Servo.h>
#include <SimpleDHT.h>
#include "dht_nonblocking.h"
#define DHT_SENSOR_TYPE DHT_TYPE_22
static const int DHT_SENSOR_PIN = 3;
DHT_nonblocking dht_sensor( DHT_SENSOR_PIN, DHT_SENSOR_TYPE );
Servo myservo;
/*
   Initialize the serial port.
*/
//---------------------------------------------------------------------
void setup( ) {
  Serial.begin( 9600);
  myservo.attach(5);
  myservo.write(90);// move servos to center position -> 90°
}
/*
   Poll for a measurement, keeping the state machine alive.  Returns
   true if a measurement is available.
*/
//---------------------------------------------------------------------
static bool measure_environment( float *temperature, float *humidity ) {
  static unsigned long measurement_timestamp = millis( );
  /* Measure once every four seconds. change the 1000  */
  if ( millis( ) - measurement_timestamp > 1000ul )  {
    if ( dht_sensor.measure( temperature, humidity ) == true )    {
      measurement_timestamp = millis( );
      return ( true );
    }
  }
  return ( false );
}
//---------------------------------------------------------------------
/*
   Main program loop.
*/
void loop( )
{
  float temperature;
  float humidity;
  /* Measure temperature and humidity.  If the functions returns
     true, then a measurement is available. */
  if ( measure_environment( &temperature, &humidity ) == true )
  {
    Serial.print( "T = " );
    Serial.print( temperature, 1 );
    Serial.print( " deg. C, H = " );
    Serial.print( humidity, 1 );
    Serial.println( "%" );
  }
  /*change the 25*/
  if (temperature > 25 )  {
    myservo.write(30);
  }
  else if (humidity > 80)  {
    myservo.write(30);
  }
  else  {
    myservo.write(90);
  }
}

Servo problems reported on this forum are almost always due to an inadequate servo power supply. Power the servo separately (4xAA battery pack works for 1 or 2 small servos), and don't forget to connect the grounds.

Thanks, my original code had the library at the top, I seem to have missed it when copying
Otherwise I can’t make out any changes to the code.. do you think the serial print is a problem when i disconnect it from the pc and only on battery?

Hey thanks for the Tipp :slight_smile: how would you recommend doing that? I don’t really understand this static stuff​:sweat_smile:

I have my servo connected to the 5v pin but don’t have any problems I think..
It works fine a few times then the led stops blinking so I’m guessing it isn’t measuring/getting a true

Oh okay thanks, I thought I‘d need a whole new function sorry

After looking around here for a while I found a user that had a similar problem and solved it by only turning the sensor on for a measurement then turning it off again. I’m guessing powering it via pwr pin? Could you guys give me a little help how I could implement that..? I also wondered if it would make sense to use something like deepsleep to save energy?

If the code works on one type of Arduino, it probably works on most types of Arduino. Mega and Nano use very similar chips, so I would say your code should work equally on either.

So I would recommend you stop focusing on ways to change the code to make it work. It's probably a hardware problem. More details on that please: schematic (or at least a wiring diagram of some kind), details of power supply(s), maybe some clear, bright photos.

That’s the code with my schematic, at the ground and vcc symbols I placed a 9v battery with i think 300mah

Ah ... there's the problem. The same old problem we see so regularly on the forum.

I suggest replacing it with 4x AA NiMH. This will give around 5V, so you can connect it to the 5V pin of the Nano (instead of Vin) and take separate wires from the battery to power the motor.

Oh okay, the tutorials mostly connected to vin and I assumed that was the proper way. Thanks for the help I’m going to try it.

Usually Vin is the proper way. But with Vin you need at least 6.5V, which would require another 2x AA. Plus, that might be too much voltage for your servo, and you can't use the 5V pin of Nano as a 5V output because the Nano's on-board regulator can't provide enough current for any kind of motor and could overheat and be damaged.

Hi guys, i got myself a nice batterybox and started playing around again. this is my new wiring schematic :

and this is the new code:

#include <Servo.h>

#include <SimpleDHT.h>

#include <DHT11.h>

#include <dht_nonblocking.h>
#define DHT_SENSOR_TYPE DHT_TYPE_11

DHT11 dht11(3);

Servo myservo;

void setup() {
  // put your setup code here, to run once:
 Serial.begin(9600);
 pinMode(10, OUTPUT);
 myservo.attach(5);
 myservo.write(90);
 delay(1000);
 myservo.write(30);
 delay(1000);
 myservo.write(90);
}

void loop() {
  // put your main code here, to run repeatedly:
    int temperature = 0;
    int humidity = 0;


digitalWrite(10, HIGH);
delay(4000);
int result = dht11.readTemperatureHumidity(temperature, humidity);
Serial.print("T");
Serial.print(temperature);
Serial.print("  H");
Serial.println(humidity);
digitalWrite(10, LOW);

if (temperature > 25 )
  {
   myservo.write(30);
  }
  else if (humidity > 80)
  {
    myservo.write(30);
  }
  else
  {
    myservo.write(90);
  }  

delay(2000); 
}

For now everything works even though I haven`t run it longer than half an hour.
Do you guys spot anything that I could further approve?
And is it a Problem with the Serial.print when its not connected to a pc?
Do you think it will be to energy consuming to run of batteries?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.