Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed MCP23017 mbed-rtos WattBob_TextLCD
Diff: main.cpp
- Revision:
- 7:84c2156e1ace
- Parent:
- 6:a026e0651ad5
- Child:
- 8:09b78bc5551f
--- a/main.cpp Tue Mar 27 13:21:36 2018 +0000
+++ b/main.cpp Tue Mar 27 14:11:17 2018 +0000
@@ -32,10 +32,12 @@
DigitalOut fluxCapacitor(p7);
DigitalIn engine(p5); // switches
-DigitalIn lightSwitch(p8);
+DigitalIn lightSwitch(p8);
DigitalIn lIndicate(p11);
DigitalIn rIndicate(p12);
+int l, r, lr;
+
Serial pc(USBTX, USBRX); // serial tx, rx
Thread sp;
@@ -49,6 +51,7 @@
Thread task8;
Thread task9;
Thread task10;
+Thread indi;
Semaphore ac(1);
typedef struct { // mail
@@ -87,8 +90,7 @@
while (1) {
if (engine.read() > 0) {
engLight = 1;
- }
- else{
+ } else {
engLight = 0;
}
Thread::wait(500);
@@ -110,9 +112,8 @@
while (1) {
if ( br > 0) {
brakeLights = 1;
- }
- else{
- brakeLights = 0;
+ } else {
+ brakeLights = 0;
}
Thread::wait(500);
@@ -123,9 +124,8 @@
while (1) {
if (kmph > 141.6) { // 141.6 km = 88 miles
fluxCapacitor = 1;
- }
- else{
- fluxCapacitor = 0;
+ } else {
+ fluxCapacitor = 0;
}
Thread::wait(1000);
@@ -149,7 +149,7 @@
Thread::wait(500);
}
}
-void send_thread (void) // 7. Send speed, acc, brake to MAILq 0.2
+void send_thread (void) // 7. Send speed, acc, brake to MAILq 0.2
{
uint32_t i = 0;
while (true) {
@@ -188,7 +188,7 @@
void lights() // 9. set side lights 1
{
while (1) {
- if (lightSwitch == 1) {
+ if (lightSwitch.read() == 1) {
sideLights = 1;
} else {
sideLights = 0;
@@ -197,28 +197,52 @@
Thread::wait(1000);
}
}
-void indicators() // 10. flash indicators 0.5
+void indicators() // 10. check indicators 0.5
{
while (1) {
-
+
if ((lIndicate == 1) && (rIndicate == 1)) { // If both switch on
- lIndicator = !lIndicator; // both LED at 2Hz
- rIndicator = !rIndicator;
- wait(0.5);
- }
- else if ((lIndicate == 1) && (rIndicate == 0)) { // if left switch on
- lIndicator = !lIndicator; // left LED at 1Hz
- wait(1);
- }
- else if ((lIndicate == 0) && (rIndicate == 1)) { // if right switch on
- rIndicator = !rIndicator; // right LED at 1Hz
- wait(1);
+ lr = 1; // both LED at 2Hz
+ } else if ((lIndicate == 1) && (rIndicate == 0)) { // if left switch on
+ l = 1; // left LED at 1Hz
+ r = 0;
+ lr = 0;
+ } else if ((lIndicate == 0) && (rIndicate == 1)) { // if right switch on
+ r = 1; // right LED at 1Hz
+ l = 0;
+ lr = 0;
+ } else if ((lIndicate == 0) && (rIndicate == 0)) { // both off
+ r = 0;
+ l = 0;
+ lr = 0;
}
Thread::wait(2000);
}
}
+void indicate() // flash indicators
+{
+ while (1) {
+ int x = 1000;
+ if (lr == 1) { // If both switch on
+ lIndicator = !lIndicator; // both LED at 2Hz
+ rIndicator = !rIndicator;
+ x = 500;
+ } else if (l == 1) { // if left switch on
+ lIndicator = !lIndicator; // left LED at 1Hz
+ rIndicator = 0;
+ x = 1000;
+ } else if (r == 1) { // if right switch on
+ rIndicator = !rIndicator; // right LED at 1Hz
+ lIndicator = 0;
+ x = 1000;
+ }
+
+ Thread::wait(x);
+ }
+}
+
int main()
{
@@ -235,8 +259,9 @@
task6.start(LCD); //2 y
// task7.start(callback(send_thread)); //0.2
// task8.start(toSerial); //0.05
-// task9.start(lights); //1
-// task10.start(indicators); //0.5
+ task9.start(lights); //1 y
+ task10.start(indicators); //0.5 y
+ indi.start(indicate); // 1-2 y
}