seriel monitor problem

Hello everybody,

I control 2 LEDs with arduino uno , and that works as long as the serial monitor is opend, but when I close the seriel monitor, then the LEDs are off. So (there is no instructions).

But I want to save the user instructions entered in the serial monitor so that LEDs stay on as if it is still opend. The code is as a following .Could someone help me?

int data=0;
int CAN12=4;
int ledboard=13;
int reset=0;
void setup() {
Serial.begin(9600); //initialize serial COM at 9600 baudrate
pinMode(ledboard, OUTPUT); //make the LED pin (13) as output
digitalWrite (LED_BUILTIN, LOW);
pinMode(reset, OUTPUT);
pinMode(CAN12, OUTPUT);
pinMode(3, OUTPUT);
Serial.println("Hi!, I am Arduino");
}
void loop() {
while (Serial.available()> 0){
data = Serial.read();
Serial.println(data);
}
if (data=='A') {
digitalWrite (3, HIGH);
}
else if (data=='a') {
digitalWrite (3, LOW);
}
if (data=='B') {
digitalWrite (4, HIGH);
}
else if (data=='b') {
digitalWrite (4, LOW);
}
}

thank you in advance

Samuel

Try changing this line

while (Serial.available()> 0){

to

if (Serial.available()> 0){

I know that opening the Serial Monitor causes the Arduino to reset, but I have never investigated whether that also happens when you close the Serial Monitor.

Of course, if you disconnect the Arduino from the PC it will forget everything except the program.

...R

hello,

i tried everything. no i do not disconnect the arduino. i just close the serial window

thanks

Don't use pin 0; it's the RX line for communication with the computer. Unless you're using 32U4 based board. So which board are you using?

Not said that it's the cause of your problem but it is wrong.

hello,

thanks for replay.

i did not use pin0 Rx.

I just tried the program from the Original Post (unchanged, except for indenting) on my Uno and it works as expected. When I operate the program in the Serial Monitor I can turn the LEDs on and when I close the Serial Monitor they stay on.

And it behaves the same when I use the Minicom terminal program.

...R

@robin2

realy ???

samuel522:
hello,

thanks for replay.

i did not use pin0 Rx.

You do in your code

int ledboard=13;
int reset=0;
void setup() { 
  ...
  ...
  pinMode(reset, OUTPUT);
  ...
  ...

samuel522:
@robin2

realy ???

What is that supposed to mean?

...R

i just mean it is not possible

samuel522:
i just mean it is not possible

Funny that it works for me too then, this time on a Nano
There are things that I would change about the code but it works as it is

Please post a diagram of the circuit that you are using. Do you have current limiting resistors in series with the LEDs, for instance ?

samuel522:
i just mean it is not possible

It is possible. Why would I tell you a lie?

...R

hi guys,

could anyone try this compleceted code? even this trivial code did not funktion. the led on the board turned off when i close the serial monitor. :confused: . this would help me a lot if i find the solution.

int data=0;
thanks a lot.

void setup() {

pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(9600);
}
void loop() {

data = Serial.read();

if(data=='A'){
digitalWrite(LED_BUILTIN, HIGH);
}
else if(data =='B'){
digitalWrite(LED_BUILTIN, LOW);
}
}

samuel522:
could anyone try this compleceted code? even this trivial code did not funktion.

Works fine for me.

What version of the Arduino IDE are you using?

What operating system are you using on your PC?

...R

thanks robin.

windows 7.

arduino ide 1.8.5

Works fine for me too

Just out of interest, try this

void setup()
{
  Serial.begin(115200);
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, HIGH);
}

void loop()
{
}

Open and close the Serial monitor. What happens ?

it is stay on like this.

What version of the Arduino IDE are you using?

it is stay on like this.

What version of the Arduino IDE are you using?

As it should, of course

I am using 1.8.5 on Win 10 with a Nano

I tried it on another laptop but it didn’t function. Maybe the problem is with the micro controller

samuel522:
Maybe the problem is with the micro controller

That seems quite possible.

...R