Problem with my code

Hi,
My name is Daniel, i am a begginer in creating codes, i have a problem in my code, i think.
I turn on the led with my android and with a sensor a turn it off and send a number to turn on another, but i just want to send once and it keeps sending according to the conditions that i have,
HOW can i fix it :frowning:

Here is the code:

char val;
int led = 8;
int sensor = 7;
int calibrationTime = 30;

void setup() {
for(int i = 0; i < calibrationTime; i++){
Serial.print(".");
delay(1000);
}
Serial.println(" done");
Serial.println("SENSOR ACTIVE");
delay(50);

pinMode(led,OUTPUT);
pinMode(sensor,INPUT);
Serial.begin(9600);

}

void loop() {
int l= digitalRead(led);
int p= digitalRead(sensor);

if(Serial.available())
val = Serial.read();

if(val == '1')

{
digitalWrite(led,HIGH);

}

if( p == HIGH)
{digitalWrite(led,LOW);
delay(500);}

if (p==HIGH && l==LOW)
{
Serial.println("2");
}

if(val =='0')
{digitalWrite(led,LOW);}

}

THANKS!

if(val =='0')
{
  digitalWrite(led,LOW);
  val = ' ';
  }
 
}

May be this hint will help?

if (condition) [color=red][b]{[/b][/color]
   instruction;
   instruction;
   instruction;
   ...
[color=red][b]}[/b][/color]

Probably do the val = ' '; unconditionally at the end of loop or before the Serial access.

thanks for your help but i still have the same results :frowning:

I add the val='0';

char val;
int led = 8;
int sensor = 7;
int calibrationTime = 30;

void setup() {
for(int i = 0; i < calibrationTime; i++){
Serial.print(".");
delay(1000);
}
Serial.println(" done");
Serial.println("SENSOR ACTIVE");
delay(50);

pinMode(led,OUTPUT);
pinMode(sensor,INPUT);
Serial.begin(9600);

}

void loop() {
int l= digitalRead(led);
int p= digitalRead(sensor);

if(Serial.available())
val = Serial.read();

if(val == '1')

{
digitalWrite(led,HIGH);

}

if( p == HIGH)
{digitalWrite(led,LOW);
delay(500);}

if (p==HIGH && l==LOW)
{
Serial.println("2");
}

if(val =='0')
{digitalWrite(led,LOW);
val=' ';
}

}

The led turns on with android, turns off with sensor, send a number to the android when sensor high and led low y android turns on the other led sending a differente number, i turn off the other led with sensor but if i pass my hand to the sensor, the condition that i hace P=high & l=low turns the second led again :frowning:
Sorry for the bother, but there is something that i am doing wrong, i am still learning the codes.

Did you try the suggestion in reply #3?

Can you please use code tags when you post code?

Check what happens when you receive an input. Maybe some {} would help

Do Serial.begin( ) before you use any Serial functions, i.e. Serial.print( ).

Dudes,
Thank you very much, i tried the post #3 and it worked.