/* * Based on the code "rgb_spectrum.c" by jamesotron. https://gist.github.com/jamesotron/766994 * Adapted by Rodrigo Gamboa. FabAcademy2019. * http://fabacademy.org/2019/labs/ied/students/rodrigo-gamboa/week12.html */ const int redPin = 2; const int greenPin = 0; const int bluePin = 1; int buttonPin1 = 3; int buttonPin2 = 4; int buttonState1, buttonState2 = 0; int lastButtonState1, lastButtonState2 = 0; int count1, count2 = 0; void setup() { // Start off with the LED off. setColourRgb(0, 0, 0); pinMode(buttonPin1, INPUT); pinMode(buttonPin2, INPUT); digitalWrite(redPin, HIGH); digitalWrite(greenPin, HIGH); digitalWrite(bluePin, HIGH); } void loop() { buttons(); unsigned int rgbColour[3]; // Start off with red. rgbColour[0] = 255; rgbColour[1] = 0; rgbColour[2] = 0; // Choose the colours to increment and decrement. for (int decColour = 0; decColour < 3; decColour += 1) { int incColour = decColour == 2 ? 0 : decColour + 1; // cross-fade the two colours. for (int i = 0; i < 255; i += 1) { rgbColour[decColour] -= 1; rgbColour[incColour] += 1; if (count1 == 1 && count2 == 1) { setColourRgb(rgbColour[0], rgbColour[1], rgbColour[2]); delay(5); } } } } void setColourRgb(unsigned int red, unsigned int green, unsigned int blue) { analogWrite(redPin, red); analogWrite(greenPin, green); analogWrite(bluePin, blue); } void buttons() { buttonState1 = digitalRead(buttonPin1); if (buttonState1 != lastButtonState1) { if (buttonState1 == HIGH) { count1 = 1; } } lastButtonState1 = buttonState1; buttonState2 = digitalRead(buttonPin2); if (buttonState2 != lastButtonState2) { if (buttonState2 == HIGH) { count2 = 1; } } lastButtonState2 = buttonState2; }