Automatic alcohol dispenser project

Hello everyone,
I am new in the world of freertos, I have to do a project that consists of an automatic alcohol dispenser that measures temperature. The parts/sensors of my project are:

  1. DHT22 for temperature (I know its not ideal but its the only one that I have).
  2. Hc-sr04 for distance measurement (ultrasound).
  3. I2c display 16x2 to show the temperature.
  4. Buzzer to make sound.
  5. Servo to dispense alcohol.

The idea of the project is that when someone comes within 15 cm of the device, the temperature is displayed on the screen, the servo moves and can dispense alcohol, and the buzzer makes a little sound.

As I understand it, I have to create a task for each activity. One to measure temperature and possibly send that information to a queue, another to read the queue and display it on the screen, another to make the sound with the buzzer, another to measure distance with the ultrasound, and another to move the servo. This is how I was asked to do it, but my question is what is the best way to organize the tasks. How do I make it so that first the distance is measured, then the temperature is measured, then it is shown on the display, the servo is moved and the sound is made? What is the best way to communicate between tasks (when a task measures less than 15 cm, tell another task to measure temperature, and then it is shown on the display, and the servo moves and makes the sound)?
I would like to see how you think about it and it would help me a lot to know. I’m very new to the subject and I’m having a hard time thinking which is the best way. I would appreciate simple solutions that not involve complicated stuff as Im having a hard time with freeRTOS.

Then just keep it simple :wink:
If you have just separate functions then use functions as in any other program.
Simply spoken multi-tasking is used to separate things done in parallel or simultaneously.
So if you have to do things sequentially just call the corresponding functions in a sequence.
Maybe you don’t need tasks resp. FreeRTOS at all for your application.

Here are my 2 Cents:

as I’m having a hard time with freeRTOS.

Why? You might find out that it is not complicated. Start little by little: one task, let it set a LED, two tasks, let them send messages to a queue. On FreeRTOS.org you will find plenty of documentation and examples. See also the demo’s on Github.

It is worth to learn about multi-tasking and about the essence of an RTOS.

An automatic Alcohol dispenser: what a nice project! I agree with Hartmut, you probably do not need multitasking. But there is nothing wrong with using FreeRTOS with only one task. At least you’ll get a time function for free: xTaskGetTickCount() :slight_smile:

Try to see each peripheral as an object, with properties and methods. In your project:

  • Temperature meter:

    • int LastMeasuredTemperature;
    • void measure();
  • Distance measurement

    • int lastDistance;
    • void measure();
  • Display 2 x 16

    • int height; // 2
    • int width; // 16
    • void init ( void );
    • void clear ( void );
    • void write ( int x, int y, const char *pcMessage );
  • Buzzer

    • int isBuzzing;
    • void buzz_start( void );
    • void buzz_stop( void );
  • Servo to dispense alcohol

    • int isDispensing;
    • void dispense_start( void );
    • void dispense_stop( void );

The first thing I would do, is find or create a driver for the LCD. The LCD will be useful to give quick feedback while testing. It can show lots of debug information, like distance and temperature.

Don’t take the above list too literal, develop your own style. I am much influenced by C++, but the same modularisation can be achieved in plain C.

Have fun!

I already have the display and all the sensors functioning. I don’t know how to make it so that the freertos first runs the task with the ultrasound and temperature, then the display, then the servo and then the buzzer, I cant put everything in one task, I was told to use separate tasks for it.

You want each of the tasks to wait to be notified by the previous step that they are supposed to do their thing. You could either use a semaphore or the direct-to-task notifications for this.

Ok. Everything is up and running! I used a Queue and 2 tasks notifications. Is it ok if all of the tasks have the same priority? Everything is working as expected but maybe its a bad practice to have all the tasks with priorityNormal.

There is no common practice for a priority scheme because it depends purely on the requirements of your application.
However, in your case it might be desirable that e.g. the dispenser control task should not be preempted by another task to have best control over the quantity of alcohol to dispense. If this task would be preempted for a while during dispensing you probably get varying quantities of alcohol dispensed, which is surely not desired. Therefore give it a higher/the highest priority of your tasks.
Just as an example.