LED Toggle Help

Hi,

I have the following code that just toggles a LED on and off:

#define LED 13

int flag=0;
byte theByte=0;

void setup(){
  pinMode(LED,OUTPUT);
  Serial.begin(9600);
}
void loop(){
  if (Serial.available()>0)
  {
    theByte=Serial.read();
    if (flag==0) {
      digitalWrite(LED,HIGH);
      flag=1;
    }else {
      digitalWrite(LED,LOW);
      flag=0;
    }
  }
}

1) I upload the code 2) I open the Serial Monitor 3) I enter a key, press enter, and the LED goes on 4) I enter a key, press enter, and the LED goes off 5) I enter a key, press, enter and the LED goes on 6) I close the Serial Monitor, and the LED goes off

Why the did the LED go off when I close the Serial Monitor? How can I make it stay on when I toggle it on and then close the Serial Monitor (aka closing the serial communication).

Looking at the code, it appears that when you close the serial connection by closing the Serial Monitor, Serial.available() goes to zero. Is this correct?

I tried putting an else statement for when Serial.available() goes to zero, and the LED still turns off after I close the Serial Monitor.

Is it possible that the serial hardware does something else when you close the serial connection? From what I'm thinking, it appears to reset the code...

That is 100% normal. Try using hyperterminal or another terminal application and you won't have those results. I'm not sure why but when you open the serial monitor sometimes it resets the sketch. It might do the same on exit.

That is 100% normal. Try using hyperterminal or another terminal application and you won't have those results. I'm not sure why but when you open the serial monitor sometimes it resets the sketch. It might do the same on exit.

Indeed it does.

Upon opening or closing of the arduino serial monitor the sketch is reset. As Scott says, using a different serial monitoring app will allow you to close and open it without the reset.

Not sure why is does but I find it more handy than a nuiscance. It means I can reset my sketch by just opening or closing the serial port. Helpful when my reset button is covered.

Mowcius

I tried the following terminals:

1) Serial Monitor 2) SecureCRT 3) Putty 4) HyperTerminal

They all did the same thing as the Serial Monitor...this means that closing the serial connection will cause the code to reset. I did some tests by using the PySerial library and Python and confirmed that after I close the connection using serial.close(), the code resets. Therefore, it must be something on the Arduino that does this. Is there a workaround this through the code without modifying Arduino's schematics or something.

This is what I want for the LED toggle:

1) Open a serial connection 2) Send a byte 3) LED goes on 4) Close serial connection 5) LED still stays on

Isn't that the way serial communication is really supposed to work?

Here's my python program:

import serial
import time

ser=serial.Serial()
ser.port=3
ser.baudrate=9600

ser.open()          # open the connection
time.sleep(4)       # give some time, allow it to connect if it has not done so
ser.write(2)         # send a byte
time.sleep(5)       # LED to stay on for 5 secs
ser.close()          # close connection. this will determine if closing the connection resets the code
time.sleep(5)       # this is to confirm that finished program execution has nothing to do with it

That's all I want to do. I have done many tests, and so far it appears that the only way around this is to modify the Arduino.

Okay I think this is my problem:

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1204641836

I wonder why most people do not seem to care about this problem lol.

Aha well there's your solution. Do your serial communications on pins 1 and 2 instead of over the USB link. I didn't know that would work!

This has been a on going issue sense the Arduino folks added the auto-reset feature. There are two ways to deal with the problem, either by software on the PC side or hardware changes on Arduino board.

For software one has to defeat DTR generation when opening and closing whatever serial comm application you are using.

For hardware changes one can do one of the following:

Remove the small capacitor that routes the DTR signal to the reset pin. Or some have had luck using a 150 ohm resistor that is wired across the existing reset pull-up resistors. Both of these changes will prevent the auto-reset function from working so the Arduino IDE will not be able to load new sketches unless you manual press the reset button at the proper time in the compile/upload sequence.

There are Arduino clone type boards that deal with this nicely with either a two position switch (Seeeduino boards) or a removable jumper clip that allows the auto-reset feature to either function or be disabled.

While using a external USB serial convertor wired to pins 0 and 1 will work it certainly isn’t the lowest cost solution.

Lefty

Thanks a lot guys! I disabled DTR on the software side through my code. For those of you who thought changing to a different terminal might do the trick, but I think different terminals set the DTR differently?

import serial
import time

ser=serial.Serial()
ser.port=3
ser.baudrate=9600

ser.open()
ser.setDTR(False)

time.sleep(1.5)
ser.write(1)

ser.close()

Does anyone know why it takes approximately 1.5 seconds for the connection to be fully made and ready to transfer data? That's what the time.sleep(1.5) is for...would it be faster if the program was written in C?

Ok, it appears that, if you are using the software method to disable DTR, you can only disable it for when you close serial connection, so it's still resetting upon opening the connection (reason for the delay).

Time to use the 120 ohm method.

Added the 120ohm resistor. Everything works great now. No reset upon opening or closing a serial connection. No need to disable DTR on the software side. No delay, so no need to write in C for faster performance.

import serial
import time

ser=serial.Serial()
ser.port=3
ser.baudrate=9600

ser.open()

ser.write(1)
time.sleep(1)
ser.write(1)
time.sleep(1)
ser.write(1)

ser.close()

Where exactly did you end up putting the 150 ohm resistor?

Where exactly did you end up putting the 150 ohm resistor?

It wires just like the existing pull-up resistor for the processors reset pin. So you can make it temporary by inserting a 150 ohm resistor between the +5vdc pin and the reset pin on the Arduino shield connectors. That way you can easily remove it when you don't want to disable auto-reset, like when you want to upload a new sketch.

Lefty

I guess I misunderstood the reset pin then. The board will reset when the reset pin is connected to ground?

I always thought it reset when it was connected to 5v.

The reset signal is more correctly called "/RESET" - it is active low, as a peek at the datasheet will confirm.

I don't know why it isn't labelled as such on the PCB.

I used a 120ohm resistor and placed in the 5v and Reset pins, according to this article:

http://www.arduino.cc/playground/Main/DisablingAutoResetOnSerialConnection

I don't know what would happen if you use something higher than 124 ohms...

I don't know what would happen if you use something higher than 124 ohms...

There will come at some higher resistance, where the auto-reset DTR pulse from the series capacitor will be able to overcome the pull-up voltage and thus cause a short reset pulse once again. I guess one could wire a 5k ohm pot between the reset pin and +5vdc and start turning down the resistance starting from 5k and see when the auto-reset stops functioning, then remove the pot and measure the 'magic' value, however it's bound to change a little with temperature and Vcc changes.

The 'best' solution in my opinion is to cut the trace between the DTR cap and the reset pin somewhere, solder a 2 pin header and then install a removable shorting clip so that one can easily enable or disable the auto-reset function whenever they want, but the temporary resistor is a good second choice.

Lefty