Lab Part A

Dependencies:   mbed

Revision:
2:b295ac52a79f
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lab2programa.cpp	Sat Sep 22 23:11:47 2018 +0000
@@ -0,0 +1,66 @@
+//Part A
+//MIDN 3/C Stites and Maly
+//Modified: 9/20/21
+#include "mbed.h"
+
+DigitalIn sw1(p16); //Assigns pin 16 as switch 1
+DigitalIn sw2(p17); //Assigns pin 17 as switch 2
+DigitalOut lights[5] = {p26,p27,p28,p29,p30}; //Puts five lights in array based on their corresponding pin
+
+int main()
+{
+    lights[0]=0; //Turns on light three and turns off other lights
+    lights[1]=0;
+    lights[2]=1;
+    lights[3]=0;
+    lights[4]=0;
+    wait(3);// Wait three seconds
+    
+    int a, b; //Variables to store switch states
+    
+    while(1) //Continue to repeat
+    {
+    
+    a = sw1.read(); //Read in switch states
+    b = sw2.read();
+    
+    if (a == 1 && b == 1) //Check to see if switches 1 and 2 are on
+    {
+        lights[0]=0; //Turns on light three and turns off other lights
+        lights[1]=0;
+        lights[2]=1;
+        lights[3]=0;
+        lights[4]=0;
+    }
+    if (a == 1 && b == 0) //Check to see if switch 1 is on and 2 is off
+    {
+        lights[2]=0; //Turns off light three
+        int i; //Stores assignment of value in array lights
+        for (i=0; i<5; i++) //Starts i at 0 and adds 1 to i while it is less than 5
+        {
+            lights[i]=1; //Turns coresponding light on
+            lights[i-1]=0; //Turns last light off
+            wait(.5); //Wait half a second
+            if (i == 4) //Turn last light in row off
+            {
+            lights[4]=0;
+            }
+        }
+    }
+    if (a == 0 && b == 1) // Check to see if switch 1 is off and 2 is on
+    {
+        lights[0]=0;//Turn off all lights
+        lights[1]=0;
+        lights[2]=0;
+        lights[3]=0;
+        lights[4]=0;
+        int j; //Stores assignment of value in array lights
+        for (j=4; j>-1; j--) //Starts j at 4 and subtracts 1 from i while it is greater than -1
+        {
+            lights[j]=1; //Turns the jth light in the array on
+            wait(.5); //Wait half a second
+            lights[j] = 0; //Turn all lights off
+        }
+    }
+    }
+}
\ No newline at end of file