/*
The circuit:
* LED (+) attached to pin 1.
* motion sensor 'signal' will be attached to pin 2
* digispark microcontroller
last edit 8/17/2017
By John A. Austin
//IMPORTANT NOTE: reverse logic is required based on LED driver board's PWM pin functionality
*/
//*******************************************************************************************
int sensorPin = 2; //motion sensor out pin connected to digital pin 2 (P2 on digispark)
int ledPin = 1; // LED driver module's PWM pin is connected to digital pin 1 (P1 on digispark)
int delay1 = 9999;
int delay2 = 1;
const int incrValue_up = 10;
const int incrValue_dn = 30;
int y = 1;
int x = 1;
//********************************************************************************************
void setup()
{
// initialize the sensorPin as an input:
pinMode(sensorPin, INPUT);
// initialize the ledPin as an output:
pinMode(ledPin, OUTPUT);
}
void INCR_UP()
{
delay1=delay1-incrValue_up; //incremental change to pulse width value
delay2=delay2+incrValue_up; //increment change to pulse width value
}
void INCR_DN()
{
delay1=delay1-incrValue_dn; //incremental change to pulse width value
delay2=delay2+incrValue_dn; //increment change to pulse width value
}
void PWM_UP() //loops 6 times for a total of .06 seconds at 100Hz
{
for ( int x = 1; x <= 6; x++)
{
digitalWrite(ledPin,HIGH);
delayMicroseconds(delay1); //decrease duty cycle at 100Hz - delay #1 - LOW
digitalWrite(ledPin,LOW);
delayMicroseconds(delay2); //delay #2 - HIGH
}}
void PWM_DN() //loops 6 times for a total of .06 seconds at 100Hz
{
for ( int x = 1; x <= 6; x++)
{
digitalWrite(ledPin,LOW);
delayMicroseconds(delay1); //increase duty cycle at 100Hz - delay #1 - HIGH
digitalWrite(ledPin,HIGH);
delayMicroseconds(delay2); //delay #2 - LOW
}}
//SKETCH FLOW:
//motion sensor triggers pin2 on digispark
//PWM_UP is called and loops 6 times for a total of .06 seconds
//then back to main code and INCR() is called to change the pulse width (HIGH/LOW) values
//INCR() is called after each loop to change the pulse width (HIGH/LOW) values by 10
//the looping increment is increased by one and the loop starts over
//after 1000 loops and 60 seconds have passed, the loop count variable is set back to 1
//the LEDS are held at full brightness for 5 minutes (10 seconds for testing)
//PWM_DN is then called and loops 6 times for a total of .06 seconds
//then back to main code and INCR() is called to change the pulse width (HIGH/LOW) values by 30
//the looping increment is increased by one and the loop starts over
//after 333 loops and 20 seconds have passed, sketch should to go back to the start and read the sensor pin again
//*************************************************************************************************
// REVERSE LOGIC APPLIES BASED ON PWM PIN ON LED DRIVER BOARD (HIGH=OFF, LOW=ON)
void loop()
{
if(digitalRead(sensorPin) == LOW)
{
delay(5000); // wait 5 seconds, then check the sensorPin again
loop;
}
else
{
for (int y = 1; y <= 1000; y++) // 60 seconds to fade up
{
PWM_UP();
INCR_UP();
}
// LEDs fade up to full brightness as expected
{
digitalWrite(ledPin,HIGH); // led off
delay(2000); // 2 seconds
}
{
digitalWrite(ledPin,LOW); // led on
delay(10000); // 10 seconds
}
{
digitalWrite(ledPin,HIGH); // led off
delay(2000); // 2 seconds
}
// LEDs come on for 2 seconds then at full brightness for 10 seconds, then back off for 2 seconds.
// Good to this point
// moving on to the fade down sequence below, the leds start flashing rapidly in what appears
// to be an endless loop. Power has to be disconnected.
{
}
// flashing starts here
for (int y = 1; y <= 333; y++) // 20 seconds to fade down
{
PWM_DN();
INCR_DN();
}
{
digitalWrite(ledPin, HIGH);
}}}