I am unable to send serial output data to Ms.Excel sheet.
I am using Proteus 8 professional software, Arduino 1.8.5 IDE with windos 7 OS in a 32bit system. Could anyone please guide me to send the data.
Here is my Ardunio code:
int incomingByte = 0;
unsigned long time;
unsigned long day = 86400000; // 86400000 milliseconds in a day
unsigned long hour = 3600000; // 3600000 milliseconds in an hour
unsigned long minute = 60000; // 60000 milliseconds in a minute
unsigned long second = 1000; // 1000 milliseconds in a second
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}
void loop() {
if (Serial.available() > 0)
{
incomingByte = Serial.read(); // read the incoming byte:
Serial.print("Time: ");
time = millis();
int days = time / day ; //number of days
int hours = (time % day) / hour; //the remainder from
days division (in milliseconds) divided by hours, this gives the full hours
int minutes = ((time % day) % hour) / minute ; //and so on...
int seconds = (((time % day) % hour) % minute) / second;
Serial.print(days,DEC);
Serial.println(hours);
Serial.println(minutes);
Serial.println(seconds);
delay(1000);
}
}
Related
It's my first time learning arduino uno and I do not know what to do
const int ledPin = LED_BUILTIN;
int ledState = LOW;
unsigned long previousMillis = 0;
const long interval = 1000;
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
for(int interval = 500; interval >= 20; interval++)
previousMillis = currentMillis;
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
digitalWrite(ledPin, ledState);
}
}
The delay for every interval the led turns On and Off increases by 1 seconds everytime the loop restarts.
Please make sure you ask your question in the question itself, not the title and also give us some more information about what you're trying to accomplish, what's working and what isn't working so we can help.
If I understand correctly, you want the LED to stay on longer and longer each time it blinks. First cycle 1 second, second cycle 2 seconds, third cycle 3 seconds, etc. You are pretty close, just need some tweaking of your logic.
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) { //Check if LED duration has expired
//Toggle LED
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
interval += 1000; //Increase the interval by 1 second
digitalWrite(ledPin, ledState); //Set the LED
previousMillis = currentMillis; //Set the time of the last transition to now
}
}
I've been trying to create a days counter that starts counting from a unix timestamp. I'm using an arduino Leonardo, an RTC DS 3231 and a 7-segment serial display(by microbot). Here's the display link: Serial Display Link
But I cannot get the display to print ny output. I think I might have messed up with the connections.
I have connected the Vcc and Gnd from the rtc to the 3.3v and gnd of the arduino and the Sda and Scl to the Sda and Scl of the arduino.
For the display I connected the Vcc to the 5V, the gnd to the gnd and the Rx to the digital output 5( Is this correct? I know I have messed up sth here)
Here is the code:
#include <Time.h>
#include <TimeLib.h>
#include <SPI.h>
#include <Wire.h>
#include "Adafruit_LEDBackpack.h"
#include "Adafruit_GFX.h"
#include "RTClib.h"
RTC_DS1307 rtc = RTC_DS1307();
Adafruit_7segment clockDisplay = Adafruit_7segment();
int hours = 0;
int minutes = 0;
int seconds = 0;
unsigned long previousMillis = 0; // will store last millis event time
unsigned long sensorpreviousMillis = 0; // will store last millis event time
unsigned long fiveMinuteInterval = 300000; // interval at which to use event time (milliseconds)
unsigned long postDaysInterval = 7200000 ; //seconds in a day 86400000
#define DISPLAY_ADDRESS 0x70
unsigned long theDateWeGotTogether = 1441843200; //in unixtime
unsigned long days ;
int weeks ;
void setup() {
Serial.begin(115200);
clockDisplay.begin(DISPLAY_ADDRESS);
rtc.begin();
bool setClockTime = !rtc.isrunning();
if (setClockTime) {
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
}
void loop() {
// DateTime now = rtc.now();
days = ((now() - theDateWeGotTogether) / 86400); //86400 is the number of seconds in a day
unsigned long currentMillis = millis();
ShowDaysReading();
time_t t = now();
if(currentMillis - sensorpreviousMillis > fiveMinuteInterval)
{
// save the last time you performed event
sensorpreviousMillis = currentMillis;
DateTime Zeit = rtc.now();
}
}
void ShowDaysReading()
{
days = ((now() - theDateWeGotTogether) / 86400); //86400 number of seconds in a day
weeks = ((now() - theDateWeGotTogether) / (86400 * 7) ); //86400 number of seconds in a day
clockDisplay.print(days);
}
I am working on an Arduino stopwatch, where it needs a start, stop, and reset button. To reset it, I am using a variable called starttime that is updated to equal to millis(), and then take the difference to display time. However, past thirty seconds, the starttime does not update correctly, and the resulting difference between starttime and millis() is equivalent to 65 seconds. Can someone explain why this is happening?
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int start = 8;
const int stp = 9;
const int reset = 10;
int seconds;
int minutes;
int timedif;
int starttime = -1;
int timestall = 0;
bool onoff = true;
void setup()
{
pinMode(start, INPUT);
pinMode(stp, INPUT);
pinMode(reset, INPUT);
lcd.begin(16, 2); //Initialize the 16x2 LCD
lcd.clear(); //Clear any old data displayed on the LCD
Serial.begin(9600);
}
void loop()
{
int bsta = digitalRead(start);//sees if start switch is pressed
int bstp = digitalRead(stp);//records if stop switch is pressed
int bres = digitalRead(reset);//records if reset switch is pressed
lcd.setCursor(0,0);
lcd.print("Stopwatch");//prints stopwatch top row
lcd.setCursor(0,1);
if (starttime == -1) { //if running first time, time dif is initiated
starttime = millis();
}
timedif = (millis() - starttime )/1000 + timestall; //will get difference in terms of seconds
minutes = floor(timedif/60); // divides by sixty, drops decimal
seconds = timedif%60; //gets remainder of divided by 60
lcd.print(minutes);
lcd.print(":");
lcd.print(seconds);
if (seconds < 10) {
lcd.setCursor(3,1);
lcd.print(' ');
}
if (bstp == HIGH) {
onoff = false;
while(onoff == false) {
if (digitalRead(start) == HIGH) {
onoff = true;
}
}
timestall = timedif;
starttime = millis();
}
if (bres == HIGH) {
delay(100);
timestall = 0;
starttime = millis();
timedif = 0;
lcd.clear();
Serial.println("stall:");
Serial.println(timestall);
Serial.println("dif");
Serial.println(timedif);
Serial.println("start");
Serial.println(millis() - starttime);
}
}
You should use long or unsigned long instead if int to declare variables that hold time values.
A int variable can only hold 32,767 millisecond hence the 32 sec. where a long variable can hold 2,147,483,647 millisecond which is something like 48 days. A unsigned long can hold double of that but can not hold a negative value.
I need to pass from microseconds (saved inside a unsigned long long int variable) to its representation as hours, minutes, seconds, milliseconds, that is:
from 47072349659 to 13:04:32.350
I found this conversion from milliseconds, but I don't seem to manage to make it work in my case. Maybe the problem is that the number is too long to be stored in certain variable type? I'm using unsigned long long int for input time and tried int, long, unsigned long long int for outputs.
Here is my C++ code:
unsigned long long int timestamp;
long milliseconds = (long) (timestamp / 1000000) % 1000;
long seconds = (long) ((timestamp / (1000)) % 60);
long minutes = (long) ((timestamp / (60000)) % 60);
long hours = (long) ((timestamp / (3600000)) % 24);
I think your mistake is in your deviders :
long milliseconds = (long) (timestamp / 1000) % 1000;
long seconds = (((long) (timestamp / 1000) - milliseconds)/1000)%60 ;
long minutes = (((((long) (timestamp / 1000) - milliseconds)/1000) - seconds)/60) %60
long hours = ((((((long) (timestamp / 1000) - milliseconds)/1000) - seconds)/60) - minutes)/60
I've seen lots of questions about high precision timers in windows, but what I really need is something that gives me the clock time in windows that's more accurate than the 10-15ms granularity GetLocalTime() offers.
I couldn't find any existing and simple solution so I came up with one of my own, not completely flushed out, but the basic idea works until midnight. Sharing it here so it can be helpful to others.
Store an anchor time when the program starts and use timeGetTime() to get the system uptime in ms (which is granular to less than 1 ms) and adjust the anchortime accordingly.
Code is in the answer.
First time you run it, it gets the time and the tickcount so they're in sync and we have something to measure by. The init section isn't threadsafe and it doesn't wrap over midnight, but that's easily added by following the carry form below....
// this solution is limited to 49+ days of uptime
int timeinitted = 0;
SYSTEMTIME anchortime;
DWORD anchorticks;
void GetAccurateTime(SYSTEMTIME *lt)
{
if (timeinitted == 0)
{ // obviously this init section isn't threadsafe.
// get an anchor time to sync up with system ticks.
GetLocalTime(&anchortime);
anchorticks = timeGetTime();
timeinitted = 1;
}
DWORD now = timeGetTime();
DWORD flyby = now - anchorticks;
// now add flyby to anchortime
memcpy (lt, &anchortime, sizeof(anchortime));
// you can't do the math IN the SYSTEMTIME because everything in it is a WORD (16 bits)
DWORD ms = lt->wMilliseconds + flyby;
DWORD carry = ms / 1000;
lt->wMilliseconds = ms % 1000;
if (carry > 0)
{
DWORD s = lt->wSecond + carry;
carry = s / 60;
lt->wSecond = s % 60;
if (carry > 0)
{
DWORD m = lt->wMinute + carry;
carry = m / 60;
lt->wMinute = m % 60;
if (carry > 0) // won't wrap day correctly.
lt->wHour = (((DWORD)lt->wHour + carry)) % 24;
}
// add day and month and year here if you're so inspired, but remember the 49 day limit
}
}