Robotics and Electronics Unit 5

Similar documents
Note. The above image and many others are courtesy of - this is a wonderful resource for designing circuits.

Robotics/Electronics Review for the Final Exam

Lesson 8: Digital Input, If Else

University of Hull Department of Computer Science C4DI Interfacing with Arduinos

Procedure: Determine the polarity of the LED. Use the following image to help:

Halloween Pumpkinusing. Wednesday, October 17, 12

Coding Workshop. Learning to Program with an Arduino. Lecture Notes. Programming Introduction Values Assignment Arithmetic.

Microcontrollers and Interfacing week 8 exercises

ArdOS The Arduino Operating System Quick Start Guide and Examples

Arduino - DigitalReadSerial

ENGR 40M Project 3c: Switch debouncing

Imperativ Programming. morning (again) Jens Dalsgaard Nielsen Aalborg University (1/24)

1/Build a Mintronics: MintDuino

Adapted from a lab originally written by Simon Hastings and Bill Ashmanskas

IME-100 Interdisciplinary Design and Manufacturing

Specification. 1.Power Supply direct from Microcontroller Board. 2.The circuit can be used with Microcontroller Board such as Arduino UNO R3.

Arduino 101 AN INTRODUCTION TO ARDUINO BY WOMEN IN ENGINEERING FT T I NA A ND AW E S O ME ME NTO R S

Smart Objects. SAPIENZA Università di Roma, M.Sc. in Product Design Fabio Patrizi

Digital Pins and Constants

Serial.begin ( ); Serial.println( ); analogread ( ); map ( );

Arduino Workshop. Overview. What is an Arduino? Why Arduino? Setting up your Arduino Environment. Get an Arduino based board and usb cable

Lab 01 Arduino 程式設計實驗. Essential Arduino Programming and Digital Signal Process

Button Input: On/off state change

Arduino Programming Part 4: Flow Control

University of Portland EE 271 Electrical Circuits Laboratory. Experiment: Arduino

Electronic Brick Starter Kit

Counter & LED (LED Blink)

The short program to blink the red LED used the delay() function. This is very handy function for introducing a short time delays into programs.

More Arduino Programming

D - Tic Tac Toe. Let's use our 9 sparkles to build a tic tac toe game! 2017 courses.techcamp.org.uk/ Page 1 of 9

Arduino Part 2. Introductory Medical Device Prototyping

Arduino Programming. Arduino UNO & Innoesys Educational Shield

Arduino 05: Digital I/O. Jeffrey A. Meunier University of Connecticut

Physics 364, Fall 2012, Lab #9 (Introduction to microprocessor programming with the Arduino) Lab for Monday, November 5

IME-100 ECE. Lab 3. Electrical and Computer Engineering Department Kettering University. G. Tewolde, IME100-ECE,

GOOD MORNING SUNSHINE

Introduction to Microcontrollers Using Arduino. PhilRobotics

KNOCK LOCK MAKE YOUR OWN SECRET LOCKING MECHANISM TO KEEP UNWANTED GUESTS OUT OF YOUR SPACE! Discover: input with a piezo, writing your own functions

Arduino Prof. Dr. Magdy M. Abdelhameed

StenBOT Robot Kit. Stensat Group LLC, Copyright 2018

2SKILL. Variables Lesson 6. Remembering numbers (and other stuff)...

Schedule. Sanford Bernhardt, Sangster, Kumfer, Michalaka. 3:10-5:00 Workshop: Build a speedometer 5:15-7:30 Dinner and Symposium: Group 2

PDF of this portion of workshop notes:

MAE106 Laboratory Exercises Lab # 1 - Laboratory tools

Arduino Programming and Interfacing

Rotary Encoder Basics

Digital Design through. Arduino

MEDIS Module 2. Microcontroller based systems for controlling industrial processes. Chapter 4: Timer and interrupts. M. Seyfarth, Version 0.

CS12020 for CGVG. Practical 1. Jim Finnis

Assembly Instructions (8/14/2014) Your kit should contain the following items. If you find a part missing, please contact NeoLoch for a replacement.

Introduction to Internet of Things Prof. Sudip Misra Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Blinking an LED 1 PARTS: Circuit 2 LED. Wire. 330Ω Resistor

Part 1. Summary of For Loops and While Loops

Introduction to Arduino Diagrams & Code Brown County Library

ECGR 4101/5101, Fall 2016: Lab 1 First Embedded Systems Project Learning Objectives:

Introduction to Arduino Diagrams & Code Brown County Library

Project 12 Piezo Sounder Melody Player

Building your own special-purpose embedded system gadget.

General Syntax. Operators. Variables. Arithmetic. Comparison. Assignment. Boolean. Types. Syntax int i; float j = 1.35; int k = (int) j;

Which LED(s) turn on? May 4, 2018 E40M Spring 2018 T. Diamandis, J. Plummer, R. Howe, C. Z. Lee 1

Digital I/O Operations

+ Inheritance. Sometimes we need to create new more specialized types that are similar to types we have already created.

Tutorial 7. Number Colour Black Brown Red Orange Yellow Green Blue Violet Gray White

The Big Idea: Background:

cs281: Introduction to Computer Systems Lab03 K-Map Simplification for an LED-based Circuit Decimal Input LED Result LED3 LED2 LED1 LED3 LED2 1, 2

PDF of this portion of workshop notes:

FUNCTIONS For controlling the Arduino board and performing computations.

Thumb Joystick Retail. Tools and parts you'll need. Things you'll want to know. How does it work? Skill Level: Beginner. by MikeGrusin March 22, 2011

WALT: definition and decomposition of complex problems in terms of functional and non-functional requirements

Intro to Arduino. Zero to Prototyping in a Flash! Material designed by Linz Craig and Brian Huang

Arduino 6: Analog I/O part 1. Jeffrey A. Meunier University of Connecticut

Arduino Programming Part 3. EAS 199A Fall 2010

Make your own secret locking mechanism to keep unwanted guests out of your space!

[ the academy_of_code] Senior Beginners

CS12020 (Computer Graphics, Vision and Games) Worksheet 1

EXPERIMENT 7 Please visit to learn all features of arduino before you start the experiments

Text Input and Conditionals

NAME EET 2259 Lab 3 The Boolean Data Type

Using PSpice to Simulate Transmission Lines K. A. Connor Summer 2000 Fields and Waves I

Basic Input/Output Operations

Which LED(s) turn on?

MAKE & COLLABORATE: SECRET KNOCK LOCK

Interrupts Arduino, AVR, and deep dark programming secrets. What is an Interrupt?

Introduction to Arduino

Build the Machine Science XBoard, with a programmable microcontroller.

m-block By Wilmer Arellano

Overview. Multiplexor. cs281: Introduction to Computer Systems Lab02 Basic Combinational Circuits: The Mux and the Adder

How to Become an IoT Developer (and Have Fun!) Justin Mclean Class Software.

Lecture 7. Processing Development Environment (or PDE)

Techgirlz Workshop Scratch and Raspberry Pi

In this simple example, it is quite clear that there are exactly two strings that match the above grammar, namely: abc and abcc

C Language Reference for ActivityBot. Driving the robot a specific distance or a specific amount of turn

Simple Sounders and Sensors

CTEC 1802 Embedded Programming Labs

ENGR 40M Project 3c: Coding the raindrop pattern

m-block By Wilmer Arellano

OrbBasic Lesson 1 Goto and Variables: Student Guide

Q1: Multiple choice / 20 Q2: C input/output; operators / 40 Q3: Conditional statements / 40 TOTAL SCORE / 100 EXTRA CREDIT / 10

EEG 101L INTRODUCTION TO ENGINEERING EXPERIENCE

Introduction To Arduino

Transcription:

Robotics and Electronics Unit 5 Objectives. Students will work with mechanical push buttons understand the shortcomings of the delay function and how to use the millis function. In this unit we will use mechanical push buttons instead of the capacitive buttons that we used in unit 2. These buttons have four prongs to make them easier to attach to breadboards. The four pins also allow the buttons to be used in more complicated circuits (that we will not have to worry about). For this class, always position a push button over the center of the breadboard (as shown in the figure). Exercise 1. Build one circuit with a button, 330-ohm resistor (orange-orangebrown), and a LED. When the button is pressed, the LED goes on. When the button is not pressed, the LED is off. Do this without writing any code. Just use the Arduino as a power source. Make sure that the current runs through the resistor, LED, and button. There are multiple ways to do this correctly. Exercise 2. Build the circuit and copy/run the code. Use a 10k resistor (brown-black-orange). void setup(){ pinmode( 8, INPUT ); void loop(){ int x = digitalread( 8 ); if ( x == HIGH ) Serial.println( "PRESSED" ); else Serial.println( "Not pressed" ); Copy and run this code. You should see PRESSED when the button is pressed and Not pressed when the button is not pressed. The 10k resistor is needed so that when the button is pressed there will not be a The 10k resistor is also needed when the button is not pressed. Without the resistor, pin 8 is not connect a circuit and it will float and return random values. 1

Exercise 3. Build the same button circuit we used in exercise 2. Copy and run this code. What is displayed if you click the button once? Think about why the circuit/code behaves this way. pinmode( 8, INPUT ); int x = digitalread( 8 ); if ( x == HIGH ) { Exercise 4. Use the same circuit as before but copy/run this code. Occasionally the program may count one click as two clicks. There is a reason for this, but we will ignore that problem for the time being. int oldx = LOW; pinmode( 5, INPUT ); int x = digitalread( 5 ); if ( x == HIGH && oldx ==LOW ) { oldx= x; Exercise 5. Build three circuits: a red LED circuit, a green LED circuit, and a button circuit. Once the button has been pressed three times, the red LED turns on and stays on. Once the button has been clicked a total of 6 times, the green LED turns on (so now there are two lights on). After that both buttons stay on. If you click slowly and carefully, it should work fine. If you click the button fast, you might find that it does not count quite correctly. It may be off by one. This is due to something called bounce. When the button is pressed, and the metal contacts hit each other (inside the button), there is some bounce at the microscopic level. There are ways to take care of this with either software or hardware but we take the third way of dealing with this problem ignoring it. At least for now. IMPORTANT. Do not use the delay function. You do not need it in this exercise and it will complicate matters (we will address this later). 2

Exercise 6. Use the same circuits as you built for the previous exercise except you will write a different program so that it behaves differently. When the program starts, both LEDs are off. Click once and the red LED is on and the green LED is off. Click a second time and the red LED is off and the green LED is on. Click a third time and both LEDs are on. Click a fourth time and both LEDs are off again. Every click after that repeats the above sequence. Hint. Create a global variable. Every time the button is clicked, add one to that variable. At a certain point, set this variable back to zero so that the sequence repeats. IMPORTANT. Do not use the delay function. More Data Types Limits Memory int a; integer from -32,768 to 32,767 16 bits unsigned int b; integer from 0 to 65,535 16 bits long c; integer from -2,147,483,648 to 2,147,483,647 32 bits unsigned long d; integer from 0 to 4,294,967,295 32 bits Unsigned means that the integer cannot be negative The millis function returns an unsigned long integer that is the number of milliseconds that have passed since the program started. To the right is some sample code. On the far right is the output from the code. unsigned long k = millis(); Serial.println( k ); unsigned long t = millis(); if ( t >= && t <= ) Serial.println( t ); 0 Exercise 7. Build a button circuit. Write a program so that when the user clicks the button, it displays the number of milliseconds since the program started. You will need to use the millis function described above. Print the word Start in the setup function. To the right is a sample run. Even though I only clicked twice, five numbers are displayed because of bounce. I first clicked 2,588 milliseconds after the program started and there was a bounce 93 milliseconds later. The second click generated two bounces: I clicked 7,799 milliseconds after the program started and then there was a bounce 104 milliseconds later and a second bounce 3 milliseconds after that. 3

Exercise 8. There is no circuit with this exercise; just copy the code and run it. Write down a few of the numbers being displayed: How much time passes between each number being displayed? unsigned long oldtime = 0; unsigned long count = 0; Serial.println( "Start" ); unsigned long t = millis(); if ( t - oldtime >= 1000 ) { oldtime = t; count = 0; Exercise 9. You do not build or program anything for this exercise. Pretend you have a working button circuit. The code to the right is identical to the code in exercise 4 except that one additional line of code was added. Circle that line of code. If the user clicks on the button three times a second, what is value of variable count at the end of four seconds? Consider the impact of that additional line of code. int oldx = LOW; pinmode( 5, INPUT ); int x = digitalread( 5 ); if ( x == HIGH && oldx ==LOW ) { oldx= x; delay( 0 ); Exercise 10. There is no circuit for this exercise. Complete the program so that it prints the Start, 1000, 2000, 3000, and so on (as shown in the figure). There should be a one second interval between each number being printed without using the delay function. There should also be a one second interval between Start and 1000 being printed. unsigned long oldtime = 0; Serial.println( "Start" ); unsigned long currenttime = millis(); if ( ) { Serial.println( currenttime ); Do not use the delay function. 4

Exercise 11. Because of bounce, we cannot accurately count the number of times the button was clicked. In this exercise we handle bounce by using the millis function. We will define a click as occurring when (1) the voltage (as detected by digitalread) goes from LOW to HIGH and (2) it has been more than 200 milliseconds since the last time the voltage went from LOW to HIGH. Note. 200 is a relatively arbitrary number based on some tests I ran. We want it to be large enough to eliminate one click being registered multiple times but not so large that legitimate multiple clicks are missed. Build a button circuit using a 10k resistor as you ve done in previous exercises. Copy the outline below and fill in the blanks. When you are done, run the program and click the button. You should find that it keeps a much more accurate count of the number of times the button was clicked. unsigned long lastclicktime = 0; int lastread = LOW; pinmode(, INPUT ); Serial.println( "Start" ); int b = digitalread( ); unsigned long currenttime = millis(); if ( the voltage is currently high but used to be low AND it has been 200 ms or more since the last click ) { Exercise 12. Build a button circuit (10k resistor, brown-black-orange) and a speaker circuit (100 ohm resistor, brown-black-brown). When the program starts, the speaker is playing a 300 hz tone. After the button is clicked 3 times, the frequency changes to hz. After 6 clicks it changes to 700 hz and stays at there. Follow the outline on the right. 3 global variables void setup(){ call the pinmode function twice void loop(){ code that counts the number of clicks use the millis function as in exercise 11 if the count is less than 3, play 300 else if the count is less than 6, then else play 700 5

Exercise 13a. Build two separate LED circuits that are connected to pins 9 and 10 - don t forget the 330, orange-orange-brown, resistors. Complete the program below so that one LED is on and the other is off; then 2 seconds later, the first LED goes off and the second LED goes on for 2 seconds. And so on. Do NOT use the delay function. Note: we did this program back in unit 2 using the delay function and this version is more complicated. You might think unnecessarily more complicated. However, in part 12b we will modify the program to do something that cannot be done using the delay function. unsigned long lasttime = 0; off int pin9 = HIGH; // track the last time we changed which LED is on and which is // track whether this LED is on or off This is a nested if statement because it is nested inside of another if statement. pinmode( 9, OUTPUT ); pinmode( 10, OUTPUT ); unsigned long currenttime = millis(); if ( two seconds have passed ) { if ( pin9 == HIGH ) { digitalwrite( 9, LOW ); digitalwrite( 10, HIGH ); do something to the variable pin9 else { digitalwrite( 9, HIGH ); digitalwrite( 10, LOW ); do something to the variable pin9 one more line of code for you to figure out Important. The variable pin9 does not cause an LED to go on or off; we are using it to track whether that LED is on or off. Exercise 13b. Build a button circuit (keep the two LED circuits). Modify the program so that while the LEDs are blinking on and off, it also keeps an accurate count of the number of times the button is clicked - these numbers should be printed using Serial.println. This code can go below the code from part 12a (but still be in the loop function). There are times, especially in simple programs, where the delay function is useful. But in general, you want your program to do multiple things at the same time (or at least feel like it is happening at the same time). For that, you need the millis function. 6

Exercise 14. We are building a very simple game. There is a speaker and it plays the following frequencies, in this order: 200, 300, 400,, 600, 700, and then repeats starting at 200 again. Each tone plays for 1 second. There is a button. If the user clicks the button when the 400 tone is playing, the program prints YOU WIN! If the user clicks while any other tone is playing, it prints You lose. Clicking the button does not stop the sound. Use a global variable to track which frequency is currently being played. four global variables usual stuff two statements if ( the button has been clicked ){ if ( the current frequency is 400 ) Serial.println( "YOU WIN!" ); else Serial.println( "You lose." ); update the variable that tracks the last time the button was clicked. update the variable that tracks the previous state of the button if ( one second has passed ) { update which frequency is playing play that frequency update the variable that tracks the last time the frequency was changed. We will revisit this program in the next unit and learn ways to make the program more interesting and learn ways to organize the code better. 7