MzTEK Programming - Part 1

67

description

First lecture on introductory programming for MzTEK. It covers basic workflow and primitive data types. It is assumed you are at least a little familiar with Processing and/or Arduino.

Transcript of MzTEK Programming - Part 1

Page 1: MzTEK Programming - Part 1
Page 2: MzTEK Programming - Part 1

WHAT WILL WE COVER?• How computers work and the tools we can use to make them

do what we want

• The core components of a program

• Data types

• Control structures

• Functions

• Understanding code someone else has written

• Look at programming for the Arduino and Processing environments

Page 3: MzTEK Programming - Part 1

INTRODUCTIONS

• Describe your experience with programming

•What is something you want to be able to do? a dream project?

Page 4: MzTEK Programming - Part 1

PAIRED PROGRAMMING

• Find a partner with similar experience as you

• You will share a computer and take turns typing

• All exercises will be done in pairs

Page 5: MzTEK Programming - Part 1

WHAT ARE PROGRAMS AND WHY DO WE NEED THEM?

Page 6: MzTEK Programming - Part 1

COMPUTERS ARE STUPID

Page 7: MzTEK Programming - Part 1

Code You’ve Written

Instructions for Computer?

Executable Program (e.g. .exe or .app)

Page 8: MzTEK Programming - Part 1

Code You’ve Written

Instructions for Computer?

Executable Program (e.g. .exe or .app)

Compiler +

Linker +

Loader

Page 9: MzTEK Programming - Part 1

Code You’ve Written

Instructions for Computer?

Executable Program (e.g. .exe or .app)

Interactive Development

Environment (IDE)

Compiler +

Linker +

Loader

Page 10: MzTEK Programming - Part 1

Think of idea

Write code

Run program

Page 11: MzTEK Programming - Part 1

Think of idea

Break down problem into tiny steps

Write code for one step

Run program

Page 12: MzTEK Programming - Part 1

EXERCISEWrite out the steps to either :

• Knit a scarf

•Make mashed potatoes

Identify the variables and break down steps to simplest instructions. What gets repeated?

What would you need to do to increase the length or increase the number of servings?

Page 13: MzTEK Programming - Part 1

WHY DO WE NEED TO COMPILE PROGRAMS?

Page 14: MzTEK Programming - Part 1

Code You’ve Written

Instructions for Computer?

Executable Program (e.g. .exe or .app)

Interactive Development

Environment (IDE)

Compiler +

Linker +

Loader

Page 15: MzTEK Programming - Part 1

An IDE like Processing or Arduino includes

• text editor

•compiler and linker

•other tools to help you

Page 16: MzTEK Programming - Part 1

?

Compiler • Translate your text into a language

(symbols) that a computer understands

Linker • Combine the compiled output of

what you’ve written with what other people have written such as libraries

Loader• On the Arduino chip, the program

needs to be moved from your computer to the chip. With Processing, your computer is running the program, so it doesn’t need to be moved.

Page 17: MzTEK Programming - Part 1

• Arduino means a piece of hardware, a programming language, and an IDE

• The hardware is a microcontroller plus surrounding circuitry and is completely open - you can build your own board if you want to!

• Language is based on Wiring which is based on Processing and C++ (a very common language)

• IDE is written in Java and derived from Processing IDE and Wiring Project

BRIEF HISTORY OF ARDUINO

Page 18: MzTEK Programming - Part 1

BRIEF HISTORY OF PROCESSING

• Processing is a set of libraries and a development environment

• Initially developed to teach programming through visuals

• The Processing Development Environment uses Java (another very common programming language)

• Can use the Processing libraries outside of the Processing Development Environment

Page 19: MzTEK Programming - Part 1

EXERCISEHook up Arduino board to computer and open the Arduino IDE.

How do you choose the compiler settings? How do you set up the loader?

Open the blink example and save as a new sketch

Change blink time so the LED stays on for 5 seconds.

/* Blink Turns on an LED on for one second, then off for one second, repeatedly. This example code is in the public domain. */

void setup() { // initialize the digital pin as an output. // Pin 13 has an LED connected on most Arduino boards: pinMode(13, OUTPUT); }

void loop() { digitalWrite(13, HIGH); // set the LED on delay(1000); // wait for a second digitalWrite(13, LOW); // set the LED off delay(1000); // wait for a second}

Page 20: MzTEK Programming - Part 1

HOW DO WE STORE DATA?-OR-

WHAT ARE VARIABLES?

Page 21: MzTEK Programming - Part 1

Heat the oil in a large, heavy-based saucepan and fry the bacon until golden over a medium heat. Add the onions and garlic, frying until softened. Increase the heat and add the minced beef. Fry it until it has browned, breaking down any chunks of meat with a wooden spoon. Pour in the wine and boil until it has reduced in volume by about a third. Reduce the temperature and stir in the tomatoes, drained mushrooms, bay leaves, oregano, thyme and balsamic vinegar.

1. Either blitz the sun-dried tomatoes in a small blender with a little of the oil to loosen, or just finely chop before adding to the pan. Season well with salt and pepper. Cover with a lid and simmer the Bolognese sauce over a gentle heat for 1-1½ hours until it's rich and thickened, stirring occasionally. At the end of the cooking time, stir in the basil and add any extra seasoning if necessary.

2. Remove from the heat to 'settle' while you cook the spaghetti in plenty of boiling salted water (for the time stated on the packet). Drain and divide between warmed plates. Scatter a little parmesan over the spaghetti before adding a good ladleful of the Bolognese sauce, finishing with a scattering of more cheese and a twist of blackhttp://www.bbc.co.uk/food/recipes/spaghettibolognese_67868

2 tbsp olive oil or sun-dried tomato oil from the jar6 rashers of smoked streaky bacon, chopped2 large onions, chopped3 garlic cloves, crushed1kg/2¼lb lean minced beef2 large glasses of red wine2x400g cans chopped tomatoes1x290g jar antipasti marinated mushrooms, drained2 fresh or dried bay leaves

1 tsp dried oregano or a small handful of fresh leaves, chopped1 tsp dried thyme or a small handful of fresh leaves, choppedDrizzle balsamic vinegar12-14 sun-dried tomato halves, in oilSalt and freshly ground black pepperA good handful of fresh basil leaves, torn into small pieces800g-1kg/1¾-2¼lb dried spaghettiLots of freshly grated parmesan, to serve

Page 22: MzTEK Programming - Part 1

Heat the oil in a large, heavy-based saucepan and fry the bacon until golden over a medium heat. Add the onions and garlic, frying until softened. Increase the heat and add the minced beef. Fry it until it has browned, breaking down any chunks of meat with a wooden spoon. Pour in the wine and boil until it has reduced in volume by about a third. Reduce the temperature and stir in the tomatoes, drained mushrooms, bay leaves, oregano, thyme and balsamic vinegar.

1. Either blitz the sun-dried tomatoes in a small blender with a little of the oil to loosen, or just finely chop before adding to the pan. Season well with salt and pepper. Cover with a lid and simmer the Bolognese sauce over a gentle heat for 1-1½ hours until it's rich and thickened, stirring occasionally. At the end of the cooking time, stir in the basil and add any extra seasoning if necessary.

2. Remove from the heat to 'settle' while you cook the spaghetti in plenty of boiling salted water (for the time stated on the packet). Drain and divide between warmed plates. Scatter a little parmesan over the spaghetti before adding a good ladleful of the Bolognese sauce, finishing with a scattering of more cheese and a twist of blackhttp://www.bbc.co.uk/food/recipes/spaghettibolognese_67868

2 tbsp olive oil or sun-dried tomato oil from the jar6 rashers of smoked streaky bacon, chopped2 large onions, chopped3 garlic cloves, crushed1kg/2¼lb lean minced beef2 large glasses of red wine2x400g cans chopped tomatoes1x290g jar antipasti marinated mushrooms, drained2 fresh or dried bay leaves

1 tsp dried oregano or a small handful of fresh leaves, chopped1 tsp dried thyme or a small handful of fresh leaves, choppedDrizzle balsamic vinegar12-14 sun-dried tomato halves, in oilSalt and freshly ground black pepperA good handful of fresh basil leaves, torn into small pieces800g-1kg/1¾-2¼lb dried spaghettiLots of freshly grated parmesan, to serve

Page 23: MzTEK Programming - Part 1

Heat the oil in a large, heavy-based saucepan and fry the bacon until golden over a medium heat. Add the onions and garlic, frying until softened. Increase the heat and add the minced beef. Fry it until it has browned, breaking down any chunks of meat with a wooden spoon. Pour in the wine and boil until it has reduced in volume by about a third. Reduce the temperature and stir in the tomatoes, drained mushrooms, bay leaves, oregano, thyme and balsamic vinegar.

1. Either blitz the sun-dried tomatoes in a small blender with a little of the oil to loosen, or just finely chop before adding to the pan. Season well with salt and pepper. Cover with a lid and simmer the Bolognese sauce over a gentle heat for 1-1½ hours until it's rich and thickened, stirring occasionally. At the end of the cooking time, stir in the basil and add any extra seasoning if necessary.

2. Remove from the heat to 'settle' while you cook the spaghetti in plenty of boiling salted water (for the time stated on the packet). Drain and divide between warmed plates. Scatter a little parmesan over the spaghetti before adding a good ladleful of the Bolognese sauce, finishing with a scattering of more cheese and a twist of blackhttp://www.bbc.co.uk/food/recipes/spaghettibolognese_67868

2 tbsp olive oil or sun-dried tomato oil from the jar6 rashers of smoked streaky bacon, chopped2 large onions, chopped3 garlic cloves, crushed1kg/2¼lb lean minced beef2 large glasses of red wine2x400g cans chopped tomatoes1x290g jar antipasti marinated mushrooms, drained2 fresh or dried bay leaves

1 tsp dried oregano or a small handful of fresh leaves, chopped1 tsp dried thyme or a small handful of fresh leaves, choppedDrizzle balsamic vinegar12-14 sun-dried tomato halves, in oilSalt and freshly ground black pepperA good handful of fresh basil leaves, torn into small pieces800g-1kg/1¾-2¼lb dried spaghettiLots of freshly grated parmesan, to serve

Page 24: MzTEK Programming - Part 1

Heat the oil in a large, heavy-based saucepan and fry the bacon until golden over a medium heat. Add the onions and garlic, frying until softened. Increase the heat and add the minced beef. Fry it until it has browned, breaking down any chunks of meat with a wooden spoon. Pour in the wine and boil until it has reduced in volume by about a third. Reduce the temperature and stir in the tomatoes, drained mushrooms, bay leaves, oregano, thyme and balsamic vinegar.

1. Either blitz the sun-dried tomatoes in a small blender with a little of the oil to loosen, or just finely chop before adding to the pan. Season well with salt and pepper. Cover with a lid and simmer the Bolognese sauce over a gentle heat for 1-1½ hours until it's rich and thickened, stirring occasionally. At the end of the cooking time, stir in the basil and add any extra seasoning if necessary.

2. Remove from the heat to 'settle' while you cook the spaghetti in plenty of boiling salted water (for the time stated on the packet). Drain and divide between warmed plates. Scatter a little parmesan over the spaghetti before adding a good ladleful of the Bolognese sauce, finishing with a scattering of more cheese and a twist of blackhttp://www.bbc.co.uk/food/recipes/spaghettibolognese_67868

2 tbsp olive oil or sun-dried tomato oil from the jar6 rashers of smoked streaky bacon, chopped2 large onions, chopped3 garlic cloves, crushed1kg/2¼lb lean minced beef2 large glasses of red wine2x400g cans chopped tomatoes1x290g jar antipasti marinated mushrooms, drained2 fresh or dried bay leaves

1 tsp dried oregano or a small handful of fresh leaves, chopped1 tsp dried thyme or a small handful of fresh leaves, choppedDrizzle balsamic vinegar12-14 sun-dried tomato halves, in oilSalt and freshly ground black pepperA good handful of fresh basil leaves, torn into small pieces800g-1kg/1¾-2¼lb dried spaghettiLots of freshly grated parmesan, to serve

We would be very irritated if there was an ingredient needed,

that wasn’t included here.

Page 25: MzTEK Programming - Part 1

Heat the oil in a large, heavy-based saucepan and fry the bacon until golden over a medium heat. Add the onions and garlic, frying until softened. Increase the heat and add the minced beef. Fry it until it has browned, breaking down any chunks of meat with a wooden spoon. Pour in the wine and boil until it has reduced in volume by about a third. Reduce the temperature and stir in the tomatoes, drained mushrooms, bay leaves, oregano, thyme and balsamic vinegar.

1. Either blitz the sun-dried tomatoes in a small blender with a little of the oil to loosen, or just finely chop before adding to the pan. Season well with salt and pepper. Cover with a lid and simmer the Bolognese sauce over a gentle heat for 1-1½ hours until it's rich and thickened, stirring occasionally. At the end of the cooking time, stir in the basil and add any extra seasoning if necessary.

2. Remove from the heat to 'settle' while you cook the spaghetti in plenty of boiling salted water (for the time stated on the packet). Drain and divide between warmed plates. Scatter a little parmesan over the spaghetti before adding a good ladleful of the Bolognese sauce, finishing with a scattering of more cheese and a twist of blackhttp://www.bbc.co.uk/food/recipes/spaghettibolognese_67868

2 tbsp olive oil or sun-dried tomato oil from the jar6 rashers of smoked streaky bacon, chopped2 large onions, chopped3 garlic cloves, crushed1kg/2¼lb lean minced beef2 large glasses of red wine2x400g cans chopped tomatoes1x290g jar antipasti marinated mushrooms, drained2 fresh or dried bay leaves

1 tsp dried oregano or a small handful of fresh leaves, chopped1 tsp dried thyme or a small handful of fresh leaves, choppedDrizzle balsamic vinegar12-14 sun-dried tomato halves, in oilSalt and freshly ground black pepperA good handful of fresh basil leaves, torn into small pieces800g-1kg/1¾-2¼lb dried spaghettiLots of freshly grated parmesan, to serve

We would be very irritated if there was an ingredient needed,

that wasn’t included here.

The units used to describe the amount of the ingredients varies according to

the culture of the person cooking.

Page 26: MzTEK Programming - Part 1

Heat the oil in a large, heavy-based saucepan and fry the bacon until golden over a medium heat. Add the onions and garlic, frying until softened. Increase the heat and add the minced beef. Fry it until it has browned, breaking down any chunks of meat with a wooden spoon. Pour in the wine and boil until it has reduced in volume by about a third. Reduce the temperature and stir in the tomatoes, drained mushrooms, bay leaves, oregano, thyme and balsamic vinegar.

1. Either blitz the sun-dried tomatoes in a small blender with a little of the oil to loosen, or just finely chop before adding to the pan. Season well with salt and pepper. Cover with a lid and simmer the Bolognese sauce over a gentle heat for 1-1½ hours until it's rich and thickened, stirring occasionally. At the end of the cooking time, stir in the basil and add any extra seasoning if necessary.

2. Remove from the heat to 'settle' while you cook the spaghetti in plenty of boiling salted water (for the time stated on the packet). Drain and divide between warmed plates. Scatter a little parmesan over the spaghetti before adding a good ladleful of the Bolognese sauce, finishing with a scattering of more cheese and a twist of blackhttp://www.bbc.co.uk/food/recipes/spaghettibolognese_67868

2 tbsp olive oil or sun-dried tomato oil from the jar6 rashers of smoked streaky bacon, chopped2 large onions, chopped3 garlic cloves, crushed1kg/2¼lb lean minced beef2 large glasses of red wine2x400g cans chopped tomatoes1x290g jar antipasti marinated mushrooms, drained2 fresh or dried bay leaves

1 tsp dried oregano or a small handful of fresh leaves, chopped1 tsp dried thyme or a small handful of fresh leaves, choppedDrizzle balsamic vinegar12-14 sun-dried tomato halves, in oilSalt and freshly ground black pepperA good handful of fresh basil leaves, torn into small pieces800g-1kg/1¾-2¼lb dried spaghettiLots of freshly grated parmesan, to serve

We would be very irritated if there was an ingredient needed,

that wasn’t included here.

It’s easier to follow the directions when you don’t have to list the amounts of

ingredients within the directions.

The units used to describe the amount of the ingredients varies according to

the culture of the person cooking.

Page 27: MzTEK Programming - Part 1

int x;int y;int width;int height;

size(200, 200);

x = 150;y = 100;width = 90;height = 80;

ellipse(x, y, width, height);

Page 28: MzTEK Programming - Part 1

int x;int y;int width;int height;

size(200, 200);

x = 150;y = 100;width = 90;height = 80;

ellipse(x, y, width, height);

ingredients or variables

Page 29: MzTEK Programming - Part 1

int x;int y;int width;int height;

size(200, 200);

x = 150;y = 100;width = 90;height = 80;

ellipse(x, y, width, height);

ingredients or variables

directions or algorithm

Page 30: MzTEK Programming - Part 1

int x;int y;int width;int height;

size(200, 200);

x = 150;y = 100;width = 90;height = 80;

ellipse(x, y, width, height);

ingredients or variables

directions or algorithm

data type

Page 31: MzTEK Programming - Part 1

int x;int y;int width;int height;

size(200, 200);

x = 150;y = 100;width = 90;height = 80;

ellipse(x, y, width, height);

ingredients or variables

directions or algorithm

data type variable name

Page 32: MzTEK Programming - Part 1

Cooking uses units of weight or volume, computers use bytes.

Page 33: MzTEK Programming - Part 1

Cooking uses units of weight or volume, computers use bytes.

A bit is a 0 or a 1.

0 1

Page 34: MzTEK Programming - Part 1

Cooking uses units of weight or volume, computers use bytes.

A bit is a 0 or a 1.

0 1

A byte is eight 0s or 1s.

Page 35: MzTEK Programming - Part 1

Cooking uses units of weight or volume, computers use bytes.

A bit is a 0 or a 1.

0 1

A byte is eight 0s or 1s.

0s and 1s are used to represent things like numbers.

It’s up to the programming language (and hardware) to determine what kinds of things 0s and 1s can represent.

Page 36: MzTEK Programming - Part 1

PRIMITIVE DATA TYPES (FOR BOTH PROCESSING AND ARDUINO UNLESS NOTED OTHERWISE)

In Arduino, byte is an unsigned number (no negative numbers)can be 0 to 255, 8-bits (2^8)B is put in front to indicate numbers are binarybyte b = B10010;

boolean is true or false boolean running = false;

In Processing, byte is a numbercan be 127 to -128, 8-bits (2^8)byte b = -125;

Arduino Only

Processing Only

Page 37: MzTEK Programming - Part 1

PRIMITIVE DATA TYPES (FOR BOTH PROCESSING AND ARDUINO UNLESS NOTED OTHERWISE)

int is an integer ranging from -32,768 to 32,767int i = 23456;

long is a large integer ranging from -2,147,483,648 to 2,147,483,647long g = 1239382345L;

Page 38: MzTEK Programming - Part 1

PRIMITIVE DATA TYPES (FOR BOTH PROCESSING AND ARDUINO UNLESS NOTED OTHERWISE)

float is a number with a decimal point ranging from -3.40282347E+38 to 3.40282347E+38float f = -234.56;

char is a single letter (or symbol like “.”)char myChar = 'A';

unsigned char is a number 0 to 255, it is better to use the byte data type insteadunsigned char myChar = 240;

Page 39: MzTEK Programming - Part 1

PRIMITIVE DATA TYPES (FOR BOTH PROCESSING AND ARDUINO UNLESS NOTED OTHERWISE)

unsigned int is a number without a decimal point ranging from 0 to 65,535 (2^16) - 1)unsigned int ui = 55424;

unsigned long is a large integer ranging from 0 to 4,294,967,295 (2^32 - 1), so no negative numberslong g = 4239382345ul;

Arduino Only

word is the same as unsigned int is a number without a decimal point ranging from 0 to 65,535 (2^16) - 1)word w = 55424;

Page 40: MzTEK Programming - Part 1

RULES FOR NAMING VARIABLES

Page 41: MzTEK Programming - Part 1

Don’t start with a character besides a letter

RULES FOR NAMING VARIABLES$3%^

Page 42: MzTEK Programming - Part 1

Don’t start with a character besides a letter

RULES FOR NAMING VARIABLES$3%^

abc123 Only use numbers and letters and “_”

Page 43: MzTEK Programming - Part 1

Don’t start with a character besides a letter

RULES FOR NAMING VARIABLES$3%^

abc123 Only use numbers and letters and “_”

if, for Can’t use words that hold meaning to the compiler

Page 44: MzTEK Programming - Part 1

Don’t start with a character besides a letter

RULES FOR NAMING VARIABLES$3%^

abc123 Only use numbers and letters and “_”

if, for Can’t use words that hold meaning to the compiler

rectHeight Be descriptive

Page 45: MzTEK Programming - Part 1

Don’t start with a character besides a letter

RULES FOR NAMING VARIABLES$3%^

abc123 Only use numbers and letters and “_”

if, for Can’t use words that hold meaning to the compiler

a A Case is important

rectHeight Be descriptive

Page 46: MzTEK Programming - Part 1

EXERCISE

In Processing, create a sketch which draws a green ellipse.

Use the functions listed below. Look up these functions in the Processing documentation for more guidance.

size(width, height)fill(value1, value2, value3)ellipse(x, y, width, height)

Page 47: MzTEK Programming - Part 1

DECLARATION VS INITIALISATIONAs in a recipe, ingredients need to be listed at the top so you know what to buy.

In code, it’s so the compiler knows how much memory to reserve. This is called declaring.

Page 48: MzTEK Programming - Part 1

DECLARATION VS INITIALISATIONAs in a recipe, ingredients need to be listed at the top so you know what to buy.

In code, it’s so the compiler knows how much memory to reserve. This is called declaring.

int myInt;

Reserve space for an int

Page 49: MzTEK Programming - Part 1

DECLARATION VS INITIALISATIONAs in a recipe, ingredients need to be listed at the top so you know what to buy.

In code, it’s so the compiler knows how much memory to reserve. This is called declaring.

We don’t have to know what value will be stored in myInt right away.

We can choose a value later. This is called initialising.

int myInt;

Reserve space for an int

Page 50: MzTEK Programming - Part 1

DECLARATION VS INITIALISATIONAs in a recipe, ingredients need to be listed at the top so you know what to buy.

In code, it’s so the compiler knows how much memory to reserve. This is called declaring.

We don’t have to know what value will be stored in myInt right away.

We can choose a value later. This is called initialising.

int myInt;

Reserve space for an int

myInt = 15;

Store the value 15 in the space reserved for myInt

15

Page 51: MzTEK Programming - Part 1

Need to declare before or at the same time as initialisation.

Page 52: MzTEK Programming - Part 1

Need to declare before or at the same time as initialisation.

myInt = 3;int myInt;

Page 53: MzTEK Programming - Part 1

Need to declare before or at the same time as initialisation.

myInt = 3;int myInt;

int myInt; myInt = 3;

Page 54: MzTEK Programming - Part 1

Need to declare before or at the same time as initialisation.

myInt = 3;int myInt;

int myInt; myInt = 3;

Can’t use a variable before it is initialised.

Page 55: MzTEK Programming - Part 1

Need to declare before or at the same time as initialisation.

myInt = 3;int myInt;

int myInt; myInt = 3;

int myInt;myInt = myInt + 7;

Can’t use a variable before it is initialised.

Page 56: MzTEK Programming - Part 1

Need to declare before or at the same time as initialisation.

myInt = 3;int myInt;

int myInt; myInt = 3;

int myInt = 3;myInt = myInt + 7;

int myInt;myInt = myInt + 7;

Can’t use a variable before it is initialised.

Page 57: MzTEK Programming - Part 1

TO CREATE A VARIABLE1. Decide what the data type should be. Usually the main

decisions are between int, float and char.

floatintchar

Page 58: MzTEK Programming - Part 1

TO CREATE A VARIABLE1. Decide what the data type should be. Usually the main

decisions are between int, float and char.

floatintchar

to create a float type

Page 59: MzTEK Programming - Part 1

TO CREATE A VARIABLE1. Decide what the data type should be. Usually the main

decisions are between int, float and char.

floatintchar

to create a float type

to create an int type

Page 60: MzTEK Programming - Part 1

TO CREATE A VARIABLE1. Decide what the data type should be. Usually the main

decisions are between int, float and char.

floatintchar

to create a float type

to create an int type

to create a char type

Page 61: MzTEK Programming - Part 1

TO CREATE A VARIABLE1. Decide what the data type should be. Usually the main

decisions are between int, float and char.

2. Decide on a name for your variable. Remember the rules.

floatintchar

to create a float type

to create an int type

to create a char type

Page 62: MzTEK Programming - Part 1

TO CREATE A VARIABLE1. Decide what the data type should be. Usually the main

decisions are between int, float and char.

2. Decide on a name for your variable. Remember the rules.

floatintchar

to create a float type

to create an int type

to create a char type

float scale;int redValue;char finalMark;

Page 63: MzTEK Programming - Part 1

TO CREATE A VARIABLE1. Decide what the data type should be. Usually the main

decisions are between int, float and char.

2. Decide on a name for your variable. Remember the rules.

floatintchar

to create a float type

to create an int type

to create a char type

float scale;int redValue;char finalMark;

you get to choose the name

Page 64: MzTEK Programming - Part 1

TO CREATE A VARIABLE1. Decide what the data type should be. Usually the main

decisions are between int, float and char.

2. Decide on a name for your variable. Remember the rules.

3. If you already know what the value of that variable is, then go ahead and set the value.

floatintchar

to create a float type

to create an int type

to create a char type

float scale;int redValue;char finalMark;

you get to choose the name

Page 65: MzTEK Programming - Part 1

TO CREATE A VARIABLE1. Decide what the data type should be. Usually the main

decisions are between int, float and char.

2. Decide on a name for your variable. Remember the rules.

3. If you already know what the value of that variable is, then go ahead and set the value.

floatintchar

to create a float type

to create an int type

to create a char type

float scale;int redValue;char finalMark;

float scale = 0.5;int redValue = 199;char finalMark = ‘B’;

you get to choose the name

Page 66: MzTEK Programming - Part 1

TO CREATE A VARIABLE1. Decide what the data type should be. Usually the main

decisions are between int, float and char.

2. Decide on a name for your variable. Remember the rules.

3. If you already know what the value of that variable is, then go ahead and set the value.

floatintchar

to create a float type

to create an int type

to create a char type

float scale;int redValue;char finalMark;

float scale = 0.5;int redValue = 199;char finalMark = ‘B’;

if you don’t know the value yet, stop at step 2. but remember to end

each line with a ;

you get to choose the name

Page 67: MzTEK Programming - Part 1

EXERCISE

In Processing, return to your sketch which draws a green ellipse.

Replace the numbers you’ve used as arguments in the fill( ) and ellipse( ) functions with variables.

What kind of datatypes do you need to use? What are good descriptive names for those variables?