Laser sensor pin returning 0

Hi,

I am newbie to Arduino and need your help please
I am trying to toggle the laser sensor output pin if a button is pressed repetitively using Arduino Uno

for example, if button pressed and laser is on, then turn it off. if button pressed and laser is off then turn it on

However, the digitalRead is always returning 0 for the laserPin status

below my code. I have tried the same code on a LedPin and it worked perfectly
I have attached the code and serial output.
the laserPin and the LedPin are both connected to a digital pin

case 3810010651:
Serial.println(“power button is detected”);
int checkpinstatus = 0;
checkpinstatus = digitalRead(laserPin);
Serial.println(checkpinstatus);
Serial.println("—");
if (checkpinstatus == 0)
{
digitalWrite(laserPin, HIGH);
Serial.println(checkpinstatus);
delay(100);
}
else
{
digitalWrite(laserPin, LOW);
Serial.println(checkpinstatus);
delay(100);
}
break;

Thanks,

Posting a picture of part of your sketch and output is a waste of time

Please see Read this before posting a programming question and follow the advice on posting a programming question

Here is the full code… the case I am asking support with is at the end of the code

#include <IRremote.h>
#include <LiquidCrystal.h>

int RECV_PIN = 3; //Receiver PIN
int LED_PIN = 2; //LED PIN
int laserPin = 4;
int buzzer = 13;

IRrecv irrecv(RECV_PIN);
decode_results results;

LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
// K GND, A 5V, D7 12, D6 11,D5 10, D4 9,E 8, RW GND, RS 7,V0 Resistor, VDD 5V, VSS GND)

void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
pinMode(LED_PIN, OUTPUT);
pinMode(laserPin, OUTPUT);
pinMode(buzzer,OUTPUT);
lcd.begin(16, 2); // set up the LCD’s number of columns and rows:

}

void loop()
{
#define delayTime 600

if (irrecv.decode(&results))
{
Serial.println(results.value, DEC);
digitalWrite(laserPin, LOW);
switch (results.value)
{
case 2534850111:
Serial.println(“Key 1 is detected”);
digitalWrite(LED_PIN,HIGH);
digitalWrite(buzzer,HIGH);
delay(delayTime);
digitalWrite(LED_PIN,LOW);
digitalWrite(buzzer,LOW);
delay(delayTime);
break;

case 1033561079:
Serial.println(“Key 2 is detected”);
for (int i = 0; i < 2; i++)
{
digitalWrite(LED_PIN,HIGH);
digitalWrite(buzzer,HIGH);
delay(delayTime);
digitalWrite(LED_PIN,LOW);
digitalWrite(buzzer,LOW);
delay(delayTime);
}
break;

case 1635910171:
Serial.println(“Key 3 is detected”);
for (int i = 0; i < 3; i++)
{
digitalWrite(LED_PIN,HIGH);
digitalWrite(buzzer,HIGH);
delay(delayTime);
digitalWrite(LED_PIN,LOW);
digitalWrite(buzzer,LOW);
delay(delayTime);
}
break;

case 2351064443:
Serial.println(“Key 4 is detected”);
for (int i = 0; i < 4; i++)
{
digitalWrite(LED_PIN,HIGH);
digitalWrite(buzzer,HIGH);
delay(delayTime);
digitalWrite(LED_PIN,LOW);
digitalWrite(buzzer,LOW);
delay(delayTime);
}
break;

case 1217346747:
Serial.println(“Key 5 is detected”);
for (int i = 0; i < 5; i++)
{
digitalWrite(LED_PIN,HIGH);
digitalWrite(buzzer,HIGH);
delay(delayTime);
digitalWrite(LED_PIN,LOW);
digitalWrite(buzzer,LOW);
delay(delayTime);
}
break;

case 71952287:
Serial.println(“Key 6 is detected”);
for (int i = 0; i < 6; i++)
{
digitalWrite(LED_PIN,HIGH);
digitalWrite(buzzer,HIGH);
delay(delayTime);
digitalWrite(LED_PIN,LOW);
digitalWrite(buzzer,LOW);
delay(delayTime);
}
break;

case 851901943:
Serial.println(“Key 7 is detected”);
for (int i = 0; i < 7; i++)
{
digitalWrite(LED_PIN,HIGH);
digitalWrite(buzzer,HIGH);
delay(delayTime);
digitalWrite(LED_PIN,LOW);
digitalWrite(buzzer,LOW);
delay(delayTime);
}
break;

case 465573243:
Serial.println(“Key 8 is detected”);
for (int i = 0; i < 8; i++)
{
digitalWrite(LED_PIN,HIGH);
digitalWrite(buzzer,HIGH);
delay(delayTime);
digitalWrite(LED_PIN,LOW);
digitalWrite(buzzer,LOW);
delay(delayTime);
}
break;

case 1053031451:
Serial.println(“Key 9 is detected”);
for (int i = 0; i < 9; i++)
{
digitalWrite(LED_PIN,HIGH);
digitalWrite(buzzer,HIGH);
delay(delayTime);
digitalWrite(LED_PIN,LOW);
digitalWrite(buzzer,LOW);
delay(delayTime);
}
break;

case 2538093563:
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(“Hi SAMIR ELBABA”);
break;

case 4039382595:
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(“Hi NAYA ELBABA”);
break;

case 3810010651:
Serial.println(“power button is detected”);
int checkpinstatus = 0;
checkpinstatus = digitalRead(laserPin);
Serial.println(checkpinstatus);
Serial.println("—");
if (checkpinstatus == 0)
{
digitalWrite(laserPin, HIGH);
Serial.println(checkpinstatus);
delay(100);
}
else
{
digitalWrite(laserPin, LOW);
Serial.println(checkpinstatus);
delay(100);
}
break;
}
irrecv.resume(); // Receive the next value
}
delay(300);
}

Thank you for the code but please see Read this before posting a programming question and follow the advice to post it in code tags

Here is the full code… the case I am asking support with is at the end of the code

#include <IRremote.h>
#include <LiquidCrystal.h>

int RECV_PIN = 3; //Receiver PIN
int LED_PIN = 2; //LED PIN
int laserPin = 4;
int buzzer = 13;

IRrecv irrecv(RECV_PIN);
decode_results results;

LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
// K GND, A 5V, D7 12, D6 11,D5 10, D4 9,E 8, RW GND, RS 7,V0 Resistor, VDD 5V, VSS GND)

void setup()
{
  Serial.begin(9600);
  irrecv.enableIRIn(); // Start the receiver
  pinMode(LED_PIN, OUTPUT);
  pinMode(laserPin, OUTPUT);
  pinMode(buzzer,OUTPUT);
  lcd.begin(16, 2); // set up the LCD's number of columns and rows:
  
}



void loop() 
{
  #define delayTime 600

  if (irrecv.decode(&results)) 
  {
    Serial.println(results.value, DEC);
    digitalWrite(laserPin, LOW);
        switch (results.value) 
        {
          case 2534850111:
          Serial.println("Key 1 is detected");
          digitalWrite(LED_PIN,HIGH);
          digitalWrite(buzzer,HIGH);
          delay(delayTime);
          digitalWrite(LED_PIN,LOW);
          digitalWrite(buzzer,LOW);
          delay(delayTime);
          break;

         case 1033561079:
         Serial.println("Key 2 is detected");
         for (int i = 0; i < 2; i++)
         {
          digitalWrite(LED_PIN,HIGH);
          digitalWrite(buzzer,HIGH);
          delay(delayTime);
          digitalWrite(LED_PIN,LOW);
          digitalWrite(buzzer,LOW);
          delay(delayTime);
         }
         break;

         case 1635910171:
         Serial.println("Key 3 is detected");
         for (int i = 0; i < 3; i++)
         {
          digitalWrite(LED_PIN,HIGH);
          digitalWrite(buzzer,HIGH);
          delay(delayTime);
          digitalWrite(LED_PIN,LOW);
          digitalWrite(buzzer,LOW);
          delay(delayTime);
         }
         break;

         case 2351064443:
         Serial.println("Key 4 is detected");
         for (int i = 0; i < 4; i++)
         {
          digitalWrite(LED_PIN,HIGH);
          digitalWrite(buzzer,HIGH);
          delay(delayTime);
          digitalWrite(LED_PIN,LOW);
          digitalWrite(buzzer,LOW);
          delay(delayTime);
         }
         break;

         case 1217346747:
         Serial.println("Key 5 is detected");
         for (int i = 0; i < 5; i++)
         {
          digitalWrite(LED_PIN,HIGH);
          digitalWrite(buzzer,HIGH);
          delay(delayTime);
          digitalWrite(LED_PIN,LOW);
          digitalWrite(buzzer,LOW);
          delay(delayTime);
         }
         break;
         
         case 71952287:
         Serial.println("Key 6 is detected");
         for (int i = 0; i < 6; i++)
         {
          digitalWrite(LED_PIN,HIGH);
          digitalWrite(buzzer,HIGH);
          delay(delayTime);
          digitalWrite(LED_PIN,LOW);
          digitalWrite(buzzer,LOW);
          delay(delayTime);
         }
         break; 

         case 851901943:
         Serial.println("Key 7 is detected");
         for (int i = 0; i < 7; i++)
         {
          digitalWrite(LED_PIN,HIGH);
          digitalWrite(buzzer,HIGH);
          delay(delayTime);
          digitalWrite(LED_PIN,LOW);
          digitalWrite(buzzer,LOW);
          delay(delayTime);
         }
         break;  

         case 465573243:
         Serial.println("Key 8 is detected");
         for (int i = 0; i < 8; i++)
         {
          digitalWrite(LED_PIN,HIGH);
          digitalWrite(buzzer,HIGH);
          delay(delayTime);
          digitalWrite(LED_PIN,LOW);
          digitalWrite(buzzer,LOW);
          delay(delayTime);
         }
         break; 

         case 1053031451:
         Serial.println("Key 9 is detected");
         for (int i = 0; i < 9; i++)
         {
          digitalWrite(LED_PIN,HIGH);
          digitalWrite(buzzer,HIGH);
          delay(delayTime);
          digitalWrite(LED_PIN,LOW);
          digitalWrite(buzzer,LOW);
          delay(delayTime);
         }
         break; 

        case 2538093563:
        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("Hi SAMIR ELBABA");
        break;

        case 4039382595:
          lcd.clear();
          lcd.setCursor(0, 0);
          lcd.print("Hi NAYA ELBABA");
        break;

        case 3810010651:
          Serial.println("power button is detected");
          int checkpinstatus = 0;
          checkpinstatus = digitalRead(laserPin);
          Serial.println(checkpinstatus);
          Serial.println("---");
          if (checkpinstatus == 0)
          {
          digitalWrite(laserPin, HIGH);
          Serial.println(checkpinstatus);
          delay(100);
          }
          else 
          {
          digitalWrite(laserPin, LOW);
          Serial.println(checkpinstatus);
          delay(100);
          }
          break;
  }
   irrecv.resume(); // Receive the next value
}
delay(300);
}

Please edit your posts to add code tags, as described in "How to use this forum".

Done, code quoted

Thanks

Hi All,

Can someone advise on this one?

Thanks,

You have set laserPin to be an output but you are trying to read from it.

wildbill:
You have set laserPin to be an output but you are trying to read from it.

That may not be what he wants to do but it is valid

In a different context

digitalWrite(pinNumber, !digitalRead(pinNumber));

will flip the state of an output pin