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 kRED 4 00004 #define kYELLOW 2 00005 #define kGREEN 1 00006 00007 #define fMargin 0.05f 00008 #define kGYThreshold 0.4f 00009 #define kGYThresholdL (kGYThreshold - fMargin) 00010 #define kGYThresholdU (kGYThreshold + fMargin) 00011 00012 #define kYRThreshold 0.6f 00013 #define kYRThresholdL (kYRThreshold - fMargin) 00014 #define kYRThresholdU (kYRThreshold + fMargin) 00015 00016 00017 //Global objects 00018 BusOut binaryOutput(D5, D6, D7); //Outputs as an integer 00019 00020 DigitalIn SW1(D3); 00021 DigitalIn SW2(D4); 00022 00023 AnalogIn AIN(A0); 00024 float fVin = 0.0; 00025 unsigned state = kGREEN; 00026 00027 //Main function 00028 int main() { 00029 00030 while(1) { 00031 00032 //Read ADC 00033 fVin = AIN; 00034 00035 //Write to terminal 00036 printf("Analog input = %6.4f\n", fVin); 00037 00038 //Next State Logic 00039 switch (state) { 00040 00041 case kGREEN: 00042 if (fVin > kGYThresholdU) { 00043 state = kYELLOW; 00044 } 00045 break; 00046 00047 case kYELLOW: 00048 if (fVin > kYRThresholdU) { 00049 state = kRED; 00050 } else if (fVin < kGYThresholdL) { 00051 state = kGREEN; 00052 } 00053 break; 00054 00055 case kRED: 00056 if (fVin < kYRThresholdL) { 00057 state = kYELLOW; 00058 } 00059 break; 00060 00061 default: 00062 state = kGREEN; 00063 } 00064 00065 //Output logic = f(State) 00066 binaryOutput = state; 00067 00068 //Wait 00069 wait(0.1); 00070 00071 } //end while(1) 00072 } //end main
Generated on Wed Jul 20 2022 19:30:24 by
1.7.2