Last active
May 12, 2017 09:36
-
-
Save ElectricImpSampleCode/33b644ba0f9035ef088c to your computer and use it in GitHub Desktop.
Example code to control an LED connected to an imp. It makes use of the imp’s PWM_OUT GPIO pin setting.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Assign a global variable, led, to the imp001 pin to which the LED is connected | |
led <- hardware.pin9; | |
// Configure it for PWM output | |
led.configure(PWM_OUT, 1.0 / 400.0, 0.0); | |
// Record the state and delta of the LED's cycle | |
ledState <- 0.0; | |
ledChange <- 0.05; | |
// Define a function to pulse the LED | |
function pulse() { | |
// Write the state value to the pin | |
led.write(ledState); | |
// Change the state value | |
ledState = ledState + ledChange; | |
// Check if we're out of bounds and flip 'ledChange' if we are | |
if (ledState >= 1.0 || ledState <= 0.0) ledChange = ledChange * -1.0; | |
// Schedule the loop to run again in 0.05 seconds | |
imp.wakeup(0.05, pulse); | |
} | |
// Start the loop by calling pulse() | |
pulse(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment