Marietjie Blignaut / Mbed 2 deprecated IceCreamMachine_

Dependencies:   mbed

Committer:
marietjie
Date:
Sun Jul 17 18:14:09 2022 +0000
Revision:
2:11714802a55d
Parent:
1:011f0bdaa7f5
rev1_

Who changed what in which revision?

UserRevisionLine numberNew contents of line
marietjie 0:28f2dba63d6c 1 /*----------------------------------------------------------
marietjie 1:011f0bdaa7f5 2 A fictional IoT-enabled Ice cream machine dispenser arm state
marietjie 1:011f0bdaa7f5 3 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
marietjie 0:28f2dba63d6c 4
marietjie 1:011f0bdaa7f5 5 Operation: Ice cream machine dispenser arm sensor provides 0-3.3 V (assume based on operating voltage
marietjie 0:28f2dba63d6c 6 of MCU) to one ADC pin on STM32F4 MCU. Voltage is based on position of dispenser arm.
marietjie 0:28f2dba63d6c 7 - voltage > 1.65V implies the arm is pressed down
marietjie 0:28f2dba63d6c 8 - voltage < 1.65V implies the arm is released
marietjie 2:11714802a55d 9 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.
marietjie 0:28f2dba63d6c 10
marietjie 0:28f2dba63d6c 11 Requirements:
marietjie 0:28f2dba63d6c 12 1. Read ADC value and print over serial port
marietjie 0:28f2dba63d6c 13 2. Digital output pin high if voltage>1.65V
marietjie 0:28f2dba63d6c 14 3. Digital output pin low if voltage <1.65V
marietjie 0:28f2dba63d6c 15
marietjie 0:28f2dba63d6c 16 Assumptions:
marietjie 0:28f2dba63d6c 17 1. The code can run at a low speed as ice cream machines do not dispense ice cream quickly
marietjie 0:28f2dba63d6c 18 2. Low accuracy and low resolution due to nature of ice cream machine dispensing arms
marietjie 0:28f2dba63d6c 19
marietjie 0:28f2dba63d6c 20 Additional features
marietjie 0:28f2dba63d6c 21 1. Temperature of ice cream mixture is important, assume temperature sensor can provide analogue
marietjie 0:28f2dba63d6c 22 voltage to MCU
marietjie 0:28f2dba63d6c 23 2. Level of ice cream mix in machine is important, assume a sensor can detect when the mixture drops
marietjie 0:28f2dba63d6c 24 below a certain level, also be analogue voltage sensor output
marietjie 0:28f2dba63d6c 25
marietjie 0:28f2dba63d6c 26 ADC data on MCU:
marietjie 0:28f2dba63d6c 27 Value from 0.0 to 1.0, depending on input voltage between 0.0V and
marietjie 0:28f2dba63d6c 28 3.3V; ADC default settings
marietjie 0:28f2dba63d6c 29 ------------------------------------------------------------*/
marietjie 0:28f2dba63d6c 30 #include "mbed.h"
marietjie 0:28f2dba63d6c 31 Ticker readADC; //Allows timed interval of reading data on ADC lines
marietjie 0:28f2dba63d6c 32
marietjie 1:011f0bdaa7f5 33 //Serial pc(USBTX, USBRX);
marietjie 0:28f2dba63d6c 34 Serial device(D8, D2); // tx, rx, default settings: 9600 8N1
marietjie 0:28f2dba63d6c 35
marietjie 0:28f2dba63d6c 36 AnalogIn DispArmIn(A0); //ADC input from dispensing arm
marietjie 0:28f2dba63d6c 37 AnalogIn TempIn(A1); //ADC input from temperature sensor
marietjie 1:011f0bdaa7f5 38 AnalogIn LevelIn(A2); //ADC input from sensor detecting level
marietjie 0:28f2dba63d6c 39
marietjie 0:28f2dba63d6c 40 float DispArm_data; //Dispenser arm data
marietjie 0:28f2dba63d6c 41 float Temp_data; //Temperature data
marietjie 0:28f2dba63d6c 42 float Level_data; //Mixture level data
marietjie 0:28f2dba63d6c 43 bool ADC_ready; //Indicate data has been read
marietjie 1:011f0bdaa7f5 44 int counter; //Hysteresis for dispenser arm
marietjie 0:28f2dba63d6c 45
marietjie 1:011f0bdaa7f5 46 DigitalOut dout(A4); //Digital output to indicate if dispensing arm has crossed the halfway mark
marietjie 0:28f2dba63d6c 47 DigitalOut led(LED2); //Digital output for general use on LED
marietjie 0:28f2dba63d6c 48
marietjie 0:28f2dba63d6c 49 void ReadAnalog()
marietjie 0:28f2dba63d6c 50 {
marietjie 0:28f2dba63d6c 51 //Assume default AnalogIn read settings
marietjie 0:28f2dba63d6c 52 DispArm_data = DispArmIn;
marietjie 0:28f2dba63d6c 53 Temp_data = TempIn;
marietjie 0:28f2dba63d6c 54 Level_data = LevelIn;
marietjie 0:28f2dba63d6c 55
marietjie 1:011f0bdaa7f5 56 if (DispArm_data>0.5f) //Use counter function to prevent rapid switching
marietjie 0:28f2dba63d6c 57 {//Trigger output pin; Add this section here as it is one of the critical requirements.
marietjie 0:28f2dba63d6c 58 counter++;
marietjie 0:28f2dba63d6c 59 if(counter>5)
marietjie 0:28f2dba63d6c 60 {
marietjie 0:28f2dba63d6c 61 dout = 1; //Arm is pressed down
marietjie 0:28f2dba63d6c 62 counter=0;
marietjie 1:011f0bdaa7f5 63 }
marietjie 0:28f2dba63d6c 64 }
marietjie 0:28f2dba63d6c 65 else {
marietjie 0:28f2dba63d6c 66 dout=0; //Arm is released
marietjie 0:28f2dba63d6c 67 counter=0;
marietjie 0:28f2dba63d6c 68 }
marietjie 0:28f2dba63d6c 69 ADC_ready=1; //ADC have been read
marietjie 0:28f2dba63d6c 70 }
marietjie 0:28f2dba63d6c 71
marietjie 0:28f2dba63d6c 72 int main()
marietjie 0:28f2dba63d6c 73 {
marietjie 0:28f2dba63d6c 74 //Initialise variables here
marietjie 0:28f2dba63d6c 75 ADC_ready=0;
marietjie 0:28f2dba63d6c 76 counter=0;
marietjie 0:28f2dba63d6c 77 DispArm_data = 0.0;
marietjie 0:28f2dba63d6c 78 Temp_data = 0.0;
marietjie 0:28f2dba63d6c 79 Level_data = 0.0;
marietjie 1:011f0bdaa7f5 80
marietjie 1:011f0bdaa7f5 81 readADC.attach(&ReadAnalog, 0.5); // the address of the function attached and the interval (0.5 seconds)
marietjie 0:28f2dba63d6c 82 device.printf("\nIce cream machine state:\n");
marietjie 0:28f2dba63d6c 83
marietjie 0:28f2dba63d6c 84 while (1) {
marietjie 0:28f2dba63d6c 85 if (ADC_ready)
marietjie 0:28f2dba63d6c 86 {
marietjie 0:28f2dba63d6c 87 //pc.printf("%f \n\r",ADCdata);
marietjie 0:28f2dba63d6c 88 device.printf("Dispenser arm %f \n\r",DispArm_data);
marietjie 0:28f2dba63d6c 89 if(Temp_data>0.7f)
marietjie 0:28f2dba63d6c 90 {
marietjie 0:28f2dba63d6c 91 device.printf("Ice cream mixture not cooling: %f \n\r",Temp_data);
marietjie 0:28f2dba63d6c 92 }
marietjie 0:28f2dba63d6c 93 if (Level_data<0.3f)
marietjie 0:28f2dba63d6c 94 {
marietjie 0:28f2dba63d6c 95 device.printf("Ice cream mixture level low: %f \n\r",Level_data);
marietjie 0:28f2dba63d6c 96 }
marietjie 0:28f2dba63d6c 97 ADC_ready=0;
marietjie 0:28f2dba63d6c 98 } else {}
marietjie 0:28f2dba63d6c 99 wait (0.2f);
marietjie 0:28f2dba63d6c 100 }
marietjie 0:28f2dba63d6c 101 }