Skip to main content
added 4 characters in body; edited title
Source Link
jfpoilpret
  • 9.2k
  • 7
  • 38
  • 54

interrupt Interrupt driven serial in Arduino Mega

thisThis is the multi serial example from arduino ideArduino 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);
  }
}

asAs 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 there any

Is it possible that i canto read when data is available without looping, just like with an interrupt. plus do you think

Also, is it a good practice to run a loop all the time is? Is there any way to sleep i? 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.

interrupt driven serial in Arduino Mega

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 there any possible that i can read when data is available without looping like interrupt. plus do you think is a good practice to run a loop all 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.

Interrupt driven serial in Arduino Mega

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.

Source Link

interrupt driven serial in Arduino Mega

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 there any possible that i can read when data is available without looping like interrupt. plus do you think is a good practice to run a loop all 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.