lesson 9: simon - reading the sequences€¦ · lesson 9: simon - reading the sequences page 2 -...

13
Page 1 - Lesson 9: Simon - Reading the sequences Introduction: Now you have got your buttons working, it is time to look at the sequence of button presses. This time, you will get the Arduino to generates a button image, and you copy it, then the Arduino checks to see if you have copied it correctly. Goals • Write a function to make a sequence • Add to the void loop to check the sequence • Write a function to reset the sequence Activity Checklist Save your project Save Lesson 9: Simon - Reading the sequences Test your Project Type code Type this code

Upload: others

Post on 14-Jul-2020

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lesson 9: Simon - Reading the sequences€¦ · Lesson 9: Simon - Reading the sequences Page 2 - Lesson 9: Simon - Reading the sequences Activity Checklist 1. Open your Simon game

Page 1 - Lesson 9: Simon - Reading the sequences

Introduction:

Now you have got your buttons working, it is time to look at the sequence

of button presses. This time, you will get the Arduino to generates a button

image, and you copy it, then the Arduino checks to see if you have copied it

correctly.

Goals

• Write a function to make a sequence

• Add to the void loop to check the sequence

• Write a function to reset the sequence

Activity Checklist

Save your project Save

Lesson 9: Simon - Reading the sequences

Test your Project

Type code Type this code

Page 2: Lesson 9: Simon - Reading the sequences€¦ · Lesson 9: Simon - Reading the sequences Page 2 - Lesson 9: Simon - Reading the sequences Activity Checklist 1. Open your Simon game

Lesson 9: Simon - Reading the sequences

Page 2 - Lesson 9: Simon - Reading the sequences

Activity Checklist

1. Open your Simon game file

Click then CodeClub > SimonYourName

You are now going to add to last sessions text and get the DIY Gamer to display an arrow, wait for your button press and then return whether it was correct or not.

(codeclub > Simon02 will also be at the same state of development if you need a catch up sketch)

2. Write code

At the top of your sketch, after initialising the gamer library, write the following. We are creating numbers to store our sequence information.

Gamer gamer;

byte sequence[50];int sequenceLength = 0;int delayTime = 300;

// Include your arrays here! // These are our arrow imagesbyte framesSimon[4][8] = { { // up B00000000, B00011000,

Type code

Keep track of your progress by ticking off the boxes below:

Write a function to make a sequenceStep 1:

Page 3: Lesson 9: Simon - Reading the sequences€¦ · Lesson 9: Simon - Reading the sequences Page 2 - Lesson 9: Simon - Reading the sequences Activity Checklist 1. Open your Simon game

Activity Checklist

3. Code Comprehension!

Let’s break this code down and see how it works:

byte sequence[50];This creates a chunk of information (byte) called sequence that can hold up to 50 items in a list.

int sequenceLength = 0;This creates a number (integer) called sequenceLength that will remember how many items have been held in the sequence. We are starting it off as zero.

int delayTime = 300;This creates a number (integer) called delayTime that will remember how many milliseconds to wait between displaying each sequence arrow. We are starting it off as 300. We will play with this later to make the game MORE FUN.

Lesson 9: Simon - Reading the sequences

Keep track of your progress by ticking off the boxes below:

Write a function to make a sequenceStep 1:

Page 3 - Lesson 9: Simon - Reading the sequences

4. Write code

At the very bottom of your sketch, below the last curley bracket, write the following code to create a function to display an arrow and store it into a sequence.

return key;}

void addToSequence() { sequence[0] = random(0,4); gamer.clear(); delay(delayTime); gamer.printImage(framesSimon[sequence[0]]); delay(delayTime);}

Type code

Page 4: Lesson 9: Simon - Reading the sequences€¦ · Lesson 9: Simon - Reading the sequences Page 2 - Lesson 9: Simon - Reading the sequences Activity Checklist 1. Open your Simon game

Activity Checklist

5. Code Comprehension!

Let’s break this code down and see how it works:

void addToSequence() {The name of our function, it starts at the first curley bracket.

sequence[0] = random(0,4);Set the sequence to a random number between 0 and 4 (1, 2 or 3).

gamer.clear();Clear the DIY Gamer screen.

delay(delayTime);Wait for the amount of time held in the variable called “delayTime”.

gamer.printImage(framesSimon[sequence[0]]);Show the new direction.

delay(delayTime);}This curley bracket defines the end of the function called addToSequence.

Lesson 9: Simon - Reading the sequences

Keep track of your progress by ticking off the boxes below:

Write a function to make a sequenceStep 1:

Page 4 - Lesson 9: Simon - Reading the sequences

6. Edit the void loop function

At the top of the void loop function, you are going to add the comand line to run the addToSequence function you have just written.

void loop() { addToSequence(); int key = waitForButtonPress(); gamer.clear(); delay(100);

Type code

Page 5: Lesson 9: Simon - Reading the sequences€¦ · Lesson 9: Simon - Reading the sequences Page 2 - Lesson 9: Simon - Reading the sequences Activity Checklist 1. Open your Simon game

Lesson 9: Simon - Reading the sequences

Keep track of your progress by ticking off the boxes below:

Write a function to make a sequenceStep 1:

Make and check a sequenceStep 2:

Page 5 - Lesson 9: Simon - Reading the sequences

Click

This will compile your code. Is everything happy with your code?

If not, go back and check for missing capitals, wrong brackets etc.

Test your project

7. Test your code

1. Find the void loop function

It should look like this:

void loop() { addToSequence(); int key = waitForButtonPress(); gamer.clear(); delay(100); gamer.printImage(framesSimon[key]);}

Type code

Activity Checklist

Page 6: Lesson 9: Simon - Reading the sequences€¦ · Lesson 9: Simon - Reading the sequences Page 2 - Lesson 9: Simon - Reading the sequences Activity Checklist 1. Open your Simon game

Lesson 9: Simon - Reading the sequences

Keep track of your progress by ticking off the boxes below:

Step 1: Make and check a sequenceStep 2:

Page 6 - Lesson 9: Simon - Reading the sequences

2. Edit the void loop function

You are now going to add some extra code to check if the arrow chosen randomly by the gamer is the same as the button that is pressed afterwards.

void loop() { addToSequence(); gamer.clear(); delay(delayTime); boolean success = true; int key = waitForButtonPress(); gamer.clear(); delay(100); gamer.printImage(framesSimon[key]); if(key != sequence[0]) { success = false; }

delay(delayTime); if(success) { gamer.printImage(right); } else { gamer.printImage(wrong); delay(1000); } delay(500);}

Type code

Activity Checklist

Page 7: Lesson 9: Simon - Reading the sequences€¦ · Lesson 9: Simon - Reading the sequences Page 2 - Lesson 9: Simon - Reading the sequences Activity Checklist 1. Open your Simon game

Activity Checklist

3. Code Comprehension!

This is starting to get tricky, get your powers of concentration sharpened.

addToSequence();Run the function addToSequence (display an arrow on screen). gamer.clear();Clear the screen delay(delayTime);Wait for the number of milliseconds held in the variable called delayTime boolean success = true;Our boolean is called success and we are setting it to true. (A boolean can be either, true or false.)

int key = waitForButtonPress();Create a variable called ‘key’ and sets it to the value returned by the function WaitForButtonPressed. gamer.clear();Clear the screen. delay(100);Wait for 100 milliseconds ( tenth of a second)so the next arrow is seperate. gamer.printImage(framesSimon[key]);Display the array image with the name held by the variable “key” (which is last key pressed). if(key != sequence[0]) {If the key pressed is not equal to the sequence value. success = false;Then you have failed, your success is false!

}End of the if statement. delay(delayTime);Wait for the number of milliseconds held in the variable called delayTime

Page 7 - Lesson 9: Simon - Reading the sequences

Lesson 9: Simon - Reading the sequences

Keep track of your progress by ticking off the boxes below:

Step 1: Make and check a sequenceStep 2:

Page 8: Lesson 9: Simon - Reading the sequences€¦ · Lesson 9: Simon - Reading the sequences Page 2 - Lesson 9: Simon - Reading the sequences Activity Checklist 1. Open your Simon game

Activity Checklist

3. Code Comprehension continued!

This is starting to get tricky, get your powers of concentration sharpened.

if(success) {If success is still true (the right button was pressed) then... gamer.printImage(right);Display the image called right. } else {Otherwise.. gamer.printImage(wrong);Display the image called wrong. delay(1000);Wait for 1000 milliseconds (one second). }This curley bracket marks the end of the if/else statement. delay(500);Wait for 500 milliseconds (half a second).

}This curley bracket marks the end of the void loop function. PHEW...

Page 8 - Lesson 9: Simon - Reading the sequences

Lesson 9: Simon - Reading the sequences

Keep track of your progress by ticking off the boxes below:

Step 1: Make and check a sequenceStep 2:

Page 9: Lesson 9: Simon - Reading the sequences€¦ · Lesson 9: Simon - Reading the sequences Page 2 - Lesson 9: Simon - Reading the sequences Activity Checklist 1. Open your Simon game

Click

This will compile your code. Is everything happy with your code?

Test your project

4. Test your code

5. Upload and Test

Click to transfer the code onto the Arduino in the DIY Gamer.

You should now have the DIY Gamer displaying an image, waiting for you to press a button and then telling you if you got it right or wrong.Awesome!

6. Save your sketch

Click to save your sketch.

Save your project Save

Well done!

You have used your growing knowledge of Arduino vocabulary to successfully get the DIY Gamer to check if you have matched a sequence. Amazing.

Lesson 9: Simon - Reading the sequences

Keep track of your progress by ticking off the boxes below:

Step 1: Make and check a sequenceStep 2:

Page 9 - Lesson 9: Simon - Reading the sequences

Page 10: Lesson 9: Simon - Reading the sequences€¦ · Lesson 9: Simon - Reading the sequences Page 2 - Lesson 9: Simon - Reading the sequences Activity Checklist 1. Open your Simon game

Annotated sketch code

Your code should now look like this...

/* * SIMON SAYS! (images) * A TWSU Gamer game by YOU!

*/

#include <Gamer.h> // Include the Gamer library

Gamer gamer;byte sequence[50];int sequenceLength = 0;int delayTime = 300;

// These are our arrow imagesbyte framesSimon[4][8] = { { // up B00000000, B00011000, B00111100, B01111110, B00011000, B00011000, B00011000, B00000000 }, { // down B00000000, B00011000, B00011000, B00011000, B01111110, B00111100, B00011000, B00000000 },

Page 10 - Lesson 9: Simon - Reading the sequences

Lesson 9: Simon - Reading the sequences

Page 11: Lesson 9: Simon - Reading the sequences€¦ · Lesson 9: Simon - Reading the sequences Page 2 - Lesson 9: Simon - Reading the sequences Activity Checklist 1. Open your Simon game

Your code should now look like this...

{ // left B00000000, B00010000, B00110000, B01111110, B01111110, B00110000, B00010000, B00000000 }, { // right B00000000, B00001000, B00001100, B01111110, B01111110, B00001100, B00001000, B00000000 }};

// This is our “GO!” imagebyte go[8] = { B00000000, B01101110, B10001010, B10001010, B10001010, B10101010, B01101110, B00100000};

// This is our tick imagebyte right[8] = { B00000001, B00000011, B00000111, B00001110, B11011100, B11111000, B01110000, B00100000};

Page 11 - Lesson 9: Simon - Reading the sequences

Lesson 9: Simon - Reading the sequences

Page 12: Lesson 9: Simon - Reading the sequences€¦ · Lesson 9: Simon - Reading the sequences Page 2 - Lesson 9: Simon - Reading the sequences Activity Checklist 1. Open your Simon game

Your code should now look like this...

// This is our cross imagebyte wrong[8] = { B11000011, B01100110, B00111100, B00011000, B00011000, B00111100, B01100110, B11000011};

void setup() { gamer.begin(); // Fire up the Gamer!}

void loop() { addToSequence(); gamer.clear(); delay(delayTime); boolean success = true; int key = waitForButtonPress(); gamer.clear(); delay(100); gamer.printImage(framesSimon[key]); if(key != sequence[0]) { success = false; }

delay(delayTime);

if(success) { gamer.printImage(right); } else { gamer.printImage(wrong); delay(1000); } delay(500); }

int waitForButtonPress() { int key = 4; //let’s wait until another key has been pressed

Page 12 - Lesson 9: Simon - Reading the sequences

Lesson 9: Simon - Reading the sequences

Page 13: Lesson 9: Simon - Reading the sequences€¦ · Lesson 9: Simon - Reading the sequences Page 2 - Lesson 9: Simon - Reading the sequences Activity Checklist 1. Open your Simon game

Your code should now look like this...

while(key==4) { if(gamer.isPressed(UP)) key=0; if(gamer.isPressed(DOWN)) key=1; if(gamer.isPressed(LEFT)) key=2; if(gamer.isPressed(RIGHT)) key=3; } return key;}

void addToSequence() { sequence[0] = random(0,4); gamer.clear(); delay(delayTime); gamer.printImage(framesSimon[sequence[0]]); delay(delayTime);}

Page 13 - Lesson 9: Simon - Reading the sequences

Lesson 9: Simon - Reading the sequences