Model Scenery Tutorials

Comprenshive Tips. Hints And Techniques For Budget Minded Diorama Maker, Model Train Enthusiast And Wargammer

Arduino Sensor Controlled Flashing Lights

Flashing lights alternate base on output of IR sensor.  Uses Arduino Nano or above.

This sketch can be modified to be used in many projects.

The code is listed here.  Wiring diagrams and a video are in the editing queue.  If you need them, contact us and we will bump them up for you.

 

[code]
/*Controlling flashing lights with an IR sensor. One of two lights will flash based on sensor output.” */

#define Sensor A0 //connects to output pin of sensor
#define ledcw 8 // connect led 1 to this pin
#define ledccw 9 // connect led 2 to this pin
int LEDflash;
long flashTime = 1000;

//millis()
long t1 = 0;
long t2;

void setup() {
Serial.begin(9600);
pinMode(Sensor, INPUT);
pinMode(ledcw, OUTPUT);
pinMode(ledccw, OUTPUT);
LEDflash = 1;
t2 = millis();
}

void loop() {

int valSensor = analogRead(Sensor);

//LED Control
// —————————-
t1 = (millis() – t2);
if (valSensor > 500) {
digitalWrite(ledccw, LOW); //LED ccw should be ‘off’

if (t1 >= flashTime) { // flash timer has timed-out – swap ‘on’ and ‘off’
LEDflash = 1 – LEDflash;
t2 = millis();
Serial.println(t1);
Serial.print(“CW direction “);
Serial.println(LEDflash);
}
digitalWrite(ledcw, LEDflash);
}

//——————————–changing from CW to CCW

if (valSensor <=500) {
digitalWrite(ledcw, LOW); //LED cw should be ‘off’

if (t1 >= flashTime) { //flash timer has timed out
LEDflash = 1 – LEDflash;
t2 = millis();
Serial.println(t1);
Serial.print(“CCW direction “);
Serial.println(LEDflash);
}
digitalWrite(ledccw, LEDflash);
}
}
[/code]

Scroll to top