Arduino Real Clock Timer Device - time

hey I have this problem that I have been dealing with for the last few day I have a way around it but I know for a fact that it is really bad way to program and I am really hoping someone can suggest a better way to go about this.
I am trying to get a time from an RTC ( real clock timer) I think that is the acronym anyway ,,,
Its using a DS 1307 IC ,,,,, every time I start and run the program under constant power it boots fine, but the moment I remove the USB cable for around 10 seconds and then reattach it will give me these funny times.
Something like year 2036 and 46 hours and 165 mins really just garbage.
So I read somewhere that these time are just the programs way of saying that there is no connection to the device. That I don't really get because its permanently plugged in but hey thats what it wants.
So here is a basic code that I got from an example that came with the library.
I thought because there is no connection just do a while loop until the device gets connection this works well but it takes like sometimes 10 seconds to boot up.
The RTC has a battery backup connected and with lines SCL to A5 and SDA A4
As I say it work but takes really long to boot up and give me the correct time.
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 RTC;
void setup () {
Serial.begin(57600);
Wire.begin();
RTC.begin();
Serial.println("RTC capturing time!");
while (! RTC.isrunning())
{
Serial.println("RTC is NOT running!");
Wire.begin();
RTC.begin();
}
Serial.println("RTC IS running!");
// following line sets the RTC to the date & time this sketch was compiled
// RTC.adjust(DateTime(__DATE__, __TIME__));
}
void loop () {
DateTime now = RTC.now();
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(' ');
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
Serial.println();
delay(1000);
}
The output looks like this just with A LOT more RTC is NOT running!
RTC is NOT running!
RTC is NOT running!
RTC is NOT running!
RTC is NOT running!
RTC is NOT running!
RTC is NOT running!
RTC is NOT running!
RTC is NOT running!
RTC is NOT running!
RTC is NOT running!
RTC is NOT running!
RTC IS running!
2013/6/11 22:22:0
2013/6/11 22:22:2
2013/6/11 22:22:3
2013/6/11 22:22:4
2013/6/11 22:22:5
and if I was to not include my while loop idea I get really messed up times and dates as I said before until it for some reason comes right by itself.
Please let me know if anyone knows of a better way to fix my problem I am really confused to why this would be occurring.

Try this code...
void setup () {
Serial.begin(57600);
Wire.begin();
RTC.begin();
Serial.println("RTC capturing time!");
while (!RTC.isrunning())
{
// do not really need this, remove after testing
Serial.println("RTC is NOT running!");
delay(10);
}
Serial.println("RTC IS running!");
// following line sets the RTC to the date & time this sketch was compiled
// RTC.adjust(DateTime(__DATE__, __TIME__));
}

You should clarify how you power the RTC when USB is disconnected. First you should check if the battery is actually good. Then you need to ensure that the Arduino notices that the RTC was battery powered. This is because the RTC will shut down I2C completely while battery powered --> I2C must be reinitialized when power is recovered. The point is that your DS1307 library might not account for that.
In doubt you need to analyze the source code of your library AND read the datasheet of your DS1307 chip.
Another thing is that the datasheet says
the device switches from battery to VCC when VCC is greater than VBAT+0.2V and
recognizes inputs when VCC is greater than 1.25 x VBAT
Did you ever measure VBAT and VCC at startup?

Related

Easiest way to get a wired signal into a PC?

for a prototype we need to have a hardware switch (e.g., a momentary pushbutton) trigger the taking of a screenshot on a PC and save it to file. Writing some windows software to take a screenshot and save it is trivial, the slightly trickier part is how to get an electrical signal (we can choose the voltage, and provide power as necessary) to the software. We absolutely want to keep this simple (i.e., no labview or anything) and reliable as possible. I see small module boxes such as this
https://labjack.com/products/u3?gclid=EAIaIQobChMI-MXkjcbB2gIVxVYNCh3C6AODEAQYAiABEgK_OvD_BwE
available, but are there even simpler solutions? I'm thinking of (but haven't taken the time to test) possibly a parallel-port-to-USB converter (which would be similar to the more common RS232-to-USB converter but may allow detection of individual high/lows(just a guess, never worked with a parallel driver from windows)), or something like that. Just querying for ideas before I spend time buying things and testing. Thanks!
This can be easily done with an Arduino Leonardo, Micro, and Due module. This page has an example very similar to your project:
// use this option for OSX:
char ctrlKey = KEY_LEFT_GUI;
// use this option for Windows and Linux:
// char ctrlKey = KEY_LEFT_CTRL;
void setup() {
// make pin 2 an input and turn on the
// pullup resistor so it goes high unless
// connected to ground:
pinMode(2, INPUT_PULLUP);
// initialize control over the keyboard:
Keyboard.begin();
}
void loop() {
while (digitalRead(2) == HIGH) {
// do nothing until pin 2 goes low
delay(500);
}
delay(1000);
// new document:
Keyboard.press(ctrlKey);
Keyboard.press('n');
delay(100);
Keyboard.releaseAll();
// wait for new window to open:
delay(1000);
}

Arduino Uno + ESP8266 12E + Blynk + Relay

I started adventure with arduino and programming 2 months ago.So, I am new in this topics.
Until now I realized few projects including Blynk connected with arduino. The last one was similar to one described in topic but I used the ENC28j60 instead of ESP8266 module and then it worked fine.
The problem started with ESP module.
Short description of project:
The main idea is to control AC light with Blynk App support.
First of all I made a connection according to picture below:
As power source I used the USB phone charger connected with step by voltage converter to get in final the 3.3V source.
I additionally connected the Arduino Uno with relay module like this:
Arduino ====> Relay module
5V ====> VCC
GND ====> GND
Pin 5 ====> IN1
Everythink you can see in pictures below (sorry for quality)
And for now I did almost every step (with so many problems). Here I mean:
1. I checked if arduino is connected with ESP module by serial port -> system report "ready" status.
2. I upload the below (template) Arduino IDE sketch for my project:
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "***";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "***";
char pass[] = "***";
void setup()
{
// Debug console--
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
}
void loop()
{
Blynk.run();
}
And finally I started the New project in Blynk. I set the Arduino uno as a hardware model and WiFi as connection type. Then I added the Button and set the Output to D5.
And now (close to the end of project) I met with a problem. After pushing the connect button (in up-right corner) I receive the information that device is connected (Online). Then when I try to push the button to Active Relay - nothing happens.
Whats more. I tried with different pins on Arduino with the same results. And I don't know why (probably because I have still small knowladge) but when I set the Button output value to D2 - after connection when I push it then the diode on ESP module Turn OFF and Turn ON.
I tried to find solution on this forum and in many other places for last 3 days but without any results. That's why I decided to ask You for help. Do you know what did I wrong or what should I add to project to make connection between the Blynk and relay work correct?
Write if you will need some more or detailed information from my side.
Why are you using both the uno and the esp? You can just use the esp instead of the combo, will make your project less power hungry and smaller. If you wonder about using just the esp, you can use the nodemcu boards (which can be found for less that 4€ per unit in China).
I've some example sketches for this (with temperature and humidity), if you want to take a look at those.
When looking at the pictures and code you have postet, it seems that you have flashed the ESP with a Arduino sketch. This is fine if you would like to activate the relay directly with the ESP (without the Arduino UNO).
Then you just have to connect the relay to the ESP instead of to the Arduino. Unfortunately not all relay boards can operate with the 3.3V logic that the ESP supplies, but maybe you'r lucky.
On the other hand, if you would like to use the Arduino UNO with the ESP as Wi-Fi, then you would have to reflash the ESP with the original AT firmware. Then you could connect it to the Arduino and use a sketch that looks something like this.
#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
char auth[] = "YourAuthToken";
char ssid[] = "YourNetworkName";
char pass[] = "YourPassword";
#include <SoftwareSerial.h>
SoftwareSerial EspSerial(2, 3);
// pin 3 connected to ESP RX and pin 2 connected to ESP TX
// Your ESP8266 baud rate:
#define ESP8266_BAUD 115200
ESP8266 wifi(&EspSerial);
void setup()
{
// Debug console
Serial.begin(9600);
delay(10);
// Set ESP8266 baud rate
EspSerial.begin(ESP8266_BAUD);
delay(10);
Blynk.begin(auth, wifi, ssid, pass);
}
void loop()
{
Blynk.run();
}
And you should remove the connection between RST and GND on the Arduino

How do I read serial data from an arduino uno with batch on windows 10?

I'm trying, more specifically, to hibernate my computer with an infrared remote using arduino. I already have it set up so that the arduino registers the button on said infrared remote, and can then trigger an action. What I'm trying to code on my computer is a batch script that will constantly run and listen for serial data from the arduino. The theory is that when the arduino gets the infrared signal, it will blast a letter through serial, or 'o'. Then, the batch script on the computer will see that, and proceed to hibernate the computer. I have already tried
MODE COM6:9600,N,8,1 >NUL
type COM6 > sample.txt
to test whether or not windows could read the serial data, but the arduino just starting broadcasting a '1' for a tenth of a second every second. Once I ran this, and a text file was generated that was completely blank. Here is the arduino code:
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.write("Hello!");
delay(1000);
}

How to wire a 12V double solenoid to an Arduino?

I have recently been trying to pneumatically actuate a cylinder using a 12 V double solenoid and an Arduino Uno. The solenoid works when tested without code and wiring, however when I try to actuate the cylinder using code, nothing happens. I have a feeling that the way in which I wired everything to the breadboard may be incorrect, so I was wondering if anyone had any tips or good schematics by which I could wire it all together.
The materials I am using are two PNP transistors, two resistors, two diodes, and then the actual solenoid and similar hardware. My code is just a simple LED blink code which can be used to send signals to the solenoids, so I do not believe that is the issue. However, I have attached it underneath just in case.
int solenoid1 = 4;
int solenoid2 = 5;
void setup() {
// put your setup code here, to run once:
pinMode(solenoid1, OUTPUT);
pinMode(solenoid2, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(solenoid1, HIGH);
delay(1000);
digitalWrite(solenoid1, LOW);
digitalWrite(solenoid2, HIGH);
delay(1000);
digitalWrite(solenoid2, LOW);
}
Arduino digital pin puts 5V when HIGH. So your 12V solenoid do not get enough voltage to run. You have to use relay and additional 12V power supply to run your solenoid with Arduino.
The code which I have written is not running when the Arduino gets plugged in
How can you see this?
A solenoid certainly cannot be controlled by 5V * 20mA from an Arduino pin. Further requirements depend on the solenoid you want to use. (Current consumption, free-wheeling diode already integrated?)

quickly toggle RTS manually when RTS_CONTROL_TOGGLE is enabled

On Windows XP, I've got a standard (on the motherboard) RS-232 serial port opened and configured with
dcb.fRtsControl = RTS_CONTROL_TOGGLE;
This works fine for my needs.
Now while there is no RX pending, I sometimes need to briefly toggle the RTS line under the control of my application. I can't just call EscapeCommFunction, as that fails when RTS_CONTROL_TOGGLE is enabled.
I tried to this:
dcb.fRtsControl = RTS_CONTROL_ENABLE; // manual RTS control and turns on RTS
SetCommState( hDevice, &dcb );
dcb.fRtsControl = RTS_CONTROL_TOGGLE; // turns off RTS since there is no TX pending
SetCommState( hDevice, &dcb );
However, this never seems to work. I never see RTS toggle. It's like the device driver notices that I just set RTS and am clearing it now, so it just aborts what I was doing.
If I put a sleep in there:
dcb.fRtsControl = RTS_CONTROL_ENABLE; // turn on RTS
SetCommState( hDevice, &dcb );
Sleep(1);
dcb.fRtsControl = RTS_CONTROL_TOGGLE; // turns off RTS since there is no TX pending
SetCommState( hDevice, &dcb );
Then I see the toggle, but it's extremely variable in length, anywhere from 1 to 15 ms (which is to be expected I guess). I use timeBeginPeriod to setup 1ms scheduling and my thread is already high priority (should I go to real time priority?).
So, is there something I can to do "flush" the RTS change to the hardware so I don't have to do the Sleep? I can busy wait (using QueryPerformanceCounter to wait for the amount of time I want), but I'm concerned that this would still not "flush" the RTS change to the device.
Thanks.
Turns out that the serial port I was using to monitor the RTS line was faulty. It was USB based and was missing the short transitions.
I put an O-scope on it and I can see a 1ms blip when I just toggle RTS on/off as I tried initially.

Resources