Guest User

Untitled

a guest
Mar 1st, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. const int ledPinRed = 11;
  2. const int ledPinGreen = 10;
  3. const int ledPinBlue = 9;
  4. const int flexPin = A0;
  5.  
  6. int pastValue = 0;
  7. int openMillis = 0;
  8. int closedMillis = 0;
  9.  
  10. // how long will the gesture have to run in millis to register as a gesture?
  11. const int lengthThreshold = 100;
  12.  
  13. void setup() {
  14. // put your setup code here, to run once:
  15. Serial.begin(9600);
  16. pinMode(ledPinRed, OUTPUT);
  17. pinMode(ledPinGreen, OUTPUT);
  18. pinMode(ledPinBlue, OUTPUT);
  19. }
  20. void loop() {
  21. int mapped = readDegrees();
  22.  
  23. // if the hand is in a fist
  24. bool isClosed = mapped > 120;
  25.  
  26. // save the current value
  27. bool currentValue = mapped;
  28.  
  29. if (isClosed == true) {
  30. // the closed fist mode overrides any other mode
  31. tunesMode();
  32. } else if (currentValue > pastValue) {
  33. // increment counter for hand closing
  34. closedMillis++;
  35. // if the hand was opening, reset that counter to reset detection
  36. if (openMillis > 0) {
  37. openMillis = 0;
  38. }
  39. // turn on warming mode if hand has been closing for time threshold
  40. if (closedMillis > lengthThreshold) {
  41. warmingMode();
  42. }
  43. } else {
  44. // increment counter for hand opening
  45. openMillis++;
  46. // if the hand was closing, reset that counter to reset detection
  47. if (closedMillis > 0) {
  48. closedMillis = 0;
  49. }
  50. // turn on messaging mode if hand has been closing for time threshold
  51. if (openMillis > lengthThreshold) {
  52. messagingMode();
  53. }
  54. }
  55.  
  56. // set the past value to compare with the current state on next loop
  57. pastValue = mapped;
  58.  
  59. // delay to ensure accurate sensor readings
  60. delay(1);
  61. }
  62. int readDegrees() {
  63. int flexValue = analogRead(flexPin);
  64. // convert the value to one that is relative to 0 and positive
  65. int normalized = abs(flexValue - 76);
  66. // convert the range to degrees (must be recalibrated according to resistor strength)
  67. return map(normalized, 0, 35, 0, 90);
  68. }
  69. void tunesMode() {
  70. // tunes mode is yellow
  71. //Serial.println("Tunes");
  72. analogWrite(ledPinRed, 255);
  73. analogWrite(ledPinBlue, 0);
  74. analogWrite(ledPinGreen, 0);
  75. }
  76. void messagingMode() {
  77. // message mode is purple
  78. //Serial.println("Messaging");
  79. analogWrite(ledPinRed, 0);
  80. analogWrite(ledPinBlue, 0);
  81. analogWrite(ledPinGreen, 255);
  82. }
  83. void warmingMode() {
  84. // warming mode is blue
  85. //Serial.println("Warming");
  86. analogWrite(ledPinRed, 0);
  87. analogWrite(ledPinBlue, 255);
  88. analogWrite(ledPinGreen, 0);
  89. }
Advertisement
Add Comment
Please, Sign In to add comment