Created
April 30, 2022 19:58
-
-
Save paseaf/e88f2019b553a3caeafeaa8d892d617f to your computer and use it in GitHub Desktop.
arduino blinking LED with different frequencies
This file contains hidden or 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
const float femaleMaleRatio = 95.0/65; | |
const int maleLight = 3; // PIN | |
const int maleBrightness = 190; // brightness for male light. value should be in [0, 255] | |
const int maleInterval = 500; // after how many ms the male light should be switched on/off | |
const int femaleLight = 11; // PIN | |
const int femaleBrightness = maleBrightness * femaleMaleRatio; | |
const int femaleInterval = maleInterval * femaleMaleRatio; | |
// the setup function runs once when you press reset or power the board | |
void setup() { | |
pinMode(maleLight, OUTPUT); | |
pinMode(femaleLight, OUTPUT); | |
} | |
// checks if a light should be active based on its interval | |
bool isActive(unsigned int interval) { | |
unsigned long currentTime = millis(); | |
return currentTime % (2 * interval) > interval ? true : false; | |
} | |
// the loop function runs over and over again forever | |
void loop() { | |
analogWrite(maleLight, maleBrightness * isActive(maleInterval)); // switch on/off male light | |
analogWrite(femaleLight, femaleBrightness * isActive(femaleInterval)); // switch on/off female light | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment