I am trying to use the arduino yun to output the number of parts a machine has produced to a google spreadsheet. With the setup I have, I am able to output data to the spreadsheet temporarily. However, after some time, the yun stops working. The red LED I have on to indicate the code is running turns off, and I can no longer see the arduino in the port list. A reset of the 32u4 chip causes the LED to turn back on, indicating the code is running, but the board still does not appear in the port menu.
I have tested it at our workshop and it has run for 7-12 hours with no problems at all. It is only when we take it on to the production floor that we start experiencing these problems. Does anyone have any idea what the issue could be? Here is the most relevant part of the code:
#include <elapsedMillis.h>
#include <Process.h>
#include <Bridge.h>
#include "TimeLib.h"
// On Arduino: 0 - 1023 maps to 0 - 5 voltsf
#define VOLTAGE_MAX 5.0
#define VOLTAGE_MAXCOUNTS 1023.0
unsigned int buttonCount = 0;
float voltage = 0;
elapsedMillis timeSinceLastCycle = 0;
elapsedMillis transmitData = 0;
int pressFlag = 0;
Process date;
int hours, minutes, seconds;
int lastSecond = -1;
Process sendData;
String printDate() {
// String currTime = String(hours) + ":" + String(minutes) + ":" + String(seconds);
if (lastSecond != seconds) { // if a second has passed
// print the time:
if (hours <= 9) {
Console.print("0"); // adjust for 0-9
}
Console.print(hours);
Console.print(":");
if (minutes <= 9) {
Console.print("0"); // adjust for 0-9
}
Console.print(minutes);
Console.print(":");
if (seconds <= 9) {
Console.print("0"); // adjust for 0-9
}
Console.println(seconds);
// restart the date process:
if (!date.running()) {
date.begin("date");
date.addParameter("+%T");
date.run();
}
}
//if there's a result from the date process, parse it:
while (date.available() > 0) {
// get the result of the date process (should be hh:mm:ss):
String timeString = date.readString();
// find the colons:
int firstColon = timeString.indexOf(":");
int secondColon = timeString.lastIndexOf(":");
// get the substrings for hour, minute second:
String hourString = timeString.substring(0, firstColon);
String minString = timeString.substring(firstColon + 1, secondColon);
String secString = timeString.substring(secondColon + 1);
// convert to ints,saving the previous second:
hours = hourString.toInt();
minutes = minString.toInt();
lastSecond = seconds; // save to do a time comparison
seconds = secString.toInt();
String currTime = hourString + ":" + minString + ":" + String(seconds);
return currTime;
}
}
void checkVoltage() {
int sensorValue = analogRead(A0);
voltage = sensorValue * (VOLTAGE_MAX / VOLTAGE_MAXCOUNTS);
Console.println(voltage);
delay(50);
if (voltage >= 4.9 && pressFlag == 0) {
Console.println("Delaying");
sensorValue = analogRead(A0);
voltage = sensorValue * (VOLTAGE_MAX / VOLTAGE_MAXCOUNTS);
if (pressFlag == 0 && voltage >= 4.9) {
unsigned long int intCycleTime = timeSinceLastCycle;
timeSinceLastCycle = 0;
digitalWrite(LED_BUILTIN, LOW); // turn the LED on (HIGH is the voltage level)
printDate();
String pressTime = printDate();
Console.print("PressTime is ");
Console.println(pressTime);
buttonCount++;
Console.println(buttonCount);
pressFlag = 1;
String part1 = "curl -X POST -H \"Content-Type: application/json\" -d '{\"value1\":\"";
String timeString = pressTime;
Console.print(timeString + " seconds");
String part2 = "\",\"value2\":\"";
String numParts = String(buttonCount);
String part3 = "\",\"value3\":\"";
String strCycleTime = String(intCycleTime / 1000); // + " seconds";
String part4 = "\"}' https://maker.ifttt.com/trigger/arduino2Request/with/key/gL8YmxeaUChOMJvmwpdXp -k";
//curl -X POST -H "Content-Type: application/json" -d '{"value1":"1","value2":"2","value3":"3"}' https://maker.ifttt.com/trigger/arduino2Request/with/key/gL8YmxeaUChOMJvmwpdXp
String curlString = part1 + timeString + part2 + numParts + part3 + strCycleTime + part4;
// The curl string sends data to oue excel spreadhsheet using the IFTTT web service
sendData.runShellCommandAsynchronously(curlString);
elapsedMillis breakTimer = 0;
/*while(sendData.running()){
if(breakTimer > 5*1000){
break;
}
} */
Console.print("Data Available: "); // A value of 32 indicates a successful transmission of data, 0 also works if run asynchronously.
Console.println(sendData.available());
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
}
}
else if (voltage < 2.5) {
pressFlag = 0;
}
}
void setup() {
Bridge.begin();
Console.begin();
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
// run an initial date process. Should return:
// hh:mm:ss :
if (!date.running()) {
date.begin("date");
date.addParameter("+%T");
date.run();
}
}
void loop() {
checkVoltage();
if ((timeSinceLastCycle > 300000) && (transmitData > 300000)) { // If 5 minutes have elapsed without a part being produced, output that the arduino is transmitting even if not part is available
sendData.runShellCommand("curl -X POST -H \"Content-Type: application/json\" -d '{\"value1\":\"1\",\"value2\":\"Arduino Transmitting\"}' https://maker.ifttt.com/trigger/transmitData/with/key/gKRo-zSur5rj6rD5rviCaV2RHI5g56Dy0Vc0S_XJ-oO -k");
transmitData = 0;
}
}
UPDATE: I added a series of print statements to the checkVoltage function. The code above has been updated to reflect this. I found that it hung when trying to use sendData.runShellCommandAsynchronously. The output looks like
1.58
1.54
5.00
Delaying
PressTime is n" -d
3
Could the network connection cutting out when trying to runShellCommand cause this issue?
So after following user3629249's solution, this issue appears to be resolved. Have now had multiple weeks of error free runtime. user3629249, if you want to repost your advice as an answer, I will accept it. Thank you to user3629249 and Patrick Trentin for your help!
My update ProcessDate function now contains:
if(date.available() >0){
while (date.available() > 0) {
// get the result of the date process (should be hh:mm:ss):
String timeString = date.readString();
// find the colons:
int firstColon = timeString.indexOf(":");
int secondColon = timeString.lastIndexOf(":");
// get the substrings for hour, minute second:
String hourString = timeString.substring(0, firstColon);
String minString = timeString.substring(firstColon + 1, secondColon);
String secString = timeString.substring(secondColon + 1);
// convert to ints,saving the previous second:
lastSecond = seconds; // save to do a time comparison
lastMinute = minutes;
lastHour = hours;
hours = hourString.toInt();
minutes = minString.toInt();
seconds = secString.toInt();
String currTime = hourString + ":" + minString + ":" + String(seconds);
return currTime;
}
else{
String currTime = ""; // If there is no result from the date process, return nothing. Should not happen.
return currTime;
}
}
Related
I have a problem to well understand what'is happening here.
I'm tring to activate a relay base on time with the user selected schedule.
The problem is when the stop time is after midnight and the start if before.
My function to check if it is the moment to toggle is:
bool lightsOn = false;
void checkOnOff() {
//Check time for ON/OFF the ligth
struct tm *curTime = getTime();
unsigned int now = curTime->tm_hour * 100 + curTime->tm_min;
unsigned int start = config.ligths_on_time.tm_hour * 100 + config.ligths_on_time.tm_min;
unsigned int stop = config.ligths_off_time.tm_hour * 100 + config.ligths_off_time.tm_min;
if ( (now >= start || now < stop) && (now <= stop) ) { //<-- this line is hard to have it in mind
if(!lightsOn) enableLights();
} else {
if(lightsOn) disableLights();
}
}
void enableLights() { //the same for disableLights() but with false, reverse HIGH/LOW and 0
lightsOn = true;
digitalWrite(EXTRA_LED_PIN, HIGH);
digitalWrite(ON_OFF_RELAY_PIN, HIGH);
sendLightsOnOffToServer(1);
Serial.println(F("Lights enabled."));
}
struct tm *getTime() {
//if (NTP_TIME_SETTED) {
time_t now = time(nullptr);
//gmtime_r(&now, &timeinfo); //utc
return localtime(&now);
//}
}
Is someone can explain to me how to do that ?
Adjust the stop time by 24 hours if it is smaller than start
Adjust the now time by 24 hours if the stop time is tomorrow, and the ON_OFF state is ON, and now < start.
Re-order your logic to use the ON_OFF state to determine whether to check for start or stop
#define HOUR 100
#define DAY (HOUR * 24)
#define ON true
#define OFF false
unsigned int now = curTime->tm_hour * HOUR +
curTime->tm_min ;
unsigned int start = config.ligths_on_time.tm_hour * HOUR +
config.ligths_on_time.tm_min;
unsigned int stop = config.ligths_off_time.tm_hour * HOUR +
config.ligths_off_time.tm_min;
// Fix-up stop and now time to account for stop in next day
if( stop < start)
{
stop += DAY ;
if( ON_OFF == ON && now < start )
{
now += DAY ;
}
}
// If OFF, check for start condition
if( ON_OFF == OFF && now >= start )
{
enableLight() ;
}
// ... otherwise if ON, check for stop condition
else if( ON_OFF == ON && now >= stop )
{
disableLight() ;
}
everyone, I am using, P10 Dot Matrix Display with Arduino Uno. I am using P10 Library from this link. P10_LED and I need to display the one-hour countdown on the display module. The given library uses TimerOne library. So for countdown i am using MsTimer2 library which usese timer2 of arduino.
When I individually run both of the libraries, my scrolling on the display is perfect and my timer library also generates a pure 1sec interrupt. Now what I did is the added both the library in my project and I am doing the countdown. But now suddenly my MsTimer2 doesn't generate pure 1sec.
Here is the code.
#include <MsTimer2.h>
#include <TimerOne.h>
#include"SPI.h"
#include <ledP10.h>
LedP10 myled;
uint8_t minute = 0, second = 0, hour = 1;
volatile bool xIsCountDone = false;
volatile bool xIsInterruptOcuured = false;
char time_buff[100];
void setup()
{
Serial.begin(9600);
myled.init(3,4,8,9 ,3);
sprintf((char*)time_buff, " %d%d:%d%d:%d%d", (hour/10), (hour%10),(minute/10), (minute%10),(second/10), (second%10));
Serial.println((char*)time_buff);
myled.showmsg_single_static((char*)time_buff, 0);
xIsInterruptOcuured = false;
//myled.showmsg_single_scroll("this is single led test",2,8,0);
MsTimer2::set(1000, count);
MsTimer2::start();
}
void loop() {
if (xIsInterruptOcuured == true)
{
sprintf((char*)time_buff, " %d%d:%d%d:%d%d", (hour/10), (hour%10),(minute/10), (minute%10),(second/10), (second%10));
Serial.println((char*)time_buff);
myled.showmsg_single_static((char*)time_buff, 0);
xIsInterruptOcuured = false;
}
}
void count(){
second--;
if (second <= 0 || second > 59)
{
second = 59;
minute--;
if (minute <= 0 || minute > 59)
{
minute = 59;
hour--;
if (hour <= 0 || hour > 12)
{
xIsCountDone =true;
}
}
}
Serial.println(millis());
xIsInterruptOcuured = true;
}
In the interrupt routine, I am printing millis() to see at after how many ms the interrupt occurs. The results are something like this.
15:33:02.684 -> 1199
15:33:04.371 -> 2396
15:33:06.059 -> 3592
15:33:07.746 -> 4783
15:33:09.434 -> 5986
15:33:11.121 -> 7181
15:33:12.855 -> 8379
15:33:14.543 -> 9578
15:33:16.230 -> 10768
15:33:17.918 -> 11974
15:33:19.605 -> 13168
15:33:21.292 -> 14365
15:33:22.980 -> 15562
15:33:24.667 -> 16751
15:33:26.402 -> 17955
When I use only MsTimer2 library the results are something like this.
15:37:21.241 -> 998
15:37:22.226 -> 1998
15:37:23.257 -> 2998
15:37:24.241 -> 3998
15:37:25.226 -> 4998
15:37:26.257 -> 5998
15:37:27.241 -> 6998
15:37:28.225 -> 7998
15:37:29.257 -> 8998
15:37:30.241 -> 9998
15:37:31.225 -> 10998
15:37:32.256 -> 11998
15:37:33.241 -> 12998
15:37:34.225 -> 13998
15:37:35.256 -> 14998
My guess, it's happening because of the TimerOne library but I couldn't find the solution. In ledP10.cpp there is a callback method for timer1 and it contains loops and may line of code. But is timer1 interrupts priority is higher than timer2? But according to the ATmega328p datasheet, the vector no. for Timer2 is less than Timer1. Doesn't that mean Timer2 has a higher priority? My ultimate goal is to do the one-hour countdown. Any help with this problem or any additional information i am missing which will be useful or any other solution other than using timer2 interrupt will be appreciated.
Regards.
EDIT
Here is the code I used with millis() and gave me around 12min difference.
uint8_t new_buff[100];
unsigned long startMillis; //some global variables available anywhere in the program
unsigned long currentMillis;
const unsigned long period = 1000; //the value is a number of milliseconds
uint8_t minute = 0, second = 0, hour = 1;
char time_buff[100];
void setup()
{
myled.init(3,4,8,9,3);
Serial.begin(9600);
sprintf((char*)time_buff, " %d%d:%d%d:%d%d", (hour/10), (hour%10),(minute/10), (minute%10),(second/10), (second%10));
//Serial.println((char*)time_buff);
myled.showmsg_single_static((char*)time_buff, 0);
startMillis = millis();
}
void loop() {
currentMillis = millis(); //get the current "time" (actually the number of milliseconds since the program started)
if (currentMillis - startMillis >= period) //test whether the period has elapsed
{
Serial.println(millis());
second--;
startMillis = currentMillis; //IMPORTANT to save the start time of the current LED state.
if (second <=0 || second > 59) {
second = 59;
minute--;
if (minute <=0 || minute > 59) {
minute = 59;
hour--;
if (hour <= 0 || hour > 12) {
hour = 0;
}
}
}
sprintf((char*)time_buff, " %d%d:%d%d:%d%d", (hour/10), (hour%10),(minute/10), (minute%10),(second/10), (second%10));
myled.showmsg_single_static((char*)time_buff, 0);
startMillis = currentMillis;
}
}
This answer targets your example using millis(). You can avoid accumulating errors over time, by not setting the next update relative to the current time. Rather just increment it by one second each time. This way, it does not matter if your main loop gets blocked by an interrupt for some milliseconds.
Also note that you don't have to save hours, minutes and seconds separately, you can just calculate them:
unsigned long nextMillis = 1000;
unsigned long targetTime = 1 * 60 * 60 * 1000; // 1 hour in milliseconds
void setup(){
myled.init(3,4,8,9,3);
Serial.begin(9600);
updateMyLed(0);
}
void updateMyLed( unsigned long elapsedTime ){
char buffer[100];
unsigned long timeLeftInSeconds = (targetTime - elapsedTime) / 1000;
uint8_t hour = timeLeftInSeconds / 3600;
timeLeftInSeconds -= hour * 3600;
uint8_t minute = timeLeftInSeconds / 60;
uint8_t second = timeLeftInSeconds - (minute * 60);
sprintf((char*)buffer, " %d%d:%d%d:%d%d", (hour/10), (hour%10), (minute/10), (minute%10), (second/10), (second%10));
myled.showmsg_single_static((char*)buffer, 0);
}
void loop() {
if( millis() >= nextMillis ){
updateMyLed(nextMillis);
nextMillis += 1000;
}
}
In the Arduino world, some libraries are disabling the interrupts.
This happens with all WS2812 LEDS and also yours. Without disabling interrupts, there would be a timing problem with the external devices.
So, you should never use interrupts or library with interrupts, if you use another one who will disable the interrupts.
Do you want use the P10 library? You can, but do not use interrupts in your code. Do not add other libraries like IR_remote, since it will not work correctly.
Back to your problem, just update your timer in the loop. And do not wait until the a second is over to update your time by 1 second! This will always be more than 1second.
You can for example convert the millis to seconds seconds = millis() / 1000;.
Your loop could be so:
void loop() {
currentSeconds = millis() / 1000; //get the current "time" (actually the number of milliseconds since the program started)
if (currentSeconds != savedSeconds) //test whether the period has elapsed
{
savedSeconds = currentSeconds;
Serial.println(millis());
second--;
if (second <=0 || second > 59) {
second = 59;
minute--;
if (minute <=0 || minute > 59) {
minute = 59;
hour--;
if (hour <= 0 || hour > 12) {
hour = 0;
}
}
}
sprintf((char*)time_buff, " %d%d:%d%d:%d%d", (hour/10), (hour%10),(minute/10), (minute%10),(second/10), (second%10));
myled.showmsg_single_static((char*)time_buff, 0);
}
}
i am currently working in a ardunio alarm clock project without and rtc for school. I have a problem with my hour variable not increasing and my conditionals that checks it.
This code mostly works i can set time in hr and minutes but when the timer reaches 59 minutes the hour doesnt increase by 1. The second code that has been posted is the bit where the problem lies. Any tips/help would be appreciated.
#include <Time.h>
#include <TimeLib.h>
#include <Wire.h>
#include <Adafruit_RGBLCDShield.h>
#include <utility/Adafruit_MCP23017.h>
#include <EEPROM.h>
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();
const int numofMenus = 4;
int currentMenu = 0;
String menu[4][2] = {{"Set hour", "hr"}, {"Set minutes", "min"}, {"Set alarm", "o"}, {"Current Time", "ha"}};
int parameters[24];
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
lcd.noBlink();
}
void loop() {
inputAction();
if (currentMenu == 0)
{
printScreen();
}
if (currentMenu == 1)
{
printScreen();
}
if (currentMenu == 2)
{
printScreen();
}
if (currentMenu == 3)
{
int hr = parameters[0];
int mn = minute(parameters[1]);
int sec = second();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Time: ");
if (parameters[1] >= 0 && parameters[1] <= 59 && sec < 59) {
parameters[0];
printtime(parameters[0]);
}
else if (parameters[1] == 59 && second() == 59) {
if (parameters[0]>=0 && parameters[0]<24){
parameters[0];
printtime(parameters[0]++);
//delay(1000);}
/*else if (parameters[0] == 24){
parameters[0]= 0;
printtime(parameters[0]);
}*/
}
}
lcd.print(":");
if (sec >= 0 && sec < 59) {
printtime(parameters[1]);
}
else if (sec == 59) {
parameters[1];
printtime(parameters[1]++);
delay(1000);
}
lcd.print(":");
printtime(sec);
/* if (sec == 60) {
mn+1;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Time: ");
printtime(hr);
lcd.print(":");
printtime(mn);
lcd.print(":");
printtime(sec);
}
if (mn == 60) {
hr+1;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Time: ");
printtime(hr);
lcd.print(":");
printtime(mn);
lcd.print(":");
printtime(sec);
}*/
delay (100);
}
}
void inputAction() {
uint8_t buttons = lcd.readButtons();
if (buttons & BUTTON_UP) {
if (currentMenu == 0) {
currentMenu = numofMenus - 1;
} else {
currentMenu--;
}
} else if (buttons & BUTTON_DOWN) {
if (currentMenu == numofMenus - 1) {
currentMenu = 0;
} else {
currentMenu++;
}
} else if (buttons & BUTTON_RIGHT) {
parameters[currentMenu]++;
} else if (buttons & BUTTON_LEFT) {
parameters[currentMenu]--;
}
}
void printScreen() {
lcd.clear();
lcd.print(menu[currentMenu][0]);
lcd.setCursor(0, 1);
lcd.print(parameters[currentMenu]);
lcd.print(" ");
lcd.print(menu[currentMenu][1]);
delay(100);
}
void printtime(int data) {
if (data >= 0 && data < 10) {
lcd.print('0');
}
lcd.print(data);
}
I don't think it is a good strategy to throw all those delays in the code. If you want to work with an RTC, you need to be mindful of those delays as they will affect the functionality of your program. If you don't account for delay properly, they will accumulate as errors. An error of 100 msecs will be seen as error in seconds easily within days.
I did not go through your code because it is hard to understand. I propose you have a look at this code which should be placed in your loop() function:
delay(100);
parameters = parameters + 100;
long rtcSeconds = parameters / 1000;
int seconds = (int) (rtcSeconds % 60); // Range 0 . 59 units: Secs
int minutes = (int)(((rtcSeconds - seconds) / 60) % 60); // Range 0 . 59 units: Mins
int hours = (int) (((rtcSeconds - minutes*60 - seconds) / (3600)) % 24); // Range 0 . 23 units: Hrs
I am using your parameters[0] field to keep track of the time in your syste, in milliseconds units. The field increases every 100 msecs as you see. You can recover the different time values using a combination of the modulus (%) and division operations. Now, I know you need to increase the time in your program. To do so, it is easy:
If you want to increase one second, do this: parameters[0] += 1000;
If you want to increase one minute, do this: parameters[0] += 1000 * 60;
If you want to increase one hour, do this: parameters[0] += 1000 * 3600;
Now, you might need to work with long variables as int have a limited range. You also need to watch for the time roll over when you change your time. If you are decreasing the time, you need to watch pramaters[0] doesn't become negative. In case it does, you need to add 3600 * 24 * 1000 msecs to force it jump one day ahead (3600 * 24 is one day in seconds). I hope you understand this is not a solution to your problem but an alternative approach using a little bit of a simpler code structure.
I am currently trying to create a program where the user gives two values (times, hh:mm:ss) and gets the difference between the two times. This works, if one would only use 12h formats; however, using the 24h format is a must.
My current time struct looks like the following:
typedef struct time {
int hours;
int minutes;
int seconds;
} time;
And my current function to calculate the difference looks like this:
time calculateTimeDiff(time time1, time time2) {
time timeResult;
timeResult.hours = time1.hours - time2.hours;
if(time1.minutes != 00 && time2.minutes != 00) {
timeResult.minutes = time1.minutes - time2.minutes;
}
else {
timeResult.minutes = 00;
}
if(time1.seconds != 00 && time2.seconds != 00) {
timeResult.seconds = time1.seconds - time2.seconds;
}
else {
timeResult.seconds = 00;
}
while(timeResult.seconds > 60) {
timeResult.seconds -= 60;
timeResult.minutes += 1;
}
while(timeResult.minutes > 60) {
timeResult.minutes -= 60;
timeResult.hours += 1;
}
return timeResult;
}
My attempts to support the 24h format have been to add 12 hours to the time if the hours "exceed" the 12 hour format, and to divide the time by two (haven't been far from complete shots in the dark, just to see what works and what wouldn't work). However, this has only resulted in getting incorrect results.
Any and all answers appreciated!
How to get the time difference between two times (24h format)
Although code could use many if-then-else's as in OP's code, it would be simple to convert the h:m:s time into seconds, subtract, and convert back to h:m:s. So I recommend a re-write:
typedef struct time {
int hours;
int minutes;
int seconds;
} time;
long time_sec(time t) {
return (t.hours * 60L + t.minutes)*60 + t.seconds;
}
time sec_time(long s) {
time t;
t.hours = s / 3600;
s %= 3600;
t.minutes = s / 60;
t.seconds = s %= 60;
return t;
}
time calculateTimeDiff(time time1, time time2) {
long t1 = time_sec(time1);
long t2 = time_sec(time2);
long diff = t1 - t2;
return sec_time(diff);
}
#include <stdio.h>
void test(time t1, time t2) {
printf("t1: %3d:%3d:%3d, ", t1.hours, t1. minutes, t1.seconds);
printf("t2: %3d:%3d:%3d, ", t2.hours, t2. minutes, t2.seconds);
time t3 = calculateTimeDiff(t1, t2);
printf("t1-t2: %3d:%3d:%3d, ", t3.hours, t3. minutes, t3.seconds);
t3 = calculateTimeDiff(t2, t1);
printf("t2-t1: %3d:%3d:%3d\n", t3.hours, t3. minutes, t3.seconds);
}
int main(void) {
test((time){14,00,00}, (time){13,00,00});
test((time){22,00,00}, (time){04,00,00});
}
Output
t1: 14: 0: 0, t2: 13: 0: 0, t1-t2: 1: 0: 0, t2-t1: -1: 0: 0
t1: 22: 0: 0, t2: 4: 0: 0, t1-t2: 18: 0: 0, t2-t1: -18: 0: 0
Note that the difference may result in negative values for the members of time returned in calculateTimeDiff().
You can change to time structure instead of both start and end time integer value
#include<iostream>
using namespace std;
void difftime(int startTime, int iRecordEndTime)
{
int duration_min = 0;
cout<<"iRecordEndTime = "<<iRecordEndTime<<endl;
cout<<"startTime = "<<startTime<<endl;
if ( (iRecordEndTime/100 < startTime/100) || ((iRecordEndTime/100 == startTime/100) && (iRecordEndTime%100 <= startTime%100)) )
{
duration_min = ((iRecordEndTime / 100 + 24)*60 + iRecordEndTime%100) - ((startTime/100)*60 + startTime%100);
}
else
{
duration_min = (iRecordEndTime / 100 - startTime/100)*60 + (iRecordEndTime%100 - startTime%100);
}
cout<<"duration_min = "<<duration_min<<endl;
}
int main()
{
cout<<"enter the start time Hour:Minutes xxxx"<<endl;
int startTime{0};
cin>>startTime;
cout<<"enter the End time Hour:Minutes xxxx"<<endl;
int iRecordEndTime{0};
cin>>iRecordEndTime;
difftime(startTime, iRecordEndTime );
}
Output
enter the start time Hour:Minutes xxxx\
2300\
enter the End time Hour:Minutes xxxx\
0000\
iRecordEndTime = 0\
startTime = 2300\
duration_min = 60
I want to run my Arduino for a specific amount of time (say 60 seconds) and collect
data from the analog pin with a specific sampling rate (say four samples a second).
I got the code working in matlab... but I want to use the arduino environment.
Please help me convert it.
a_pin = 0;
fs = 4; % sampling frequency (samplings per second)
mt = 20; % time for measurements
ind = 1;
nind = 1;
last_beep = 0;
tic;
while toc < mt
time(ind) = toc;
v(ind) = a.analogRead(a_pin);
% wait for appropriate time for next measurement
while( nind == ind )
nind = floor(toc*fs) + 1;
end
ind = nind;
end
Ok this is what i have so far in my sketch. Would this measure for 10 seconds taking
readings every 5?
int sensePin = 0;
unsigned long starttime = 0;
unsigned long endtime = 0;
int i = 0;
int n;
const int sizeofv = 50;
int v[sizeofv];
void setup(){
pinMode(sensePin, INPUT);
Serial.begin(9600);
}
void loop() {
starttime = millis();
endtime = starttime;
while ((endtime - starttime) <= 10000) // do this loop for up to 1000mS
{
i = i + 1;
v[i] = analogRead(sensePin);
endtime = millis();
delay(5000);
}
for(n=0; n < sizeofv; n++)
{
Serial.print(v[n]);
Serial.print('\n');
}
while(1) { }
}
I recommend using SimpleTimer library, it does all the work for you and is road proven.
There's one bug in your code, you're incrementing i prior to assigning the first value. So when you'll loop over the v[] array, you'll end up printing v[0] which will have an undefined value, and you'll never print the last recorded value of v (if you did not end up overflow the buffer and write outside its boundaries).
There's one glitch with your code, it's that you make a condition that you need to end it at 10000, and you make two delays of 5000. Theoretically it looks ok, but in practice, you may get a surprise having only one value. That's because code takes time to execute, and an analogRead() is particularly slow.
I think using loops within the loop() function make things more complicated, and add close to no flexibility. So here you have another take you could try, to use the loop() only by tracking states of the loop, based on some simple calculations:
#define TIME_WINDOW 10000
#define NB_STEPS 2
void setup() {
// ...
endtime = millis();
}
bool over = false;
void loop() {
if (!over) {
curtime = endtime - millis();
// get in this block for up to 1000mS
if (curtime <= TIME_WINDOW) {
v[i] = analogRead(sensePin);
// increments only once when 5000 has been reached.
if ((curtime / (TIME_WINDOW/NB_STEPS)) != i)
i = (curtime / (TIME_WINDOW/NB_STEPS));
} else {
for(n=0; n < NB_STEPS; n++)
Serial.println(v[n]);
over = true;
}
}
}
I did not test my code, so it may fail or have typos, but the idea is the following:
we define the time window and the number of measure steps
while the time window is not over:
store the measurement in the current array at current index
if integer division of spent time over time_window is different from current index
store the new value into the current index
e.g.: i = 4000/(10000/2) <=> i = 4000/5000 <=> i = 0 and i = 6000/(10000/2) <=> i = 1
when the time window is over:
print out each result
set over to true thus we're not entering the loop again
But a simpler (non flexible) solution for you would be:
void setup() {
int value1, value2, value3, value4;
value1 = analogRead(sensePin);
delay(2500);
value2 = analogRead(sensePin);
delay(2500);
value3 = analogRead(sensePin);
delay(2500);
value4 = analogRead(sensePin);
Serial.println("first value: %d", value1);
Serial.println("second value: %d", value2);
Serial.println("third value: %d", value3);
Serial.println("fourth value: %d", value4);
}
and there you got your solution!
HTH