Serial monitor problem

how to access a value which was an input done by the serial monitor.

in the code below i want to ON the motor when 0 is put to the Serial monitor and OFF when 9 is put.For some reason my code is not working.i think its wrong in two places.

 Serial.write(val); // write the value in the serial monitor.

because im not sure what this does

and

 Serial.write(val); // write the value in the serial monitor.

im not sure wheather the value zero is written in the correct way.(eg:== '0' or just == 0)

full code is below.

int val; // Declaring an integer variable named ‘val’ to receive data from the serial port.
int motor = 11; //Naming digital pin  11 as motor.

void setup () 

{

  pinMode(motor, OUTPUT); //initialize digital pin 11 as an output pin.
  Serial.begin(9600);// initialize serial communication at 9600 bits per second.

}

void loop() 

{

  if( Serial.available() ) // check weather data is available to read.
  {
    val = Serial.read(); // read the value from the serial monitor and store it in variable 'val'.
    Serial.write(val); // write the value in the serial monitor.
  }
  
  if( val == '0' ) //if 0 was entered to the serial monitor.

  { 
   digitalWrite(motor, LOW); //If 0 was entered to the serial monitor turn off the motor.
  }
  
  if( val == '9' ) //if 9 was entered to the serial monitor.            
  {
   analogWrite(motor, 255); //Analog writes the value 255 to digital pin 11. 
  }
}

'0' is correct, and try Serial.print((char)val); instead.

Also, you don't need to keep writing to the pin - it'll remember just fine.

it still doesnt work

I'll leave you to ponder why that is one of my least favourite phrases.

Josh987:
it still doesnt work

You have never told us what you mean by "doesn't work".

Tell us what actually happens and what is supposed to happen.

What motor driver and motor are you using?
And what power supply are you using for the motor (volts and amps)?

You should NOT be powering a motor from the Arduino.

...R

Robin2:
You have never told us what you mean by "doesn't work".

Tell us what actually happens and what is supposed to happen.

What motor driver and motor are you using?
And what power supply are you using for the motor (volts and amps)?

You should NOT be powering a motor from the Arduino.

...R

what i mean by not working is that it doesnt stop when zero is entered it just spins as soon as the code is upoaded.

the motor should get off when zero is beng input by the Serial monitor and when 9 is being the input it should get on.thats what i expect to happen.

No the motor uses a 6V external power supply. (four AA batteries)

What values are you seeing on the Serial monitor ?

Have you changed Serial.write(val); to Serial.println(val);

Add this debugging code (and similar for the other IF statement

 if( val == '0' ) //if 0 was entered to the serial monitor.
 {
  digitalWrite(motor, LOW);
  Serial.println("Motor Off");   // <---- NEW
 }

And post the very latest version of your code.

…R

Robin2:
What values are you seeing on the Serial monitor ?

Have you changed Serial.write(val); to Serial.println(val);

Add this debugging code (and similar for the other IF statement

 if( val == '0' ) //if 0 was entered to the serial monitor.

{
  digitalWrite(motor, LOW);
  Serial.println(“Motor Off”);  // <---- NEW
}



And post the very latest version of your code.

...R

ill check it and get back to you,
do you know exactly what this Serial write function does cause im not sure what this function will do.
thank you