Pages: [1]   Go Down
Author Topic: Arduino Pro Sends Data to the serial port for only a limited amount of time...  (Read 591 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Can I get it to send data continually while it is connected?
Thanks for the help!
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 638
Posts: 50304
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Can I get it to send data continually while it is connected?
Yes.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How?
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 638
Posts: 50304
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

We can play one word question and answer games all day, or you can post some code, and define why the Arduino Pro quits sending or the whatever quits receiving.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

const int sensor1 = A0;                   // pin that the sensor is attached to
const double offSetCorrection = .170;


void setup() {
  Serial.begin(9600);//Sets up COM port
//  Serial.println("Power Monitering System!");

}

void loop() {

  int endTime = millis() + 21.66;

  double sensor1Sum = 0;
  int count = 0;

  while (endTime  > millis()){
      int value = analogRead(sensor1);
      double sensedVoltage = value * .0049;
      double current = ((sensedVoltage-2.5) / .1);
      sensor1Sum = (sensor1Sum + (current * current));
      count++;
        }
       
  double RMS = sqrt((sensor1Sum/count));
 
  if ( RMS < offSetCorrection){
    RMS = 0;
  }
 
 
  Serial.print(":");
  Serial.print(RMS, 4);
  delay(1000);

  }
 
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 638
Posts: 50304
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

int endTime = millis() + 21.66;
You don't see a problem with this?

Suppose that this loop starts right away, and that millis returns 0. If that happens, endTime will be set to 21 milliseconds.

You take readings for 21 milliseconds, and send some stuff to the serial port. Then, you wait 1000 milliseconds. The loop() function ends, and gets called again. This takes a little bit of time, as does the sending of the serial data.

So, next time loop gets called, millis() will return a value around 1030, so endTime will be set to 1051, and the process repeats.

Each time, endTime increases by approximately 1030, to 2081, 3111, 4141, etc.

What happens after 30 seconds or so. endTime gets incremented, but can't hold a number larger than 32,768, so it rolls over. Because it is a signed variable, endTime goes negative.

Code:
  while (endTime  > millis()){
A negative value will NEVER be greater than millis(), so this while loop will never end, and no more serial data will ever be sent.

You need to record when the last while loop was run (initially 0, and incremented after each while loop), subtract then from "now" (as returned by millis()), and compare that to the interval.
Code:
unsigned long then = 0;
unsigned long interval = 21;

void loop()
{
   unsigned long now = millis();
   while(now - then < interval)
   {
      // measure
      now = millis();
   }
   then = now;

   // compute

   // send
   delay(1000);
}

Logged

Pages: [1]   Go Up
Jump to: