// "The Arduino Kick Starter" Project // For ICStation.com's 2015 DIY competition // See it in action: https://www.youtube.com/watch?v=lrRGC9Ypedc // Code for Sender Module // by Ido Gendel, 2015 // Share and enjoy! #include #include #define MAX_DELAY_MS 5000 #define MIN_DELAY_MS 1000 #define MAX_WAIT_MS 1000 #define FEEDBACK_MS 1000 const int RSEED_ADDRESS = 345; const byte sensorPin = 2; volatile byte movementDetected; // RGB controls struct tLightRec { byte pin; byte state; } light[3] {{12, 0}, {13, 0}, {11, 0}}; //----------------------------------------------------------- void setLight(const byte index, const byte newState) { light[index].state = newState; digitalWrite(light[index].pin, light[index].state); } // setLight //----------------------------------------------------------- void rotateLights(const byte cycles = 60) { // LEDs on strip are GRB... byte actualOrder[3] = {1, 0, 2}; byte currentIndex = 0; for (int cycle = 0; cycle < cycles; cycle++) { setLight(actualOrder[currentIndex], 0); currentIndex = (currentIndex + 1) % 3; setLight(actualOrder[currentIndex], 1); delay(80); } // for // "Cleanup" setLight(actualOrder[currentIndex], 0); } // rotateLights //----------------------------------------------------------- unsigned int getRSeed() { unsigned int r; r = EEPROM.read(RSEED_ADDRESS); r <<= 8; r += EEPROM.read(RSEED_ADDRESS + 1); return r; } // getRSeed //----------------------------------------------------------- void setRSeed(unsigned int r) { EEPROM.write(RSEED_ADDRESS + 1, r & 255); EEPROM.write(RSEED_ADDRESS, r >> 8); } // setRSeed //----------------------------------------------------------- void setup() { unsigned int RSeed; for (byte i = 0; i < 3; i++) pinMode(light[i].pin, OUTPUT); // Initialise the IO and ISR vw_set_tx_pin(A1); vw_set_rx_pin(9); // Prevent rx from disturbing pin 11! vw_set_ptt_inverted(true); // Required for DR3100 vw_setup(2000); // Bits per sec // Prevent repeating random patterns after reset RSeed = getRSeed(); randomSeed(RSeed); setRSeed(RSeed + 1); rotateLights(); delay(5000); pinMode(sensorPin, INPUT_PULLUP); attachInterrupt(0, sensorISR, FALLING); } // setup //----------------------------------------------------------- void sensorISR() { movementDetected = 1; } //----------------------------------------------------------- void loop() { char failMsg[] = "FA1L"; char timeMsg[] = "0000"; byte success = 0; unsigned long timerStart, timerStop; delay(MIN_DELAY_MS + random(MAX_DELAY_MS)); setLight(1, 1); // Green ON timerStart = millis(); movementDetected = 0; while (millis() - timerStart < MAX_WAIT_MS) { if (movementDetected) { timerStop = millis(); success = 1; break; } // if } // while setLight(1, 0); // Green OFF if (success) { setLight(2, 1); // Blue ON // Timing message sprintf(timeMsg, "%4d", timerStop - timerStart); vw_send((uint8_t *)timeMsg, strlen(timeMsg)); vw_wait_tx(); // Wait until the whole message is gone } else { setLight(0, 1); // Red ON vw_send((uint8_t *)failMsg, strlen(failMsg)); vw_wait_tx(); // Wait until the whole message is gone } // else delay(FEEDBACK_MS); for (byte i = 0; i < 3; i++) setLight(i, 0); } // loop