Some error in coding a proximity Sensor.. Anyone can help me?

Hello everyone!! I would like to know what is my error in my code… So first, I should tell what I’m doing/ what I have… I have a Gizduino Plus 644 ( a copy of Arduino with ATmega 644 here in the Philippines…), a IR Proximity Sensor (Digital - 0 or 1), LED and some resistor.

In this code, everything is working perfectly…
If the proximity sense something, the LED will turn on.

int PROX = 12;
int SENSOR = 2;
int SENSOR_LED = 7;

void setup()
{
  pinMode(PROX, OUTPUT);
  pinMode(SENSOR, INPUT_PULLUP);
  pinMode(SENSOR_LED, OUTPUT);
}

void loop()
{
  digitalWrite(PROX, HIGH);
  digitalWrite(SENSOR_LED, LOW);
  
  int VAL = digitalRead(SENSOR);
  
  if (VAL > 0)
  {
    digitalWrite(SENSOR_LED, HIGH);
  }
}

But when I try it to this code… There’s some error… If I run the program (upload it), and type “QRIN” the LED is already ON even if the Proximity doesn’t sense anything… And if I type “QROUT”, the LED is still on… In which it should not.

int LOCK = 13;
int PROX = 12;
int SENSOR = 2;
int PROX_SENSOR_LED = 7;

void setup()
{
  Serial.begin(9600);
  pinMode(LOCK, OUTPUT);
  pinMode(PROX, OUTPUT);
  pinMode(SENSOR, INPUT_PULLUP);
  pinMode(PROX_SENSOR_LED, OUTPUT);
}

void loop()
{ 
  char data = Serial.read();
  
  switch (data)
  {
    case 'QRIN':
                digitalWrite(PROX, HIGH);
                digitalWrite(LOCK, HIGH);
                digitalWrite(PROX_SENSOR_LED, HIGH);
                SENSOR_LED();
                break;
    case 'QROUT':
                digitalWrite(PROX, LOW);
                LED_OFF();
                digitalWrite(LOCK, HIGH);
                delay(10000);
                digitalWrite(LOCK, LOW);
                break;
  }
}

void SENSOR_LED()
{
  int VAL = digitalRead(SENSOR);
  if (VAL > 0)
  {
    digitalWrite(PROX_SENSOR_LED, HIGH);
  }
}

void LED_OFF()
{
  int VAL = digitalRead(SENSOR);
  if (VAL > 0)
  {
    digitalWrite(PROX_SENSOR_LED, LOW);
  }
}

I’m doing this because I need this for a project… I have a c# program that if the QR that my Webcam reads is correct, the “QRIN” will execute and if the other Webcam reads the QR is correct, it will execute “QROUT”… ← this is already working… The only problem is my code in Arduino IDE…

I’m really sorry for the long post… Please bear with me… I’m really new to Arduino…

Single quote marks define only a single byte. Therefore it can not contain you sequence. A Serial.read reads only a single byte therefore it can not hold your sequence of key presses.

You should put your serial input into a char array and compare then with an if statement.

You should put your serial input into a char array and compare then with an if statement.

The if statement needs to contain a call to strcmp(), too.

Serial Input Basics - updated is a good place to start.