Repeating for Switch() Case Statement with single click

Hi guys, I'm currently working on a project with VB.net and I need to send case to VB.net so that it could read the command from arduino. My question is how do I repeat the case '1' after 1 is entering in the serial? Thanks in advance guys.

#include <Servo.h> 

int IR_Pin = 7;  // This is our input pin
int Obstacle = HIGH;  // HIGH MEANS NO OBSTACLE
int RED_PIN = 9;
int GREEN_PIN = 10;
int BLUE_PIN = 11;
int LED = 13;

Servo myservo;  // create servo object to control a servo 
 
void setup() 
{ 
  pinMode(IR_Pin, INPUT);
  pinMode(LED, OUTPUT);
  pinMode(RED_PIN, OUTPUT);
  pinMode(GREEN_PIN, OUTPUT);
  pinMode(BLUE_PIN, OUTPUT);
  digitalWrite(LED, LOW);
  digitalWrite(RED_PIN, HIGH);
  digitalWrite(GREEN_PIN, LOW);
  digitalWrite(BLUE_PIN, LOW);
  myservo.write(90);  
  
  myservo.attach(8);  // attaches the servo on pin 9 to the servo object 
  Serial.begin(9600); //begins serial communication
} 
  
void loop() 
{ 
 if (Serial.available() > 0) {
  int pos = Serial.read();
  switch (pos)
  {
  case '1':
  Obstacle = digitalRead(IR_Pin);
  if (Obstacle == LOW)
   {
          digitalWrite(RED_PIN, LOW);
          digitalWrite(GREEN_PIN, HIGH);
          myservo.write(0);
          delay(1000);
   }

        //digitalWrite(RED_PIN, HIGH);
        //digitalWrite(GREEN_PIN, LOW);
        //myservo.write(90);
        //delay(1000);
        //break;
      }
   }
}

You could take the switch/case out of the test as to whether Serial data is available so that if no serial data is available then the value of pos will be preserved. ie only update pos if new data is available

I assume that you are going to have more cases.

Hi UKHeliBob, I cannot take the switch/case out since I need to send the case from the vb.net

SerialPort1.Open()
SerialPort1.Write("1")
SerialPort1.Close()

Also, I only have one case in this coding. Is there any other methods to execute my command without case? Appreciate much with your helps.

You are not sending a case, you are sending a '1' and using it as a case in a switch statement. If you want to do something every time a '1' comes in, replace:

switch (pos)
  {
  case '1':

With:

if(pos == '1')
{

Yes, I want to do something when '1' is entering in serial monitor. But problem is, how do I let '1' keep entering?

My project is quite simple, when a button is clicked in the vb.net, then the gate is opened and in the same time the IR sensor need to detect whether a person is pass through or not. If IR sensor can't detect anything, then the gate will be close.

It is quite straightforward in arduino, but it is hard to communicate with vb.net since the IR sensor need to detect right after the button is clicked in vb.net.

how do I let '1' keep entering?

Have you taken note of the advice given ?

You do not need switch/case as there is only 1 case

fkk0924:
Hi UKHeliBob, I cannot take the switch/case out since I need to send the case from the vb.net

SerialPort1.Open()
SerialPort1.Write("1")
SerialPort1.Close()

Opening the port will probably reset the Arduino, why would you want to do that?