Advertisement
Jordan1711

bluetooth

Mar 18th, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.43 KB | None | 0 0
  1. #include <FastLED.h>
  2. #include <SoftwareSerial.h>
  3. SoftwareSerial mySer(8, 9);
  4. #define LED_PIN 8 // Data pin for led tape
  5. #define NUM_LEDS 80
  6. int button_move_R = 7;
  7. int button_move_L = 9;
  8. int button_attack = 5;
  9. int i = 0;
  10. int lastmovement;// 0 left, 1 right
  11. int direct;
  12. long previousTime = 0;
  13. CRGB leds[NUM_LEDS];
  14. //int enemyPos[80]; // has the enemy poition
  15. int playerPos[80];
  16. bool isAlive=true;
  17.  
  18. struct enemy {
  19. bool live = true;
  20. int pos;
  21. };
  22.  
  23. struct enemy enemies[7];
  24.  
  25. int attack(int x, int y) {
  26.  
  27. if (x+2 == y){
  28.  
  29.  
  30. enemies.pos[y] = 0;
  31. leds[x - 2] = CRGB(0,0,255);
  32. leds[x - 1] = CRGB(0,0,255);
  33. leds[x + 1] = CRGB(0,0,255);
  34. leds[x + 2] = CRGB(0,0,255);
  35. FastLED.show();
  36.  
  37. }
  38. if(y-x <2){
  39. if(enemies.pos[y] == 0) {
  40. leds[y] = CRGB(0,0,0);
  41. isAlive = false;
  42. //Serial.println("HIT HT HIT");
  43. FastLED.show();
  44. }
  45. }
  46. leds[x - 2] = CRGB(0,0,255);
  47. leds[x - 1] = CRGB(0,0,255);
  48. leds[x + 1] = CRGB(0,0,255);
  49. leds[x + 2] = CRGB(0,0,255);
  50. leds[x] = CRGB(0,255,0);
  51. FastLED.show();
  52. leds[x - 2] = CRGB(0,0,0);
  53. leds[x - 1] = CRGB(0,0,0);
  54. leds[x + 1] = CRGB(0,0,0);
  55. leds[x + 2] = CRGB(0,0,0);
  56. FastLED.show();
  57.  
  58. }
  59.  
  60. void setup() {
  61. FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);
  62. pinMode(button_move_R, INPUT);
  63. pinMode(button_move_L, INPUT);
  64. pinMode(button_attack, INPUT);
  65. Serial.begin(9600);
  66. mySer.begin(9600);
  67. }
  68.  
  69.  
  70. //int x = 80;
  71. int c = 0;
  72.  
  73. void loop() {
  74. // while(mySer.available())
  75. // {
  76. // Serial.print(mySer.read());
  77. // }
  78. if(mySer.read() == 1){
  79. //Serial.println("move right");
  80. if(i >= 79){
  81. for(int i = 0; i < 80; i++) {
  82. leds[i] = CRGB(0, 170, 255);
  83. FastLED.show();
  84. //i++;
  85. }
  86. while(mySer.read() == 1 && mySer.read() == 2 && mySer.read() == 3);
  87. for(int i = 0; i < 80; i++) {
  88. leds[i] = CRGB(0, 0, 0);
  89. FastLED.show();
  90. //i++;
  91. }
  92. i = 0;
  93. //x = 79;
  94. }
  95. if(leds[i] == CRGB(255,0,0)) {
  96. for(int i = 0; i < 80; i++) {
  97. leds[i] = CRGB(255, 0, 0);
  98. FastLED.show();
  99. //i++;
  100. }
  101. for(int i = 0; i < 80; i++){
  102. for(int i = 0; i < 80; i++) {
  103. leds[i] = CRGB(0, 0, 0);
  104. FastLED.show();
  105. //i++;
  106. }
  107. }
  108. i = 0;
  109. //x = 79;
  110. }
  111. playerPos[i] = 1;
  112. playerPos[i-1] = 0;
  113. if(playerPos[i] == 1 && playerPos[i - 1] == 0) {
  114. leds[i-1] = CRGB(0, 0, 0);
  115. leds[i] = CRGB(0, 255, 0);
  116. }
  117. i++;
  118. if(i > lastmovement) {
  119. direct = 1;
  120. }
  121. lastmovement=i;
  122. FastLED.show();
  123. delay(50);
  124. }
  125.  
  126. else if (mySer.read() == 2){
  127. // Serial.println("move left");
  128. if(i == 0){
  129. i = 1;
  130. }
  131. playerPos[i]=0;
  132. playerPos[i-1]=1;
  133. if(playerPos[i] == 0 && playerPos[i - 1] == 1) {
  134. leds[i] = CRGB(0, 0, 0);
  135. leds[i-1] = CRGB(0, 255, 0);
  136. }
  137. i--;
  138. if(i < lastmovement) {
  139. direct = 0;
  140. }
  141. lastmovement=i;
  142. FastLED.show();
  143. delay(50);
  144. }
  145.  
  146. if(Interval(millis(), 200, &previousTime))
  147. {
  148. if(isAlive == false){
  149. enemies.pos[c] = -1;
  150. enemies.alive[c] = true;
  151. }
  152. enemies.pos[c]--;
  153. if (x == -1) {
  154. enemies.pos[c] = 79;
  155. }
  156.  
  157. /*second = x + 2;
  158. third = x + 3;
  159. forth = x + 4;
  160. fifth = x + 5;*/
  161. //if(enemyPos[x] != playerPos[i])
  162. //Serial.println("1: ");
  163. //Serial.println(x);
  164. enemies.pos[c] = 1;
  165. enemies[c + 1].pos = 0;
  166. leds[c] = CRGB(255,0,0);
  167. leds[c + 1] = CRGB(0,0,0);
  168. // Serial.println("2: ");
  169. // Serial.println(x);
  170. FastLED.show();
  171. // Serial.println("3: ");
  172. // Serial.println(x);
  173. }
  174.  
  175. if(mySer.read() == 3){
  176. if(direct == 1) {
  177. attack(i-1, x-1);
  178. Serial.println("right i-1");
  179. Serial.println(i-1);
  180. Serial.println("right x-1");
  181. Serial.println(x-1);
  182.  
  183. }
  184.  
  185. if(direct == 0) {
  186. attack(i, x);
  187. Serial.println("left i");
  188. Serial.println(i);
  189. Serial.println("left x");
  190. Serial.println(x);
  191. }
  192. }
  193. }
  194.  
  195. bool Interval(unsigned long currTime, int interval, long *previousTime)
  196. {
  197. if(currTime - *previousTime >= interval)
  198. {
  199. *previousTime = currTime;
  200. return true;
  201. }
  202. return false;
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement