กลับไปหน้ารวมไฟล์
serial-timer-bf3736-en.md

This project is to demonstrate how to create timer outputs

#include "SerialTimer.h"

//* Objects

Timer clock1(3);

Timer clock2(4);

Timer clock3(13);

First add the library and define our objects (outputs)

void timer_switch(int isPin, float Time, bool serial_on_off, int mode) //Funcion para los multiples timers
{ //los casos son a necesidad del usuario
unsigned long past = millis();
switch (mode)
{
case 0:
Serial.println("\
The timer has started");
switch (isPin) // Aqui puedes agregar los casos que necesites
{
case 1:
clock1.set_timer(Time, past);//asignacion de tiempo
break;
case 2:
clock2.set_timer(Time, past);
break;
case 3:
clock3.set_timer(Time, past);
break;
default:
Serial.println("Error");
}
break;
case 1:
switch (isPin) // aqui tambien debes añadir el numero de casos necesarios
{
case 1:
clock1.switch_pin(serial_on_off);//cambio de estado
Serial.println(clock1.get_pin());//lectura de estado
break;
case 2:
clock2.switch_pin(serial_on_off);
Serial.println(clock2.get_pin());
break;
case 3:
clock3.switch_pin(serial_on_off);
Serial.println(clock3.get_pin());
break;
default:
Serial.println("Error");
break;
}
break;
default:
Serial.println("Error");
}

If you need to use more than 3 outputs you should edit this part and add them as cases

void setup()
{
Serial.begin(9600);
menu();
}

It's very important initialize the Serial and add the menu function

void loop()
{
rx_menu(3); //mainn menu
clock1.get_valor(); // read if the time is complete
clock2.get_valor();
clock3.get_valor();
}

In the loop, you only need to declare the function "rx_menu" and "get_valor" for each object's timer to run

Bluetooth

You can use an HC05 or other Bluetooth module and use the "Serial Bluetooth" application to send commands

EXPANDED TECHNICAL DETAILS

Microsecond-Level Code Execution Profiling

The Serial Timer is a vital diagnostic tool designed to measure the exact execution time of specific code blocks, helping developers optimize their firmware.

  • Hardware-Based Elapsed Tracking: Uses the micros() function to record the timestamp before and after a target function. The Arduino calculates the difference and reports the "Delta" (in microseconds) to the Serial Monitor.
  • Loop Frequency Analysis: (Advanced version) The timer can calculate the "Average Loop-Time" over 1,000 cycles, providing a statistical baseline for identifying bottlenecks in real-time control systems.

Practical Use Case

  • Efficiency Benchmarking: Ideal for comparing the speed of different algorithms (like Bubble Sort vs. Quick Sort) directly on the physical target hardware.

ข้อมูล Frontmatter ดั้งเดิม

title: "Serial Timer"
description: "Timer for Arduino pins controlled by Serial monitor"
author: "scardeath0101"
category: "Lab Stuff"
tags:
  - "home automation"
  - "clocks"
  - "communication"
views: 6400
likes: 1
price: 1499
difficulty: "Intermediate"
components:
  - "1x Arduino Nano R3"
  - "1x Relay Module (Generic)"
  - "1x HC-05 Bluetooth Module"
tools: []
apps:
  - "1x Arduino IDE"
downloadableFiles:
  - "https://projects.arduinocontent.cc/e511b908-074f-46c9-b136-1057bf3dbcfc.zip"
  - "https://projects.arduinocontent.cc/e511b908-074f-46c9-b136-1057bf3dbcfc.zip"
documentationLinks: []
passwordHash: "992916679a9c31b3a6a56e6cf1b16cbdbcda8a5203e95e084b2566bb9bdecfa0"
encryptedPayload: "U2FsdGVkX18uNkrNIq9yiOWmY+yLSPXBzRAKLaV0oAaM9XyI9oop4s+cDTy3vX8NmWPxeTx6hO9rix5q06Kn5uoYD8Bekh/OdY7I4/FEk0I="
seoDescription: "Control Arduino pins with this Serial Timer project via Serial monitor. Simple and efficient timing control."
videoLinks: []
heroImage: "https://cdn.jsdelivr.net/gh/bigboxthailand/arduino-assets@main/images/projects/serial-timer-bf3736_cover.jpg"
lang: "en"