Hello, i have strange problem, i have arduino and i need to get sensors's values to my pc via serial/usb port. Seems like simple task, i wrote python script which collects data from arduino when arduino sends that. Everything is working for few hours and after that stops, i though my script is bad, so i tried another software - RS232 Data logger, i got the same problem, i though it's because of my windows system, i tried it on different machine, no luck. Sometimes it's stop getting values after 30 minutes, sometimes after 5 hours, but it stops finally and i need to unplug and plug again my cable to start it working again.
should i put some led flashing in my loop where arduino sends signals to Serial port ? i think i am able to do that, just need to put some stuff off my board.
okay, still no luck, it stops from time to time... my sketch is quite big, approx. 1000 lines, so i will post some parts of programme..the beginning of programme:
#include <Elapsed.h> // ========== Could it be the problem of that library ?
#include <SimpleTimer.h>
#include <OneWire.h>
#include <MenuBackend.h> //MenuBackend library - copyright by Alexander Brevig
#include <LiquidCrystal.h> //this library is included in the Arduino IDE
#include <DallasTemperature.h>
#include <Math.h>
#define ONE_WIRE_BUS 3
#define LED 13 // Define the led's pin
int adc_key_val[6] ={
10, 374, 548, 649, 714,762};
int NUM_KEYS = 6;
int adc_key_in;
int key=-1;
.
.
.
void setup()
{
//pinMode (12, OUTPUT);
pinMode(A4, OUTPUT); //akumuliacines voztuvas
pinMode(A1,OUTPUT); // termostatas
pinMode(13, OUTPUT); //zibuks
pinMode (12, OUTPUT); //pavaros uzdarymas
pinMode (A3,OUTPUT); //boilerio voztuvas;
pinMode(A2,OUTPUT); //katilo voztuvas;
pinMode(10,OUTPUT);//ventiliatoriaus pinas;
pinMode(11,OUTPUT);
pinMode(A0,OUTPUT);
//digitalWrite(LED,state);
lcd.clear();
lcd.begin(16, 2);
Serial.begin (9600);
menu.getRoot().add(menu1Item1);
menu1Item1.addRight(menu1Item2).addRight(menu1Item3).addRight(menu1Item4).addRight(menu1Item5).addRight(menu1Item6).addRight(menu1Item7).addRight(menu1Item8).addRight(menu1Item9).addRight(menu1Item10);//addRight(menu1Item11).addRight(menu1Item12);
menu1Item1.add(menuItem1SubItem1).addRight(menuItem1SubItem2).addRight(menuItem1SubItem3).addRight(menuItem1SubItem4).addRight(menuItem1SubItem5);
menu1Item2.add(menuItem2SubItem1).addRight(menuItem2SubItem2);
menu1Item3.add(menuItem3SubItem1).addRight(menuItem3SubItem2).addRight(menuItem3SubItem3);
menu1Item4.add(menuItem4SubItem1).addRight(menuItem4SubItem2);
menu1Item5.add(menuItem5SubItem1).addRight(menuItem5SubItem2);
menu1Item6.add(menuItem6SubItem1).addRight(menuItem6SubItem2);
menu.toRoot();
lcd.setCursor(0,0);
sensors.begin();
} // setup()...
void loop()
{
kp = sensors.getTempC (kpp);
kg = sensors.getTempC (kgg);
sp = sensors.getTempC (spp);
sg = sensors.getTempC (sgg);
ap = sensors.getTempC (app);
tl = sensors.getTempC (tll);
sensors.requestTemperatures();
if (millis() > 2000) { //it's because of reading sensors's values
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) { // that's my serial printing, i guess it is going to fail for some reason from time to time, and i can't get values..
previousMillis = currentMillis;
Serial.print (kp);
Serial.print (" ");
Serial.print (kg);
Serial.print (" ");
Serial.print (ap);
Serial.print (" ");
Serial.print (tl);
Serial.println();
// set the LED with the ledState of the variable:
}
if (kg>startstemp && ikaito) {
startsiurblys =true;
}
if (kg>startstemp && !ikaito)
digitalWrite (11, HIGH);
if (kg < startstemp && !ikaito)
digitalWrite (11,LOW);
if (kg<stopstemp && ap<stopstemp){
startsiurblys =false;
digitalWrite (11,LOW);
}
if (startsiurblys){
digitalWrite (11,HIGH);
//Serial.println ("siurblys veikia");
}
if ((present+1)>sp && startpav && !sure){
// Serial.println ("atidaro pavara");
digitalWrite (close_pin, LOW);
if (digitalRead (open_pin) == HIGH) // if already on // there is elapsed library and code with millis, but i doubt it could be a problem for sending values to serial print
{
// time up?
if (t1.intervalMs () > pulsepav)
{
digitalWrite (open_pin, LOW); // turn off
t1.reset (); // reset timer
} // if period up
}
else // not on
{
// time up?
if (t1.intervalMs () > pausepav)
{
digitalWrite (open_pin, HIGH); // turn off
t1.reset (); // reset timer
} // if period up
}
}
// and there goes whole code....
okay, move the
unsigned long currentMillis, previousMillis, and interval
definitions ahead of void setup. You don't want to declare those new every time thru loop
unsigned long interval = 60000; make this 60000UL, maybe not needed, but the recommendation comes up a lot.
if (millis() > 2000) { //it's because of reading sensors's values
This will always be true after the first 2 seconds. That seems to be intended from the comment?
Don't know what the rest of your code is doing, maybe something that is using all the RAM?
Try adding this somewhere:
okay, it worked longer as usually, after adding UL. ~11 hours without stopping.. http://zuviaganys.mintis.in/MyFCPHPClassCharts/First/jox.php you can see there a graph and time on X axis. Serialprintln (freeMemory()); didn't worked , so i found a library on the internet (#include <MemoryFree.h>) and copied this code to get free memory
and it prints all the time 455. Actually i don't know if it works well, and if i am getting right free memory value. But when serial reading fails, programme works still fine without any troubles (so i doubt it's memory problem). I am stuck, and don't know what to do, strange things goes there, i guess. Waiting any suggests from you guys, i really appreciate it
"But when serial reading fails, programme works still fine without any troubles "
Is it possible the receiving end is the problem then?
Can you put something in this section
if(currentMillis - previousMillis > interval) { // that's my serial printing, i guess it is going to fail for some reason from time to time, and i can't get values..
previousMillis = currentMillis;
Serial.print (kp);
Serial.print (" ");
Serial.print (kg);
Serial.print (" ");
Serial.print (ap);
Serial.print (" ");
Serial.print (tl);
Serial.println();
// set the LED with the ledState of the variable:
}
to make an LED flash or something & conrfirm this section is actually being run?
Have you noticed a longer ON duration of the TX LED on the Arduino board during the period when it should be sending serial data?
I have the Uno and I also have the same exact problem - after 5 hours or so no more serial data. Yet the TX LED still comes on when it should (albeit the duration is MUCH longer). I also have an LCD connected to the Uno - that continues to update so I'm certain the issue is not my program.
When this happens RESET does not fix it - I have to physical disconnect the USB cable and reattach to reconnect again. This would seem to indicate an issue with the Anduino USB driver or other PC conflict.
CrossRoads - i will try to check it with led state, but later, can't do it right now, there would be an answer if all my millis/previous millis cycle fails or my usb transfering. finkjsc - yes, i do need to plug and unplug cable, and no, i didn't solve that yet. Actually i am thinking if it's related with usb and drivers and windows machine, i could try to do that in unix like system, maybe there is a problem.. Thanks. =)
Problem is not with my sketch, because led is change state and nothing backs to pc, so i will change usb cable, if it won't help, i will try that in linux
It fails even in linux with new cable, i think maybe i will print in serial without any millis() but always, maybe it somehow timing out or so, btw i am using Putty to gather all information from serial.
I am experiencing the same problem of failure of Serial after a varying amount of time. I use an Arduino 2009 to write Serial data and read it in using Matlab R2008b. Using USB and a simulated COM-port. I tried all kinds of things like dividing the message into shorter parts using both the standard Serial object, and the arduino object (Arduino Playground - Matlab -- which creates a serial object anyway).
In my case, however, after a certain period of no data, e.g., 5-30 minutes, it starts working again! Do you find that, too?
Did you also use USB? Or an actual COM port?
Then, I thought I should reset the Arduino through a serial command when I notice in Matlab that serial fails. But I didn't have time to make a proper setup using a 555 to do the reset (http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1242847196) - and after reading this whole thread it seems resetting wouldn't have been enough to bring serial comm back to life, anyway.
"Solution":
In my case, I could live with a longer period without data and simply decided to stop checking for serial input for 10 minutes as soon as the serial fails. This avoids a slowing down of my Matlab program due to the timeout period used in trying to read serial data when it is not there. After 10 minutes I let Matlab try again, and so on until it gets something. This works for me, but will not for most applications.
Possible root cause?
I found that this is a problem that happens with other serial comm (not Arduino) as well (like http://www.mathkb.com/Uwe/Forum.aspx/matlab/110479/Failure-to-read-serial-port-after-5000-asynch-reads) which almost makes me think (sorry if this is stupid - I don't know the first thing about computers): is this something to do with the serial port (buffer?) as it is handled by Windows? Or the USB port driver or something?
Does anybody know something about this stuff?
Thanks for your response. Actually i found out that the problem is based on electrical noises, interesting is that, why arduino and serial port is so sensitive ?