Led turns on and off with unexpected delay

hi
i make a simple project about turn on or off the led and the options to make it blink or turn up and down the light and it worked but the problem is that when i enter the data in serial monitor
for turn it on or off it wait for 1 sec and perform the command .
just want to know why its slow and i know my arduino is ok i tested with other project.
here is the sketch

int led =3;
int potensio ;
int ohmi;
int shedat;
int _speed;
int onoff=0;
char choice=0;

void setup() 
{
  Serial.begin(9600);  
  pinMode(led,OUTPUT);
}

void loop() 
{
  
  potensio = analogRead(A0);    // reading potentiometer and pass a number between 0_1023
  
  while (Serial.available())
  {
          
    onoff = Serial.parseInt();           // argument that determination led is off or on
    choice = Serial.parseInt();       //   argument that determination led is on or blinking or we can 
turn on or off the led
   
  if(onoff == 1 && choice == 0)   // choice = 0 means we just want to turn on led
  {
    digitalWrite(led,HIGH);       // turn on led
  }

  else if(onoff == 0 )           // it does not matter what choice is
  {
    digitalWrite(led,LOW);      // turn off led
  }

    ohmi = map(potensio,0,1023,0,10000);     // using 10k ohm potetiometer
    Serial.print(ohmi);                      // printing true potensiometer value
  
  }

  if(onoff ==1 && choice == 1)               // choice 1 means we want to change the blinking time
  {
    _speed = map(potensio,0,1023,0,1000);   //delay with speed mapping to 0 to 1000 mili sec
    digitalWrite(led,HIGH);      //turn on led           
    delay(_speed);               //delaying
    digitalWrite(led,LOW);       //turn off led
    delay(_speed);              // delaying
  }

  else if(onoff ==1 && choice == 2)     // choice 2 means we want to turn up or down the led 
  {
    shedat= map(potensio,0,1023,0,256);

    analogWrite(led,shedat);   // using pwm for light of led
  } 

}

Maybe check the documentation?

Please remember to use code tags when posting code

1 Like

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