-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSERIAL_TMP36_Pulse.ino
213 lines (169 loc) · 6.92 KB
/
SERIAL_TMP36_Pulse.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/**************************************************************************
Date: 04/03/2021
This is an example for Serial console reading from TMP36 and Pulse sensors.
Pulse SIG pin connects to Arduino Nano 33 IoT pin D3.
TMP36 SIG pin connects to Arduino Nano 33 IoT pin D2.
**************************************************************************/
#include <SPI.h>
#include <Wire.h>
#include <FreeRTOS_SAMD21.h>
TaskHandle_t Handle_aTask;
TaskHandle_t Handle_bTask;
TaskHandle_t Handle_monitorTask;
int pulsePin = A1;
int pulseLED = 16;
int tempPin = A0;
int tempLED = LED_BUILTIN;
int startMillis, count;
void setup() {
pinMode(tempLED,OUTPUT);
pinMode(pulseLED,OUTPUT);
startMillis = millis();
Wire.begin();
Serial.begin(9600);
delay(1000); // prevents usb driver crash on startup, do not omit this
while (!Serial) ; // Wait for serial terminal to open port before starting program
// Create the threads that will be managed by the rtos
// Sets the stack size and priority of each task
// Also initializes a handler pointer to each task, which are important to communicate with and retrieve info from tasks
xTaskCreate(threadA, "Task Pulse", 256, NULL, tskIDLE_PRIORITY + 3, &Handle_aTask);
xTaskCreate(threadB, "Task Temperature", 256, NULL, tskIDLE_PRIORITY + 2, &Handle_bTask);
xTaskCreate(taskMonitor, "Task Monitor", 256, NULL, tskIDLE_PRIORITY + 1, &Handle_monitorTask);
// Start the RTOS, this function will never return and will schedule the tasks.
vTaskStartScheduler();
// error scheduler failed to start
// should never get here
while(1)
{
Serial.println("Scheduler Failed!");
Serial.flush();
delay(1000);
}
}
void myDelayMs(int ms)
{
vTaskDelay( (ms * 1000) / portTICK_PERIOD_US );
}
//*****************************************************************
// Task will periodically print out useful information about the tasks running
// Is a useful tool to help figure out stack sizes being used
// Run time stats are generated from all task timing collected since startup
// No easy way yet to clear the run time stats yet
//*****************************************************************
static char ptrTaskList[400]; //temporary string buffer for task stats
void taskMonitor(void *pvParameters)
{
int x;
int measurement;
Serial.println("Task Monitor: Started");
// run this task afew times before exiting forever
while(1)
{
myDelayMs(10000); // print every 10 seconds
Serial.flush();
Serial.println("");
Serial.println("****************************************************");
Serial.print("Free Heap: ");
Serial.print(xPortGetFreeHeapSize());
Serial.println(" bytes");
Serial.print("Min Heap: ");
Serial.print(xPortGetMinimumEverFreeHeapSize());
Serial.println(" bytes");
Serial.flush();
Serial.println("****************************************************");
Serial.println("Task ABS %Util");
Serial.println("****************************************************");
vTaskGetRunTimeStats(ptrTaskList); //save stats to char array
Serial.println(ptrTaskList); //prints out already formatted stats
Serial.flush();
Serial.println("****************************************************");
Serial.println("Task State Prio Stack Num Core" );
Serial.println("****************************************************");
vTaskList(ptrTaskList); //save stats to char array
Serial.println(ptrTaskList); //prints out already formatted stats
Serial.flush();
Serial.println("****************************************************");
Serial.println("[Stacks Free Bytes Remaining] ");
measurement = uxTaskGetStackHighWaterMark( Handle_aTask );
Serial.print("Thread Pulse: ");
Serial.println(measurement);
measurement = uxTaskGetStackHighWaterMark( Handle_bTask );
Serial.print("Thread Temperature: ");
Serial.println(measurement);
measurement = uxTaskGetStackHighWaterMark( Handle_monitorTask );
Serial.print("Monitor Stack: ");
Serial.println(measurement);
Serial.println("****************************************************");
Serial.flush();
}
// delete ourselves.
// Have to call this or the system crashes when you reach the end bracket and then get scheduled.
Serial.println("Task Monitor: Deleting");
vTaskDelete( NULL );
}
static void threadA( void *pvParameters )
{
Serial.println("Thread Pulse: Starting");
while(1){
read_pulse();
}
// delete ourselves.
// Have to call this or the system crashes when you reach the end bracket and then get scheduled.
Serial.println("Thread Pulse: Deleting");
vTaskDelete( NULL );
}
static void threadB( void *pvParameters )
{
Serial.println("Thread Temperature: Starting");
while(1){
read_temperature();
}
// delete ourselves.
// Have to call this or the system crashes when you reach the end bracket and then get scheduled.
Serial.println("Thread Temperature: Deleting");
vTaskDelete( NULL );
}
void read_temperature() {
//getting the voltage reading from the temperature sensor
int reading = analogRead(tempPin);
// converting that reading to voltage, for 3.3v arduino use 3.3
float voltage = reading * 3.3;
voltage /= 1024.0;
// print out the voltage
Serial.print(voltage); Serial.println(" volts");
// now print out the temperature
float temperatureC = (voltage - 0.5) * 100 ; //converting from 10 mv per degree wit 500 mV offset
//to degrees ((voltage - 500mV) times 100)
Serial.print(temperatureC); Serial.println(" degrees C");
// now convert to Fahrenheit
float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;
Serial.print(temperatureF); Serial.println(" degrees F");
digitalWrite(tempLED, HIGH);
myDelayMs(500);
digitalWrite(tempLED, LOW);
myDelayMs(500);
}
void read_pulse() {
int Signal, Threshold = 510;
if(millis() < startMillis + 10000) {
Signal = analogRead(pulsePin); // Read the PulseSensor's value.
// Assign this value to the "Signal" variable.
Serial.println(Signal); // Send the Signal value to Serial Plotter.
if(Signal > Threshold){
count++;
digitalWrite (pulseLED,HIGH);// If the signal is above "550", then "turn-on" Arduino's on-Board LED.
myDelayMs(500);
digitalWrite (pulseLED,LOW);// Else, the sigal must be below "550", so "turn-off" this LED.
myDelayMs(500);
}
}
else {
Serial.println("BPM "+ String(count * 6));
startMillis = millis();
count = 0;
}
}
void loop() {
//read_temperature();
//read_pulse();
}