0

I have loaded an example sketch that came with the installation of the Arduino software onto my Uno.

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


void loop() {

  int sensorValue = analogRead(A0);
  Serial.println(sensorValue);
  delay(1);       
}

From this time TX light is continuously on. It's remaining on even after resetting it. How do I end void loop so that TX light goes off.

  • 3
    Stop outputting via the serial connection. – Ignacio Vazquez-Abrams Sep 5 '14 at 21:32
  • I need to setup multitask in a loop, so I can take a snap shoot of data from the SD reader. – joe Oct 30 '17 at 11:16
  • 2
    I'm voting to close this question because it shows a fundamental missunderstanding of how Arduino works. The setup and loop model is the very first thing you have to learn before writing your first sketch. It's explained in pag. 36 in the book that's come with Arduino Starter Kit. – user31481 Oct 30 '17 at 11:55
  • HIGHLY recommend you begin by loading the Blink example program and start there. Then modify it to suit. As for your little program, your delay is only 1 millisecond. You might as well not even have it there because the Serial.println takes longer than that. Change it to 5000 for 5 seconds, maybe. Also, you never set the A0 pin to be of type INPUT. But first go back to Blink and try to walk (digital) before you try to run (analog). – SDsolar Oct 31 '17 at 0:52
  • I'm voting to close this question as off-topic because of the age of this one and the lack of useful description in the title. – SDsolar Oct 31 '17 at 0:53
8

The implied question is how to turn off the Tx LED, which has already been answered and is probably the answer you're looking for. But the question you asked is how to end the loop() function. Not to be pedantic but to clarify the use of loop, the simple answer is, "you can't". loop() is called from a forever loop in the C/C++ main program, function main(), so returning from it, either with a return statement or by reaching the final close } causes loop() to be called again. This is intentional as the main() function in an embedded system is never supposed to return, thus it must keep re-running the application program.

Update (since this question just reappeared on the front page, I'll add the following to my answer):

loop() reads and prints continuously. If you had wanted that code to be executed only once, you could put in setup() and leave loop() empty. Then the sketch would initialize the serial output, read an analog value, print it - once - followed by entering the empty loop() and remaining there until power-down.

A way to execute the loop function once is to write an infinite loop at the end of it, so loop() would never return and thus never be re-executed:

void loop() {

  int sensorValue = analogRead(A0);
  Serial.println(sensorValue);
  delay(1);

  while(1)
     ;
}
6

The TX LED is on, because this sketch is periodically printing the value of the analog pin 0 onto the serial port. The LED seems to be constantly on, because the delay between two Serial.println() calls is only approx. 1 ms.

If you replace the 1 with 1000 for example, the TX LED should flash once per second. Start the serial monitor in your Arduino IDE too see the transmitted analog values. Delete the Serial. calls to remove the serial port functionality altogether.

You might want to read the documentation of the loop() and the delay() functions.

2

Seeing as questions about turning off the LED, and the nature of exiting loop have been answered, I'll also answer the question they both seem to lead to:

If the overall intention is to "turn off" the MCU, outside of turning off the power supply, it can be put into sleep mode.

Depending on how you configure the MCU before entering sleep mode, it can even be allowed to wake up on further USART communication.

2

Don't write code in the loop() that should be executed only a few times. Or use a counter. Or an external button. For the permanent light, increase the delay between calls.

  • Could you elaborate on how to do the stuff you say to do in this answer? – The Guy with The Hat Sep 6 '14 at 16:50

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.