Last active
September 20, 2021 08:58
-
-
Save billykwok/fa2c939b6edadba22c995c1925b03e27 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 SAMPLING_RATE 100 | |
#define LED_RED 9 | |
#define LED_GREEN 10 | |
#define LED_BLUE 11 | |
#define POT_SPEED A3 | |
#define POT_RED A2 | |
#define POT_GREEN A1 | |
#define POT_BLUE A0 | |
const uint8_t PHASE = SAMPLING_RATE / 3; | |
uint8_t t = 0; | |
uint8_t dt = 1; | |
void setup() { | |
pinMode(LED_RED, OUTPUT); | |
pinMode(LED_GREEN, OUTPUT); | |
pinMode(LED_BLUE, OUTPUT); | |
Serial.begin(BAUD_RATE); | |
} | |
void loop() { | |
int r = (1023 - analogRead(POT_RED)) >> 2; | |
int g = (1023 - analogRead(POT_GREEN)) >> 2; | |
int b = (1023 - analogRead(POT_BLUE)) >> 2; | |
int dt = (1023 - analogRead(POT_SPEED)) >> 7; | |
if (dt == 0) { | |
analogWrite(LED_RED, r); | |
analogWrite(LED_GREEN, g); | |
analogWrite(LED_BLUE, b); | |
} else { | |
double rx = 0.5 + 0.5 * sin(2 * PI * t / SAMPLING_RATE); | |
double gx = 0.5 + 0.5 * sin(2 * PI * (t + PHASE) / SAMPLING_RATE); | |
double bx = 0.5 + 0.5 * sin(2 * PI * (t + 2 * PHASE) / SAMPLING_RATE); | |
analogWrite(LED_RED, r * rx); | |
analogWrite(LED_GREEN, g * gx); | |
analogWrite(LED_BLUE, b * bx); | |
} | |
t = (t + dt) % SAMPLING_RATE; | |
delay(10); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment