I apologize in advance, this is my first forum post.
I have an arduino code that sends out pulses based on a user's input.
I was hoping to have the arduino send out the exact (to the millisecond) time of day of when the pulses were sent out. I know that the arduino doesn't recognize the time, but only much time has passed since the start of the program, but I'd appreciate any suggestions.
Here is the code, I would like the timestamp to be sent out only when the LED is set to high:
#include <NewTone.h>//Use NewTone library to allow frequencies below 31Hz
#include <Bounce2.h>
#define digiOut 12
#define button 4
#define LED 2
Bounce debouncer = Bounce();
long onTime; //Parameters for output characteristics
long offTime;
long frequency;
long repeats;
int inByte = 0; // incoming serial byte
int byteCount;
int repeatCount;
int LabChart = 5;
boolean handshake = false;
boolean stringComplete = false;
boolean hiLo = false; //state of digiout
String onTimeString;
String offTimeString;
String inputString = "";
String lastUpdate;
boolean complete = true; //Has the last sequence finished?
boolean simpleMode = false; //Is the frequency parameter being used
unsigned long onCount;//Timers for output control
unsigned long offCount;
void setup()
{
// start serial port at 9600 bps:
Serial.begin(9600);
pinMode(digiOut, OUTPUT); // digital sensor is on digital pin 2
pinMode(button, INPUT_PULLUP);
pinMode(LED, OUTPUT);
pinMode(LabChart, OUTPUT);
inputString.reserve(300);
debouncer.attach(button);
debouncer.interval(5);
}
void parseIt() {
int onDex = lastUpdate.indexOf('A') + 1; //On Time
int offDex = lastUpdate.indexOf('B') + 1; //Off Time
int freqDex = lastUpdate.indexOf('C') + 1; //Frequency
int repDex = lastUpdate.indexOf('D') + 1; //Sequence Repeats
String onTS = lastUpdate.substring(onDex, onDex + 10);
onTime = onTS.toInt();
String offTS = lastUpdate.substring(offDex, offDex + 10);
offTime = offTS.toInt();
String freqS = lastUpdate.substring(freqDex, freqDex + 10);
frequency = freqS.toInt();
String repS = lastUpdate.substring(repDex, repDex + 10);
repeats = repS.toInt();
Serial.println(onTime);
Serial.println(offTime);
Serial.println(frequency);
Serial.println(repeats);
}
void loop()
{
debouncer.update();
int value = debouncer.read();
if ( value == LOW && complete )
{
complete = false;
if ( frequency < 1 )
{
simpleMode = true;
}
else
{
simpleMode = false;
}
}
laserControl();
if (stringComplete)
{
lastUpdate = inputString;
stringComplete = false;
inputString = "";
delay(5);
parseIt();
}
}
void serialEvent() {
while (Serial.available())
{
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '\n') {
stringComplete = true;
}
}
}
void laserControl(){
if ( simpleMode && !complete )
{
simpleControl();
}
else if (!simpleMode && !complete )
{
freqControl();
}
}
void simpleControl()
{
if ( repeatCount < repeats )
{
digitalWrite(digiOut, HIGH);
digitalWrite(LED, HIGH);
digitalWrite(LabChart, HIGH);
delay(onTime);
digitalWrite(digiOut, LOW);
digitalWrite(LED, LOW);
digitalWrite(LabChart, LOW);
delay(offTime);
repeatCount++;
}
else
{
complete = true;
repeatCount = 0;
}
}
void freqControl()
{
if ( repeatCount < repeats )
{
NewTone(digiOut, frequency, onTime);
digitalWrite(LED, HIGH);
digitalWrite(LabChart, HIGH);
delay(onTime + offTime);
repeatCount++;
}
else
{
complete = true;
repeatCount = 0;
digitalWrite(LED, LOW);
digitalWrite(LabChart, LOW);
}
}
As far as I understand, that returns that number of milliseconds that have passed since the board began running the current program. I want it to send the time of day (e.g. 12:51.532) according to the time of the computer it's running through.
It's plugged into the computer the entire time due to the nature of what we're doing, so I was thinking it could somehow be calibrated to the computer.
Sorry, but this is not a sufficient description. If you expect to maintain that accuracy for very long (i.e. longer than a few hours) you will have to continuously synchronized to a global time reference such as GPS or SNTP. That, or attach a rubidium clock. Which of those sounds good to you?
Even a very good RTC module will be off by many milliseconds after one day...
You could have the computer relay the NTP time to the board, but it seems ridiculous since the computer is also the device that is receiving the time from the Arduino, no? Also there will be latency due to the serial connection. NTP time itself is not accurate to milliseconds due to network delays as well...
Is there any way to have it send out the time of day of the computer it's plugged into? It doesn't matter to me if the computer itself is off, I just need the arduino to send the time the computer has. Does that make sense? Sorry, I don't mean to complicate things.
That is why I'm reaching out to people who are much more knowledgeable than myself and hoping to get some ideas on if and how I can achieve this.
To put it simply - I do not care about the time of day. Whenever the LED is on in the code, a laser, that is attached to the arduino, sends out a pulse. I want to know exactly when that pulse was sent. I am using a different program at the same time that records the time (according to the computer) and other parameters, so I was hoping the arduino can send the time as well so that I can align it with changes in that program.
I am recording different parameters in response to the laser pulse.
The parameters are being measured using a program that I run on the computer. That program also records the time of day. This program and the arduino do not talk to each other, there is no way to have them talk to each other. However, I would like to somehow sync or align the two programs (the laser pulse and this other program). What I care about is the moment in which the LED is HIGH. Since I am running the arduino and the program on a computer, and since this other program already uses the time of day according to the computer, I wanted the arduino to use the time as well. So if my computer is off by three hours, the program will also be off by those exact three hours and the arduino will supposedly also be off by those exact three hours.
As a new user, you are limited in the number of posts you can make in the first 24 hours. I know you think you are providing reams and reams of explanation, but your language omits a lot of the context of the project that would be required to provide technical solutions.
So take a deep breath, imagine that you know absolutely nothing about the project, and fully describe all the hardware and software components, and explain in detail the purpose and method of your project. What is this LED, this laser? What do they do? Why? Is the LED actually the laser, same thing? See my problem?
Unless I missed it, you also glossed over my questions about temporal accuracy. I can almost guarantee you, unless you bear down and provide a detailed, clear description of your need, you will soon leave here empty handed.
This program and the arduino do not talk to each other, there is no way to have them talk to each other.
If there is no connection between Arduino and computer, both of them will need to synch to some global time source(s). For an UNO, that means adding GPS using a PPS output. For the computer, you have a problem because as I mentioned, the NTP time that it uses is only accurate to a few hundred milliseconds.
If there is no connection between Arduino and computer, how will the computer know what is going on with the Arduino? How would it receive data from it?
I have 13 ESP32's and they are running PubSubClient connecting to a MQTT Broker. I run a MQTT Broker on a Raspberry Pi. The Raspberry Pi sends out a once per second time signal to the ESP32's. Around midnight the ESP32's take the time stamp being sent to them by the Raspberry Pi and uses it to reset the ESP32's built in RTC. In this case the 'PC' tells the clients what time it is.