Last active
September 17, 2021 04:35
-
-
Save billykwok/2a2f78bb80a96f33ea27dae929baf0ae to your computer and use it in GitHub Desktop.
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
#define BAUD_RATE 9600 | |
#define DELIMITER '\n' | |
#define SAMPLING_RATE 100 | |
#define LED_RED 9 | |
#define LED_GREEN 10 | |
#define LED_BLUE 11 | |
int t = 0; | |
int dt = 1; | |
int elapse = 1; | |
uint8_t rgb[3]; | |
void setup() { | |
pinMode(LED_RED, OUTPUT); | |
pinMode(LED_GREEN, OUTPUT); | |
pinMode(LED_BLUE, OUTPUT); | |
Serial.begin(BAUD_RATE); | |
Serial.println("READY"); | |
} | |
void loop() { | |
if (Serial.available()) { | |
int n = Serial.readBytesUntil(DELIMITER, rgb, sizeof(rgb)); | |
} | |
if (elapse <= 0) { | |
int x = rand(); | |
if (x % 3 == 0) { | |
dt = x % 2 + 2; | |
} else { | |
dt = 1; | |
} | |
elapse = SAMPLING_RATE / dt; | |
} else { | |
--elapse; | |
} | |
t = (t + dt) % SAMPLING_RATE; | |
double strength = 0.75 + 0.25 * sin(2 * PI * t / SAMPLING_RATE); | |
analogWrite(LED_RED, round(strength * rgb[0])); | |
analogWrite(LED_GREEN, round(strength * rgb[1])); | |
analogWrite(LED_BLUE, round(strength * rgb[2])); | |
delay(20); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment