![]() |
Lab 4
Code for ECE 474 lab4
|
Code for lab 4 Project part. More...
#include <Arduino.h>#include <IRremote.hpp>#include <LiquidCrystal.h>#include <dht.h>#include <Arduino_FreeRTOS.h>#include "queue.h"Macros | |
| #define | temp_pin A0 |
| Analog Pin sensor is connected to. More... | |
| #define | LED_PIN 49 |
| LED pin. More... | |
| #define | RECV_PIN 10 |
| IR recever's pin. More... | |
| #define | LCD_RS 35 |
| LCD screen pin rs. More... | |
| #define | LCD_EN 33 |
| LCD screen pin E. More... | |
| #define | LCD_D4 31 |
| LCD screen pin d4. More... | |
| #define | LCD_D5 29 |
| LCD screen pin d5. More... | |
| #define | LCD_D6 27 |
| LCD screen pin d6. More... | |
| #define | LCD_D7 25 |
| LCD screen pin d7. More... | |
| #define | SPEAKER_DUR 500 |
| duration for speaker to play More... | |
| #define | LCD_DUR 2000 |
| delay for the LCD screen and the the tempreture/humidity sensor More... | |
| #define | STACK 128 |
| default stack size More... | |
| #define | STACK_MOTOR 256 |
| stack size for motor function More... | |
| #define | STACK_MOTOR_CTR 512 |
| stack size for motor controller function More... | |
| #define | STACK_QUEUE 128 |
| stack size for queue More... | |
| #define | MOTOR_SLOW 127 |
| speed for motor to spin at slower speed More... | |
| #define | MOTOR_FAST 255 |
| speed for motor to spin at faster speed More... | |
| #define | MOTOR_NULL 0 |
| speed for motor to turn off More... | |
| #define | LCD_COL 16 |
| total numbers of letters on each row of the LCD display More... | |
| #define | LCD_ROW 2 |
| total numbers of row of the LCD display More... | |
| #define | RMT_PWR 0xFFA25D |
| remode button code for power More... | |
| #define | C4 3830 |
| 261Hz frequency to play More... | |
| #define | SLIENCE 0 |
| frequency to silence More... | |
| #define | TEMP_THRESH 30 |
| termpreture that will change the motor speed More... | |
| #define | LED_ON 100 |
| time in miliseconds for the LED to stay on More... | |
| #define | LED_OFF 200 |
| time in miliseconds for the LED to stay off More... | |
Functions | |
| LiquidCrystal | lcd (LCD_RS, LCD_EN, LCD_D4, LCD_D5, LCD_D6, LCD_D7) |
| Initialize LCD and its pins. More... | |
| IRrecv | irrecv (RECV_PIN) |
| Initialize IR remote receiver and its pins. More... | |
| void | speaker (void *pvParameters) |
| play an beeping sound on an external speaker More... | |
| void | temp_sensor (void *pvParameters) |
| detect tempreture on external tempreture and humidity sensor More... | |
| void | screen (void *pvParameters) |
| display temperature and humidity on the external LCD display More... | |
| void | MotorControl (void *pvParameters) |
| set the speed of the motor More... | |
| void | MotorSpeed (void *pvParameters) |
| controls the on/off and speed of the motor and receive the IR remote's data More... | |
| void | TaskBlink (void *pvParameters) |
| blink an external LED More... | |
| void | setup () |
| Funciton to setup RtOS tasks. More... | |
| void | loop () |
| loop though different funcitons, not in use for this lab More... | |
Variables | |
| QueueHandle_t | motorQueue = xQueueCreate(STACK, sizeof(int)) |
| FreeRTOS Queue for motor. More... | |
| QueueHandle_t | tempQueue = xQueueCreate(STACK, sizeof(int)) |
| FreeRTOS Queue for tempreture sensor. More... | |
| QueueHandle_t | humQueue = xQueueCreate(STACK, sizeof(int)) |
| FreeRTOS Queue for humidity sensor. More... | |
| QueueHandle_t | screenQueue = xQueueCreate(STACK, sizeof(int)) |
| FreeRTOS Queue for LCD screen. More... | |
| QueueHandle_t | beepQueue = xQueueCreate(STACK, sizeof(int)) |
| FreeRTOS Queue for speaker. More... | |
| TaskHandle_t | speakerHandle |
| FreeRTOS task handle for speaker function. More... | |
| dht | DHT |
| Initialize termpreture and humidity sensor. More... | |
| unsigned long | motorOn = 0 |
| motor on/off toggle. More... | |
Code for lab 4 Project part.
The program to use FreeRTOS library to achieve the lab 4 project. It will turn on a motor when the power button of a remote control is pressed,if the motor is previously off, and similarly turn off a running motor. When power button is pressed and motor gets turned on/off it will make an external speaker for 500 miliseconds in a seperate FreeRTOS task. If the temperature goes over 30 degrees Celsius, the motor spins quicker; if the temperature goes below 30 degrees Celsius, the motor spins slower. The tempreture is determined by an external tempreture/humidity sensor. The temperature and humidity are displayed on the LCD screen. While all the mentioned are running an external LED at pin 49 flashes on for 100ms and off for 200ms. This program uses the provided FreeRTOS similar to other part of this lab.
| #define C4 3830 |
261Hz frequency to play
| #define LCD_COL 16 |
total numbers of letters on each row of the LCD display
| #define LCD_D4 31 |
LCD screen pin d4.
| #define LCD_D5 29 |
LCD screen pin d5.
| #define LCD_D6 27 |
LCD screen pin d6.
| #define LCD_D7 25 |
LCD screen pin d7.
| #define LCD_DUR 2000 |
delay for the LCD screen and the the tempreture/humidity sensor
| #define LCD_EN 33 |
LCD screen pin E.
| #define LCD_ROW 2 |
total numbers of row of the LCD display
| #define LCD_RS 35 |
LCD screen pin rs.
| #define LED_OFF 200 |
time in miliseconds for the LED to stay off
| #define LED_ON 100 |
time in miliseconds for the LED to stay on
| #define LED_PIN 49 |
LED pin.
| #define MOTOR_FAST 255 |
speed for motor to spin at faster speed
| #define MOTOR_NULL 0 |
speed for motor to turn off
| #define MOTOR_SLOW 127 |
speed for motor to spin at slower speed
| #define RECV_PIN 10 |
IR recever's pin.
| #define RMT_PWR 0xFFA25D |
remode button code for power
| #define SLIENCE 0 |
frequency to silence
| #define SPEAKER_DUR 500 |
duration for speaker to play
| #define STACK 128 |
default stack size
| #define STACK_MOTOR 256 |
stack size for motor function
| #define STACK_MOTOR_CTR 512 |
stack size for motor controller function
| #define STACK_QUEUE 128 |
stack size for queue
| #define temp_pin A0 |
Analog Pin sensor is connected to.
| #define TEMP_THRESH 30 |
termpreture that will change the motor speed
| IRrecv irrecv | ( | RECV_PIN | ) |
Initialize IR remote receiver and its pins.
Initialize LCD and its pins.
| void loop | ( | ) |
loop though different funcitons, not in use for this lab
This program uses RTOS so this function is not in use. Things are done in Tasks.
| void MotorControl | ( | void * | pvParameters | ) |
set the speed of the motor
after setup the correct registers and counters, it recieve the motor speed value from motorQueue using xQueueReceive() and store that value on motorSpeed . Then it put this value on register/pin OCR3A (pin 5) which is connected to the motor driver L239D, which is connected to the motor
| pvParameters | parameters that FreeRTOS can pass in |
MotorSpeed() | void MotorSpeed | ( | void * | pvParameters | ) |
controls the on/off and speed of the motor and receive the IR remote's data
Using the IRRemote library, decode the remote button's messgage using its decode() function and put it into results . Using this value to determine if is the power button with code RMT_PWR being press. If so toggle motorOn to turn motor on/off, set the speaker's duration SPEAKER_DUR into duration , and sent the duration to beepQueue using RTOS' xQueueSendToBack() . Activate speaker() function using vTaskResume() to have the speaker beep on the background and using IRRemote's resume() function to keep listen for remote input. If not motorOn set the motor's speed MOTOR_NULL to turn it off. Otherwise read the temperature value from tempQueue using xQueueReceive() . Set motorSpeed MOTOR_FAST if temperature passes threshold tempreture TEMP_THRESH. Otherwise set the motor's speed to MOTOR_SLOW . Using xQueueReceive() to sent motorSpeed pointer to motorQueue so that MotorControl() can use it.
| pvParameters | parameters that FreeRTOS can pass in |
| void screen | ( | void * | pvParameters | ) |
display temperature and humidity on the external LCD display
Using the Arduino's LiquidCrystal library, display the tempreture on an external LCD display. Fist grab temperature and humidity from screenQueue and humQueue and put the data into temperature and humidity using xQueueReceive() . Then it first set the parmeter of the LCD display using LiquidCrystal's begin() function with LCD_COL on each row for LCD_ROW row(s). It then uses LiquidCrystal's setCursor to select the first row to write and write the tempreture using the library's pint() function. It does the same thing for the second row to write the humidity of the surrounding environment. Then use RTOS vTaskDelay() to wait for LCD_DUR miliseconds to display the next data recieved
| pvParameters | parameters that FreeRTOS can pass in |
temp_sensor() | void setup | ( | ) |
Funciton to setup RtOS tasks.
Setup serial communication method and creat RTOS tasks. Set the priority of each task as well as its size.
| void speaker | ( | void * | pvParameters | ) |
play an beeping sound on an external speaker
Upon started on correct registers and timers, it will start to recieve duration from beepQueue which set amount of time that the speaker will play. Then it plays the speaker at C4 on OCR4A register/port (pin 6) for duration amount of time then set it to SLIENCE freqency to stop it. It then uses vTaskSuspend() to stop itself
| pvParameters | parameters that FreeRTOS can pass in |
| void TaskBlink | ( | void * | pvParameters | ) |
blink an external LED
First it setup the LED in this function. Then in an infinite loop, it is going to turn LED on using digitalWrite() function and wait for LED_ON miliseconds using vTaskDelay() . Similarly then it uses the same functions to turn it off for LED_OFF miliseconds
| pvParameters | parameters that FreeRTOS can pass in |
| void temp_sensor | ( | void * | pvParameters | ) |
detect tempreture on external tempreture and humidity sensor
Using the DHT package, it will read the data from the sensor and put them in temperature and humidity variable. It then sent temperature to tempQueue and screenQueue for motor control and display on the LCD screen. It will also sent humidity to the humQueue to display humidity on the screen as well using xQueueSendToBack() . Then it will wait for LCD_DUR miliseconds using vTaskDelay to wait for the LCD screen to respond.
| pvParameters | parameters that FreeRTOS can pass in |
screen() MotorSpeed() | QueueHandle_t beepQueue = xQueueCreate(STACK, sizeof(int)) |
FreeRTOS Queue for speaker.
| dht DHT |
Initialize termpreture and humidity sensor.
| QueueHandle_t humQueue = xQueueCreate(STACK, sizeof(int)) |
FreeRTOS Queue for humidity sensor.
| unsigned long motorOn = 0 |
motor on/off toggle.
With 1 being on and 0 being off
| QueueHandle_t motorQueue = xQueueCreate(STACK, sizeof(int)) |
FreeRTOS Queue for motor.
| QueueHandle_t screenQueue = xQueueCreate(STACK, sizeof(int)) |
FreeRTOS Queue for LCD screen.
| TaskHandle_t speakerHandle |
FreeRTOS task handle for speaker function.
| QueueHandle_t tempQueue = xQueueCreate(STACK, sizeof(int)) |
FreeRTOS Queue for tempreture sensor.