Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define REDPIN 5
- #define GREENPIN 6
- #define BLUEPIN 3
- int currentR = 0;
- int currentG = 0;
- int currentB = 0;
- bool pulse = false;
- bool inProgress = false;
- unsigned long currentMillis = millis();
- bool alternating = false;;
- void setup() {
- pinMode(REDPIN, OUTPUT);
- pinMode(GREENPIN, OUTPUT);
- pinMode(BLUEPIN, OUTPUT);
- Serial.begin(9600);
- }
- void loop() {
- if (pulse) {
- if (!alternating) {
- SmoothFade(currentR, currentG, currentB, 0, 0, 0);
- alternating = true;
- }
- else {
- SmoothFade(0, 0, 0, currentR, currentG, currentB);
- alternating = false;
- }
- }
- }
- void serialEvent() {
- if (Serial.available() == 1) {
- String input = Serial.readString();
- if (input[0] == 35) {
- long number = strtol(&input[1], NULL, 16);
- long r = number >> 16;
- long g = number >> 8 & 0xFF;
- long b = number & 0xFF;
- Serial.println("Color set to: " + input);
- SmoothFade(currentR, currentG, currentB, r, g, b);
- currentR = r;
- currentG = g;
- currentB = b;
- }
- if (input == "pulse") {
- if (pulse == false) {
- pulse = true;
- Serial.println("Pulsing!");
- }
- else {
- pulse = false;
- Serial.println("Pulsing NOT!");
- }
- }
- }
- }
- void SmoothFade(int currR, int currG, int currB, int r, int g, int b) {
- int curR = currR, curG = currG, curB = currB;
- int stepR = curR - r, stepG = curG - g, stepB = curB - b;
- for (int i = 255; i > 0; i--)
- {
- if (i % (255 / abs(stepR)) == 0 && stepR != 0) {
- if (stepR < 0 && curR < 255) {
- curR++;
- }
- else if (curR > 0) {
- curR--;
- }
- analogWrite(REDPIN, curR);
- }
- if (i % (255 / abs(stepG)) == 0 && stepG != 0) {
- if (stepG < 0 && curG < 255) {
- curG++;
- }
- else if (curG > 0) {
- curG--;
- }
- analogWrite(GREENPIN, curG);
- }
- if (i % (255 / abs(stepB)) == 0 && stepB != 0) {
- if (stepB < 0 && curB < 255) {
- curB++;
- }
- else if (curB > 0) {
- curB--;
- }
- analogWrite(BLUEPIN, curB);
- }
- delay(5);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement