These three codes make an led come on or blink using two break beam sensors. The first code makes the led come on when the first sensor is made or LOW and makes the led go off when the second sensor is made or LOW. The second code makes the led blink when the first sensor is made or LOW and stop blinking when the second sensor is made or LOW. The third code makes the led blink when the first sensor is LOW, after that if the first sensor is HIGH the led stays on solid until the second sensor is made or LOW.
This code makes the led come on when sensor one is made and go off when sensor two is made.
const byte SENSORPIN1 = 5;
const byte SENSORPIN2 = 6;
const byte LEDPIN = 8;
int sensorState1 = 0, lastState1 = 0;
int sensorState2 = 0, lastState2 = 0;
void setup() {
pinMode(SENSORPIN1, INPUT_PULLUP);
pinMode(SENSORPIN2, INPUT_PULLUP);
pinMode(LEDPIN, OUTPUT);
}
void loop() {
led_switch();
}
void led_switch(){
sensorState1 = digitalRead(SENSORPIN1);
sensorState2 = digitalRead(SENSORPIN2);
if (sensorState1 == LOW){
digitalWrite(LEDPIN, HIGH);
}
if (sensorState2 == LOW){
digitalWrite (LEDPIN, LOW);
}
}
This code makes the led blink after the first sensor is made and stop blinking when the second sensor is made.
const byte SENSORPIN1 = 5;
const byte SENSORPIN2 = 6;
const byte LEDPIN = 8;
boolean blinkState = LOW;
boolean ledState = LOW;
int sensorState1 = 0, lastState1 = 0;
int sensorState2 = 0, lastState2 = 0;
unsigned long previousMillis = 0;
unsigned long currentMillis = 0;
const long interval = 250;
void setup() {
pinMode(SENSORPIN1, INPUT_PULLUP);
pinMode(SENSORPIN2, INPUT_PULLUP);
pinMode(LEDPIN, OUTPUT);
blinkState = HIGH;
}
void loop() {
led_switch();
led_blink();
}
void led_switch(){
sensorState1 = digitalRead(SENSORPIN1);
sensorState2 = digitalRead(SENSORPIN2);
if (sensorState1 == LOW){
blinkState = LOW;
}
if (sensorState2 == LOW){
blinkState = HIGH;
digitalWrite (LEDPIN, LOW);
}
}
void led_blink(){
unsigned long currentMillis = millis();
if (blinkState == LOW){
if (currentMillis - previousMillis >= interval){
previousMillis = currentMillis;
if (ledState == LOW){
ledState = HIGH;
}
else{
ledState = LOW;
}
digitalWrite(LEDPIN, ledState);
}
}
}
This code makes the led blink while sensor one is LOW after that when sensor one is HIGH the led stays on solid until sensor two is made.
const byte SENSORPIN1 = 5;
const byte SENSORPIN2 = 6;
const byte LEDPIN = 8;
boolean blinkState = LOW;
boolean ledState = LOW;
boolean blinkdone = false;
boolean event;
int sensorState1 = 0, lastState1 = 0;
int sensorState2 = 0, lastState2 = 0;
unsigned long previousMillis = 0;
unsigned long currentMillis = 0;
const long interval = 250;
void setup() {
pinMode(SENSORPIN1, INPUT_PULLUP);
pinMode(SENSORPIN2, INPUT_PULLUP);
pinMode(LEDPIN, OUTPUT);
blinkState = HIGH;
event = true;
}
void loop() {
led_switch();
led_blink();
}
void led_switch(){
sensorState1 = digitalRead(SENSORPIN1);
sensorState2 = digitalRead(SENSORPIN2);
if (sensorState1 == LOW){
blinkState = LOW;
}
if (sensorState1 == HIGH){
blinkState = HIGH;
digitalWrite (LEDPIN, LOW);
}
if ((blinkdone == true) && (sensorState1 == HIGH) && (sensorState2 == HIGH)){
digitalWrite (LEDPIN, HIGH);
event = false;
}
if (sensorState2 == LOW){
digitalWrite(LEDPIN, LOW);
blinkdone = false;
event = true;
}
}
void led_blink(){
unsigned long currentMillis = millis();
if (event == true){
if (blinkState == LOW){
if (currentMillis - previousMillis >= interval){
previousMillis = currentMillis;
if (ledState == LOW){
ledState = HIGH;
blinkdone = true;
}
else{
ledState = LOW;
}
digitalWrite(LEDPIN, ledState);
}
}
}
}