/*
VISIBLE_FEEDER_LIGHT_SKETCH_700ma
The circuit:
* LED (+) attached to PWM pin 1.
* motion sensor 'signal' attached to pin 2
* digispark microcontroller
last edit 8/17/2017
By John A. Austin
*/
int sensorPin = 2; //motion sensor out pin connected to digital pin 2 (P2 on digispark)
int ledPin = 1; // LED connected to digital pin 1 (P1 on digispark)
int fadeValue = 255; //initial led brightness (variable)
//int analogValue = analogRead(ledPin);
void setup()
{
// initialize the sensorPin as an input:
pinMode(sensorPin, INPUT);
// initialize the ledPin as an output:
pinMode(ledPin, OUTPUT);
}
//FUNCTIONALITY is reverse logic based on LED driver board PWM pin:
//read status of motion sensor (HIGH or LOW)
//If HIGH, fade leds from off to on over a one minute period
//then fade to off over a 32 second period
//wait 5 seconds and read sensor pin again
void loop()
{
analogWrite(ledPin, 255);
delay(5000);
if(digitalRead(sensorPin) == LOW) //if no signal is sent to P2 from sensor, it will loop back to start, then delay 5 seconds and read again:
{
loop;
}
else
{
for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 1)
{
analogWrite(ledPin, fadeValue);
delay(235);
}
{
analogWrite(ledPin, 0);
delay(300000);
}
{
for (int fadeValue = 0 ; fadeValue <= 255 ; fadeValue += 1)
{
analogWrite(ledPin, fadeValue);
delay(125);
}
}
}
}
}
//loop while ...
//digitalWrite(ledPin,1);
//delayMicroseconds (4750); //95% duty cycle at 200Mz - delay 1
//digitalWrite(ledPin,0);
//delayMicroseconds(250); //delay 2
//end loop
//where 0<x<5000
Yes, and the 100Hz problem is as simple as :
pin=some pin; //a contant.
Per=1e6/PwmFreq; //a constant.
x=3900; //a variable related to duty cycle such that 0<x<Per
Loop while ... //some kind of loop...
digitalwrite(pin,high);end loop;
delaymicroseconds (x);
digitalwrite(pin,low);
delaymicroseconds(Per-x);
Actually still confused..... I need my leds to fade on slowly over a 60 second time period, hold at HIGH for 5 minutes, then slowly fade down. I probably need to keep my sketch but need to figure out how to accommodate the 200Hz..
I ran across that video several days ago. The problem is he simply indicates that the board will handle PWM by attaching a AA battery and nothing further.You might find this video helpful, seems it is the same chip as your board.
Though it looks at the PWM pins only so far as showing how to switch off the led; it does not mention the pwm frequency.
Not seen any mention of using these boards with PWM and nothing in the Arduino arena, which make me suspicious.
Now you can see / have it wired correctly, have you tried a simple sketch using the C++ AnalogueWrite at 500hz to see if it does run with that frequency ? eg the /Examples/ Analogue/ Fading sketch from the Arduino ide
Yes, make the code snippet I posted a local function which will replace AnalogWrite(). Say we call it PWM(). In addition to pin and value, it will need a third argument passed to it which is how long (duration, based on time) to do the PWM loop before returning to the calling code.
To do the slow fade over 60 seconds, call the new PWM(pin,value,duration) function 60 times, each time the pin=constant and duration=1s, but value will change second by second.
To do the 5min constant on, I would just set the pin high and use a variant of this to do the timing.
Mike,In addition to pin and value, it will need a third argument passed to it which is how long (duration, based on time) to do the PWM loop before returning to the calling code.
To do the slow fade over 60 seconds, call the new PWM(pin,value,duration) function 60 times, each time the pin=constant and duration=1s, but value will change second by second.
/*
VISIBLE_FEEDER_LIGHT_SKETCH_700ma
The circuit:
* motion sensor 'signal' will be attached to pin 2
* digispark microcontroller
* digispark's Pin 1 is connected to the PWM pin on the XL4001 led driver module (reverse logic)
last edit 8/17/2017
By John A. Austin
*/
int sensorPin = 2; //motion sensor out pin connected to digital pin 2 (P2 on digispark)
int ledPin = 1; // LED connected to digital pin 1 (P1 on digispark)
int delay1 = 0; //HIGH pulse
int delay2 = 0; //LOW pulse
const int delayValue = 20;
void setup()
{
// initialize the sensorPin as an input:
pinMode(sensorPin, INPUT);
// initialize the ledPin as an output:
pinMode(ledPin, OUTPUT);
}
void PWM_UP() //to be called 180 times
{
while(0<delay1>5000);
digitalWrite(ledPin,1);
delayMicroseconds(delay1); //increase duty cycle at 200Hz - delay #1 - HIGH
digitalWrite(ledPin,0);
delayMicroseconds(delay2); //delay #2 - LOW
delay1 = delay1+delayValue;
delay2 = delay2-delayValue;
}
void PWM_DN() //to be called 180 times
{
while(0<delay1>5000);
digitalWrite(ledPin,0);
delayMicroseconds(delay1); //increase duty cycle at 200Hz - delay #1 - HIGH
digitalWrite(ledPin,0);
delayMicroseconds(delay2); //delay #2 - LOW
delay1 = delay1-delayValue;
delay2 = delay2+delayValue;
}
//FUNCTIONALITY: (REVERSE LOGIC FOR PWM ON DRIVER MODULE)
//read status of motion sensor (HIGH or LOW)
//If HIGH, fade leds from off to on over a one minute period
//then fade to off over a 32 second period
//wait 5 seconds and read sensor pin again
//IMPORTANT NOTE: reverse logic is required based on LED driver board's PWM pin functionality
void loop()
{
analogWrite(ledPin,255);
delay(5000);
if(digitalRead(sensorPin) == LOW) //if no signal is sent to P2 from motion sensor, then loop:
{
loop;
}
else
{
PWM_DN();
}
{
analogWrite(ledPin, 0); //remember reverse logic is required
delay(300000); //hold LED at full brightness for 5 minutes
}
PWM_UP();
}
The simplest way of doing anything 180 times is the For statement.
The index variable used by the For statement can be the PWM duty cycle argument directly.
void PWM_UP() //to be called 60 times
{
while(0<delay1>5000);
digitalWrite(ledPin,HIGH);
delayMicroseconds(delay1); //increase duty cycle at 200Hz - delay #1 - HIGH
digitalWrite(ledPin,LOW);
delayMicroseconds(delay2); //delay #2 - LOW
delay1=delay1+delayValue;
delay2=5000-delay1;
}
void PWM_DN() //to be called 180 times
{
while(0<delay1>5000);
digitalWrite(ledPin,HIGH);
delayMicroseconds(delay1); //increase duty cycle at 200Hz - delay #1 - HIGH
digitalWrite(ledPin,LOW);
delayMicroseconds(delay2); //delay #2 - LOW
delay1=5000-delayValue;
delay2=delay2+delay1;
}
void loop()
{
analogWrite(ledPin,255);
delay(5000);
if(digitalRead(sensorPin) == LOW) //if no signal is sent to P2 from motion sensor, then loop:
{
loop;
}
else
{
for (int i=0; i <= 180; i++)
{
PWM_DN();
}
{
analogWrite(ledPin, 255); //remember reverse logic is required
delay(10000); //hold LED at full brightness for 10 seconds for testing
}
for (i=0; i <= 60; i++)
{
PWM_UP();
}
[QUOTE="MikeMl, post: 1303091, member: 125427"]The index variable used by the For statement can be the PWM duty cycle argument directly.[/QUOTE]
}
}
To do the slow fade over 60 seconds, call the new PWM(pin,value,duration) function 60 times, each time the pin=constant and duration=1s, but value will change second by second.
The index variable used by the For statement can be the PWM duty cycle argument directly.
/*
VISIBLE_FEEDER_LIGHT_SKETCH_700ma
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
*/
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; //HIGH pulse
int delay2 = 0; //LOW pulse
const int delayValue = 20;
int i = 1;
int ledLvl=1;
void setup()
{
// initialize the sensorPin as an input:
pinMode(sensorPin, INPUT);
// initialize the ledPin as an output:
pinMode(ledPin, OUTPUT);
}
void PWM_UP() //to be called 500 times
{
while(0<delay1>10000) {
digitalWrite(ledPin,LOW);
delayMicroseconds(delay1); //decrease duty cycle at 100Hz - delay #1 - LOW
digitalWrite(ledPin,HIGH);
delayMicroseconds(delay2); //delay #2 - HIGH
delay1=delay1-delayValue;
delay2=delay2+delayValue;
}
void PWM_DN() //to be called 500 times
{
while(0<delay1>10000) {
digitalWrite(ledPin,HIGH);
delayMicroseconds(delay1); //increase duty cycle at 100Hz - delay #1 - HIGH
digitalWrite(ledPin,LOW);
delayMicroseconds(delay2); //delay #2 - LOW
delay1=delay1-delayValue;
delay2=delay2+delayValue;
}
//FUNCTIONALITY: (WILL CHANGE TO REVERSE LOGIC FOR PWM ON DRIVER MODULE)
//read status of motion sensor (HIGH or LOW)
//If HIGH, fade leds from off to on over a one minute period
//then fade to off over a 32 second period
//wait 5 seconds and read sensor pin again
//IMPORTANT NOTE: reverse logic is required based on LED driver board's PWM pin functionality
//REVERSE LOGIC - CHANGE AFTER TESTING
void loop()
{
analogWrite(ledPin,0);
delay(5000);
if(digitalRead(sensorPin) == LOW) //if no signal is sent to P2 from motion sensor, then loop:
{
loop;
}
else
{
for (int i=0; i <= 500; i++)
{
PWM_UP();
ledLvl=analogRead(ledPin);
}
{
analogWrite(ledPin, ledLvl);
delay(10000);
}
{
for (i=0; i <= 500; i++)
{
PWM_DN();
ledLvl=analogRead(ledPin);
}
analogWrite(ledPin, ledLvl);
delay(10000);
}
}
}
}
}
}
I think "}" is missing at the end of the PWM_UP() and PWM_DN() functions.
/*
VISIBLE_FEEDER_LIGHT_SKETCH_700ma
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
*/
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; //HIGH pulse
int delay2 = 0; //LOW pulse
const int delayValue = 20;
int i = 1;
int ledLvl=1;
void setup()
{
// initialize the sensorPin as an input:
pinMode(sensorPin, INPUT);
// initialize the ledPin as an output:
pinMode(ledPin, OUTPUT);
}
void PWM_UP()
{
//for (int i=0; i <= 180; i++) //need to add .333 sec delay somewhere for a 1 min overall fade up duration
while(0<delay1>10000)
digitalWrite(ledPin,LOW);
delayMicroseconds(delay1); //decrease duty cycle at 100Hz - delay #1 - LOW
digitalWrite(ledPin,HIGH);
delayMicroseconds(delay2); //delay #2 - HIGH
delay1=delay1-delayValue;
delay2=delay2+delayValue;
}
void PWM_DN()
{
//for (int i=0; i <= 60; i++) //need to add .333 sec delay somewhere for a 20 sec overall fade down duration
while(0<delay1>10000)
digitalWrite(ledPin,HIGH);
delayMicroseconds(delay1); //increase duty cycle at 100Hz - delay #1 - HIGH
digitalWrite(ledPin,LOW);
delayMicroseconds(delay2); //delay #2 - LOW
delay1=delay1-delayValue;
delay2=delay2+delayValue;
}
//FUNCTIONALITY: (WILL CHANGE TO REVERSE LOGIC FOR PWM ON DRIVER MODULE)
//read status of motion sensor (HIGH or LOW)
//If HIGH, fade leds from off to on over a one minute period
//Hold leds at 100% brightness for 5 minutes // this delay needs to be added back in
//then fade to off over a 20 second period
//wait 5 seconds and read sensor pin again //this delay needs to be added back in
//IMPORTANT NOTE: reverse logic is required based on LED driver board's PWM pin functionality
//REVERSE LOGIC - CHANGE AFTER TESTING
void loop()
{
if(digitalRead(sensorPin) == LOW) //if no signal is sent to P2 from motion sensor, then loop:
{
loop;
}
else
{
{
PWM_UP();
}
//analogWrite(ledPin, 255); //set leds at full brightness
//delay(300000); //hold at full brightness for 5 minutes
{
PWM_DN();
}
//delay(5000);
}
}
,but isn't the 'duty cycle' a specific analog value from the digispark at a 500Hz frequency?? This is the main reason I am confused. In my mind, I have the two functions that adjust the pulse widths of the low/high in a fade up or fade down direction at 100Hz. So, I need to somehow have those functions loop at specific pulse width combinations (brightness levels) for 1/3 seconds before adjusting the pulse width to the next level, either brighter or dimmer.The index variable used by the For statement can be the PWM duty cycle argument directly.
/*
The circuit:
* LED (+) attached to pin 1.
* motion sensor 'signal' will be attached to pin 2
* digispark microcontroller
last edit 8/19/2017
By John A. Austin
*/
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; //HIGH pulse
int delay2 = 0; //LOW pulse
const int delayValue = 20;
int i = 1;
int ledLvl=1;
int x = 0;
void setup()
{
// initialize the sensorPin as an input:
pinMode(sensorPin, INPUT);
// initialize the ledPin as an output:
pinMode(ledPin, OUTPUT);
}
//FUNCTIONALITY: (WILL CHANGE TO REVERSE LOGIC FOR PWM ON DRIVER MODULE)
//read status of motion sensor (HIGH or LOW)
//If HIGH, fade leds from off to on over a one minute period
//then fade to off over a 32 second period
//wait 5 seconds and read sensor pin again
//IMPORTANT NOTE: reverse logic is required based on LED driver board's PWM pin functionality
//REVERSE LOGIC - CHANGE AFTER TESTING
void loop()
{
if(digitalRead(sensorPin) == LOW) //if no signal is sent to P2 from motion sensor, then loop:
{
loop;
}
else
{
void PWM_UP()
while(0<delay1>10000)
{
digitalWrite(ledPin,LOW);
delayMicroseconds(delay1);
digitalWrite(ledPin,HIGH);
delayMicroseconds(delay2);
delay1=delay1-delayValue;
delay2=delay2+delayValue;
PWM();
}
{
void PWM()
{
void PWM_DN()
while(0<delay1>10000)
{
digitalWrite(ledPin,HIGH);
delayMicroseconds(delay1);
digitalWrite(ledPin,LOW);
delayMicroseconds(delay2);
delay1=delay1-delayValue;
delay2=delay2+delayValue;
PWM();
}
}
//analogWrite(ledPin, 255); //set leds at full brightness
//delay(300000); //hold at full brightness for 5 minutes
}
}
}
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?