ECE 4180 Laser Tag

Dependencies:   Motordriver mbed SDFileSystem mbed-rtos wave_player

Revision:
1:6a78c0e371d5
Parent:
0:4953f3704bb8
--- a/main.cpp	Thu Apr 14 15:25:03 2016 +0000
+++ b/main.cpp	Sat Apr 30 19:07:07 2016 +0000
@@ -1,19 +1,35 @@
 #include "mbed.h"
 #include "motordriver.h"
+#include "SDFileSystem.h"
+#include "wave_player.h"
 #include "Speaker.h"
+#include "rtos.h"
 Motor m1(p22, p6, p5, 1); // pwm, fwd, rev, can brake left most motor (a)
 Motor m2(p23, p7, p8, 1); //right most motor (b)
-Speaker mySpeaker(p21);
-Serial blue(p13,p14);
+SDFileSystem sd(p11, p12, p13, p14, "sd"); // the pinout on the mbed Cool Components workshop board
+AnalogOut DACout(p18); //set up analog out
+wave_player waver(&DACout); //set up wave_player, with DACout as input to the funciton
+Serial blue(p9,p10); //used p13 and p14 before
 
-DigitalOut reset(p26);
+//instantiate Ins and Outs
+InterruptIn detector(p25);
 DigitalOut led1(LED1);
 DigitalOut led2(LED2);
 DigitalOut led3(LED3);
 DigitalOut led4(LED4);
+DigitalOut laser(p20);
+DigitalIn shot(p26);
 
-int main() {
-    while(1) {
+
+volatile bool laserstate = false;
+volatile bool downstate = false;
+
+void gothit_isr() { // runs when robot gets shot
+    downstate = true;
+}
+//thread deals with shooting/movement (communication with bluetooth chip)
+void movement_thread(void const *args) {
+     while(1) {
         char bnum=0;
         char bhit=0;
         if (blue.getc()=='!') {
@@ -24,73 +40,74 @@
                     switch (bnum) {
                         case '1': //number button 1
                             if (bhit=='1') {
-                                led1 = 1;//add hit code here
+                                //led1 = 1;
                             } else {
-                                led1 = 0;//add release code here
+                                //led1 = 0;
                             }
                             break;
                         case '2': //number button 2
                             if (bhit=='1') {
-                                led2 = 1;//add hit code here
+                               // led2 = 1;
                             } else {
-                                led2 = 0;//add release code here
+                               // led2 = 0;
                             }
                             break;
                         case '3': //number button 3
                             if (bhit=='1') {
-                                led3 = 1;//add hit code here
+                               // led3 = 1;
                             } else {
-                                led3 = 0;//add release code here
+                               // led3 = 0;
                             }
                             break;
                         case '4': //number button 4
                             if (bhit=='1') {
-                                led4 = 1;//add hit code here
+                               // led4 = 1;
                             } else {
-                                led4 = 0;//add release code here
+                                //led4 = 0;
                             }
                             break;
                         case '5': //button 5 up arrow
                             if (bhit=='1') {
-                                m1.speed(1.0);
-                                m2.speed(1.0);      
+                                //move robot forward when up button is pressed
+                                m1.speed(0.95);
+                                m2.speed(0.95);      
                             } else {
+                                //stop robot when button is released
                                 m1.speed(0.0);
                                 m2.speed(0.0);
-                                //add release code here
                             }
                             break;
                         case '6': //button 6 down arrow
                             if (bhit=='1') {
-                                //add hit code here
-                                m1.speed(-1.0);
-                                m2.speed(-1.0);
+                                //move robot backward when down button is pressed
+                                m1.speed(-0.95);
+                                m2.speed(-0.95);
                             } else {
+                                //stop robot when button is released
                                 m1.speed(0.0);
                                 m2.speed(0.0);
-                                //add release code here
                             }
                             break;
                         case '7': //button 7 left arrow
                             if (bhit=='1') {
-                                m1.speed(-1.0);
-                                m2.speed(1.0);
-                                //add hit code here
+                                //move robot to the left by rotating both wheels when left button is pressed
+                                m1.speed(-0.75);
+                                m2.speed(0.75);
                             } else {
+                                //stop robot when button is released
                                 m1.speed(0.0);
                                 m2.speed(0.0);
-                                //add release code here
                             }
                             break;
                         case '8': //button 8 right arrow
                             if (bhit=='1') {
-                                //add hit code here
-                                m1.speed(1.0);
-                                m2.speed(-1.0);
+                                //move robot to the right by rotating both wheels when right button is pressed
+                                m1.speed(0.75);
+                                m2.speed(-0.75);
                             } else {
+                                //stop robot when button is released
                                 m1.speed(0.0);
                                 m2.speed(0.0);
-                                //add release code here
                             }
                             break;
                         default:
@@ -99,5 +116,37 @@
                 }
             }
         }
+        Thread::yield();
+    }
+    
+}
+
+int main() {
+    // main() handles the shooting and getting shot
+    Thread thread(movement_thread);
+    detector.rise(&gothit_isr); // rising edge interrupt, used to see if robot got hit
+    while(1) {
+        if(shot ==1){
+            led4 =1; //turn led4 on if robot got shot
+        }
+        else{
+            led4 =0;
+            }
+        if (downstate) { // robot got hit
+            led1 = 1; //turn led1 if robot gets hit
+            Thread::wait(3000); // movement_thread loops meanwhile, allowing for robot to still move when shot
+            laserstate = false; // resets laser so that laser cannot shoot
+            downstate = false; //resets downstate so that robot can be hit again
+            led1 =0;
+            }
+        if (laserstate) { // robot is shooting
+            led2 = 1; //led2 turns on when shooting
+            laser = 1;
+            Thread::wait(250); // movement_thread loops meanwhile
+            laserstate = false;
+            laser = 0;
+            led2 = 0;
+            }
+        Thread::yield(); // movement_thread runs once
     }
 }
\ No newline at end of file