#include // define all the pins used #define LED 5 #define PROX 9 #define ADJ 8 #define PWRMODE 10 #define TX 1 #define RX 2 #define CONTROL 6 // define global variables volatile int brightness = 0; volatile boolean dir = true; volatile boolean detect = false; volatile int counter = 0; // for debugging purposes SoftwareSerial mySerial(RX, TX); // function to reset the proximity detector void resetProx() { digitalWrite(PWRMODE, LOW); delay(5); digitalWrite(PWRMODE, HIGH); } void setup() { pinMode(LED, OUTPUT); pinMode(PROX, INPUT); pinMode(ADJ, OUTPUT); pinMode(PWRMODE, OUTPUT); // Proximity sensor parameters // LOW ADJ means higher sensitivity, HIGH means lowest sensitivity // HIGH PWRMODE means normal mode, LOW means sleep mode digitalWrite(PWRMODE, HIGH); digitalWrite(ADJ, HIGH); // analogWrite(ADJ, 130); mySerial.begin(9600); // allow 1 second before sensing delay(1000); } void loop() { if (digitalRead(PROX) == LOW) { if (detect == false && counter < 500) { if (brightness > 10) { brightness = 0; dir = true; } else { brightness = 254; dir = false; } } detect = true; counter++; } else { detect = false; counter = 0; } if (counter > 500 && detect == true) { if (dir == true) { brightness++; } if (dir == false) { brightness--; } // define the direction of the brightness change if (brightness > 254) { dir = false; delay(500); } if (brightness < 1) { dir = true; delay(500); } delay(10); } analogWrite(LED, brightness); analogWrite(CONTROL, brightness); delay(1); }