Guest
Public paste!

Debounced Car Blinkers

By: a guest | Apr 8th, 2010 | Syntax: C | Size: 8.52 KB | Hits: 276 | Expires: Never
Copy text to clipboard
  1. /*
  2.  Debounced car circuit test
  3.  
  4.  The circuit:
  5.  * LED attached from pin 3 to ground
  6.  * reed switch attached from pin 7 to ground
  7.  * LED attached from pin 4 to ground
  8.  * reed switch attached from pin 8 to ground
  9.  * speaker attached from pin 11 to ground
  10. */
  11.  
  12. // constants won't change. They're used here to
  13. // set pin numbers:
  14. const int     leftBlinker  = 3;   // left blinker pin
  15. const int     rightBlinker = 4;   // right blinker pin
  16. const int     leftSensor   = 7;   // left sensor pin
  17. const int     rightSensor  = 8;   // right sensor pin
  18. const int     speakerPin   = 11;  // left blinker pin
  19. // set other constants
  20. const long    timerLength      = 0;  // delay before blinker/alarm in seconds
  21. const long    blinkTimerLength = 2;  // delay before blinker/alarm in seconds
  22. const int     theTone          = 440; // tone in hertz
  23. const double  theDuration      = .25;  //duration in seconds
  24.  
  25.  
  26. // Variables will change:
  27. int ledState = LOW;               // the current state of the output pin
  28. int leftSensorState;              // the current reading from the input pin
  29. int lastLeftSensorState = HIGH;   // the previous reading from the input pin
  30. int rightSensorState;              // the current reading from the input pin
  31. int lastRightSensorState = HIGH;   // the previous reading from the input pin
  32. //boolean flip = true;
  33.  
  34. int toneDelay = 0;
  35. int toneDuration = 0;
  36.  
  37. // the following variables are long's because the time, measured in miliseconds,
  38. // will quickly become a bigger number than can be stored in an int.
  39. long lastDebounceTimeLeft  = 0;  // the last time the output pin was toggled
  40. long lastDebounceTimeRight = 0;  // the last time the output pin was toggled
  41. long debounceDelay = 50;    // the debounce time; increase if the output flickers
  42.  
  43. int     lastMode      = 1;
  44. int     lastModeLeft  = LOW;
  45. int     lastModeRight = LOW;
  46. int     lastModeTemp  = 0;
  47. boolean timer         = false;
  48. boolean blinkTimer    = false;
  49. long    startTime     = 0;    // when mode last changed
  50. long    blinkStartTime= 0;    // when mode last changed
  51.  
  52.  
  53. void changeState(int mode)
  54. {
  55.   // case 1 / default -         | : :- -: : |     <- Left LOW , Right LOW
  56.   // case 2           -        :|:- -: :    |     <- Left HIGH, Right LOW
  57.   // case 3           -     : :-|-: :       |     <- Left HIGH, Right HIGH
  58.   // case 4           -         |    : :- -:|:    <- Left LOW , Right HIGH
  59.   boolean speaker = false;
  60.  
  61.   switch(mode)
  62.   {
  63.     case 2:
  64.       // case 2           -        :|:- -: :    |     <- Left HIGH, Right LOW
  65.       //                  -   if coming from state 1, turn on left blinker
  66.       //                  -   if coming from state 3, maintain state
  67.       if (mode != lastMode)
  68.       {
  69.         if(lastMode == 1)
  70.         {
  71.           startTime    = millis();
  72.           timer        = true;
  73.           speaker      = false;
  74.           lastModeLeft = HIGH;
  75.           lastModeRight= LOW;
  76.         }
  77.         lastModeTemp = lastMode;
  78.         lastMode     = mode;
  79.       }
  80.       if ((millis() - startTime) > (long)(timerLength * 1000))
  81.       {
  82.         digitalWrite(leftBlinker , lastModeLeft);
  83.         digitalWrite(rightBlinker, lastModeRight);
  84.         speaker = true;
  85.       }
  86.      
  87.       break;
  88.     case 3:
  89.       // case 3           -     : :-|-: :       |     <- Left HIGH, Right HIGH
  90.       //                  -   if coming from state 2, maintain state
  91.       //                  -   if coming from state 4, maintain state
  92.       /*
  93.       if (mode != lastMode)
  94.       {
  95.         if(lastMode == 1)
  96.         {
  97.           startTime = millis();
  98.           timer = true;
  99.           speaker = false;
  100.           lastModeLeft = HIGH;
  101.           lastModeRight= LOW;
  102.         }
  103.         lastModeTemp = lastMode;
  104.         lastMode     = mode;
  105.       }
  106.       */
  107.       if ((millis() - startTime) > (timerLength * 1000))
  108.       {
  109.         digitalWrite(leftBlinker , lastModeLeft);
  110.         digitalWrite(rightBlinker, lastModeRight);
  111.         speaker = true;
  112.       }
  113.      
  114.       break;
  115.     case 4:
  116.       // case 4           -         |    : :- -:|:    <- Left LOW , Right HIGH
  117.       //                  -   if coming from state 1, turn on right blinker
  118.       //                  -   if coming from state 3, maintain state
  119.       if (mode != lastMode)
  120.       {
  121.         if(lastMode == 1)
  122.         {
  123.           startTime    = millis();
  124.           timer        = true;
  125.           speaker      = false;
  126.           lastModeLeft = LOW;
  127.           lastModeRight= HIGH;
  128.         }
  129.         lastModeTemp = lastMode;
  130.         lastMode     = mode;
  131.       }
  132.       if ((millis() - startTime) > (timerLength * 1000))
  133.       {
  134.         digitalWrite(leftBlinker , lastModeLeft);
  135.         digitalWrite(rightBlinker, lastModeRight);
  136.         speaker = true;
  137.       }
  138.       break;
  139.     default:
  140.       // case 1 / default -         | : :- -: : |     <- Left LOW , Right LOW
  141.       // both blinkers off (rest state)
  142.       if (mode != lastMode)
  143.       {
  144.         lastMode = 1;
  145.         timer    = false;
  146.         blinkTimer = true;
  147.         blinkStartTime = millis();
  148.       }
  149.   }
  150.   if (blinkTimer && ((millis() - blinkStartTime) > (blinkTimerLength * 1000)))
  151.   {
  152.       for (int i=0; i < (2*(1000000/(4*toneDelay))); i++)
  153.       {
  154.         digitalWrite(speakerPin,HIGH);
  155.         delayMicroseconds(toneDelay);
  156.         digitalWrite(speakerPin, LOW);
  157.         delayMicroseconds(toneDelay);
  158.       }
  159.      
  160.       digitalWrite(rightBlinker, LOW);
  161.       digitalWrite(leftBlinker , LOW);
  162.       speaker  = false;
  163.       blinkTimer = false;
  164.   }
  165.  
  166.   //Serial.println("speaker: ");
  167.  
  168.   if(speaker)
  169.   {
  170.     //Serial.println("true");
  171.     digitalWrite(speakerPin, LOW);
  172.     delayMicroseconds(toneDelay);
  173.     for (int i=0; i < toneDuration; i++)
  174.     {
  175.     //  if (flip)
  176.         digitalWrite(speakerPin,HIGH);
  177.       delayMicroseconds(toneDelay);
  178.       digitalWrite(speakerPin, LOW);
  179.       delayMicroseconds(toneDelay);
  180.     }
  181.     digitalWrite(speakerPin,HIGH);
  182.     //flip = !flip;
  183.   }
  184.   else
  185.   {
  186.     //Serial.println("false");
  187.   }
  188. }
  189.  
  190. int getMode (int leftState, int rightState)
  191. {
  192.   // case 1 / default -         | : :- -: : |     <- Left LOW , Right LOW
  193.   // case 2           -        :|:- -: :    |     <- Left HIGH, Right LOW
  194.   // case 3           -     : :-|-: :       |     <- Left HIGH, Right HIGH
  195.   // case 4           -         |    : :- -:|:    <- Left LOW , Right HIGH
  196.   //Serial.println("left:");
  197.   //Serial.println(leftState);
  198.   //Serial.println("right:");
  199.   //Serial.println(rightState);
  200.  
  201.  
  202.   if(leftState == HIGH)
  203.   {
  204.     if(rightState == HIGH)
  205.       return 1;
  206.     else
  207.       return 4;
  208.   }
  209.   else
  210.   {
  211.     if(rightState == HIGH)
  212.       return 2;
  213.     else
  214.       return 3;
  215.   }
  216. }
  217.  
  218. void setup() {
  219.   pinMode(leftSensor, INPUT);
  220.   digitalWrite(leftSensor, HIGH); //activate pullup
  221.   pinMode(leftBlinker, OUTPUT);
  222.   pinMode(rightSensor, INPUT);
  223.   digitalWrite(rightSensor, HIGH); //activate pullup
  224.   pinMode(rightBlinker, OUTPUT);
  225.   pinMode(speakerPin, OUTPUT);
  226.   //Serial.begin(9600);
  227.  
  228.   toneDelay    = 1000000 / theTone / 2;
  229.   toneDuration = theDuration * theTone;
  230. }
  231.  
  232. void loop() {
  233.   // read the state of the switch into a local variable:
  234.   int leftReading  = digitalRead(leftSensor);
  235.   int rightReading = digitalRead(rightSensor);  
  236.  
  237.   // check to see if you just pressed the button
  238.   // (i.e. the input went from LOW to HIGH),  and you've waited
  239.   // long enough since the last press to ignore any noise:  
  240.  
  241.   // If the switch changed, due to noise or pressing:
  242.   if (leftReading != lastLeftSensorState) {
  243.     // reset the debouncing timer
  244.     lastDebounceTimeLeft = millis();
  245.   }
  246.   if (rightReading != lastRightSensorState) {
  247.     // reset the debouncing timer
  248.     lastDebounceTimeRight = millis();
  249.   }
  250.  
  251.   if ((millis() - lastDebounceTimeLeft) > debounceDelay) {
  252.     // whatever the reading is at, it's been there for longer
  253.     // than the debounce delay, so take it as the actual current state:
  254.     leftSensorState = leftReading;
  255.   }
  256.   if ((millis() - lastDebounceTimeRight) > debounceDelay) {
  257.     // whatever the reading is at, it's been there for longer
  258.     // than the debounce delay, so take it as the actual current state:
  259.     rightSensorState = rightReading;
  260.   }
  261.   //Serial.println(" Left Reading: ");
  262.   //Serial.println(leftReading);
  263.   //Serial.println(" Right Reading: ");
  264.   //Serial.println(rightReading);
  265.   int state=getMode(leftSensorState, rightSensorState);
  266.   //Serial.println(" State: ");
  267.   //Serial.println(state);
  268.   changeState(state);
  269.  
  270.   lastLeftSensorState = leftReading;
  271.   lastRightSensorState = rightReading;
  272. }