0

This is the multi serial example from Arduino IDE

void setup() {
  // initialize both serial ports:
  Serial.begin(9600);
  Serial1.begin(9600);
}

void loop() {
  // read from port 1, send to port 0:
  if (Serial1.available()) {
  // plus do some operations
    int inByte = Serial1.read();
    Serial.write(inByte);
  }

  // read from port 0, send to port 1:
  if (Serial.available()) {
    // plus do some operations
    int inByte = Serial.read();
    Serial1.write(inByte);
  }
}

As you can see, the loop is running multiple times doing almost nothing at most of cycles, this will eat up battery life (if it has one).

Is it possible to read when data is available without looping, just like with an interrupt.

Also, is it a good practice to run a loop all the time? Is there any way to sleep? I have seen examples like this one http://playground.arduino.cc/Learning/ArduinoSleepCode which uses some other pin to wake up. I don't want that.

1

Serial input can wake the processor from sleep, assuming it is useful for it to sleep (for example, if it is battery-powered).

My page about power shows various technicques including using serial input to wake it from sleep (see reply #8 on that page). However a disadvantage of that is, it take a few microseconds to wake from sleep - sometimes quite a few - so you are likely to lose the first byte received from serial if you are asleep.

Also, is it a good practice to run a loop all the time?

There is no harm, except for battery consumption.

2
  • I am extremely newbie in these electronics. thanks man you are genius .but small question "you are likely to lose the first byte received from serial if you are asleep" don't that data will be saved in serial buffer ?? – WorkaroundNewbie Aug 24 '16 at 7:30
  • No, because the clock (used for all operations) take a few microseconds to "spool up" after sleep. The processor is held inoperative until it has - the exact time depends on fuse settings. Without a clock it can't clock in serial data. Some clock modes don't stop the clock, but they don't save as much power. – Nick Gammon Aug 24 '16 at 20:43
1

Why do you not want to sleep the processor? If it's running at all, it doesn't matter whether it is polling the UARTs or executing something else; it will still be using power.

The first several paragraphs of the article you linked to describe putting the processor into low-power mode and reducing the power consumption further by disabling some internal hardware modules. When the UART receives a byte, the processor will wake up and continue execution from where it slept. Isn't that what you are asking for?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.