Problem with project(looping) PLEASE HELP!!!!

Hi guys,

I am working on a simple project that uses two photoelectric sensors as an analog input,buzzer as ouput and 3 LED is to indicate whether the sensor is detecing something.

  • One sensor to detect someone = "People"
  • One sensor to detect item = "Item"

How I want my project to work is that only when the People sensor detect someone, it will run the Item sensor which will then detect if there are any item.

Then I want to activate a buzzer with only the following condition: No people + have Item.
But when this happen, I want my buzzer to be activated for only 3 second and then the whole program should restart regardless if there is still an item.

Now, the problem I am facing is
1)Even when there is no one + there is an item, the program will execute the first if statement [ignoring the if(People==false && item== true)] which only light up a LED, therefore the buzzer is not activated.

2)The program stops after fulfilling the last condition. It does not loop back the whole program.

I have attached my program below. Anyone's help and advice will be greatly appreciated. :slight_smile:

int analogInPinA = 0;
int analogInPinB = 1;
const int greenPinA = 8;
const int redPinA = 9;
const int greenPinB = 10;
const int buzzer = 11;

int People = 0;
int Item = 0;
int var = 0;
void setup()
{
  Serial.begin(9600);
  pinMode(greenPinA, OUTPUT);
  pinMode(redPinA, OUTPUT);
  pinMode(greenPinB, OUTPUT);
  pinMode(buzzer, OUTPUT);
  pinMode(analogInPinA, INPUT);
  pinMode(analogInPinB, INPUT);
  //for (int test = 0; test <2; test++);
}

void loop()
{
  while(true)
  {
  People = analogRead(analogInPinA);
  Item = analogRead(analogInPinB);
  
  if(People > 500)
  {
   People = false;
  }
  else
  {
   People = true;
  }
  if(Item > 500)
  {
  Item = false;
  }
  else
  {
   Item = true;
  }
  Serial.print("People = ");
  Serial.println(People);
  
  Serial.print("Item = ");
  Serial.println(Item);

  if(People == false) //<-----problem here: if nobody, straight away they execute this function ignoring "else if(People == false && Item == false)" function
  {
    digitalWrite(greenPinA, HIGH);
    digitalWrite(redPinA, LOW);
    digitalWrite(greenPinB, LOW);
    digitalWrite(buzzer, LOW);
  }
  else if(People == true)
  {
    digitalWrite(greenPinA, LOW);
    digitalWrite(redPinA, HIGH);
    digitalWrite(greenPinB, HIGH);
    digitalWrite(buzzer, LOW);
  }
   else if(People == true && Item == true)
  {
    digitalWrite(greenPinA, LOW);
    digitalWrite(redPinA, HIGH);
    digitalWrite(greenPinB, HIGH);
    digitalWrite(buzzer, LOW);
  }
  else if(People == true && Item == false)
  {
    digitalWrite(greenPinA, LOW);
    digitalWrite(redPinA, HIGH);
    digitalWrite(greenPinB, HIGH);
    digitalWrite(buzzer, LOW);
  }
  else if(People == false && Item == true)
  {    
    digitalWrite(greenPinA, LOW);
    digitalWrite(redPinA, HIGH);
    digitalWrite(greenPinB, LOW);
    digitalWrite(buzzer, HIGH);
    delay(3*1000);
    digitalWrite(buzzer, LOW);
    
    while(1){} //to make this loop run once. BUT problem is: when stop, the program cannot run anymore unless manually restart
  }
  /*else if(People == false && Item == false)
  {
    digitalWrite(greenPinA, HIGH);
    digitalWrite(redPinA, LOW);
    digitalWrite(greenPinB, LOW);
    digitalWrite(buzzer, LOW);
  }*/
  delay(250);
 }
}

There are several problems with this code.

First you are treating People as both an integer and a Boolean value, which may work but is not a good idea.

Second, the logic is tangled. If you have already tested for (People == false), why test that condition again, as in "else if(People == false && Item == true)" ?

You need to think this entire process through again. To make things very simple, just use "if" statements and avoid use of "else if".