Example of using threads to run different actions happening at the same time.

Dependencies:   Motor Servo

Fork of Fa2018-es200-1121-3321-thread-example by Fa2018-es200-1121-project2-herndon-toss

Files at this revision

API Documentation at this revision

Comitter:
m214962
Date:
Tue Oct 23 13:29:33 2018 +0000
Parent:
7:b7257f01ced7
Commit message:
Final Project2 code. Works with 2 separate switches. Other switches change leds.

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Mon Oct 22 15:23:45 2018 +0000
+++ b/main.cpp	Tue Oct 23 13:29:33 2018 +0000
@@ -11,12 +11,15 @@
 #include "Servo.h"
 
 // Declare some stuff
-DigitalIn sw1(p18); // switch for activating motor 
+DigitalIn sw1(p17); // switch for activating led5
 DigitalIn sw2(p16); // switch for activating servo 
 DigitalIn sw3(p20); // switch for activating servo2
-DigitalOut led1(LED1); // status LED for sw1
-DigitalOut led2(LED2); // status LED for sw2
-DigitalOut led3(LED3); // status LED for sw3
+DigitalIn sw4(p19); // switch for led4
+DigitalOut led1(p5); // status LED for motor
+DigitalOut led2(p6); // status LED for sw2
+DigitalOut led3(p7); // status LED for sw3
+DigitalOut led4(p8); // status LED for sw4
+DigitalOut led5(p11); //status LED for sw1
 DigitalOut heartbeat(LED4); // heartbeat LED adds visual effects
 Motor m(p26,p30,p29); 
 Servo s1(p21);
@@ -56,9 +59,9 @@
     while(1){
         heartbeat = !heartbeat; // blink heartbeat once a second
         //ThisThread::sleep_for(1000);
-        Thread::wait(1000);
+        Thread::wait(500+rand()%1000);
                 
-        // I don't have to do my motor or servo stuff here because they are
+        // motor or servo stuff not needed here because they are
         // running in their own threads. 
         } // main loop
 } // main()
@@ -71,16 +74,23 @@
     while(1) {
         
             led1.write(1); // light a light for entertaining purposes
-            m.speed(0.5); // spin motor ahead to move players together
-            wait (0.7); //time of motor movement
+            m.speed(-0.45); // spin motor ahead to move players together
+            wait (0.65); //time of motor movement
             m.speed(0.0); //stop players when they are close.
-            wait (1+rand()%5); //let them fight for a random amount of time
+            wait (3+rand()%5); //let them fight for a random amount of time
             led1.write(0); 
-            m.speed(-0.5); // move the players back
-            wait (0.7); //time of opposite motor movement
+            m.speed(0.45); // move the players back
+            wait (1.5); //time of opposite motor movement
+            
+            if (sw4.read()) {
+                led4.write(1);
+                }
+            if (sw1.read()) {
+                led5.write(1);
+                }
                 
-        //ThisThread::sleep_for(200); // this thread executes 20x a second
-       // Thread::wait(50); //possibly change to small or erase
+        //ThisThread::sleep_for(200); // this thread executes 100x a second
+        Thread::wait(50); //possibly change to small or erase
     } // while(1)
 } // m_callback()
     
@@ -95,10 +105,10 @@
             }
             else{
                 led2.write(0); 
-                s1.write(sw1.read()-0.5); // move servo a step to left
+                s1.write(0.0); // move servo a step to left
                 }
-        //ThisThread::sleep_for(200); // this thread executes 20x a second
-       // Thread::wait(50); //possibly change
+        //ThisThread::sleep_for(200); // this thread executes 100x a second
+        Thread::wait(50); //Not used so that switches move players immediately - - - may have to use so that switches can move independently
         } // while(1)
 } // s1_callback()
 
@@ -111,9 +121,9 @@
             }
             else{
                 led3.write(0); 
-                s2.write(s2.read()-0.5); // move servo a step to left
+                s2.write(0.0); // move servo a step to left
                 }
-        //ThisThread::sleep_for(200); // this thread executes 20x a second
-       // Thread::wait(50); //possibly change
+        //ThisThread::sleep_for(200); // this thread executes 100x a second
+        Thread::wait(50); //possibly change
         } // while(1)
 } // s2_callback()