Sensors don't make lcd to output a counter. Please Help I'm new!

Hi, I have 2 Pir sensors and I want them to both have input but make 2 different outputs (2 separate counters) on 1 lcd can someone please help?

#include <LiquidCrystal.h> 

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int pirPin = 10; 
int pirPin1 = 9;
int pirState = LOW; // we start, assuming no motion detected
int pirState1 = LOW;
int val = 0; // variable for reading the pin status
int val1 = 0;
int counter = 0;
int counter1 = 0;
int currentState = 0;
int previousState = 0;
int currentState1 = 0;
int previousState1=0;
void setup() {
pinMode(pirPin1, INPUT);
pinMode(pirPin, INPUT); // declare sensor as input
 lcd.begin(16, 2);
 lcd.setCursor(0, 0);
 lcd.print("RedTeam-BlueTeam");
 lcd.setCursor(8,1);
 lcd.print("-");
}
void loop(){
val = digitalRead(pirPin); // read PIR sensor input value
if (val == HIGH) { // check if the input is HIGH

if (pirState == LOW) {
// we have just turned on
currentState = 1;
// We only want to print on the output change, not state
pirState = HIGH;
delay(1000);
  }
} else {

if (pirState == HIGH){
// we have just turned off
currentState = 0;
// We only want to print on the output change, not state
pirState = LOW;
}

//second pir sensor stuff
 
val1 = digitalRead(pirPin1); // read PIR sensor input value
if (val1 == HIGH) { // check if the input is HIGH

if (pirState1 == LOW) {
// we have just turned on
currentState1 = 1;
// We only want to print on the output change, not state
pirState1 = HIGH;
delay(1000);
  }
} else {
  }

if (pirState == HIGH){
// we have just turned off
currentState = 0;
// We only want to print on the output change, not state
pirState = LOW;
} else {

if(currentState != previousState){
if(currentState == 1){
counter = counter + 1;
lcd.setCursor(5,1);
lcd.print(counter);
delay(5000);
if(currentState1 != previousState1){
if(currentState1 == 1){
counter1 = counter1 + 1;
lcd.setCursor(7,1);
lcd.print(counter1);
delay(5000);
     }else {
       }
      }
     }
    }
  }
 }
}

Does this get printed on the LCD?

Not your main problem, but ... you need assign currentState to previousState at the end of your loop.

Use the the Serial Monitor for debugging

how can I use the serial monitor

A quick google search for "arduino serial" gives me Serial - Arduino Reference

Your code is a lot more complicated than it needs to be. You don't need extra variables for "state" - you just need to know what the previous digitalRead value was for each pin.

Untested...

#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int pirPin = 10;
int pirPin1 = 9;

int val = LOW; 
int valPrev = LOW; 
int counter = 0;

int val1 = LOW;
int val1Prev = LOW;
int counter1 = 0;

void setup() 
{
  pinMode(pirPin1, INPUT);
  pinMode(pirPin,  INPUT); 
  
  lcd.begin(16, 2);
  lcd.setCursor(0, 0);
  lcd.print("RedTeam-BlueTeam");
  lcd.setCursor(8, 1);
  lcd.print("-");
}

void loop() 
{
  val = digitalRead(pirPin); 
  if (val == HIGH && valPrev == LOW)
  {
    lcd.setCursor(5, 1);
    lcd.print(++counter);
  }
  valPrev = val;

  val1 = digitalRead(pirPin1); 
  if (val1 == HIGH && val1Prev == LOW)
  {
    lcd.setCursor(7, 1);
    lcd.print(++counter1);
  }
  val1Prev = val1;
}

Depending on the type of PIR (and whether the signal stays HIGH for a period) you may need to include some timing code... but don't use delay.

Thank you so much, with only one or 2 tweaks, it worked! Have an amazing weekend and stay safe :slight_smile:

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