Rewrite this code so I can always check for the IR command rather than waiting for the…
- User
Rewrite this code so I can always check for the IR command rather than waiting for the entire loop operation to finish:
#include <IRremote.hpp>
const int irPin = 2;
const int pinRed = 3; const int pinGreen = 5; const int pinBlue = 6; const int pinPower = 9;
const int wait = 5; const int contrast = 255; // 0 - 255 int brightness = 255; // 0 - 255
void setup() { // Set the pin mode to output pinMode(pinRed, OUTPUT); pinMode(pinGreen, OUTPUT); pinMode(pinBlue, OUTPUT); // IR support IrReceiver.begin(irPin, ENABLE_LED_FEEDBACK); }
void loop() { // - == 21 // + == 9 // Read IR if (IrReceiver.decode()) { const int speed = 10; switch (IrReceiver.decodedIRData.command) { case 21: brightness -= speed; break; case 9: brightness += speed; break; } brightness = constrain(brightness, 0, 255); IrReceiver.resume(); } analogWrite(pinPower, brightness); // Red to yellow for (int i = 0; i <= contrast; i++) { analogWrite(pinRed, contrast); analogWrite(pinGreen, i); analogWrite(pinBlue, 0); delay(wait); }
// Yellow to green for (int i = contrast; i >= 0; i--) { analogWrite(pinRed, i); analogWrite(pinGreen, contrast); analogWrite(pinBlue, 0); delay(wait); }
// Green to cyan for (int i = 0; i <= contrast; i++) { analogWrite(pinRed, 0); analogWrite(pinGreen, contrast); analogWrite(pinBlue, i); delay(wait); }
// Cyan to blue for (int i = contrast; i >= 0; i--) { analogWrite(pinRed, 0); analogWrite(pinGreen, i); analogWrite(pinBlue, contrast); delay(wait); }
// Blue to magenta for (int i = 0; i <= contrast; i++) { analogWrite(pinRed, i); analogWrite(pinGreen, 0); analogWrite(pinBlue, contrast); delay(wait); }
// Magenta to red for (int i = contrast; i >= 0; i--) { analogWrite(pinRed, contrast); analogWrite(pinGreen, 0); analogWrite(pinBlue, i); delay(wait); } }
- Assistant