evening all, ive wrote this coding to have a rbg switch colours via a ir remote, the remote does everything as asked apart from when purple is selected the whole Arduino freezes and has to be reset to work again, all the other colours work as should and change with no issues. any help would be greatly apprcaited also and explaination to why it has to be done that way so I can understand better many thanks Michael.
///libary////
#include <IRremote.h>
////ir rec pin//////
#define RECV_PIN 2
///// led pins///////
#define REDP 11
#define GREENP 10
#define BLUEP 9
/////remote codes//////////
#define BLUE 16753245
#define RED 16736925
#define GREEN 16769565
#define YELLOW 16720605
#define PURPLE 16712445
#define AQUA 16761405
#define OFF 16769055
unsigned long preMillis;
unsigned long val;
IRrecv irrecv(RECV_PIN);
decode_results results;
void red(){
digitalWrite(REDP, HIGH);
digitalWrite(GREENP, LOW);
digitalWrite(BLUEP, LOW);
Serial.println("red!");
}
void green(){
digitalWrite(REDP, LOW);
digitalWrite(GREENP, HIGH);
digitalWrite(BLUEP, LOW);
Serial.println("green!");
}
void blue(){
digitalWrite(REDP, LOW);
digitalWrite(GREENP, LOW);
digitalWrite(BLUEP, HIGH);
Serial.println("blue!");
}
void yellow(){
digitalWrite(REDP, HIGH);
digitalWrite(GREENP, HIGH);
digitalWrite(BLUEP, LOW);
Serial.println("yellow!");
}
void purple(){
analogWrite(REDP, 80);
analogWrite(GREENP, 0);
analogWrite(BLUEP, 80);
Serial.println("purple!");
}
void aqua(){
digitalWrite(REDP, LOW);
digitalWrite(GREENP, HIGH);
digitalWrite(BLUEP, HIGH);
Serial.println("aqua!");
}
void off(){
digitalWrite(REDP, LOW);
digitalWrite(GREENP, LOW);
digitalWrite(BLUEP, LOW);
Serial.println("off!");
}
void setup() {
// put your setup code here, to run once:
pinMode(REDP, OUTPUT);
pinMode(GREENP, OUTPUT);
pinMode(BLUEP, OUTPUT);
Serial.begin(9600);
irrecv.enableIRIn();
}
void loop() {
// put your main code here, to run repeatedly:
if (irrecv.decode(&results)){
preMillis = millis();
val = results.value;
irrecv.resume();
switch(val){
case BLUE: blue(); break;
case RED: red(); break;
case GREEN: green(); break;
case YELLOW: yellow(); break;
case PURPLE: purple(); break;
case AQUA: aqua(); break;
case OFF: off(); break;
}
}
}