Please help, how do I make the switch OFF on a serial monitor?

int HIGH_PIN = 12;
int LOW_PIN = 5;
int i;
char data;
void setup() {
  Serial.begin(9600);
  for (i = LOW_PIN; i <= HIGH_PIN ; i++) {
    pinMode(i, OUTPUT);
  }
}

void loop() {
  String command;
  while (Serial.available() != 0) {
    data = Serial.read();
    command = command + data;
    delay(10);
  }
  if (command.length() != 0) {
    Serial.print("Command = ");
    Serial.println(command);
  }

  int pin = command.toInt();
  for (int i = LOW_PIN; i <= HIGH_PIN ; i++)
    if (pin == i) {
      digitalWrite(i,HIGH);
    }
}

how to make switch OFF with same command ?
Thank you

To open up the serial monitor, go up to tools and then select the serial monitor. Alternatively, CTRL+SHIFT+M will also bring up the same window. Make sure you are connected to the Arduino otherwise the window won't open up. myprepaidcenter

Read the state of the pin using digitalRead(). It will work even though it is set as an OUTPUT. If its state is HIGH, set it LOW else set it HIGH

indianabent:
To open up the serial monitor, go up to tools and then select the serial monitor. Alternatively, CTRL+SHIFT+M will also bring up the same window. Make sure you are connected to the Arduino otherwise the window won't open up.

i know how to open Serial monitor, what i'm asking is how to make switch OFF on my code

UKHeliBob:
Read the state of the pin using digitalRead(). It will work even though it is set as an OUTPUT. If its state is HIGH, set it LOW else set it HIGH

can give me example code please

thank you

UKHeliBob:
Read the state of the pin using digitalRead(). It will work even though it is set as an OUTPUT. If its state is HIGH, set it LOW else set it HIGH

int HIGH_PIN = 12;
int LOW_PIN = 5;
int i;
boolean c = 0;
char data;
void setup() {
  Serial.begin(9600);
  for (i = LOW_PIN; i <= HIGH_PIN ; i++) {
    pinMode(i, OUTPUT);
    pinMode(i, INPUT);
  }
}

void loop() {
  String command;
  while (Serial.available() != 0) {
    data = Serial.read();
    command = command + data;
    //    command = Serial.readStringUntil('\n');
    delay(10);
  }
  if (command.length() != 0) {
    Serial.print("Command = ");
    Serial.println(command);
  }
  int pin = command.toInt();
  for (int i = LOW_PIN; i <= HIGH_PIN ; i++)
    if (pin == i) {
      digitalWrite(i, HIGH);
      c = digitalRead(i) ;
    }
  if (pin == i) {
    if (c == HIGH) {
      digitalWrite(i, LOW);
    }
  }
}

i try with this code but just 50% light

digitalWrite(i, HIGH);
      c = digitalRead(i) ;

You’ve just written it HIGH.
What do you expect the value of c to be?

(No, not the speed of light)

TheMemberFormerlyKnownAsAWOL:

digitalWrite(i, HIGH);

c = digitalRead(i) ;



You've just written it HIGH.
What do you expect the value of c to be?


(No, not the speed of light)

i just want c to be 1

guermo:
i just want c to be 1

usually the easiest way is to achieve this is to writec = 1;

J-M-L:
usually the easiest way is to achieve this is to write

c = 1;

of course i know that, what i wanted just ON OFF switch on serial monitor please see first code, and give me what should i do

Read the pin. Use digitalRead
If it reads back “HIGH”, then write “LOW”.
If it reads back “LOW”, then write “HIGH”.

You’ll just need an if/else, because digitalRead can only return HIGH or LOW.
Or just use the ternary operator.

i wanted just ON OFF switch on serial monitor

Sorry I don't understand what that means.

I would suggest to study Serial Input Basics to handle reading from the Serial monitor

Read the pin. Use digitalRead
If it reads back "HIGH", then write "LOW".
If it reads back "LOW", then write "HIGH".

Let's hope that you have more luck than I did with that suggestion

TheMemberFormerlyKnownAsAWOL:
Read the pin. Use digitalRead
If it reads back "HIGH", then write "LOW".
If it reads back "LOW", then write "HIGH".

You'll just need an if/else, because digitalRead can only return HIGH or LOW.
Or just use the ternary operator.

that's what i'm try to find, how to make digitalRead without input set ? i stuck in there
you have example code for that ?

that's what i'm try to find, how to make digitalRead without input set ?

Go back and read reply #2 carefully

may be a bit more of a hint - something like this:

selectedPinNumber = <whatever the end user entered>;
...
if (selectedPinNumber exists and its state is HIGH) then 
  set selectedPinNumber to LOW
else
  set selectedPinNumber to HIGH

empty selectedPinNumber

guermo:
that’s what i’m try to find, how to make digitalRead without input set ? i stuck in there

What are you stuck on?

You are using an AVR, aren’t you?

If we can’t see your code, and your observations of how it works, it’s hard to help you.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.