Arduino Pro Sends Data to the serial port for only a limited amount of time...

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

Can I get it to send data continually while it is connected?



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.

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));

double RMS = sqrt((sensor1Sum/count));

if ( RMS < offSetCorrection){
RMS = 0;

Serial.print(RMS, 4);


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.

  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.

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