Serial Problems

Hello, I am not very good at C programming so I need some help. :~ I am trying to create a program so when you enter 1 it turns on an led, but when you enter 0 it turns it off. I got it working to where when you type 1 it turns on, but when you enter 0 it still stays on.

int led = 13;

 
void setup() {
  Serial.begin(9600);
  pinMode(led, OUTPUT);
  
}
 void loop(){
   if (Serial.available()){
   int ser = Serial.read();
   if (ser = 1){
     digitalWrite(led, HIGH);
   }
   if (ser = 0){
      digitalWrite(led, LOW);
   }
   }
 }

The comparison operator in C/C++ is not "=" it is "==".
You also will have a problem because when testing for the character 1 it is not the same as the integer 1.
You should use:

if (ser == '1') {

and do the same when testing for the zero.

Pete

I tried it and it works! Thanks Pete!

Something similar.

// zoomkat 8-6-10 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later

int ledPin = 13;
String readString;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT); 
  Serial.println("serial on/off test 0021"); // so I can keep track
}

void loop() {

  while (Serial.available()) {
    delay(3);  
    char c = Serial.read();
    readString += c; 
  }

  if (readString.length() >0) {
    Serial.println(readString);
    
    if (readString == "on")     
    {
      digitalWrite(ledPin, HIGH);
    }
    if (readString == "off")
    {
      digitalWrite(ledPin, LOW);
    }

    readString="";
  } 
}