0% found this document useful (0 votes)
9 views5 pages

Report 6 Corrected Pages

This document contains code snippets and descriptions for monitoring pulse and step data from sensors using an Arduino and displaying the results on an OLED screen. It uses FreeRTOS to create multiple concurrent tasks to read sensors, update display data, and keep time. Circuit diagrams and pin connections are shown for the pulse and step sensors.

Uploaded by

Maaz Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

Report 6 Corrected Pages

This document contains code snippets and descriptions for monitoring pulse and step data from sensors using an Arduino and displaying the results on an OLED screen. It uses FreeRTOS to create multiple concurrent tasks to read sensors, update display data, and keep time. Circuit diagrams and pin connections are shown for the pulse and step sensors.

Uploaded by

Maaz Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

6.4.

1 Circuit Schematics

Figure 2 Circuit diagram in Proteus environment

6.4.2 Code Snippet

#include <AnalogIO.h>
#include <PulseSensorPlayground.h>
//#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Arduino_FreeRTOS.h>
#include "task.h"
#include "semphr.h"
#include <TimeLib.h>
#include <DS1307RTC.h>

#define SCREEN_WIDTH 128


#define SCREEN_HEIGHT 64
#define OLED_RESET 4
#define SCREEN_ADDRESS 0x3D
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
TaskHandle_t xHandleD = NULL;
TaskHandle_t xHandleT = NULL;
SemaphoreHandle_t sem;

int timeout = 0;
unsigned long seconds;
unsigned int minutes;
unsigned int hours;
unsigned int BPM ;
unsigned int steps = 0;
uint8_t disp_clear = 0;
tmElements_t tm;

unsigned long previousMillis = 0;


const long interval = 1000;
const int LEDPIN = 13;
const int inputpin = 2;
unsigned int PULSE_SENSOR_PIN = A0;
unsigned int STEP_SENSOR_PIN = A8;
int STEP_THRESHOLD = 0;

PulseSensorPlayground pulseSensor;

SemaphoreHandle_t rtcSem;
SemaphoreHandle_t displaySem;

void vibrationMonitor(void *pvParameters)


{
Serial.println("Starting");

while (1)
{
// Assuming STEP_SENSOR_PIN is an analog pin, read its value
int vibrationValue = analogRead(STEP_SENSOR_PIN);

if (vibrationValue > STEP_THRESHOLD)


{
if (xSemaphoreTake(sem, portMAX_DELAY) == pdPASS)
{
steps++;
xSemaphoreGive(sem);
}
}
vTaskDelay(pdMS_TO_TICKS(100));
}
}

void vApplicationIdleHook()
{
if (xSemaphoreTake(sem, portMAX_DELAY) == pdPASS)
{
Serial.println("update test 1");

BPM = pulseSensor.getBeatsPerMinute();

Serial.println(BPM);

xSemaphoreGive(sem);
}
}

void setup()
{
Serial.begin(9600);
setSyncProvider(RTC.get); // Set the RTC as the time provider

sem = xSemaphoreCreateBinary();
if (sem != NULL)
{
xSemaphoreGive(sem);
}

pinMode(LEDPIN, OUTPUT);
pinMode(inputpin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(inputpin), button_press, FALLING);

if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3D))
{
Serial.println("SSD1306 allocation failed");
while (1)
; // Don't proceed, loop forever
}
display.clearDisplay();
display.setTextColor(WHITE);
Serial.println("1");
xTaskCreate(Display, "Display", 4096, NULL, 1, &xHandleD);
xTaskCreate(Time, "Time", 128, NULL, 2, &xHandleT);
xTaskCreate(vibrationMonitor, "VibrationMonitor", 1000, NULL, 3, NULL); // Create
the vibration monitoring task
vTaskStartScheduler();
}

void loop() {}

void button_press()
{
timeout = 0;
}

void Display(void *pvParameters)


{
if (xSemaphoreTake(sem, portMAX_DELAY) == pdPASS)
{
while (1)
{
if (disp_clear == 0)
{
display.clearDisplay();
display.setTextSize(1);
display.setCursor(1, 1);
display.print("BPM: ");
display.print(BPM);
display.setCursor(90, 1);
display.print("S:");
display.print(steps);
display.setCursor(30, 35);
display.print(hours);
display.print(':');
display.print(minutes);
display.print(':');
display.print(seconds);
display.display();
}
else
{
display.clearDisplay();
display.setTextSize(1);
display.setCursor(1, 1);
display.display();
}
}
}
xSemaphoreGive(sem);
//vTaskDelay(pdMS_TO_TICKS(100));
}

void updateData() {
while (1) {
if (xSemaphoreTake(displaySem, pdMS_TO_TICKS(100)) == pdTRUE) {
Serial.println("update test 1");
// Generate a new random BPM every 100 milliseconds
hours = tm.Hour;
minutes = tm.Minute;
seconds = tm.Second;
Serial.println(RTC.read(tm));

xSemaphoreGive(displaySem);
}
vTaskDelay(pdMS_TO_TICKS(100));
}
}

void Time(void *pvParameters)


{
while (1)
{
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval)
{
previousMillis = currentMillis;
seconds = currentMillis / 1000;
minutes = seconds / 60;
hours = minutes / 60;
timeout++;
if (timeout >= 3)
disp_clear = 1;
else
disp_clear = 0;
}
vTaskDelay(pdMS_TO_TICKS(100));
}
}

You might also like