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.
main.cpp
00001 #include "mbed.h" 00002 00003 #define RED_TOGGLE 1 00004 #define YELLOW_TOGGLE 2 00005 #define GREEN_TOGGLE 4 00006 00007 //Function declarations 00008 void FunctionRed(); 00009 void FunctionYellow(); 00010 void FunctionGreen(); 00011 00012 //I/O 00013 DigitalOut onBoardLED(LED1); 00014 DigitalOut redLED(D7); 00015 DigitalOut yellowLED(D6); 00016 DigitalOut greenLED(D5); 00017 00018 DigitalIn onBoardSwitch(USER_BUTTON); 00019 DigitalIn SW1(D4); 00020 DigitalIn SW2(D3); 00021 00022 00023 //Each of the following 3 functions is listening for a signal 00024 void FunctionRed() 00025 { 00026 while (true) { 00027 Thread::signal_wait(RED_TOGGLE); 00028 redLED = !redLED; 00029 } 00030 } 00031 00032 void FunctionYellow() 00033 { 00034 while (true) { 00035 Thread::signal_wait(YELLOW_TOGGLE); 00036 yellowLED = !yellowLED; 00037 } 00038 } 00039 00040 void FunctionGreen() 00041 { 00042 while (true) { 00043 Thread::signal_wait(GREEN_TOGGLE); 00044 greenLED = !greenLED; 00045 } 00046 } 00047 00048 //Main thread 00049 int main() { 00050 redLED = 0; 00051 yellowLED = 0; 00052 greenLED = 0; 00053 00054 //Create threads 00055 Thread t1; 00056 Thread t2; 00057 Thread t3; 00058 00059 //Start threads 00060 t1.start(FunctionRed); 00061 t2.start(FunctionYellow); 00062 t3.start(FunctionGreen); 00063 00064 //Main loop 00065 while(1) { 00066 00067 //Read stdin (serial port) 00068 int selection, hits; 00069 char strInput[64]; 00070 do { 00071 puts("Please choose:"); 00072 puts("1 - Red"); 00073 puts("2 - Yellow"); 00074 puts("3 - Green"); 00075 scanf("%64s", strInput); //Read a string 00076 hits = sscanf(strInput, "%d", &selection); //Look for an integer 00077 } while (hits != 1); //Repeat if not found 00078 00079 //Signal the thread 00080 switch (selection) { 00081 case 1: 00082 //Signal thread 1 00083 t1.signal_set(RED_TOGGLE); 00084 break; 00085 case 2: 00086 //Signal thread 2 00087 t2.signal_set(YELLOW_TOGGLE); 00088 break; 00089 case 3: 00090 //Signal thread 3 00091 t3.signal_set(GREEN_TOGGLE); 00092 break; 00093 default: 00094 puts("Invalid option"); 00095 break; 00096 } //end switch 00097 00098 } //end while 00099 } //end main
Generated on Sat Jul 23 2022 07:31:45 by
1.7.2