living with the lab

advertisement
living with the lab
tips for fishtank programming
𝑜𝑢𝑡𝑝𝑢𝑡 = 1274.1 ∙ 𝑠𝑎𝑙𝑖𝑛𝑖𝑡𝑦 0.1325
𝑠𝑎𝑙𝑖𝑛𝑖𝑡𝑦 = 3.6686 10
© 2013 David Hall
−24
∙ 𝑜𝑢𝑡𝑝𝑢𝑡 7.5472
living with the lab
write an Arudino program to compute salinity
𝑠𝑎𝑙𝑖𝑛𝑖𝑡𝑦 = 3.6686 10
−24
∙ 𝑜𝑢𝑡𝑝𝑢𝑡 7.5472
what data type should we use
to store the output?
what data type should we use
to store the salinity?
go to the arduino.cc website to see if you can figure
out which data type might be best
tip 1: use the correct data type
2
living with the lab
tip 2: take small steps & verify the result of each step
print out EVERYTHING you calculate
3
living with the lab
look on the LWTL website if you can’t remember how to print things out
4
living with the lab
tip 2 example: small steps with verification
𝑠𝑎𝑙𝑖𝑛𝑖𝑡𝑦 = 3.6686 10
−24
∙ 𝑜𝑢𝑡𝑝𝑢𝑡 7.5472
don’t worry about this
part right now
how do we enter this number?
double salinity;
void setup()
{
Serial.begin(9600);
}
void loop()
{
salinity =
Serial.println(salinity);
}
try to figure out how to enter
this number
Don’t go to the next slide until you play around a few minutes, trying to figure this out on your own.
Learning how to figure out things that you don’t know is an important part of programming!
5
living with the lab
tip 2 example continued:
𝑠𝑎𝑙𝑖𝑛𝑖𝑡𝑦 = 3.6686 10
−24
double salinity;
void setup()
{
Serial.begin(9600);
}
void loop()
{
salinity = 3.6687E-24;
Serial.println(salinity);
}
The program seems to work, but only zeros are printed. How can we fix this?
HINT: We did something similar when working with the LCD screen.
6
living with the lab
tip 2 example continued:
𝑠𝑎𝑙𝑖𝑛𝑖𝑡𝑦 = 3.6686 10
−24
from LCD program:
double salinity;
void setup()
{
Serial.begin(9600);
}
void loop()
{
salinity = 3.6687E-24;
Serial.println(salinity);
}
Change the print command to something like this and run the program:
7
living with the lab
𝑠𝑎𝑙𝑖𝑛𝑖𝑡𝑦 = 3.6686 10
−24
∙ 𝑜𝑢𝑡𝑝𝑢𝑡 7.5472
tip 3: put in dummy values so calculations can be checked
double salinity;
int output;
void setup()
{
Serial.begin(9600);
}
comment out your analogRead
statement and just enter an estimated
value for the output so that you
can check your calculations!
void loop()
{
// output=analogRead(0);
output=500;
salinity = 3.6687E-24*pow(output,7.5472);
Serial.println(output);
Serial.println(salinity,29);
}
8
living with the lab
𝑠𝑎𝑙𝑖𝑛𝑖𝑡𝑦 = 3.6686 10
−24
∙ 𝑜𝑢𝑡𝑝𝑢𝑡 7.5472
tip 4: check EVERY number you compute
double salinity;
int output;
void setup()
{
Serial.begin(9600);
}
void loop()
{
// output=analogRead(0);
output=500;
salinity = 3.6687E-24*pow(output,7.5472);
Serial.println(output);
Serial.println(salinity,29);
}
Where did we fine out how to use the
“pow”
function?
you MUST check this number
on your calculator or in
Excel . . . not doing so is
just asking for trouble 
9
living with the lab
tip 5: format your print statements to keep track of things
double salinity;
int output;
void setup()
{
Serial.begin(9600);
}
void loop()
{
// output=analogRead(0);
output=500;
salinity = 3.6687E-24*pow(output,7.5472);
Serial.print("output = "); Serial.print(output);
Serial.print("
salinity = "); Serial.println(salinity,29);
}
Comment out print statements when you are done with them, just in case you
need them later.
10
living with the lab
tip 6: save all your old programs dealing with hardware
(1) your conductivity calibration program
a.
b.
c.
if you start getting erroneous readings from your conductivity sensor,
then rerun the calibration program.
if it doesn’t work correctly, then you have a HARDWARE problem.
if it does work correctly, then you have a SOFTWARE problem.
(2) your original LCD program
(3) a program to make the solenoid valves click on and off
•
Generally, if an old program doesn’t work anymore, then you have a HARDWARE problem
(assuming things are wired to the same pins as in the original program).
•
Isolating the problem as hardware or software related is a critical troubleshooting step.
•
Most of the old programs are on the LWTL site under “content” if you lose them for some
reason.
11
living with the lab
tip 7: put “i am here” statements in your functions
Serial.print("opening salty valve");
Serial.print("in upper main loop");
Serial.print("just finished measuring conductivity");
12
living with the lab
tip 8: restrain yourself people
resist the temptation of typing in large chunks of code
before checking program flow and calculation accuracy
remember that you are a systematic
problem solving machine
13
Download