Serial.print for x amount of time

I want to print x variable for x amount of time.

Example:

If i have a Serial.print("A") and i enter a 5000, i want to print "A" for 5 sec. (The amount of times that "A" will be print is going to be determinated by a delay).

time = serial.read()
start = millis()
while millis() - start < time do
  serial.print('A')
  delay(print_delay)
end

That doesn't compile :frowning:

unsigned long time = Serial.parseInt();
unsigned long start = millis();
while (millis() - start < time ) {
  Serial.print('A');
}

Note: you will get a LOT of output

does this mean repeatedly for 5 secs, after 5 secs, ???

When is the assignment due?

isnt a assignment bro xd just a personal proyect.

only repeat it for 5 secs, not 5 times. And starting instantly xd

Have you tried the code in reply #3?

What use does it have?

yes bro, but doesnt read the serial. :confused:

Post your complete sketch, in code tags please.

Im making a kind of "monitoring center", I want that when I send a certain command (26 / millis), it will print the variables of my sensors for that time.

For example:

~,100/26/10000

When i send this command the serial should print the variables for 10 secs xd

unsigned long time = Serial.parseInt();
unsigned long start = millis();

void setup() {
  Serial.begin(9600);

}

void loop() {
while (millis() - start < time ) {
  Serial.print('A');
  delay(1000);
}

}

just copy and paste to see if it works

char s [80];

#define Period  5000
unsigned long msecLst;

void
loop (void)
{
    unsigned long msec = millis ();

    if (msecLst && (msec - msecLst) < Period)
        Serial.println (s);

    if (Serial.available ())  {
        int n = Serial.readBytesUntil ('\n', s, sizeof(s));
        s [n] = 0;

        msecLst = msec;
    }
}

void
setup (void)
{
    Serial.begin (9600);
}

I regard that as very unclear, you only explain the last number ~,100/26/10000.

I interpret the 26 as millis between prints,
and the part before it as some kind of parameter specification.

So the action for the example is to print millis and ~,100 all 26 millis, for 10000 millis.

I execute the example from setup,
after that it should accept serial lines separated by CR or NL and CR.

const char example[] = "~,100/26/10000";

bool printActive;
uint32_t printStart;
uint32_t printLast;
uint32_t printFor;
uint32_t printRepeatAll;
char	what[10];

void setup() {
  Serial.begin(115200);
  Serial.println(F("print for"));
  oneLineReceived(example);
}

void loop() {
  handleSerial();
  if (printActive) {
    if (millis() - printStart >= printFor) {
      printActive = false;
    } else if (millis() - printLast >= printRepeatAll) {
      printData();
    }
  }
}

void printData() {
  Serial.print(millis());
  Serial.print(F(": "));
  Serial.println(what);
  printLast = millis();
}

void oneLineReceived(const char* buffer) {
  char* ptr = strchr(buffer, '/');
  if (ptr != nullptr) {
    strncpy(what, buffer, min((uint16_t)(ptr - buffer), sizeof(what) - 1));
    printRepeatAll = strtoul(++ptr, &ptr, 0);
    if (*ptr == '/') {
      printFor = strtoul(++ptr, nullptr, 0);
      printActive = true;
      printStart = millis();
      printData();
      return;
    }
  }
  Serial.print(F("could not understand '"));
  Serial.print(buffer);
  Serial.println(F("'"));
}

const byte maxMsgLen = 32;

void handleSerial() {
  static uint8_t bIndex;
  static uint8_t buffer[maxMsgLen + 1];
  bool lineReady = false;
  if (Serial.available()) {
    uint8_t inChar = Serial.read();
    if (inChar != 10) {
      if (inChar == 13) {
        lineReady = true;
      } else {
        buffer[bIndex++] = inChar;
        lineReady = (bIndex == maxMsgLen);
      }
      if (lineReady) {
        buffer[bIndex] = 0;
        oneLineReceived((const char*)buffer);
        bIndex = 0;
      }
    }
  }
}
1 Like

Thx bro, it isnt what I wanted to say exactly but with ur code i was able to get what i was looking for!

You forgot to post the working sketch, so we could see, what you were unable to describe.

Of course it does not.. OP was fishing for a copy-paste solution, and I posted the answer as pseudo code. OP got a copy-paste solution and probably learned nothing from it.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.