ProtoSnaps and Processing Serial Interfacing and Sensor Calibration Materials by Lindsay Craig and Ben Leduc-Mills.

Slides:



Advertisements
Similar presentations
JQuery MessageBoard. Lets use jQuery and AJAX in combination with a database to update and retrieve information without refreshing the page. Here we will.
Advertisements

Introduction to Macromedia Director 8.5 – Lingo
Summer Computing Workshop. Introduction to Variables Variables are used in every aspect of programming. They are used to store data the programmer needs.
EMS1EP Lecture 4 Intro to Programming Dr. Robert Ross.
Computer Basics Hit List of Items to Talk About ● What and when to use left, right, middle, double and triple click? What and when to use left, right,
Processing the Danger Shield Everything you wanted to know about Processing but were afraid to ask By Ben Leduc-Mills.
Jason Howard. Agenda I. How to download robotc II. What is tele-op used for? III. How to build a basic tele-op program IV. Getting the robot to drive.
Lab7: Introduction to Arduino
Mini-SumoBot Construction and Programming
 Variables  What are they?  Declaring and initializing variables  Common uses for variables  Variables you get “for free” in Processing ▪ Aka: Built-in.
Variables Conditionals Loops The concept of Iteration Two types of loops: While For When do we use them? Iteration in the context of computer graphics.
Tips for New Mac Users 20 simple tips to make using a Mac quicker and easier.
Vex Robotics Introduction to Sensors. introduction to sensors Sensors assist robots in seeing and feeling the physical world through which they travel.
Programing Concept Ken Youssefi/Ping HsuIntroduction to Engineering – E10 1 ENGR 10 Introduction to Engineering (Part A)
Week 2: Primitive Data Types 1.  Programming in Java  Everything goes inside a class  The main() method is the starting point for executing instructions.
IAT 800 Lab 1: Loops, Animation, and Simple User Interaction.
1 Introduction to Coding. 2 Example Codes A lot of example codes are given with Arduino IDE A code can often be based on a previous example rather than.
Programming – Touch Sensors Intro to Robotics. The Limit Switch When designing robotic arms there is always the chance the arm will move too far up or.
Fundamentals of Programming in Visual Basic 3.1 Visual basic Objects Visual Basic programs display a Windows style screen (called a form) with boxes into.
Mr. Wortzman. Tabs (Block Categories) Available Blocks Script Area Sprite Stage All sprites in this project.
Games and Simulations O-O Programming in Java The Walker School
Working with Arduino: Lesson #1: Getting Acquainted with the Kit EGN1007.
What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework.
1 - buttons Click “Step Forward” to execute one line of the program. Click “Reset” to start over. “Play,” “Stop,” and “Step Back” are disabled in this.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
Working with Numbers in Alice - Converting to integers and to strings - Rounding numbers. - Truncating Numbers Samantha Huerta under the direction of Professor.
by Chris Brown under Prof. Susan Rodger Duke University June 2012
Week 2 - Monday.  What did we talk about last time?  Software development  Lab 1.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Iteration. Adding CDs to Vic Stack In many of the programs you write, you would like to have a CD on the stack before the program runs. To do this, you.
ProtoSnap Introduction to Arduino Casey Haskell, Pete Lewis, David Stillman, Jim Lindblom, Pete Dokter, Lindsay Levkoff, Trevor Zylstra.
Introduction to Arrays. definitions and things to consider… This presentation is designed to give a simple demonstration of array and object visualizations.
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education.
ATLAS 2013 Super Fast Intro to Arduino and Processing Materials by Lindsay Craig, David Stillman and Ben Leduc-Mills.
Visit to download CodeAwesomeness.
Tweaking Your Simon Adding a photoresistor and changing code Instruction by Pete Lewis and Linz Craig.
L ILY P AD T RAINING C ENTENNIAL E LEMENTARY 2012 Material by Linz Craig Revision by Sarah Bloms Additional images by Modkit & Adam Meyer.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
Downloading and Installing Autodesk Inventor Professional 2015 This is a 4 step process 1.Register with the Autodesk Student Community 2.Downloading the.
Making Python Pretty!. How to Use This Presentation… Download a copy of this presentation to your ‘Computing’ folder. Follow the code examples, and put.
Game Maker – Getting Started What is Game Maker?.
Code The Arduino Environment.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
MediaLive 2012 Danger Shield, Arduino and Processing Materials by Lindsay Craig, David Stillman and Ben Leduc-Mills.
C++ LANGUAGE TUTORIAL LESSON 1 –WRITING YOUR FIRST PROGRAM.
Tutorial for Arrays and Lists. Description This presentation will cover the basics of using Arrays and Lists in an Alice world It uses a set of chickens.
Lesson 2: Reading a program. Remember: from yesterday We learned about… Precise language is needed to program Actors and Classes Methods – step by step.
The desktop (overview) Working with desktop icons The desktop is the main screen area that you see after you turn on your computer and log on to Windows.
Who Are You, Arduino? Er. Sahil Khanna
Introduction to Computer Programming - Project 2 Intro to Digital Technology.
1 Project 2: Using Variables and Expressions. 222 Project 2 Overview For this project you will work with three programs Circle Paint Ideal_Weight What.
Introduction to Programming the Arduino Dr Gaw 3/21/14.
Creating a Simple Game in Scratch Barb Ericson Georgia Tech May 2009.
Game Maker Tutorials Introduction Clickball IntroductionClickball Where is it? Shooting Where is it?Shooting.
Digital Literacy Introduction to Computers Unit 1, Lesson 1.
Introduction to Programming in RobotC
Scratch for Interactivity
MediaLive 2012 Danger Shield, Arduino and Processing
Arduino Part 1 Topics: Microcontrollers Programming Basics
Computation as an Expressive Medium
Automation and Robotics
1 Code
File Handling Programming Guides.
Conditions and Ifs BIS1523 – Lecture 8.
Working with Arduino: Lesson #1: Getting Acquainted with the Kit
Welcome to Digital Electronics using the Arduino Board
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Creating a Simple Game in Scratch
Presentation transcript:

ProtoSnaps and Processing Serial Interfacing and Sensor Calibration Materials by Lindsay Craig and Ben Leduc-Mills

ProtoSnap and Processing Class Overview What is Processing? Installation and Intro Serial Comm from Arduino to Processing Calibration of Sensors Sensor Variables Serial Comm from Processing to Arduino Controlling and Using Physical Aspects of ProtoSnap Interface Controlling Serial Comm and Handling Specific Events

Processing? Processing is a free, open source, cross- platform programming language and environment for people who want to create images, animations, and interactions. Created in 2001 by Casey Reas and Ben Fry at the MIT Media Lab. Downloads, updates, reference, forums, etc. at:

Installation For Linux: Download the.tar.gz file to your home directory, then open a terminal window and type: Tar xvfz processing-xxxx.tgz (replace xxxx with the rest of the file’s name, which is the version number) This will create a folder named processing-1.5 or something similar. Then change to that directory: cd processing-xxxx and run processing:./processing For Mac: Double-click the.dmg file and drag the Processing icon from inside this file to your applications folder, or any other location on your computer. Double click the Processing icon to start Processing. For Windows: Double-click the.zip file and drag the folder inside labeled Processing to a location on your hard drive. Double click the Processing icon to start Processing. If you are stuck go to for help.

Anatomy of a sketch A sketch is a file or project you create in Processing. When you first open up a new sketch it will be completely blank.

Setup() This function runs once, at the very beginning of your sketch. You will use setup to set up certain aspects of your sketch, makes sense right? This is a good place to put your sensor calibration code. (More on sensor calibration later.) Most importantly for this class you will begin Serial communication in the setup function. The setup function without anything in it looks like this:

Draw() This function is where everything happens in your sketch. The draw loop is the portion of code that keeps repeating while the Processing sketch is open. Any animation, interaction or changes to the images or variables in your sketch will need to be programmed inside of this loop. The draw loop looks like this:

How to Make Basic Graphics Basics here

Reference One very convenient way to access Processing’s Help Reference is to highlight a function or a word used in your code, right click and select Find in Reference (which brings you to the processing.org reference page):

Hooking into Processing We can send values from the ProtoSnap to Processing through an Arduino, and use these values to control things in Processing First step: Open a new Arduino sketch Like Processing, we have a setup() function In that function, we need to open Serial communication: Serial.begin(9600);

Hooking into Processing We now have to set up all pins to the right pinMode, in our setup function as well as pulling any internal pull up resistors HIGH. We’ll get to the establishContact() function in a bit, but don’t forget to put it in now.

Hooking into Processing In our loop() function, we need to read from all the sensors and send the values out the serial port by calling Serial.print We used a delimiter to separate the sensor values so that the sensor values will be easier to distinguish when we get them into Processing.

Hooking into Processing Here are the rest of the loop() statements. Don’t forget the ln in the last Serial.println(digitalRead (7)); statement. There is more than this in the code on your ProtoSnap, we’ll get into that later. For now these are the basics you need to send Serial from Arduino to Processing.

Receiving Data in Processing import processing.serial.*; Serial usbPort; int [ ] sensors = null; boolean firstContact = false; void setup() { usbPort = new Serial (this, Serial.list( ) [0], 9600); usbPort.bufferUntil (‘\n’); } Now that we’re sending data from the ProtoSnap, we need a way to receive it in Processing. Luckily, there’s a library for that. First, import the serial library. We’ll also need a Serial object to define which serial port we’ll be using, as well as an integer array for our sensor data. We also have a boolean variable to keep track of whether we’ve heard from Arduino or not. In your setup function, you need to initialize your Serial object, passing it the parent object (don’t worry about this) which port you want to use, and the baud rate. Make sure you pick the same baud rate that you defined in the Arduino sketch. The bufferUntil function just stores our incoming data in a buffer until we’re ready to do something with it.

Receiving Data in Processing Our next step is to define a SerialEvent function – this function automatically gets called every time the character in our bufferUntil() statement is read from the serial port. We then read a chunk of data into a String, trim whitespace, and split it using our delimiter character (told you it would be useful!) into our sensors[ ] integer array. This puts each sensor value into its own addressable place in the array. There is a println that should be printing out the sensors values it sees – try running the sketch to make sure you’re getting values.

There’s a lot going on here, so don’t worry if it doesn’t make sense at first. Basically, we check for the Serial Communication from Arduino. Once it reads a carriage return it checks to make sure the data string is not empty. If it’s got something, we split the string up by our delimiter character into an array. This lets us put each sensor value into its own variable. If you don’t put the array values into their own variables you will get a null pointer exception when you try to use them in your code. Receiving Data in Processing

Waiting for Serial Communication Establishment One of the first things you will have to do is create a conditional that keeps your game from starting while Processing initiates Serial Communication. To do this use the firstContact variable as a condition for changing the screen state. Only when you have made contact with the Arduino will your game move to the next section of code.

Using Received Data in the Processing Sketch: The Button Next let’s keep Processing querying another conditional that waits for the user to press the button on the ProtoSnap. To do this we will need to check a variable (gameOn boolean variable) that tells us whether we have started playing the game yet. While that variable is false (or an integer that indicates we have not started playing the game) Processing will be stuck in a while loop that looks for a button press. When the button is pressed Processing takes a sensor calibration reading and changes the gameOn variable to true. This allows us to begin playing the game.

Using Received Data in the Processing Sketch: The Photoresistor In the first iteration of my game I simply mapped the light sensor value to the height of my screen. But my character never got to the top of the screen and it made my character jump all over the place due to the speed at which the light sensor values change. Before we fix this let’s look at how we use the light sensor to control the Y position of the character. Basically the closer our hand is to the light sensor the lower the value is. This means that we mapped the lowest possible value (0) to the bottom of the screen and the highest possible value (1023) to the top of the screen. But this assumes that we have enough light to actually get that value of 1023!

Using Received Data in the Processing Sketch: The Photoresistor So first I changed the map values so that the character was more in sync with the light values of the room in which I was programming. The room was a little dim so I used an upper light value of 150. So I wrote this: Instead of this:

Using Received Data in the Processing Sketch: The Photoresistor Or to fix the issue of sensor calibration we can get a value from the light sensor at the beginning of the game that we compare to the current light sensor variable. This way we can compensate for a brighter or darker environment. Remember this in the menu function? upLightVal is our calibration variable. And then we can use upLightVal in our mapping code in the draw loop. But it still jumps around a lot, huh?

Using Received Data in the Processing Sketch: The Photoresistor We can use the light sensor to move the character instead of using it to decide the position of the character. This is a subtle but important difference. This code says two things: 1. If the light sensor is between zero and fifteen (and the character is not at the bottom of the screen) move the character down by five pixels. 2. If the light sensor is higher than five below the initial sensor calibration value (and the character is not at the top of the screen) move the character up by five pixels.

Using Received Data in the Processing Sketch: The Photoresistor This works pretty well, but sometimes we get a bad initial calibration value and then the gamer is destined to hang out either at the top or the bottom of the screen. Since we only have two inputs to work with we need to figure out a way to use the other input (button) to trigger taking a new calibration reading. In order to allow normal usage of the button during game play and sensor recalibration we will look for three rapid pushes of the button, something that would rarely happen during normal play.

Using Received Data in the Processing Sketch: Programming the Game From this point out it’s a question of game design, programming and using the variables you are receiving from Arduino in the proper manner. With more inputs this gets more exciting, but two simple inputs (one Analog and one Digital) is a great way to learn this stuff. Any questions about the game code? Next up we will learn how to send communication the other way, from Processing to the Arduino.

Sending Data from Processing to Arduino: The Processing Code Good news! Since you have already imported the Processing Serial library sending Serial Communication from Processing to the Arduino is pretty simple. There are two pieces you need, very similar to the Arduino to Processing communication, but in reverse. To send a Serial message from Processing all you have to do is find the place in your Processing sketch where you want to send a message and type the following line: Serial.write (variable) ; Where variable is the value you want to send to your Arduino. The next 4 slides show where the game uses the Serial.write(); function.

Sending Data from Processing to Arduino: Low Health This portion of the code indicates that the character is low on life. The character ‘D’ is sent to the Arduino each time draw executes if the character’s life is below twenty five.

Sending Data from Processing to Arduino: Hit Test This is the hit test code. There is a boolean variable in it so it will only send Serial Communication or decrement the health meter when the character is first hit with an obstacle and not the entire time the character and the obstacle are touching.

Sending Data from Processing to Arduino: Shield On This portion of the code indicates that the user is pressing the ProtoSnap button and the shield has been activated. The variable shieldSwitch is used to limit the amount of Serial Data sent to the Arduino so there is only a signal when the shield is first activated.

Sending Data from Processing to Arduino: Shield Out This portion of the code indicates that the user is pressing the ProtoSnap button and has tried to activate the shield but there is no shield left to use. The character ‘N’ is sent every time, there is no limiting variable.

Receiving Data from Arduino to Processing: The Arduino Code Declaring a Buffer Variable and Assigning it a Value Receiving information is usually the most difficult aspect of Serial Communication. First we need to declare a Serial buffer variable to hold the Serial Communication, do this before your setup or loop functions. For my game I am only sending one char at a time so I declare a char variable and fill it with a zero like this. char Signal = ‘0’;

Receiving Data from Arduino to Processing: The Arduino Code Declaring a Buffer Variable and Assigning it a Value Then I use the following lines inside the loop function to fill that variable with the char that Arduino reads over the Serial line. if (Serial.available () > 0) { signal = Serial.read (); } If you want to send more than one value at a time you will need to create a char array instead of a signal char variable and use a for loop to check each value in the array. You will also need to make sure that Processing sends a full array each time it sends Serial Communication or have Arduino clear the array each loop cycle so you don’t wind up with outdated data in your Serial buffer. In this case you will need to change the if loop to a while loop because otherwise Arduino won’t catch the entirety of the communication.

Using the Serial Variable: The Arduino Code Now that we have data in our signal variable we can use it to control aspects of the ProtoSnap. You can do this in a bunch of different ways. We will cover two different ways to do this. One of them is a switch case and the other is an if statement.

Using the Serial Variable: The Arduino Code Switch Case Remember to put single quotes around each variable as you create each case statement.

Using the Serial Variable: The Arduino Code If Statement One cool thing about using if statements is that you can check if to see if the signal variable is NOT equal to a certain value.

Limiting Communication An important thing to note is that often you will only want to send Serial Communication at the beginning or end of an event. Such as with the hit test. You may want to have something happen on the Arduino when your character is first hit with a bad guy, but the hit test will continue to trigger Serial Communication after the first contact. You can limit this on either side of the communication. We already saw how you would limit that in Processing, here is how you might limit that on the Arduino side. Declare a boolean variableAnd assign the variable

Handling Different Types of Input from the Same Sensor Put 3 presses of button here, combination for hue in menu select

Other Ways to Handle Sensor Data Weighted variables here Running average

Other Ways to Handle Sensor Data Various ways to weight variables here as well Running average with recent weighted

Using Physical Aspects of the ProtoSnap Interface to Affect the Sensors The photoresistor is right next to the RGB LED. It is possible to use the LED to affect the photoresistor sensor depending on what is happening in the game. You may have noticed that when you use the shield the RGB LED lights up causing the character to move up a little. Useful if you want the character to move a little erratically when it has a low health. Make sure the red LED doesn’tInstead of just turning the LED on send it turn off if character has low healtha random value, remember to put this code outside of your Serial.available(); if statement because we want the value to change each loop, not each time Arduino receives data.

Sensors Dave pointed out that back in the day people were able to use a single button to send morse code. There are many different ways to stretch the interface capabilities of your sensors. Think about creating handlers for different distinct sensor values and combinations. What kind of special moves or easter eggs will the user discover if they mess around with the sensors long enough?

Questions?

Longbow Drive, Suite 200 Boulder, Colorado 80301