source: orbit/arduino/libraries/PWM/examples/PWM_lib_example/PWM_lib_example.ino @ 1f70352

RawEEGRaw_EEG_PlotServoTab_Interfacepyramid
Last change on this file since 1f70352 was 1f70352, checked in by AzureViolin <zhanghaotz@…>, 9 years ago

arduino fly Orbit

  • Property mode set to 100644
File size: 1.5 KB
Line 
1/*
2
3 Mimics the fade example but with an extra parameter for frequency. It should dim but with a flicker
4 because the frequency has been set low enough for the human eye to detect. This flicker is easiest to see when
5 the LED is moving with respect to the eye and when it is between about 20% - 60% brighness. The library
6 allows for a frequency range from 1Hz - 2MHz on 16 bit timers and 31Hz - 2 MHz on 8 bit timers. When
7 SetPinFrequency()/SetPinFrequencySafe() is called, a bool is returned which can be tested to verify the
8 frequency was actually changed.
9 
10 This example runs on mega and uno.
11 */
12
13#include <PWM.h>
14
15//use pin 11 on the Mega instead, otherwise there is a frequency cap at 31 Hz
16int led = 9;                // the pin that the LED is attached to
17int brightness = 0;         // how bright the LED is
18int fadeAmount = 5;         // how many points to fade the LED by
19int32_t frequency = 35; //frequency (in Hz)
20
21void setup()
22{
23  //initialize all timers except for 0, to save time keeping functions
24  InitTimersSafe();
25
26  //sets the frequency for the specified pin
27  bool success = SetPinFrequencySafe(led, frequency);
28 
29  //if the pin frequency was set successfully, turn pin 13 on
30  if(success) {
31    pinMode(13, OUTPUT);
32    digitalWrite(13, HIGH);   
33  }
34}
35
36void loop()
37{
38  //use this functions instead of analogWrite on 'initialized' pins
39  pwmWrite(led, brightness);
40
41  brightness = brightness + fadeAmount;
42
43  if (brightness == 0 || brightness == 255) {
44    fadeAmount = -fadeAmount ;
45  }     
46 
47  delay(30);     
48}
49
Note: See TracBrowser for help on using the repository browser.