Example: short timearray[] = {500, 400, 500, 400, 100, 200, 100, 200, -1};

Similar documents
Goal: Strengthen our understanding of C and program our Mudduino boards

Goal: Understand how to write programs for the Mudduino

Computer Science & Engineering 150A Problem Solving Using Computers

3/12/2018. Structures. Programming in C++ Sequential Branching Repeating. Loops (Repetition)

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #44. Multidimensional Array and pointers

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

Decision Making and Loops

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Fall 2009

Embedded Controller Programming 2

UNIVERSITY OF CALIFORNIA, SANTA CRUZ BOARD OF STUDIES IN COMPUTER ENGINEERING

Arrays and Loops. Programming for Engineers Winter Andreas Zeller, Saarland University

Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays)

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

Arduino Prof. Dr. Magdy M. Abdelhameed

CS Introduction to Programming Midterm Exam #1 - Prof. Reed Spring 2009

REPETITION CONTROL STRUCTURE LOGO

Computer Programming: C++

Information Science 1

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

Connecting Arduino to Processing

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

COP 3223 Introduction to Programming with C - Study Union - Spring 2018

Array. Prepared By - Rifat Shahriyar

Repetition Structures

Physical Programming with Arduino

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

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

E11 Lecture 4: More C!!! Profs. David Money Harris & Sarah Harris Fall 2011

Chapter Goals. Contents LOOPS

E11 Lecture 4: More C!!! Profs. David Money Harris & Sarah Harris Fall 2011

Maltepe University Computer Engineering Department. BİL 133 Algorithms and Programming. Chapter 8: Arrays

An array is a collection of data that holds fixed number of values of same type. It is also known as a set. An array is a data type.

EE C Program Elements

Unit 3 Decision making, Looping and Arrays

CSE 351: The Hardware/Software Interface. Section 2 Integer representations, two s complement, and bitwise operators

Class C{ int a; } what variables below are objects : a. C c; b. String str; c. Scanner scanner; d. int num; e. float f;

6. User Defined Functions I

AMCAT Automata Coding Sample Questions And Answers

MAKE & COLLABORATE: SECRET KNOCK LOCK

CS2141 Software Development using C/C++ C++ Basics

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

Programming. The Programmable Box! with. Programming made fun Ages 10+ V1.0 Copyright 2014, 2015 Your Inner Geek, LLC

COMP 161 Lecture Notes 16 Analyzing Search and Sort

Government Polytechnic Muzaffarpur.

CS16 Exam #1 7/17/ Minutes 100 Points total

McGill University School of Computer Science COMP-202A Introduction to Computing 1

Robotics and Electronics Unit 5

Sample Final Exam. 1) (24 points) Show what is printed by the following segments of code (assume all appropriate header files, etc.

Information Science 1

1 Overview. 2 Basic Program Structure. 2.1 Required and Optional Parts of Sketch

Juniper: A Functional Reactive Programming Language for the Arduino

Review of the syntax and use of Arduino functions, with special attention to the setup and loop functions.

Microcontrollers and Interfacing week 8 exercises

Introduction to Computer Science Midterm 3 Fall, Points

Programming for Engineers Iteration

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

Decision Making -Branching. Class Incharge: S. Sasirekha

ME 461 C review Session Fall 2009 S. Keres

Chapter 7 Functions. Now consider a more advanced example:

Q1 Q2 Q3 Q4 Q5 Total 1 * 7 1 * 5 20 * * Final marks Marks First Question

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

Functions. Lecture 6 COP 3014 Spring February 11, 2018

MAE106 Laboratory Exercises Lab # 1 - Laboratory tools

ECET 264 C Programming Language with Applications. C Program Control

Fundamentals of Computer Programming Using C

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

Questions Bank. 14) State any four advantages of using flow-chart

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop

Quiz 1: Functions and Procedures

This is the basis for the programming concept called a loop statement

Arrays. Lecture 11 CGS 3416 Fall October 26, 2015

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. The Increment and Decrement Operators

University of Hull Department of Computer Science C4DI Interfacing with Arduinos

Instructor: Eng.Omar Al-Nahal

Energia MSP-430!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 1

Chapter 2 The Basic Functions

Review Activity 1 CALL and RET commands in assembler

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types. COMP-202 Unit 6: Arrays

C Course. IIT Kanpur. Lecture 3 Aug 31, Rishi Kumar <rishik>, Final year BT-MT, CSE

Introduction to Arduino

CS113: Lecture 4. Topics: Functions. Function Activation Records

// The REAL DEAL - Running Code. int led22 = 22; int led23 = 23; int led24 = 24; int led25 = 25; int led27 = 27; int led29 = 29; int led31 = 31;

Prototyping & Engineering Electronics Kits Basic Kit Guide

Grove - PIR Motion Sensor

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

Arrays. Lecture 11 CGS 3416 Spring March 6, Lecture 11CGS 3416 Spring 2017 Arrays March 6, / 19

Touch Board User Guide. Introduction

Outline Arrays Examples of array usage Passing arrays to functions 2D arrays Strings Searching arrays Next Time. C Arrays.

Scope. Scope. Region of a program in which a defined object is visible. Defined Objects. Two types of regions. Variables Functions

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Algorithmic Thinking and Structured Programming (in Greenfoot) Teachers: Renske Smetsers-Weeda Sjaak Smetsers Ana Tanase

THE INTEGER DATA TYPES. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

APS105. Collecting Elements 10/20/2013. Declaring an Array in C. How to collect elements of the same type? Arrays. General form: Example:

Arduino C++ Introduction to programming Antony Watts M0IFA

Arrays and Applications

How to declare an array in C?

tablica: array: dane_liczbowe

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Transcription:

Write a sketch that once you enter g for go it turns the LED on and off for the amount of time in an array. Have the program terminate when it reaches a negative value (as time is always positive). Example: short timearray[] = 500, 400, 500, 400, 100, 200, 100, 200, -1; This would have the LED on for 500 ms, off for 400 ms, on for 500 ms, off for 400 ms, on for 100 ms, off 200 for ms

#define LED 13 short timearray[] = 500, 400, 500, 400, 100, 200, 100, 200, -1; Serial.println("Ready..."); //setup LED pin pinmode(led, OUTPUT); unsigned char command; if (Serial.available() > 0) command = Serial.read(); int i = 0; int highlow = 1; if(command == 'g') while(timearray[i] >= 0) digitalwrite(led, highlow); delay(timearray[i]); highlow =!highlow; i++; Here is one solution to the problem First we setup the serial port, print that the program is running, then we setup the LED pin as an output In the main loop we are more careful with the serial port and make sure there is something to read before trying to read it, then if it is g we increment though the array until we reach a negative value flipping the value of the LED by negating highlow.

Write a sketch that allows you to blink both the red and the green LED for different amounts of time simultaneously. The green LED is on pin 5. Example: short timearrayr[] = 500, 400, 500, 400, 100, 200, 100, 200, -1; short timearrayg[] = 100, 200, 100, 300, 50, 50, 50, 50, 50, 50, -1;

#define RedLED 13 #define GreenLED 5 #define True 1 #define False 0 short timearrayr[] = 500, 400, 500, 400, 100, 200, 100, 200, -1; short timearrayg[] = 100, 200, 100, 300, 50, 50, 50, 50, 50, 50, -1; Serial.println("Ready..."); //setup LED pins pinmode(redled, OUTPUT); pinmode(greenled, OUTPUT); unsigned char command; if (Serial.available() > 0) command = Serial.read(); int i = 0; int j = 0; int notdone = True; int notdonered = True; int notdonegreen = True; long currenttime = 0; if(command == 'g') while(notdone) notdonered = setled(currenttime, timearrayr, RedLED); notdonegreen = setled(currenttime, timearrayg, GreenLED); notdone = notdonegreen notdonered; delay(1); currenttime++; int setled(long currenttime, int edges[], int pin) int i = 0; long totaltime = 0; while(edges[i] > 0) totaltime += edges[i]; if(currenttime < totaltime) if(i%2 == 0)digitalWrite(pin, HIGH); else digitalwrite(pin, LOW); return True; i++; return false;

//Sketch Multi #define rows 6 int mymatrix[rows][columns]; int i, j; // counters //initialize my array mymatrix[i][j] = j*(1+i); //print array Serial.print(myMatrix[i][j]); Serial.print("\t"); Serial.print("\n"); In your homework you will eventually meet not only arrays but multi-dimensional arrays (or arrays of arrays). In this example we see a 2-D array which could be thought of as a matrix of numbers. Though we can type in the numbers using the notation, (for a 2x2 matrix that would look like 1,2, 3,4) here we initialize programmatically with nested for loops that index over the elements in the arrays. Then we print out the matrix using the tab character \t to space things out nicely. Try it out, then modify the print function so that it prints the sum of each row rather than the individual values.

//Sketch Multi #define rows 6 int mymatrix[rows][columns]; int i, j; // counters //initialize my array mymatrix[i][j] = j*(1+i); //print array Serial.print(myMatrix[i][j]); Serial.print("\t"); Serial.print("\n"); In your homework you will eventually meet not only arrays but multi-dimensional arrays (or arrays of arrays). In this example we see a 2-D array which could be thought of as a matrix of numbers. Though we can type in the numbers using the notation, (for a 2x2 matrix that would look like 1,2, 3,4) here we initialize programmatically with nested for loops that index over the elements in the arrays. Then we print out the matrix using the tab character \t to space things out nicely. Try it out, then modify the print function so that it prints the sum of each row rather than the individual values.

//Sketch Multi #define rows 6 int mymatrix[rows][columns]; int i, j; // counters int sumvalue; //initialize my array mymatrix[i][j] = j*(1+i); //print array sumvalue = 0; sumvalue = sumvalue + mymatrix[i][j]; Serial.println(sumValue); Here is one possible solution This program is the whole algorithm in just the main (setup) function. As your program grows this will become completely in practical. Its often better to think of sub-procedures (functions) to create functions for. Good candidates for functions are things you have to do more than once that are testable. In the code here the ideas that are distinct are initializing the matrix and printing it. These should be functions.

#define rows1 2 #define rows2 10 int matrix1[rows1][columns]; int matrix2[rows2][columns]; //function prototypes void initalizematrix(int amatrix[][columns], int void printmatrix(int amatrix[][columns], int Once we use functions we can write down our core idea first compactly. Here we create two 2-D arrays as global variables, and our main routine just wants to initialize them and print them out. initalizematrix(matrix1, rows1); initalizematrix(matrix2, rows2); printmatrix(matrix1, rows1); printmatrix(matrix2, rows2);

#define rows1 2 #define rows2 10 int matrix1[rows1][columns]; int matrix2[rows2][columns]; //function prototypes void initalizematrix(int amatrix[][columns], int void printmatrix(int amatrix[][columns], int initalizematrix(matrix1, rows1); initalizematrix(matrix2, rows2); printmatrix(matrix1, rows1); printmatrix(matrix2, rows2); Once we use functions we can write down our core idea first compactly. Here we create two 2-D arrays as global variables, and our main routine just wants to initialize them and print them out. To do this we specify how these functions will work via prototypes, they need a reference to the array, and how many rows it has. [In C you have to provide all the sizes of the array but the last one when passing the array to a function.] Our function needs the number of rows to avoid going into memory we do not own. With this done we can write the functions that accomplish these sub-tasks.

#define rows1 2 #define rows2 10 int matrix1[rows1][columns]; int matrix2[rows2][columns]; //function prototypes void initalizematrix(int amatrix[][columns], int void printmatrix(int amatrix[][columns], int initalizematrix(matrix1, rows1); initalizematrix(matrix2, rows2); // C requires the size of all the indexes of multidimensional arrays at compile time // Thus the int amatrix[][columns] void initalizematrix(int amatrix[][columns], int numberofrows) int i, j; // counters //initialize my array for(i = 0; i<numberofrows; i++) amatrix[i][j] = j*(1+i); void printmatrix(int amatrix[][columns], int numberofrows) int i, j; // counters printmatrix(matrix1, rows1); printmatrix(matrix2, rows2); The code is very similar to what was in main but now reusable; try it out //print array for(i = 0; i<numberofrows; i++) Serial.print(aMatrix[i][j]); Serial.print("\t"); Serial.print("\n"); Serial.print("\n");