Advertisement
Guest User

Arduino code for bluetooth remote controller.

a guest
May 21st, 2012
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. //Copyright Jorgga 2012
  2. //jorgga@gmail.com
  3. //You may modify and use this code but you must give me credit.
  4. //You may not use this code for profitable applications.
  5.  
  6. //Muuttujat
  7. const int NextButton = 2; //Play nappula porttiin 2
  8. const int StopButton = 3; //Stop 3
  9. const int PlayPause = 4; //Play/pause 4
  10. const int PrevButton = 5; //Prev 5
  11. int ButtonStates[4] = {0,0,0,0}; //Nappuloiden tilat
  12. boolean Playing = true; //Toistetaanko, Juu Muutentaan false jos pausea painetaan
  13. boolean Connected = false; //Ei oo yhistetty oletuksena
  14. int Delay = 1000; //Pingauksen aikaväli
  15. String InputString = ""; //Minne tulevat viestit piillotetaan
  16. boolean StringComplete = false; //Onko tuleva viesti valmis
  17. long Pinged = 0; //Edellisen pingin aika
  18.  
  19. //Ashetuksia
  20. void setup() {
  21. Serial.begin(9600); //Seriaalin nopeus
  22. pinMode(NextButton, INPUT); //Portit nappuloille
  23. pinMode(StopButton, INPUT); //sisäänpäin
  24. pinMode(PlayPause, INPUT);
  25. pinMode(PrevButton, INPUT);
  26.  
  27. InputString.reserve(200); //Jotain bufferia
  28.  
  29. }
  30. void loop() {
  31. if (Serial.available() > 0) seriaaliEventti(); //Mikäli sarjadataa tulee
  32.  
  33. if(Connected == false)
  34. {
  35. Ping(); //pingataan jos ei oo yhteyttä
  36. }
  37. else
  38. {
  39. Ping(); //Pingataan kuitenkin
  40. Buttons();
  41. }
  42.  
  43.  
  44.  
  45. if(StringComplete)
  46. {
  47. Serial.print("Answer: ");
  48. Serial.print(InputString);
  49.  
  50. if(InputString == "Pong\n")
  51. {
  52. //Serial.print("Set Connected True\n");
  53. Delay = 10000;
  54. Pinged = millis() / 1000;
  55. //Serial.print(Pinged);
  56. //Serial.print("\n");
  57. Connected = true;
  58.  
  59. }
  60.  
  61. StringComplete = false;
  62. InputString = "";
  63. }
  64. }
  65.  
  66. //Loopin loppu
  67.  
  68. void Ping()
  69. {
  70.  
  71.  
  72. if((millis() % Delay) == 0)
  73. {
  74. Serial.print("Ping ");
  75. Serial.print(millis()/1000);
  76. Serial.print("\n");
  77. delay(1);
  78. } else if((millis() / 1000) > (Pinged + 10))
  79. {
  80. Connected = false;
  81. Delay = 1000;
  82. }
  83. }
  84.  
  85.  
  86. void Buttons()
  87. {
  88. //Buttons Begin
  89. if(digitalRead(NextButton) == HIGH)
  90. {
  91. ButtonStates[0] = 1;
  92. } else if(digitalRead(StopButton) == HIGH)
  93. {
  94. ButtonStates[1] = 1;
  95. } else if(digitalRead(PlayPause) == HIGH)
  96. {
  97. ButtonStates[2] = 1;
  98. } else if(digitalRead(PrevButton) == HIGH)
  99. {
  100. ButtonStates[3] = 1;
  101. }
  102.  
  103.  
  104. if(digitalRead(NextButton) == LOW && ButtonStates[0] == 1)
  105. {
  106. Serial.print("Next\n");
  107. ResetButtons();
  108. } else if(digitalRead(StopButton) == LOW && ButtonStates[1] == 1)
  109. {
  110. Serial.print("Stop\n");
  111. ResetButtons();
  112. } else if(digitalRead(PlayPause) == LOW && ButtonStates[2] == 1)
  113. {
  114. if(Playing == true)
  115. {
  116. Playing = false;
  117. Serial.print("Pause\n");
  118.  
  119. } else if(Playing == false)
  120. {
  121. Playing = true;
  122. Serial.print("Play\n");
  123.  
  124. }
  125. ResetButtons();
  126. } else if(digitalRead(PrevButton) == LOW && ButtonStates[3] == 1)
  127. {
  128. Serial.print("Prev\n");
  129. ResetButtons();
  130. }
  131. //Buttons End
  132.  
  133. }
  134.  
  135. void ResetButtons()
  136. {
  137. for(int i = 0;i<4;i++)
  138. {
  139. ButtonStates[i] = 0;
  140. }
  141. }
  142.  
  143. void seriaaliEventti()
  144. {
  145. while (Serial.available())
  146. {
  147. char inChar = (char)Serial.read();
  148. InputString += inChar;
  149. if(inChar == '\n')
  150. {
  151. StringComplete = true;
  152. }
  153. }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement