B57as L03
B57as L03
B57AS
Lecture 03
This week:
• More on Operators
• Time functions
• Using mathematical library functions
• Symbolic constants
• A case study involving acid rain
• Common programming errors
Assignment Operations – Part 2
• Coercion: Forcing a data value to another data type
double x = 2.3;
double y = 3.2;
• Variable on the left side may also be used on the right side of
an assignment statement
int sum = x + y; // output is 5
Assignment Operations – Part 2
Accumulation statement: Has the effect of accumulating, or
totaling
Syntax:
variable = variable + newValue;
void setup(){
Serial.begin(9600);
}
void loop() {
Serial.print(“Time: “);
time = micros();
//prints time since program started
Serial.println(time);
// wait a second so as not to send massive amounts of data
delay(1000); // in milliseconds
delayMicroseconds(500); // waits for 500 microseconds
}
Mathematical Functions
Math.h contains the trigonometry function's prototype.
void setup()
{
Serial.begin(9600);
}
Using Floating-Point Numbers
void loop()
{
// reduce value by 0.1 each time through the loop
value = value - 0.1;
if( value == 0)
Serial.println("The value is exactly zero");
else if(almostEqual(value, 0)) // call custom function
{
Serial.print("The value ");
Serial.print(value,7); // print to 7 decimal places
Serial.println(" is almost equal to zero");
}
else
Serial.println(value);
delay(100);
}
Using Floating-Point Numbers
pH level Acidity
7 Neutral
<7 Acidic
>7 Alkaline
Code the Solution