Need fast response from some inputs/outputs, slower for other

Hello,
I’m doing an environment controller that will measure temperature, RH, Co2 levels, light conditions and some other stuff.
I want the program to be very responsive when it comes to user input, but only need to read sensors and refresh data on screen at a lower rate, about 10 times slower.
I am thinking to do a fast sketch that will handle most of the things I need to be fast, but make a counter to do the things I need to be slower only every 10 loops.
Is this the right aproach or there is another, better way that I can’t think of yet? (I’m still a begginer in arduino coding.)

magnetman: Hello, I'm doing an environment controller that will measure temperature, RH, Co2 levels, light conditions and some other stuff. I want the program to be very responsive when it comes to user input, but only need to read sensors and refresh data on screen at a lower rate, about 10 times slower. I am thinking to do a fast sketch that will handle most of the things I need to be fast, but make a counter to do the things I need to be slower only every 10 loops. Is this the right aproach or there is another, better way that I can't think of yet? (I'm still a begginer in arduino coding.)

No, you don't want to do it based on the number of loops. The loops will take variable amounts of time based on factors that are out of your control. Instead you should use the millis() function to record the current time. Then wait until the desired amount of time has passed before triggering the next action.

Take a look at the sample app "BlinkWithoutDelay" that is included with the IDE. It will give you the basic idea.ß

There is an extended demo of the BWoD technique in the first post in this Thread.

…R

Thank you very much, I'll look into that.

The arduino will react very quickly even your polling some ics every cycle. Unless they are display functions or some other really complex bit of code. I poll some temp sensors every cycle then take the average of 1000 reads to make sure its stable and the arduino still responds to input from the phone as well as physical switches just as it was before. So I think you will be fine just letting it be a simple code and adding in timed triggers if something is slowing you down.

drksam:
The arduino will react very quickly even your polling some ics every cycle. Unless they are display functions or some other really complex bit of code. I poll some temp sensors every cycle then take the average of 1000 reads to make sure its stable and the arduino still responds to input from the phone as well as physical switches just as it was before. So I think you will be fine just letting it be a simple code and adding in timed triggers if something is slowing you down.

This is all good advice (particularly @Robin2’s recommendations) however one thing to keep in mind is that certain sensors (e.g. humidity) may heat up if they are read too often, thus affecting the readings.

just something to keep in mind…

Thanks nice to know.

Thank you all for the responses. Now, I need your input on another problem regarding same environment controller project.

I have a sensor that goes seems to have some problems and shows random values after 1-2 days of running well. I can recognise that behaivior very simple, as it goes out of range, but I need code that cuts the power to the sensor for 5-10 minutes (using a relay board on the sensor power input line) every time sensor gets out of range. I have a DS3231 RTC I was hoping to use to time the 5-10 minutes but I have no ideea how to do that. Could you guys help me on that?

I also need code that averages the readings for the last 5-10 minutes for every sensor to print them to a sd card, so if you guys help me to time actions using my rtc, then I can solve both problems. even some pointers on where to study more would be great.

I doubt if it is necessary to power down the sensor for a precise length of time so the simplest way to manage the time is using millis().

Averaging the readings should as simple as adding subsequent readings and keeping a count of the number of readings. Then divide the total by the count to get the average and reset the total and the count back to 0 to start again.

As far as I know you can use the RTC in a similar way to millis - record the time at the start and subtract it from a later time to see if X minutes or seconds have elapsed.

...R

If you can control the power to the sensor, then consider only powering it for the period that you are actually taking the readings.

How frequently do you need to take readings and record the data?

If you are talking every 10, 20 or 30 mins, then you can easily write a routine to manage that.

You can use an RTC to calculate elapsed time, as Robin2 points out.

Averaging sensor readings will be very easy, it is a simple exercise with an array if all of the sensors are on the same board or connected otherwise.

thank you very much for your guidance. i now know what direction should I go. the sensor has a 3 min warm-up period so i rather leave it running and only "reset it" when it goes out of bounds.

Don’t those CO2 sensors need to be in for 24 hours before they stablise. You don’t want to turn it off at all.

datasheet said should be left on for 8 hours first use. then it has a 3 min warm-up period. but it works for 10-20-30 hours then starts to drop under 400... if i leave it on almost hits 0 after some hours.

Just had another ideea, found some ir receivers for computer tv tuner boards around the house and was thinking about using one of them as my ir receiver to add a remote to my project, sure would look nicer with it’s ready made case.
Do those look like they could work with arduino?
How do i figure out how is it wired so I know how to connect to arduino?

Yes they will work but you may have to do a little work to find a pin out or just hack it and make your own.

Think I can measure which is VCC and GND in the tv tuner output jack, isn't it?

Most likely

Thank you very much for the help. I already figured out which is GND. Also measured 750 Ohms between GND and one of the other contacts on the jack, and 570 Ohms with the other one. Second ir receiver has them same, one bigger than the other but not the same values. Could that help me figure it out without searching whole house for the tv tuners?

Probably but you would need an ir receiver with out the cable and case to compare too or maybe look up a data sheat for a popular id.

Well, clearly the "sleeve" on the phone plug should be the ground.

These are simply a standard 38 KHz IR receiver in an encapsulation.

OK, if you make a test circuit to imitate the Application Circuit on the first page of the datasheet, with a 1µF capacitor and a 470 ohm resistor to the supply terminal on the sensor, and for the output terminal, you have another 470 ohm resistor in series with a high-efficiency LED going to your 5V supply (on your breadboard from your Arduino), then you can test it.

The two resistors prevent any damage to the device if your guess for the correct connection is wrong, and when you have them the correct way around, the LED will flicker when you point an activated remote control at it.