Marietjie Blignaut / Mbed 2 deprecated IceCreamMachine_

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*----------------------------------------------------------
00002 A fictional IoT-enabled Ice cream machine dispenser arm state
00003 The machine runs with an STM32F4 MCU; The Nucleo-F410RB is chosen for implementation, as it has serial communication, several digital outputs and 16 channel 12-bit ADC and more
00004 
00005 Operation: Ice cream machine dispenser arm sensor provides 0-3.3 V (assume based on operating voltage 
00006     of MCU) to one ADC pin on STM32F4 MCU. Voltage is based on position of dispenser arm. 
00007 - voltage > 1.65V implies the arm is pressed down 
00008 - voltage < 1.65V implies the arm is released 
00009 To prevent rapid switching, hysteresis is introduced with a counter function, if arm is pressed down for more than x seconds, the digital output will go high.
00010 
00011 Requirements:
00012 1. Read ADC value and print over serial port
00013 2. Digital output pin high if voltage>1.65V
00014 3. Digital output pin low if voltage <1.65V
00015 
00016 Assumptions:
00017 1. The code can run at a low speed as ice cream machines do not dispense ice cream quickly 
00018 2. Low accuracy and low resolution due to nature of ice cream machine dispensing arms
00019 
00020 Additional features
00021 1. Temperature of ice cream mixture is important, assume temperature sensor can provide analogue 
00022     voltage to MCU
00023 2. Level of ice cream mix in machine is important, assume a sensor can detect when the mixture drops 
00024     below a certain level, also be analogue voltage sensor output
00025 
00026 ADC data on MCU: 
00027 Value from 0.0 to 1.0, depending on input voltage between 0.0V and 
00028 3.3V; ADC default settings
00029 ------------------------------------------------------------*/
00030 #include "mbed.h"
00031 Ticker readADC;     //Allows timed interval of reading data on ADC lines
00032 
00033 //Serial pc(USBTX, USBRX); 
00034 Serial device(D8, D2);  // tx, rx, default settings: 9600 8N1
00035 
00036 AnalogIn DispArmIn(A0); //ADC input from dispensing arm
00037 AnalogIn TempIn(A1);    //ADC input from temperature sensor
00038 AnalogIn LevelIn(A2);   //ADC input from sensor detecting level
00039 
00040 float DispArm_data;     //Dispenser arm data
00041 float Temp_data;        //Temperature data
00042 float Level_data;       //Mixture level data
00043 bool ADC_ready;         //Indicate data has been read
00044 int counter;            //Hysteresis for dispenser arm
00045 
00046 DigitalOut dout(A4);    //Digital output to indicate if dispensing arm has crossed the halfway mark
00047 DigitalOut led(LED2);   //Digital output for general use on LED
00048 
00049 void ReadAnalog()
00050 {
00051 //Assume default AnalogIn read settings
00052     DispArm_data = DispArmIn;
00053     Temp_data  = TempIn;
00054     Level_data = LevelIn;
00055     
00056     if (DispArm_data>0.5f) //Use counter function to prevent rapid switching
00057     {//Trigger output pin; Add this section here as it is one of the critical requirements.
00058         counter++;
00059         if(counter>5)
00060         { 
00061             dout = 1;  //Arm is pressed down
00062             counter=0;
00063         }
00064     }
00065     else {
00066          dout=0;    //Arm is released
00067          counter=0;
00068     }
00069     ADC_ready=1;    //ADC have been read
00070 }
00071 
00072 int main() 
00073 {
00074     //Initialise variables here
00075     ADC_ready=0;
00076     counter=0;    
00077     DispArm_data = 0.0;
00078     Temp_data  = 0.0;
00079     Level_data = 0.0;
00080     
00081     readADC.attach(&ReadAnalog, 0.5);   // the address of the function attached and the interval (0.5 seconds)
00082     device.printf("\nIce cream machine state:\n");
00083     
00084     while (1) {
00085         if (ADC_ready)
00086         {
00087            //pc.printf("%f \n\r",ADCdata);
00088             device.printf("Dispenser arm %f \n\r",DispArm_data); 
00089             if(Temp_data>0.7f)
00090             {   
00091                 device.printf("Ice cream mixture not cooling: %f \n\r",Temp_data); 
00092             }
00093             if (Level_data<0.3f)
00094             {
00095                 device.printf("Ice cream mixture level low: %f \n\r",Level_data); 
00096             }
00097             ADC_ready=0;
00098         } else {}
00099         wait (0.2f);
00100     }
00101 }