Advertisement
igendel

The Arduino Kick Starter - Sender Module

Feb 27th, 2015
795
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.62 KB | None | 0 0
  1. // "The Arduino Kick Starter" Project
  2. // For ICStation.com's 2015 DIY competition
  3. // See it in action: https://www.youtube.com/watch?v=lrRGC9Ypedc
  4. // Code for Sender Module
  5. // by Ido Gendel, 2015
  6. // Share and enjoy!
  7.  
  8. #include <VirtualWire.h>
  9. #include <EEPROM.h>
  10.  
  11. #define MAX_DELAY_MS 5000
  12. #define MIN_DELAY_MS 1000
  13. #define MAX_WAIT_MS 1000
  14. #define FEEDBACK_MS 1000
  15.  
  16. const int RSEED_ADDRESS = 345;
  17.  
  18. const byte sensorPin = 2;
  19.  
  20. volatile byte movementDetected;
  21.  
  22. // RGB controls
  23. struct tLightRec {
  24.   byte pin;
  25.   byte state;
  26. } light[3] {{12, 0}, {13, 0}, {11, 0}};
  27.  
  28. //-----------------------------------------------------------
  29. void setLight(const byte index, const byte newState) {
  30.  
  31.   light[index].state = newState;
  32.   digitalWrite(light[index].pin, light[index].state);
  33.  
  34. } // setLight
  35.  
  36. //-----------------------------------------------------------
  37. void rotateLights(const byte cycles = 60) {
  38.  
  39.   // LEDs on strip are GRB...
  40.   byte actualOrder[3] = {1, 0, 2};
  41.   byte currentIndex = 0;
  42.  
  43.   for (int cycle = 0; cycle < cycles; cycle++) {
  44.    
  45.     setLight(actualOrder[currentIndex], 0);
  46.     currentIndex = (currentIndex + 1) % 3;
  47.     setLight(actualOrder[currentIndex], 1);
  48.     delay(80);
  49.    
  50.   } // for
  51.  
  52.   // "Cleanup"
  53.   setLight(actualOrder[currentIndex], 0);  
  54.  
  55. } // rotateLights
  56.  
  57. //-----------------------------------------------------------
  58. unsigned int getRSeed() {
  59.  
  60.   unsigned int r;
  61.   r = EEPROM.read(RSEED_ADDRESS);
  62.   r <<= 8;
  63.   r += EEPROM.read(RSEED_ADDRESS + 1);
  64.   return r;
  65.  
  66. } // getRSeed
  67.  
  68. //-----------------------------------------------------------
  69. void setRSeed(unsigned int r) {
  70.  
  71.   EEPROM.write(RSEED_ADDRESS + 1, r & 255);
  72.   EEPROM.write(RSEED_ADDRESS, r >> 8);
  73.  
  74. } // setRSeed
  75.  
  76. //-----------------------------------------------------------
  77. void setup()
  78. {
  79.  
  80.   unsigned int RSeed;
  81.  
  82.   for (byte i = 0; i < 3; i++) pinMode(light[i].pin, OUTPUT);
  83.  
  84.     // Initialise the IO and ISR
  85.     vw_set_tx_pin(A1);
  86.     vw_set_rx_pin(9); // Prevent rx from disturbing pin 11!
  87.     vw_set_ptt_inverted(true); // Required for DR3100
  88.     vw_setup(2000);  // Bits per sec
  89.    
  90.   // Prevent repeating random patterns after reset
  91.   RSeed = getRSeed();
  92.   randomSeed(RSeed);
  93.   setRSeed(RSeed + 1);
  94.  
  95.   rotateLights();
  96.   delay(5000);  
  97.  
  98.   pinMode(sensorPin, INPUT_PULLUP);
  99.   attachInterrupt(0, sensorISR, FALLING);
  100.    
  101. } // setup
  102.  
  103. //-----------------------------------------------------------
  104. void sensorISR() {
  105.   movementDetected = 1;
  106. }
  107.  
  108. //-----------------------------------------------------------
  109. void loop()
  110. {
  111.     char failMsg[] = "FA1L";  
  112.     char timeMsg[] = "0000";      
  113.     byte success = 0;
  114.  
  115.     unsigned long timerStart, timerStop;
  116.  
  117.     delay(MIN_DELAY_MS + random(MAX_DELAY_MS));
  118.     setLight(1, 1); // Green ON
  119.     timerStart = millis();
  120.    
  121.     movementDetected = 0;
  122.     while (millis() - timerStart < MAX_WAIT_MS) {
  123.        if (movementDetected) {
  124.          
  125.          timerStop = millis();
  126.          success = 1;
  127.           break;
  128.          
  129.        }  // if
  130.     } // while
  131.    
  132.     setLight(1, 0); // Green OFF    
  133.  
  134.     if (success) {
  135.       setLight(2, 1); // Blue ON
  136.       // Timing message
  137.       sprintf(timeMsg, "%4d", timerStop - timerStart);
  138.       vw_send((uint8_t *)timeMsg, strlen(timeMsg));
  139.       vw_wait_tx(); // Wait until the whole message is gone
  140.  
  141.     } else {
  142.         setLight(0, 1); // Red ON
  143.         vw_send((uint8_t *)failMsg, strlen(failMsg));
  144.         vw_wait_tx(); // Wait until the whole message is gone
  145.       } // else  
  146.  
  147.     delay(FEEDBACK_MS);  
  148.     for (byte i = 0; i < 3; i++) setLight(i, 0);
  149.  
  150. } // loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement