Skip to main content

emergency program

// IR receiver program
// Connect LEDs to pin 13, 12, 11
// Connect the receiver input to digital pin 5
// 06_07_2013 Stephen Pithouse
long pattern[20];
long patternStart = 0;
long time = 0;
int a = 0;
int redTog = 0;
int yellowTog = 0;
int greenTog = 0;

long red[] = {332, 2036, 2372, 3072, 3380, 4116, 4452, 6168, 6496, 7196, 7504, 9248, 9584, 10284, 10592, 11308, 11616, 12344, 12680, 13372
};

long yellow[] = {304, 2048, 2344, 3076, 3380, 4120, 4428, 6172, 6468, 8216, 8520, 9252, 9556, 10284, 10584, 11312, 11620, 12348, 12656, 14388
};

long green[] = {2328, 2036, 2368, 3068, 3376, 4116, 4420, 5152, 5456, 7192, 7500, 8228, 8536, 10280, 10608, 11308, 11624, 12340, 12676, 13368
};

void setup() {
pinMode(5, INPUT); //IR receiver input
pinMode(11, OUTPUT); //LED
pinMode(12, OUTPUT); //LED
pinMode(13, OUTPUT); //LED
pinMode(10, OUTPUT);
digitalWrite(10, LOW);
Serial.begin(115200);
}
void loop() {

getPattern();
compare();
delay(250);

}

void getPattern() {
while (digitalRead(5) == HIGH) { //wait for the pattern to start
}
time = millis(); //Reset loop varibles
patternStart = micros(); // "
a = 0; // "
while (millis() - time < 200) { //record first 200mS
if (digitalRead(5) == LOW) { //Trough in signal
while (digitalRead(5) == LOW) {
pattern[a] = micros() - patternStart; //Record time until trough ends
}
a++;
}
if (digitalRead(5) == HIGH) { //Peak in signal
while (digitalRead(5) == HIGH) {
pattern[a] = micros() - patternStart; //Record time until peak ends
}
a++;
}
}
debug();
}
void debug() { //Prints the pulse code in two forms
// for (int b = 0; b < 20; ++b) { //Display first 20 times
// Serial.println(pattern[b]); //Column Form
// }
for (int b = 0; b < 19; ++b) { //Display first 20 times
Serial.print(pattern[b]); //Single line form - suitable to be copied into program
Serial.print(", ");
}
Serial.println(pattern[19]);
}
void compare() {
int redDif = 0;
int yellowDif = 0;
int greenDif = 0;
for (int c = 10; c < 19; c++) {
redDif += abs(pattern[c] - red[c]) / 100;
yellowDif += abs(pattern[c] - yellow[c]) / 100;
greenDif += abs(pattern[c] - green[c]) / 100;
}
if (redDif < 20 || yellowDif < 20 || greenDif < 20) {
if (redDif < yellowDif && redDif < greenDif) {
Serial.println("RED!");
redToggle(11);
}
else if (yellowDif < greenDif) {
Serial.println("YELLOW!");
yellowToggle(12);
}
else if (greenDif < 20) {
Serial.println("GREEN!");
greenToggle(13);
}
}
else
{
Serial.println("No match!");
}
Serial.println(redDif);
Serial.println(yellowDif);
Serial.println(greenDif);
}
void redToggle(int colour) {
if (redTog == 1) { //Currently on .: turn off
digitalWrite(colour, LOW); //Turn off
}
else { //Currently off .: turn on
digitalWrite(colour, HIGH);
}
redTog = abs(redTog - 1);
}
void yellowToggle(int colour) {
if (yellowTog == 1) { //Currently on .: turn off
digitalWrite(colour, LOW); //Turn off
}
else { //Currently off .: turn on
digitalWrite(colour, HIGH);
}
yellowTog = abs(yellowTog - 1);
}
void greenToggle(int colour) {
if (greenTog == 1) { //Currently on .: turn off
digitalWrite(colour, LOW); //Turn off
}
else { //Currently off .: turn on
digitalWrite(colour, HIGH);
}
greenTog = abs(greenTog - 1);
}