KEMBAR78
B57AS Computing For Engineers - Lecture 2 - Structures | PDF | Boolean Data Type | Computer Programming
0% found this document useful (0 votes)
23 views47 pages

B57AS Computing For Engineers - Lecture 2 - Structures

The document outlines Lecture 2 of the B57AS Mechanical Engineering course, focusing on programming concepts such as FOR loops and Boolean logic using Arduino. It includes practical tasks for students to set up accounts in TinkerCAD, create a Larson scanner, and simulate a fluid tank with level indicators. Additionally, it provides resources and contact information for further assistance and tutorials.

Uploaded by

grokking Stuff
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views47 pages

B57AS Computing For Engineers - Lecture 2 - Structures

The document outlines Lecture 2 of the B57AS Mechanical Engineering course, focusing on programming concepts such as FOR loops and Boolean logic using Arduino. It includes practical tasks for students to set up accounts in TinkerCAD, create a Larson scanner, and simulate a fluid tank with level indicators. Additionally, it provides resources and contact information for further assistance and tutorials.

Uploaded by

grokking Stuff
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 47

Computing for

B57AS Mechanical Engineering 1

Engineers
Steven Hammer
s.hammer@hw.ac.uk

Lecture 2:
Structures
A quick recap
Mechanical engineering isn’t just about
gears, beams and lumps of metal…
…it’s also about making …and about measuring
things move the way you things to control them or
want them to… make sure they work as
you expect.

https://commons.wikimedia.org/wiki/File:Gran
gemouth04nov06.jpg
User:John, CC BY-CommonsSA 3.0
<http://creativecommons.org/licenses/by-sa/3.
0/>, via Wikimedia
Things to do from lecture 1:

1 2 3
Set up an Try “Blink” in Check out the
account in TinkerCAD materials about
TinkerCAD A/B/C++ in
Teams and on
Canvas
Getting started with
TinkerCAD
• Go to
https://www.tinkercad.com/jo
inclass/8A92BS8HY94A
• This will allow you to join the
class “2021-2022 B57AS
Computing for Engineers”
• Use your Heriot-Watt
username to create an
account
• E.g. if your email address is
hw123@hw.ac.uk
• Use “hw123” as your
nickname
Blink
What are we going
to learn?
This week we’re going to look
at
• Larson scanner

• FOR Loops

• Logic
Larson scanner
https://commons.wikimedia.org/wiki/File:KITT_at_Universal_Studios,_original_uncropp
ed_version.jpg
Larson scanner
Larson scanner code
• It looks simple.

• How easy is it to
understand?
• How easy is it to extend?
• How could we do it
differently?
Larson scanner with a FOR
loop
The FOR loop
• Do a set task for a fixed number of repetitions

for (initialisation; condition; increment) {


// do stuff
}

• More info:
https://www.arduino.cc/reference/en/language/structure/con
trol-structure/for/
• Additional example in tutorial 3: RGB LED
The FOR loop
What does “int” mean?
• Integer: int PinCount declares an integer variable called
PinCount
• Other data types are available, including:
Name Type Example
bool Boolean ButtonPressed = TRUE;
LEDOn = FALSE;
string A string of characters UserName = “Steven”;
Answer = “Forty-two”;
float Floating point Temperature = 36.2;
number Gravity = 9.81;

• For more information, see https://www.arduino.cc/reference/en/


What do these lines mean?
PinCount--;

and

PinCount++;
++ and --
Increment operator: More info:
https://www.arduino.cc/reference/en/language/
PinCount++; structure/compound-operators/increment/

is the same as
PinCount = PinCount + 1;

Decrement operator:
PinCount--;
is the same as
PinCount = Pincount - 1;
++ and --
Increment operator: More info:
https://www.arduino.cc/reference/en/language/
PinCount++; structure/compound-operators/increment/

is the same as
PinCount = PinCount + 1;

Decrement operator:
PinCount--; Can you see the mistake I’ve
is the same as made?
PinCount = Pincount - 1;
Deliberate mistake
• Did you notice it?

• Variable names are cAsE SeNsItIvE

PinCount is not the same as Pincount


The FOR loop – Larson
scanner
// Start at the bottom LED
// Light LEDs in sequence from bottom to top
for (int PinCount=0; PinCount <=3; PinCount++){
digitalWrite(PinCount,HIGH); // Switch on
delay(1000); // Wait 1s
digitalWrite(PinCount,LOW); // Switch off
}
• If you wanted to extend the
Larson scanner to have eight
LEDs, which version would be
easier to modify?
Larson scanner: summary
• When programming, there are often lots of ways to do
the same thing.
• Larson scanner with individual statements to control LEDs
• Larson scanner with a FOR loop
• Larson scanner with an array and a FOR loop
• Larson scanner with a WHILE loop
• …
• The question to ask when programming is: what’s the
most efficient way to perform the task?
Flash! (Ah-
Let’s leave the Larson

scanner and leap into

logic…
Boolean logic: fluid tank
• Fluid tank with two level sensors

• High level sensor


• Checks if fluid level is too high
• Tank is at risk of overflowing

• Low level sensor


• Checks if fluid level is too low
• Air may enter the pipework

• How do we check its level and


warn the user about these
problems?
Simulate a tank using
Arduino
Simulated tank
• Top switch – high level sensor
• Bottom switch – low level sensor

• Green LED – tank full indicator


• Red LED – tank empty indicator

• This is the second tutorial example (“Alarm”) and is available on Canvas


https://canvas.hw.ac.uk/courses/3741/pages/arduino-exercises
Switch logic
• IF the top level switch is switched ON…
• …the tank is full. Light the green LED

• IF the bottom level switch is switched OFF…


• …the tank is empty. Light the red LED.
Tank level switches code

if statement:
if (condition) do something;

More info:
https://www.arduino.cc/reference/en/language/structure
/control-structure/if/
Comparisons
Code Meaning
x == y x is equal to y
x != y x is not equal to y
x < y x is less than y
x > y x is greater than y
x <= y x is less than or equal to y
x >= y x is greater than or equal to y
Careful: x = y means something different to x == y
• x = y means “make the value of x the same as the value of
y”
• x == y means “check to see if the value of x is the same as
the value of y”
Let’s add an extra indicator
LED

A: tank partly B: tank empty C: tank full D: ERROR


filled
Switch logic
• Tank is empty
• Switch on yellow LED

• Tank is partly filled


• Switch on green LED

• Tank is full
• Switch on red LED
Logic for LEDs

Switch released =
HIGH
Switch pressed =
LOW

Component A: tank partly B: tank C: tank full D: ERROR


filled empty
Top switch (switchA) HIGH HIGH LOW LOW
Bottom switch LOW HIGH LOW HIGH
(switchB)
Green LED (ledA) ON OFF OFF OFF
Yellow LED (ledB) OFF ON OFF OFF
Red LED (ledC) OFF OFF ON OFF
Extra LED code
Three new things
1. AND &&
2. else
3. Serial.print(…)
Logical AND (&&) Boolean
operator
• Checks to see if two conditions are both true

if(valueA == HIGH && valueB == LOW){…};

If valueA is HIGH and valueB is LOW then…

More info:
https://www.arduino.cc/reference/en/language/structure
/boolean-operators/logicaland/
Other logical operators
OR ||
https://www.arduino.cc/reference/en/language/structure
/boolean-operators/logicalor/

NOT !
https://www.arduino.cc/reference/en/language/structure
/boolean-operators/logicalnot/
else
• Group several tests together

if(valueA == HIGH && valueB == LOW){


//do something
}else if(valueA == HIGH && valueB == HIGH){
// do something else
}else{
// do yet another thing
}

More info:
https://www.arduino.cc/reference/en/language/structure/control-struct
ure/else/
Output data to the Serial
Monitor
• How do I know the code is
working?
• How can I communicate with
the user?
• Answer: the Serial Monitor
Output data to the Serial
Monitor
Serial.begin(9600);
Put Serial.begin(…) in the void setup(){…} function
9600 refers to the baud rate – 9600 bits per second.

Serial.println("LED A on");
Print “LED A on” to the serial port with a linefeed (new line) character at the end.

Serial.print("LED B on");//No linefeed character

More info:
https://www.arduino.cc/reference/en/language/functions/communication/serial/pr
int/
What have we covered?
• Larson scanner – two different ways to do the same
thing
• FOR loops
• ++ and --
• Declaring variables

• Logic – giving warnings of fluid level in a tank


• if statements: if(condition){do something} else{…};
• Comparisons e.g. x == y
• Logic: AND (&&), OR (||), NOT (!)
• Outputting messages to the Serial Monitor
This week’s tasks

1 2 3
Create a Larson Try the tank level Come to a tutorial. Try
scanner using a FOR indicator examples in using a real Arduino.
loop in TinkerCAD TinkerCAD Friday (Edinburgh
(Tutorial 2) campus)
10-11 (groups A-H)
11-12 (groups I-P)
How does that look?
• This is straightforward. I want to do more.
• Look ahead to tutorials 3 and 4 – RGB LED, DC servomotor
and pulse wave modulation

• I’m not sure what to do


• Work up from Blink step-by-step
• Extend Blink to create the Larson scanner – add more LEDs and
resistors.
• Extend Blink to create the tank example – add push buttons and their
resistors.
How does that look?
• I’m still struggling
• Ask for help in the Teams
channel:
• Book a help session:
https://outlook.office365.co
m/owa/calendar/B57ASCom
putingforEngineers@heriotw
att.onmicrosoft.com/booking
s/
This week’s tasks

1 2 3
Create a Larson Try the tank level Come to a tutorial. Try
scanner using a FOR indicator examples in using a real Arduino.
loop in TinkerCAD TinkerCAD Friday (Edinburgh
(Tutorial 2) campus)
10-11 (groups A-H)
11-12 (groups I-P)
Thanks for listening!
Dr Steven Hammer
GRID Project Manager/Developer in Mechanical
Engineering
Management/Support Office
GRID
s.hammer@hw.ac.uk

To book a help session (Wednesday or Friday afternoons):


https://outlook.office365.com/owa/calendar/B57ASComp
utingforEngineers@heriotwatt.onmicrosoft.com/bookings/

You might also like