Hello, I would like to ask how can I save the state of my IR break beam sensor?
The process:
If A0 Breaks, state = 1 and if the state = 1 then no beep sound even if A1 Breaks
else
If A0 not break, state = 0 and when the A1 breaks, beeps will sound
Scenario
If the person pass thru A0, then A1 will not alarm. But if the person did not pass A0 and go directly to A1, the alarm sounds.
My code works partially but A0 is needs a continuously reading in order to not alarm at A1. I need to know how can I save the state where someone breaks already at A0?
const int buzzer = 9; //buzzer to arduino pin 9
int sensorPin1 = A1; // select the input pin for the IR1
int ledPin1 = 7; // select the pin for the LED1
int sensorValue1 = 0; // variable to store the value coming from the sensor1
int sensorPin0 = A0; // select the input pin for the IR0
int ledPin0 = 6; // select the pin for the LED0
int sensorValue0 = 0; // variable to store the value coming from the sensor0
//state
int state = 0;
void setup() {
Serial.begin(9600);
pinMode(ledPin1, OUTPUT);// declare the ledPin as an OUTPUT:
pinMode(ledPin0, OUTPUT);// declare the ledPin as an OUTPUT:
pinMode(buzzer, OUTPUT); // Set buzzer - pin 9 as an output
}
void loop() {
sensorValue1 = analogRead(sensorPin1);// read the value from the sensor:
sensorValue0 = analogRead(sensorPin0);// read the value from the sensor:
//Serial.print(sensorValue0);
Serial.println(state);
// delay (100);
// Serial.print(sensorValue1);
//A0
if (sensorValue0 >=100){
state = 1;
delay(100); // ...for 1 sec
noTone(buzzer); // Stop sound...
} else {
//A1
if (sensorValue1 <=100){
state = 0;
digitalWrite(ledPin1, LOW);
delay(100); // ...for 1 sec
noTone(buzzer); // Stop sound...
} else {
state = 1;
digitalWrite(ledPin1, HIGH);
tone(buzzer, 4000); // Send 1KHz sound signal...
delay(100); // ...for 1 sec
noTone(buzzer); // Stop sound...
delay(100); // ...for 1sec
digitalWrite(ledPin1, LOW);
}
}
}
