Академический Документы
Профессиональный Документы
Культура Документы
Gerald Recktenwald
Portland State University
gerry@me.pdx.edu
Top-down design
❖ One technique for creating a plan for large, multi-step problems
❖ Not tied to flow charts, but can be used effectively with flow
charts
Specify constants
Initialize LCD
Read salinity
Initialize LCD
Read salinity
Read salinity
output pin,
input pin
Turn on power
Wait
analog
Read analog input
value
Stop
Arduino Programming Part 7: EAS 199B 9
Wednesday, February 20, 2013
Exercise 3
output pin,
input pin
Turn on power
Wait
analog
Read analog input
value
Stop
n readings,
input pin
analog
Read analog input
value
yes
Counter<n?
no
Average = sum/n
Stop
Arduino Programming Part 7: EAS 199B 12
Wednesday, February 20, 2013
Top-down design
Specify constants
Initialize LCD
Read salinity
analog
Display value to LCD Read analog input
value
Stop
analog
Display value to LCD Read analog input
value
analog
Read analog input
value
Turn off power
yes
Counter<n?
no
Average = sum/n
Stop
analog
Display value to LCD Read analog input
value
analog
Read analog input
value
Turn off power
yes
Counter<n?
void setup() {
Serial.begin(9600);
last_salinity_update = millis(); // Initial value; change later
}
void loop() {
float LCL, UCL, salinity;
int deadtime = ... ;
if ( salinity>UCL ) {
// add DI water: several missing steps
last_salinity_update = millis();
}
if ( salinity<LCL ) {
// add salty water: several missing steps
last_salinity_update = millis();
}
}
}
void loop() {
float LCL, UCL, salinity;
int deadtime = ... ; Don’t even consider making changes while
we are in the deadtime. “In the deadtime”
salinity = salinity_reading( ... );
update_LCD( ... ); means “current time minus time of last
system change” is greater than deadtime
// -- Check for deadtime
if ( ( millis() – last_salinity_update ) > deadtime ) {
if ( salinity>UCL ) {
// add DI water: several missing steps
last_salinity_update = millis();
} Update the “time of last system change”
if ( salinity<LCL ) { every time the salinity is changed
// add salty water: several missing steps
last_salinity_update = millis();
}
}
}
void setup() {
Serial.begin(9600);
last_salinity_update = millis();
}
if ( salinity>UCL ) {
// add DI water: several missing steps
last_salinity_update = millis(); 3. Function to determine size of the correction
} and open the valve. One function could
handle both corrections if you design it to
if ( salinity<LCL ) {
// add salty water: several missing steps use the right input arguments
last_salinity_update = millis();
}
}
}