<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>http://socialledge.com/sjsu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Charley</id>
		<title>Embedded Systems Learning Academy - User contributions [en]</title>
		<link rel="self" type="application/atom+xml" href="http://socialledge.com/sjsu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Charley"/>
		<link rel="alternate" type="text/html" href="http://socialledge.com/sjsu/index.php/Special:Contributions/Charley"/>
		<updated>2026-05-11T13:30:48Z</updated>
		<subtitle>User contributions</subtitle>
		<generator>MediaWiki 1.27.1</generator>

	<entry>
		<id>http://socialledge.com/sjsu/index.php?title=ADC_Interface_with_Ultrasonic_Proximity_Sensor&amp;diff=1893</id>
		<title>ADC Interface with Ultrasonic Proximity Sensor</title>
		<link rel="alternate" type="text/html" href="http://socialledge.com/sjsu/index.php?title=ADC_Interface_with_Ultrasonic_Proximity_Sensor&amp;diff=1893"/>
				<updated>2012-11-15T18:54:25Z</updated>
		
		<summary type="html">&lt;p&gt;Charley: Created page with &amp;quot; == Overview ==  The LV-MaxSonar-EZ0 ultrasonic sensor interfaces with your microcontroller using an ADC interface. The microcontroller on your development board is equipped w...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
== Overview ==&lt;br /&gt;
&lt;br /&gt;
The LV-MaxSonar-EZ0 ultrasonic sensor interfaces with your microcontroller using an ADC&lt;br /&gt;
interface. The microcontroller on your development board is equipped with an analog-to-digital &lt;br /&gt;
converter. This converts an analog signal (continuous voltage that varies over time) into &lt;br /&gt;
corresponding digital values (discrete integers that approximate the input voltage). For example, &lt;br /&gt;
let’s say that a 12-bit ADC operates on 3.3V, which is the case for your SJSU One development &lt;br /&gt;
board. This means that a voltage range between 0V and 3.3V can be divided into 2&amp;lt;sup&amp;gt;12&amp;lt;/sup&amp;gt;&lt;br /&gt;
or 4096 discrete steps. If the ADC is returning an integer value of 2048, you can calculate that a sensor &lt;br /&gt;
connected to the ADC is outputting a voltage of 1.65 volts.&lt;br /&gt;
&lt;br /&gt;
== Pin Connections ==&lt;br /&gt;
&lt;br /&gt;
The ultrasonic sensor has 7 pins. We will connect 3 of them to your board:&lt;br /&gt;
# Use a red wire to connect Pin “+5” on the sensor to Pin “3.3V” on your board. &lt;br /&gt;
# Use a black wire to connect Pin “GND” on the sensor to Pin “Gnd” on your board.&lt;br /&gt;
# Use another color wire to connect Pin “AN” on the sensor to Pin “A0-4” on your board.&lt;br /&gt;
&lt;br /&gt;
== Source Code Description ==&lt;br /&gt;
&lt;br /&gt;
Add &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;#include “adc0.h”&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt; to the top of your main.c. This gives you access to the ADC &lt;br /&gt;
functions.&lt;br /&gt;
&lt;br /&gt;
Your microcontroller has 80 pins but many more features. Therefore, one pin can do many &lt;br /&gt;
different things. We will need to tell it to internally connect pin P1.30 to the ADC &lt;br /&gt;
hardware as opposed to some other hardware. To do this we will need to write the &lt;br /&gt;
appropriate value to a Pin Select Register:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;LPC_PINCON-&amp;gt;PINSEL3 |= (3 &amp;lt;&amp;lt; 28);&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt;&lt;br /&gt;
&lt;br /&gt;
A function has been written to initialize ADC functionality. This function can be called &lt;br /&gt;
by:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;adc0_initialize();&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now, you can read values from the ADC with a call to:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;adc0_getReading(4);&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The “4” represents that we are reading values from ADC channel 4. Your board has multiple &lt;br /&gt;
ADC channels. For example, the light sensor is connected to channel 2, so we can’t use &lt;br /&gt;
that one.&lt;br /&gt;
&lt;br /&gt;
== Sample Code ==&lt;br /&gt;
&amp;lt;font size = 3&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang = &amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include “adc0.h”&lt;br /&gt;
&lt;br /&gt;
int main(void)&lt;br /&gt;
{&lt;br /&gt;
    int reading;&lt;br /&gt;
&lt;br /&gt;
    LPC_PINCON-&amp;gt;PINSEL3 |=  (3 &amp;lt;&amp;lt; 28); // ADC-4 is on P1.30, select this as ADC0.4&lt;br /&gt;
    adc0_initialize(); // Initialize ADC&lt;br /&gt;
    reading = adc0_getReading(4); // Read current value of ADC-4&lt;br /&gt;
    printf(&amp;quot;\nADC Reading: %d&amp;quot;, reading);&lt;br /&gt;
&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/font&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Remaining Work ==&lt;br /&gt;
&lt;br /&gt;
Your job is to turn the integer values you receive from ADC into numbers that are more &lt;br /&gt;
useful. Do this by converting the ADC value into a voltage value. Next, convert the &lt;br /&gt;
voltage value into a distance measurement value. The conversion rate needed to convert &lt;br /&gt;
voltage to inches/centimeters is in the sensor data sheet, which I will leave for you to &lt;br /&gt;
find. &lt;br /&gt;
&lt;br /&gt;
== Adding a second ADC type sensor == &lt;br /&gt;
&lt;br /&gt;
It is possible to connect a second sensor that uses ADC to your board using ADC Channel &lt;br /&gt;
5. This will require a different pin select code so ask a TA for assistance.&lt;/div&gt;</summary>
		<author><name>Charley</name></author>	</entry>

	<entry>
		<id>http://socialledge.com/sjsu/index.php?title=Sensor_Interfaces&amp;diff=1892</id>
		<title>Sensor Interfaces</title>
		<link rel="alternate" type="text/html" href="http://socialledge.com/sjsu/index.php?title=Sensor_Interfaces&amp;diff=1892"/>
				<updated>2012-11-15T18:52:27Z</updated>
		
		<summary type="html">&lt;p&gt;Charley: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page lists the interface hints for various different sensors and peripherals.&lt;br /&gt;
&lt;br /&gt;
* [[PING))) Ultrasonic Sensor]]&lt;br /&gt;
* [[TSOP IR Detector]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;BR/&amp;gt;&lt;br /&gt;
== SJ-One Board Interfaces ==&lt;br /&gt;
*  [[ADC Interface with Ultrasonic Proximity Sensor]]&lt;br /&gt;
*: This shows you how to use an '''A'''nalog to '''D'''igital '''C'''onverter with a Proximity Sensor example.  &lt;br /&gt;
*  [[PWM Motor Controller Interface]]&lt;br /&gt;
*  [[GPIO Interface]]&lt;br /&gt;
*:  This shows you how to use '''G'''eneral '''P'''urpose pins for '''I'''nput or '''O'''utput&lt;/div&gt;</summary>
		<author><name>Charley</name></author>	</entry>

	<entry>
		<id>http://socialledge.com/sjsu/index.php?title=Sensor_Interfaces&amp;diff=1891</id>
		<title>Sensor Interfaces</title>
		<link rel="alternate" type="text/html" href="http://socialledge.com/sjsu/index.php?title=Sensor_Interfaces&amp;diff=1891"/>
				<updated>2012-11-15T18:46:44Z</updated>
		
		<summary type="html">&lt;p&gt;Charley: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page lists the interface hints for various different sensors and peripherals.&lt;br /&gt;
&lt;br /&gt;
* [[PING))) Ultrasonic Sensor]]&lt;br /&gt;
* [[TSOP IR Detector]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;BR/&amp;gt;&lt;br /&gt;
== SJ-One Board Interfaces ==&lt;br /&gt;
*  [[ADC Interface MaxBotics Proximity Sensor]]&lt;br /&gt;
*  [[PWM Motor Controller Interface]]&lt;br /&gt;
*  [[GPIO Interface]]&lt;br /&gt;
*:  This shows you how to use '''G'''eneral '''P'''urpose pins for '''I'''nput or '''O'''utput&lt;/div&gt;</summary>
		<author><name>Charley</name></author>	</entry>

	<entry>
		<id>http://socialledge.com/sjsu/index.php?title=ES101_-_Lesson_11_:_State_Machine_and_Board_IO&amp;diff=1799</id>
		<title>ES101 - Lesson 11 : State Machine and Board IO</title>
		<link rel="alternate" type="text/html" href="http://socialledge.com/sjsu/index.php?title=ES101_-_Lesson_11_:_State_Machine_and_Board_IO&amp;diff=1799"/>
				<updated>2012-11-02T19:34:39Z</updated>
		
		<summary type="html">&lt;p&gt;Charley: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Objective ==&lt;br /&gt;
The objective of this lab is to learn how to design a state machine.&lt;br /&gt;
&lt;br /&gt;
== State Machine Design ==&lt;br /&gt;
State machine design can be easily constructed by considering &amp;lt;i&amp;gt;what&amp;lt;/i&amp;gt; you want to do based on a certain system state. For example, you might want consider building a state machine for an obstacle-avoidance robot, which acts differently based on if it detects an obstacle or not. In other words, you might have a state to avoid an obstacle, and another state to follow a light-source if there is an obstacle in the way. This helps separate the functionality of what should happen when an obstacle is in the way, and what should happen if an obstacle is not in the way.&lt;br /&gt;
&lt;br /&gt;
Each state should contain meaningful code that should be executed in that state. Furthermore, state transition code should be put in place that provides a way out of the current state to the next state/&lt;br /&gt;
&lt;br /&gt;
== ENUM ==&lt;br /&gt;
The &amp;lt;b&amp;gt;enum&amp;lt;/b&amp;gt; provides a way to assign names to a sequence of integer values. For example, if a state machine contains two unique states, you should use an &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;enum&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt; to declare the names, and the compiler guarantees that the names have a unique identifier. The alternative is to declare separate variables set to a different value, but this approach uses more unnecessary memory and is error-prone.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;font size = 3&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
// Auto-set the identifiers of the variables&lt;br /&gt;
enum{stateSTART, stateEND, stateSENSOR};&lt;br /&gt;
&lt;br /&gt;
// Manually set the identifiers of the variables&lt;br /&gt;
enum{stateSTART = 1, stateEND = 2, stateSENSOR = 3};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/font size&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The enumerator variables can be automatically assigned a value, or the value can be manually chosen. In either case, unique values should be set for enumerator variables. An enumerator does not have to be used just for a state machine design, but it can be used anytime when you need to simply set unique identifiers to certain names.&lt;br /&gt;
&lt;br /&gt;
==Switch Statement==&lt;br /&gt;
&lt;br /&gt;
The C/C++ programming languages feature &amp;lt;b&amp;gt;switch/case&amp;lt;/b&amp;gt; statements. This is similar to using a series of &amp;lt;b&amp;gt;if/else&amp;lt;/b&amp;gt; statements. However, it is easier to read and write code using &amp;lt;b&amp;gt;switch/case&amp;lt;/b&amp;gt; statements in some situations such as for a state machine design.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;BR&amp;gt;&lt;br /&gt;
&amp;lt;font size = 3&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
// Test the &amp;quot;currentState&amp;quot; variable with one of the cases given below&lt;br /&gt;
switch(currentState)&lt;br /&gt;
{  &lt;br /&gt;
   case stateSTART:&lt;br /&gt;
      i = 0;&lt;br /&gt;
      break;&lt;br /&gt;
   case stateEND:&lt;br /&gt;
      i = 0;&lt;br /&gt;
      break;&lt;br /&gt;
   case stateSENSOR:&lt;br /&gt;
      i = 2;&lt;br /&gt;
      break;&lt;br /&gt;
   default:&lt;br /&gt;
      i = 3;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/font&amp;gt;&lt;br /&gt;
&amp;lt;BR&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As seen by the previous figure, the &amp;lt;b&amp;gt;switch/case&amp;lt;/b&amp;gt; statements look elegant, and are easier to read compared to &amp;lt;b&amp;gt;if/else&amp;lt;/b&amp;gt; statements had they been used instead. The variable inside the round brackets &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;(currentState)&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt; is what is being compared to the other cases. Each statement has a colon (:) at the end. Also, each case ends with a break. Forgetting to include the break will force the code to go to the next state without comparison. For example, if &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;currentState&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt; was equal to &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;stateSTART&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt; and there was no &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;break&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt; statement at the end of the &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;stateSTART&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt; case, the code would continue without &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;currentState&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt; comparison with &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;stateEND&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt; and execute the code inside the &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;stateEND&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt; case as well. This may appear to be a programming flaw, however, the switch/case statements were designed this way and some complex designs omit the break statements on purpose.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;default&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt; case should be the last case, it is for the case if the variable &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;currentState&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt; does not match any of the case statements, and this case does not have the &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;break statement&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt;. The default case should be used to indicate an error state or unexpected state fault.  &lt;br /&gt;
&lt;br /&gt;
==Static Declaration of Variables==&lt;br /&gt;
&lt;br /&gt;
So far, you have learned how to locally declare variables inside a function, but sometimes, you want to declare the local variables as static so their values are preserved across multiple calls to the same function.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;BR&amp;gt;&lt;br /&gt;
&amp;lt;font size = 3&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
void foo()&lt;br /&gt;
{&lt;br /&gt;
   int i = 0;&lt;br /&gt;
   i++;&lt;br /&gt;
   printf(&amp;quot;Value of i = %i\n&amp;quot;, i);&lt;br /&gt;
}&lt;br /&gt;
int main(void)&lt;br /&gt;
{&lt;br /&gt;
   foo();  // Prints: Value of i = 1&lt;br /&gt;
   foo();  // Prints: Value of i = 1&lt;br /&gt;
   foo();  // Prints: Value of i = 1&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/font&amp;gt;&lt;br /&gt;
&amp;lt;BR&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the code above, whenever &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;foo()&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt; is called, it declares a new variable called &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt; and sets it to 0. Then it increments by 1 and prints it out. Whenever &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt; calls it, &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;foo()&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt; does the same thing, and each time it sets &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt; as 1 and when it exits, the variable &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt; goes out of scope.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;BR&amp;gt;&lt;br /&gt;
&amp;lt;font size = 3&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
void foo()&lt;br /&gt;
{&lt;br /&gt;
   static int i = 0;&lt;br /&gt;
   i++;&lt;br /&gt;
   printf(&amp;quot;Value of i = %i\n&amp;quot;, i);&lt;br /&gt;
}&lt;br /&gt;
int main(void)&lt;br /&gt;
{&lt;br /&gt;
   foo();  // Prints: Value of i = 1&lt;br /&gt;
   foo();  // Prints: Value of i = 1&lt;br /&gt;
   foo();  // Prints: Value of i = 1&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/font&amp;gt;&lt;br /&gt;
&amp;lt;BR&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the code above, the result is different because the variable &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt; is declared as &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;static int&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt;. It should be observed that the variable &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt; is only declared once, and the function &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;foo()&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt; preserves the value of &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt; over repeated function calls. For this assignment, if your state machine is enclosed in a function called &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;stateMachine()&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt;, the please decide which variables should preserve their value, and declare them as static. Declaring a static variable is a better programming practice than declaring a global variable which would be visible to the entire program.&lt;br /&gt;
&lt;br /&gt;
==Assignment== &lt;br /&gt;
Build the state machine given in the next figure. Enclose your state machine inside a function and call the function every 100ms from your &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt; function. You will notice that the SENSOR state goes to IDLE state upon Button#1, and using the same button, the IDLE state transitions to SENSOR state. First, answer the question in the &amp;quot;Questions&amp;quot; section of this document. &lt;br /&gt;
&lt;br /&gt;
[[File:stateMachine10.png|600px|center|State Machine Design]]&lt;br /&gt;
&lt;br /&gt;
Whenever the state is changed, print on the screen what state the system is in. The statement should only print once - not every time the state machine function is called.&lt;br /&gt;
&lt;br /&gt;
==Sample Code==&lt;br /&gt;
&amp;lt;BR&amp;gt;&lt;br /&gt;
&amp;lt;font size = 3&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
void myStateMachine()&lt;br /&gt;
{&lt;br /&gt;
   // Use typedef to create a new variable type called States&lt;br /&gt;
   typedef enum {start, end} States; // Name your states inside { and }&lt;br /&gt;
   static States currentState = start; // Initialize static variable of States&lt;br /&gt;
&lt;br /&gt;
   switch(currentState)&lt;br /&gt;
   {&lt;br /&gt;
      case start:&lt;br /&gt;
         // Check if button 0 is pressed in this state and transition out of this state&lt;br /&gt;
         if( /*button 0 is pressed */)&lt;br /&gt;
         {&lt;br /&gt;
            currentState = end;&lt;br /&gt;
            printf(&amp;quot;Current state: END. Press Button 1 to go to START&amp;quot;);&lt;br /&gt;
         }&lt;br /&gt;
         break;&lt;br /&gt;
      case end:&lt;br /&gt;
         // Check if button 0 is pressed in this state and transition out of this state&lt;br /&gt;
         if( /*button 1 is pressed */)&lt;br /&gt;
         {&lt;br /&gt;
            currentState = start;&lt;br /&gt;
            printf(&amp;quot;Current state: START. Press Button 0 to go to END&amp;quot;);&lt;br /&gt;
         }&lt;br /&gt;
         break;&lt;br /&gt;
       default:&lt;br /&gt;
         printf(&amp;quot;State machine ERROR!\n&amp;quot;);&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/font&amp;gt;&lt;br /&gt;
&amp;lt;BR&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Questions==&lt;br /&gt;
# Describe the behavior you see if you press and hold Button#1 for longer than 100ms in the SENSOR state.&lt;/div&gt;</summary>
		<author><name>Charley</name></author>	</entry>

	<entry>
		<id>http://socialledge.com/sjsu/index.php?title=ES101_-_Lesson_11_:_State_Machine_and_Board_IO&amp;diff=1798</id>
		<title>ES101 - Lesson 11 : State Machine and Board IO</title>
		<link rel="alternate" type="text/html" href="http://socialledge.com/sjsu/index.php?title=ES101_-_Lesson_11_:_State_Machine_and_Board_IO&amp;diff=1798"/>
				<updated>2012-11-02T19:18:31Z</updated>
		
		<summary type="html">&lt;p&gt;Charley: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Objective ==&lt;br /&gt;
The objective of this lab is to learn how to design a state machine.&lt;br /&gt;
&lt;br /&gt;
== State Machine Design ==&lt;br /&gt;
State machine design can be easily constructed by considering &amp;lt;i&amp;gt;what&amp;lt;/i&amp;gt; you want to do based on a certain system state. For example, you might want consider building a state machine for an obstacle-avoidance robot, which acts differently based on if it detects an obstacle or not. In other words, you might have a state to avoid an obstacle, and another state to follow a light-source if there is an obstacle in the way. This helps separate the functionality of what should happen when an obstacle is in the way, and what should happen if an obstacle is not in the way.&lt;br /&gt;
&lt;br /&gt;
Each state should contain meaningful code that should be executed in that state. Furthermore, state transition code should be put in place that provides a way out of the current state to the next state/&lt;br /&gt;
&lt;br /&gt;
== ENUM ==&lt;br /&gt;
The &amp;lt;b&amp;gt;enum&amp;lt;/b&amp;gt; provides a way to assign names to a sequence of integer values. For example, if a state machine contains two unique states, you should use an &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;enum&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt; to declare the names, and the compiler guarantees that the names have a unique identifier. The alternative is to declare separate variables set to a different value, but this approach uses more unnecessary memory and is error-prone.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;font size = 3&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
// Auto-set the identifiers of the variables&lt;br /&gt;
enum{stateSTART, stateEND, stateSENSOR};&lt;br /&gt;
&lt;br /&gt;
// Manually set the identifiers of the variables&lt;br /&gt;
enum{stateSTART = 1, stateEND = 2, stateSENSOR = 3};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/font size&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The enumerator variables can be automatically assigned a value, or the value can be manually chosen. In either case, unique values should be set for enumerator variables. An enumerator does not have to be used just for a state machine design, but it can be used anytime when you need to simply set unique identifiers to certain names.&lt;br /&gt;
&lt;br /&gt;
==Switch Statement==&lt;br /&gt;
&lt;br /&gt;
The C/C++ programming languages feature &amp;lt;b&amp;gt;switch/case&amp;lt;/b&amp;gt; statements. This is similar to using a series of &amp;lt;b&amp;gt;if/else&amp;lt;/b&amp;gt; statements. However, it is easier to read and write code using &amp;lt;b&amp;gt;switch/case&amp;lt;/b&amp;gt; statements in some situations such as for a state machine design.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;BR&amp;gt;&lt;br /&gt;
&amp;lt;font size = 3&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
// Test the &amp;quot;currentState&amp;quot; variable with one of the cases given below&lt;br /&gt;
switch(currentState)&lt;br /&gt;
{  &lt;br /&gt;
   case stateSTART:&lt;br /&gt;
      i = 0;&lt;br /&gt;
      break;&lt;br /&gt;
   case stateEND:&lt;br /&gt;
      i = 0;&lt;br /&gt;
      break;&lt;br /&gt;
   case stateSENSOR:&lt;br /&gt;
      i = 2;&lt;br /&gt;
      break;&lt;br /&gt;
   default:&lt;br /&gt;
      i = 3;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/font&amp;gt;&lt;br /&gt;
&amp;lt;BR&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As seen by the previous figure, the &amp;lt;b&amp;gt;switch/case&amp;lt;/b&amp;gt; statements look elegant, and are easier to read compared to &amp;lt;b&amp;gt;if/else&amp;lt;/b&amp;gt; statements had they been used instead. The variable inside the round brackets &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;(currentState)&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt; is what is being compared to the other cases. Each statement has a colon (:) at the end. Also, each case ends with a break. Forgetting to include the break will force the code to go to the next state without comparison. For example, if &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;currentState&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt; was equal to &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;stateSTART&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt; and there was no &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;break&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt; statement at the end of the &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;stateSTART&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt; case, the code would continue without &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;currentState&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt; comparison with &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;stateEND&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt; and execute the code inside the &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;stateEND&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt; case as well. This may appear to be a programming flaw, however, the switch/case statements were designed this way and some complex designs omit the break statements on purpose.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;default&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt; case should be the last case, it is for the case if the variable &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;currentState&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt; does not match any of the case statements, and this case does not have the &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;break statement&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt;. The default case should be used to indicate an error state or unexpected state fault.  &lt;br /&gt;
&lt;br /&gt;
==Static Declaration of Variables==&lt;br /&gt;
&lt;br /&gt;
So far, you have learned how to locally declare variables inside a function, but sometimes, you want to declare the local variables as static so their values are preserved across multiple calls to the same function.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;BR&amp;gt;&lt;br /&gt;
&amp;lt;font size = 3&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
void foo()&lt;br /&gt;
{&lt;br /&gt;
   int i = 0;&lt;br /&gt;
   i++;&lt;br /&gt;
   printf(&amp;quot;Value of i = %i\n&amp;quot;, i);&lt;br /&gt;
}&lt;br /&gt;
int main(void)&lt;br /&gt;
{&lt;br /&gt;
   foo();  // Prints: Value of i = 1&lt;br /&gt;
   foo();  // Prints: Value of i = 1&lt;br /&gt;
   foo();  // Prints: Value of i = 1&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/font&amp;gt;&lt;br /&gt;
&amp;lt;BR&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the code above, whenever &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;foo()&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt; is called, it declares a new variable called &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt; and sets it to 0. Then it increments by 1 and prints it out. Whenever &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt; calls it, &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;foo()&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt; does the same thing, and each time it sets &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt; as 1 and when it exits, the variable &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt; goes out of scope.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;BR&amp;gt;&lt;br /&gt;
&amp;lt;font size = 3&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
void foo()&lt;br /&gt;
{&lt;br /&gt;
   static int i = 0;&lt;br /&gt;
   i++;&lt;br /&gt;
   printf(&amp;quot;Value of i = %i\n&amp;quot;, i);&lt;br /&gt;
}&lt;br /&gt;
int main(void)&lt;br /&gt;
{&lt;br /&gt;
   foo();  // Prints: Value of i = 1&lt;br /&gt;
   foo();  // Prints: Value of i = 1&lt;br /&gt;
   foo();  // Prints: Value of i = 1&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/font&amp;gt;&lt;br /&gt;
&amp;lt;BR&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the code above, the result is different because the variable &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt; is declared as &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;static int&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt;. It should be observed that the variable &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt; is only declared once, and the function &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;foo()&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt; preserves the value of &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt; over repeated function calls. For this assignment, if your state machine is enclosed in a function called &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;stateMachine()&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt;, the please decide which variables should preserve their value, and declare them as static. Declaring a static variable is a better programming practice than declaring a global variable which would be visible to the entire program.&lt;br /&gt;
&lt;br /&gt;
==Assignment== &lt;br /&gt;
Build the state machine given in the next figure. Enclose your state machine inside a function and call the function every 100ms from your &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt; function. You will notice that the SENSOR state goes to IDLE state upon Button#1, and using the same button, the IDLE state transitions to SENSOR state. First, answer the question in the &amp;quot;Questions&amp;quot; section of this document. &lt;br /&gt;
&lt;br /&gt;
[[File:stateMachine10.png|600px|center|State Machine Design]]&lt;br /&gt;
&lt;br /&gt;
Whenever the state is changed, print on the screen what state the system is in. The statement should only print once - not every time the state machine function is called.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
==Questions==&lt;br /&gt;
# Describe the behavior you see if you press and hold Button#1 for longer than 100ms in the SENSOR state.&lt;/div&gt;</summary>
		<author><name>Charley</name></author>	</entry>

	<entry>
		<id>http://socialledge.com/sjsu/index.php?title=ES101_-_Lesson_11_:_State_Machine_and_Board_IO&amp;diff=1797</id>
		<title>ES101 - Lesson 11 : State Machine and Board IO</title>
		<link rel="alternate" type="text/html" href="http://socialledge.com/sjsu/index.php?title=ES101_-_Lesson_11_:_State_Machine_and_Board_IO&amp;diff=1797"/>
				<updated>2012-11-02T19:18:02Z</updated>
		
		<summary type="html">&lt;p&gt;Charley: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Objective ==&lt;br /&gt;
The objective of this lab is to learn how to design a state machine.&lt;br /&gt;
&lt;br /&gt;
== State Machine Design ==&lt;br /&gt;
State machine design can be easily constructed by considering &amp;lt;i&amp;gt;what&amp;lt;/i&amp;gt; you want to do based on a certain system state. For example, you might want consider building a state machine for an obstacle-avoidance robot, which acts differently based on if it detects an obstacle or not. In other words, you might have a state to avoid an obstacle, and another state to follow a light-source if there is an obstacle in the way. This helps separate the functionality of what should happen when an obstacle is in the way, and what should happen if an obstacle is not in the way.&lt;br /&gt;
&lt;br /&gt;
Each state should contain meaningful code that should be executed in that state. Furthermore, state transition code should be put in place that provides a way out of the current state to the next state/&lt;br /&gt;
&lt;br /&gt;
&amp;lt;BR/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== ENUM ==&lt;br /&gt;
The &amp;lt;b&amp;gt;enum&amp;lt;/b&amp;gt; provides a way to assign names to a sequence of integer values. For example, if a state machine contains two unique states, you should use an &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;enum&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt; to declare the names, and the compiler guarantees that the names have a unique identifier. The alternative is to declare separate variables set to a different value, but this approach uses more unnecessary memory and is error-prone.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;font size = 3&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
// Auto-set the identifiers of the variables&lt;br /&gt;
enum{stateSTART, stateEND, stateSENSOR};&lt;br /&gt;
&lt;br /&gt;
// Manually set the identifiers of the variables&lt;br /&gt;
enum{stateSTART = 1, stateEND = 2, stateSENSOR = 3};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/font size&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The enumerator variables can be automatically assigned a value, or the value can be manually chosen. In either case, unique values should be set for enumerator variables. An enumerator does not have to be used just for a state machine design, but it can be used anytime when you need to simply set unique identifiers to certain names.&lt;br /&gt;
&lt;br /&gt;
==Switch Statement==&lt;br /&gt;
&lt;br /&gt;
The C/C++ programming languages feature &amp;lt;b&amp;gt;switch/case&amp;lt;/b&amp;gt; statements. This is similar to using a series of &amp;lt;b&amp;gt;if/else&amp;lt;/b&amp;gt; statements. However, it is easier to read and write code using &amp;lt;b&amp;gt;switch/case&amp;lt;/b&amp;gt; statements in some situations such as for a state machine design.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;BR&amp;gt;&lt;br /&gt;
&amp;lt;font size = 3&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
// Test the &amp;quot;currentState&amp;quot; variable with one of the cases given below&lt;br /&gt;
switch(currentState)&lt;br /&gt;
{  &lt;br /&gt;
   case stateSTART:&lt;br /&gt;
      i = 0;&lt;br /&gt;
      break;&lt;br /&gt;
   case stateEND:&lt;br /&gt;
      i = 0;&lt;br /&gt;
      break;&lt;br /&gt;
   case stateSENSOR:&lt;br /&gt;
      i = 2;&lt;br /&gt;
      break;&lt;br /&gt;
   default:&lt;br /&gt;
      i = 3;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/font&amp;gt;&lt;br /&gt;
&amp;lt;BR&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As seen by the previous figure, the &amp;lt;b&amp;gt;switch/case&amp;lt;/b&amp;gt; statements look elegant, and are easier to read compared to &amp;lt;b&amp;gt;if/else&amp;lt;/b&amp;gt; statements had they been used instead. The variable inside the round brackets &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;(currentState)&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt; is what is being compared to the other cases. Each statement has a colon (:) at the end. Also, each case ends with a break. Forgetting to include the break will force the code to go to the next state without comparison. For example, if &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;currentState&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt; was equal to &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;stateSTART&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt; and there was no &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;break&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt; statement at the end of the &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;stateSTART&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt; case, the code would continue without &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;currentState&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt; comparison with &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;stateEND&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt; and execute the code inside the &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;stateEND&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt; case as well. This may appear to be a programming flaw, however, the switch/case statements were designed this way and some complex designs omit the break statements on purpose.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;default&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt; case should be the last case, it is for the case if the variable &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;currentState&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt; does not match any of the case statements, and this case does not have the &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;break statement&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt;. The default case should be used to indicate an error state or unexpected state fault.  &lt;br /&gt;
&lt;br /&gt;
==Static Declaration of Variables==&lt;br /&gt;
&lt;br /&gt;
So far, you have learned how to locally declare variables inside a function, but sometimes, you want to declare the local variables as static so their values are preserved across multiple calls to the same function.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;BR&amp;gt;&lt;br /&gt;
&amp;lt;font size = 3&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
void foo()&lt;br /&gt;
{&lt;br /&gt;
   int i = 0;&lt;br /&gt;
   i++;&lt;br /&gt;
   printf(&amp;quot;Value of i = %i\n&amp;quot;, i);&lt;br /&gt;
}&lt;br /&gt;
int main(void)&lt;br /&gt;
{&lt;br /&gt;
   foo();  // Prints: Value of i = 1&lt;br /&gt;
   foo();  // Prints: Value of i = 1&lt;br /&gt;
   foo();  // Prints: Value of i = 1&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/font&amp;gt;&lt;br /&gt;
&amp;lt;BR&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the code above, whenever &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;foo()&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt; is called, it declares a new variable called &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt; and sets it to 0. Then it increments by 1 and prints it out. Whenever &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt; calls it, &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;foo()&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt; does the same thing, and each time it sets &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt; as 1 and when it exits, the variable &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt; goes out of scope.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;BR&amp;gt;&lt;br /&gt;
&amp;lt;font size = 3&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
void foo()&lt;br /&gt;
{&lt;br /&gt;
   static int i = 0;&lt;br /&gt;
   i++;&lt;br /&gt;
   printf(&amp;quot;Value of i = %i\n&amp;quot;, i);&lt;br /&gt;
}&lt;br /&gt;
int main(void)&lt;br /&gt;
{&lt;br /&gt;
   foo();  // Prints: Value of i = 1&lt;br /&gt;
   foo();  // Prints: Value of i = 1&lt;br /&gt;
   foo();  // Prints: Value of i = 1&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/font&amp;gt;&lt;br /&gt;
&amp;lt;BR&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the code above, the result is different because the variable &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt; is declared as &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;static int&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt;. It should be observed that the variable &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt; is only declared once, and the function &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;foo()&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt; preserves the value of &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt; over repeated function calls. For this assignment, if your state machine is enclosed in a function called &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;stateMachine()&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt;, the please decide which variables should preserve their value, and declare them as static. Declaring a static variable is a better programming practice than declaring a global variable which would be visible to the entire program.&lt;br /&gt;
&lt;br /&gt;
==Assignment== &lt;br /&gt;
Build the state machine given in the next figure. Enclose your state machine inside a function and call the function every 100ms from your &amp;lt;font size = 3&amp;gt;&amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt;&amp;lt;/font&amp;gt; function. You will notice that the SENSOR state goes to IDLE state upon Button#1, and using the same button, the IDLE state transitions to SENSOR state. First, answer the question in the &amp;quot;Questions&amp;quot; section of this document. &lt;br /&gt;
&lt;br /&gt;
[[File:stateMachine10.png|600px|center|State Machine Design]]&lt;br /&gt;
&lt;br /&gt;
Whenever the state is changed, print on the screen what state the system is in. The statement should only print once - not every time the state machine function is called.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
==Questions==&lt;br /&gt;
# Describe the behavior you see if you press and hold Button#1 for longer than 100ms in the SENSOR state.&lt;/div&gt;</summary>
		<author><name>Charley</name></author>	</entry>

	<entry>
		<id>http://socialledge.com/sjsu/index.php?title=ES101_-_Lesson_11_:_State_Machine_and_Board_IO&amp;diff=1796</id>
		<title>ES101 - Lesson 11 : State Machine and Board IO</title>
		<link rel="alternate" type="text/html" href="http://socialledge.com/sjsu/index.php?title=ES101_-_Lesson_11_:_State_Machine_and_Board_IO&amp;diff=1796"/>
				<updated>2012-11-02T19:01:11Z</updated>
		
		<summary type="html">&lt;p&gt;Charley: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Objective ==&lt;br /&gt;
The objective of this lab is to learn how to design a state machine.&lt;br /&gt;
&lt;br /&gt;
== State Machine Design ==&lt;br /&gt;
State machine design can be easily constructed by considering &amp;lt;i&amp;gt;what&amp;lt;/i&amp;gt; you want to do based on a certain system state. For example, you might want consider building a state machine for an obstacle-avoidance robot, which acts differently based on if it detects an obstacle or not. In other words, you might have a state to avoid an obstacle, and another state to follow a light-source if there is an obstacle in the way. This helps separate the functionality of what should happen when an obstacle is in the way, and what should happen if an obstacle is not in the way.&lt;br /&gt;
&lt;br /&gt;
Each state should contain meaningful code that should be executed in that state. Furthermore, state transition code should be put in place that provides a way out of the current state to the next state/&lt;br /&gt;
&lt;br /&gt;
&amp;lt;BR/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== ENUM ==&lt;br /&gt;
The &amp;lt;b&amp;gt;enum&amp;lt;/b&amp;gt; provides a way to assign names to a sequence of integer values. For example, if a state machine contains two unique states, you should use an &amp;lt;code&amp;gt;enum&amp;lt;/code&amp;gt; to declare the names, and the compiler guarantees that the names have a unique identifier. The alternative is to declare separate variables set to a different value, but this approach uses more unnecessary memory and is error-prone.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
// Auto-set the identifiers of the variables&lt;br /&gt;
enum{stateSTART, stateEND, stateSENSOR};&lt;br /&gt;
&lt;br /&gt;
// Manually set the identifiers of the variables&lt;br /&gt;
enum{stateSTART = 1, stateEND = 2, stateSENSOR = 3};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The enumerator variables can be automatically assigned a value, or the value can be manually chosen. In either case, unique values should be set for enumerator variables. An enumerator does not have to be used just for a state machine design, but it can be used anytime when you need to simply set unique identifiers to certain names.&lt;br /&gt;
&lt;br /&gt;
==Switch Statement==&lt;br /&gt;
&lt;br /&gt;
The C/C++ programming languages feature &amp;lt;b&amp;gt;switch/case&amp;lt;/b&amp;gt; statements. This is similar to using a series of &amp;lt;b&amp;gt;if/else&amp;lt;/b&amp;gt; statements. However, it is easier to read and write code using &amp;lt;b&amp;gt;switch/case&amp;lt;/b&amp;gt; statements in some situations such as for a state machine design.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
// Test the &amp;quot;currentState&amp;quot; variable with one of the cases given below&lt;br /&gt;
switch(currentState)&lt;br /&gt;
{  &lt;br /&gt;
   case stateSTART:&lt;br /&gt;
      i = 0;&lt;br /&gt;
      break;&lt;br /&gt;
   case stateEND:&lt;br /&gt;
      i = 0;&lt;br /&gt;
      break;&lt;br /&gt;
   case stateSENSOR:&lt;br /&gt;
      i = 2;&lt;br /&gt;
      break;&lt;br /&gt;
   default:&lt;br /&gt;
      i = 3;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As seen by the previous figure, the &amp;lt;b&amp;gt;switch/case&amp;lt;/b&amp;gt; statements loog elegant, and are easier to read compared to &amp;lt;b&amp;gt;if/else&amp;lt;/b&amp;gt; statements had they been used instead. The variable inside the round brackets &amp;lt;code&amp;gt;(currentState)&amp;lt;/code&amp;gt; is what is being compared to the other cases. Each statement has a colon (:) at the end. Also, each case ends with a break. Forgetting to include the break will force the code to go to the next state without comparison. For example, if &amp;lt;code&amp;gt;currentState&amp;lt;/code&amp;gt; was equal to &amp;lt;code&amp;gt;stateSTART&amp;lt;/code&amp;gt; and there was no &amp;lt;code&amp;gt;break&amp;lt;/code&amp;gt; statement at the end of the &amp;lt;code&amp;gt;stateSTART&amp;lt;/code&amp;gt; case, the code would continue without &amp;lt;code&amp;gt;currentState&amp;lt;/code&amp;gt; comparison with &amp;lt;code&amp;gt;stateEND&amp;lt;/code&amp;gt; and execute the code inside the &amp;lt;code&amp;gt;stateEND&amp;lt;/code&amp;gt; case as well. This may appear to be a programming flaw, however, the switch/case statements were designed this way and some complex designs omit the break statements on purpose.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;default&amp;lt;/code&amp;gt; case should be the last case, it is for the case if the variable &amp;lt;code&amp;gt;currentState&amp;lt;/code&amp;gt; does not match any of the case statements, and this case does not have the &amp;lt;code&amp;gt;break statement&amp;lt;/code&amp;gt;. The default case should be used to indicate an error state or unexpected state fault.  &lt;br /&gt;
&lt;br /&gt;
==Static Declaration of Variables==&lt;br /&gt;
&lt;br /&gt;
So far, you have learned how to locally declare variables inside a function, but sometimes, you want to declare the local variables as static so their values are preserved across multiple calls to the same function.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
void foo()&lt;br /&gt;
{&lt;br /&gt;
   int i = 0;&lt;br /&gt;
   i++;&lt;br /&gt;
   printf(&amp;quot;Value of i = %i\n&amp;quot;, i);&lt;br /&gt;
}&lt;br /&gt;
int main(void)&lt;br /&gt;
{&lt;br /&gt;
   foo();  // Prints: Value of i = 1&lt;br /&gt;
   foo();  // Prints: Value of i = 1&lt;br /&gt;
   foo();  // Prints: Value of i = 1&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the code above, whenever &amp;lt;code&amp;gt;foo()&amp;lt;/code&amp;gt; is called, it declares a new variable called &amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt; and sets it to 0. Then it increments by 1 and prints it out. Whenever &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; calls it, &amp;lt;code&amp;gt;foo()&amp;lt;/code&amp;gt; does the same thing, and each time it sets &amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt; as 1 and when it exits, the variable &amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt; goes out of scope.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
void foo()&lt;br /&gt;
{&lt;br /&gt;
   static int i = 0;&lt;br /&gt;
   i++;&lt;br /&gt;
   printf(&amp;quot;Value of i = %i\n&amp;quot;, i);&lt;br /&gt;
}&lt;br /&gt;
int main(void)&lt;br /&gt;
{&lt;br /&gt;
   foo();  // Prints: Value of i = 1&lt;br /&gt;
   foo();  // Prints: Value of i = 1&lt;br /&gt;
   foo();  // Prints: Value of i = 1&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the code above, the result is different because the variable &amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt; is declared as &amp;lt;code&amp;gt;static int&amp;lt;/code&amp;gt;. It should be observed that the variable &amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt; is only declared once, and the function &amp;lt;code&amp;gt;foo()&amp;lt;/code&amp;gt; preserves the value of &amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt; over repeated function calls. For this assignment, if your state machine is enclosed in a function called &amp;lt;code&amp;gt;stateMachine()&amp;lt;/code&amp;gt;, the please decide which variables should preserve their value, and declare them as static. Declaring a static variable is a better programming practice than declaring a global variable which would be visible to the entire program.&lt;br /&gt;
&lt;br /&gt;
==Assignment== &lt;br /&gt;
Build the state machine given in the next figure. Enclose your state machine inside a function and call the function every 100ms from your &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; function. You will notice that the SENSOR state goes to IDLE state upon Button#1, and using the same button, the IDLE state transitions to SENSOR state. First, answer the question in the &amp;quot;Questions&amp;quot; section of this document. &lt;br /&gt;
&lt;br /&gt;
[[File:stateMachine10.png|600px|center|State Machine Design]]&lt;br /&gt;
&lt;br /&gt;
Whenever the state is changed, print on the screen what state the system is in. The statement should only print once - not every time the state machine function is called.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
==Questions==&lt;br /&gt;
# Describe the behavior you see if you press and hold Button#1 for longer than 100ms in the SENSOR state.&lt;/div&gt;</summary>
		<author><name>Charley</name></author>	</entry>

	<entry>
		<id>http://socialledge.com/sjsu/index.php?title=File:StateMachine10.png&amp;diff=1795</id>
		<title>File:StateMachine10.png</title>
		<link rel="alternate" type="text/html" href="http://socialledge.com/sjsu/index.php?title=File:StateMachine10.png&amp;diff=1795"/>
				<updated>2012-11-02T18:57:31Z</updated>
		
		<summary type="html">&lt;p&gt;Charley: State Machine Design&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;State Machine Design&lt;/div&gt;</summary>
		<author><name>Charley</name></author>	</entry>

	<entry>
		<id>http://socialledge.com/sjsu/index.php?title=ES101_-_Lesson_11_:_State_Machine_and_Board_IO&amp;diff=1794</id>
		<title>ES101 - Lesson 11 : State Machine and Board IO</title>
		<link rel="alternate" type="text/html" href="http://socialledge.com/sjsu/index.php?title=ES101_-_Lesson_11_:_State_Machine_and_Board_IO&amp;diff=1794"/>
				<updated>2012-11-02T18:44:18Z</updated>
		
		<summary type="html">&lt;p&gt;Charley: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Objective ==&lt;br /&gt;
The objective of this lab is to learn how to design a state machine.&lt;br /&gt;
&lt;br /&gt;
== State Machine Design ==&lt;br /&gt;
State machine design can be easily constructed by considering &amp;lt;i&amp;gt;what&amp;lt;/i&amp;gt; you want to do based on a certain system state. For example, you might want consider building a state machine for an obstacle-avoidance robot, which acts differently based on if it detects an obstacle or not. In other words, you might have a state to avoid an obstacle, and another state to follow a light-source if there is an obstacle in the way. This helps separate the functionality of what should happen when an obstacle is in the way, and what should happen if an obstacle is not in the way.&lt;br /&gt;
&lt;br /&gt;
Each state should contain meaningful code that should be executed in that state. Furthermore, state transition code should be put in place that provides a way out of the current state to the next state/&lt;br /&gt;
&lt;br /&gt;
&amp;lt;BR/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== ENUM ==&lt;br /&gt;
The &amp;lt;b&amp;gt;enum&amp;lt;/b&amp;gt; provides a way to assign names to a sequence of integer values. For example, if a state machine contains two unique states, you should use an &amp;lt;code&amp;gt;enum&amp;lt;/code&amp;gt; to declare the names, and the compiler guarantees that the names have a unique identifier. The alternative is to declare separate variables set to a different value, but this approach uses more unnecessary memory and is error-prone.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
// Auto-set the identifiers of the variables&lt;br /&gt;
enum{stateSTART, stateEND, stateSENSOR};&lt;br /&gt;
&lt;br /&gt;
// Manually set the identifiers of the variables&lt;br /&gt;
enum{stateSTART = 1, stateEND = 2, stateSENSOR = 3};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The enumerator variables can be automatically assigned a value, or the value can be manually chosen. In either case, unique values should be set for enumerator variables. An enumerator does not have to be used just for a state machine design, but it can be used anytime when you need to simply set unique identifiers to certain names.&lt;br /&gt;
&lt;br /&gt;
==Switch Statement==&lt;br /&gt;
&lt;br /&gt;
The C/C++ programming languages feature &amp;lt;b&amp;gt;switch/case&amp;lt;/b&amp;gt; statements. This is similar to using a series of &amp;lt;b&amp;gt;if/else&amp;lt;/b&amp;gt; statements. However, it is easier to read and write code using &amp;lt;b&amp;gt;switch/case&amp;lt;/b&amp;gt; statements in some situations such as for a state machine design.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
// Test the &amp;quot;currentState&amp;quot; variable with one of the cases given below&lt;br /&gt;
switch(currentState)&lt;br /&gt;
{  &lt;br /&gt;
   case stateSTART:&lt;br /&gt;
      i = 0;&lt;br /&gt;
      break;&lt;br /&gt;
   case stateEND:&lt;br /&gt;
      i = 0;&lt;br /&gt;
      break;&lt;br /&gt;
   case stateSENSOR:&lt;br /&gt;
      i = 2;&lt;br /&gt;
      break;&lt;br /&gt;
   default:&lt;br /&gt;
      i = 3;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As seen by the previous figure, the &amp;lt;b&amp;gt;switch/case&amp;lt;/b&amp;gt; statements loog elegant, and are easier to read compared to &amp;lt;b&amp;gt;if/else&amp;lt;/b&amp;gt; statements had they been used instead. The variable inside the round brackets &amp;lt;code&amp;gt;(currentState)&amp;lt;/code&amp;gt; is what is being compared to the other cases. Each statement has a colon (:) at the end. Also, each case ends with a break. Forgetting to include the break will force the code to go to the next state without comparison. For example, if &amp;lt;code&amp;gt;currentState&amp;lt;/code&amp;gt; was equal to &amp;lt;code&amp;gt;stateSTART&amp;lt;/code&amp;gt; and there was no &amp;lt;code&amp;gt;break&amp;lt;/code&amp;gt; statement at the end of the &amp;lt;code&amp;gt;stateSTART&amp;lt;/code&amp;gt; case, the code would continue without &amp;lt;code&amp;gt;currentState&amp;lt;/code&amp;gt; comparison with &amp;lt;code&amp;gt;stateEND&amp;lt;/code&amp;gt; and execute the code inside the &amp;lt;code&amp;gt;stateEND&amp;lt;/code&amp;gt; case as well. This may appear to be a programming flaw, however, the switch/case statements were designed this way and some complex designs omit the break statements on purpose.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;default&amp;lt;/code&amp;gt; case should be the last case, it is for the case if the variable &amp;lt;code&amp;gt;currentState&amp;lt;/code&amp;gt; does not match any of the case statements, and this case does not have the &amp;lt;code&amp;gt;break statement&amp;lt;/code&amp;gt;. The default case should be used to indicate an error state or unexpected state fault.  &lt;br /&gt;
&lt;br /&gt;
==Static Declaration of Variables==&lt;br /&gt;
&lt;br /&gt;
So far, you have learned how to locally declare variables inside a function, but sometimes, you want to declare the local variables as static so their values are preserved across multiple calls to the same function.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
void foo()&lt;br /&gt;
{&lt;br /&gt;
   int i = 0;&lt;br /&gt;
   i++;&lt;br /&gt;
   printf(&amp;quot;Value of i = %i\n&amp;quot;, i);&lt;br /&gt;
}&lt;br /&gt;
int main(void)&lt;br /&gt;
{&lt;br /&gt;
   foo();  // Prints: Value of i = 1&lt;br /&gt;
   foo();  // Prints: Value of i = 1&lt;br /&gt;
   foo();  // Prints: Value of i = 1&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the code above, whenever &amp;lt;code&amp;gt;foo()&amp;lt;/code&amp;gt; is called, it declares a new variable called &amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt; and sets it to 0. Then it increments by 1 and prints it out. Whenever &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; calls it, &amp;lt;code&amp;gt;foo()&amp;lt;/code&amp;gt; does the same thing, and each time it sets &amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt; as 1 and when it exits, the variable &amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt; goes out of scope.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
void foo()&lt;br /&gt;
{&lt;br /&gt;
   static int i = 0;&lt;br /&gt;
   i++;&lt;br /&gt;
   printf(&amp;quot;Value of i = %i\n&amp;quot;, i);&lt;br /&gt;
}&lt;br /&gt;
int main(void)&lt;br /&gt;
{&lt;br /&gt;
   foo();  // Prints: Value of i = 1&lt;br /&gt;
   foo();  // Prints: Value of i = 1&lt;br /&gt;
   foo();  // Prints: Value of i = 1&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the code above, the result is different because the variable &amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt; is declared as &amp;lt;code&amp;gt;static int&amp;lt;/code&amp;gt;. It should be observed that the variable &amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt; is only declared once, and the function &amp;lt;code&amp;gt;foo()&amp;lt;/code&amp;gt; preserves the value of &amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt; over repeated function calls. For this assignment, if your state machine is enclosed in a function called &amp;lt;code&amp;gt;stateMachine()&amp;lt;/code&amp;gt;, the please decide which variables should preserve their value, and declare them as static. Declaring a static variable is a better programming practice than declaring a global variable which would be visible to the entire program.&lt;br /&gt;
&lt;br /&gt;
==Assignment== &lt;br /&gt;
Build the state machine given in the next figure. Enclose your state machine inside a function and call the function every 100ms from your &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; function. You will notice that the SENSOR state goes to IDLE state upon Button#1, and using the same button, the IDLE state transitions to SENSOR state. First, answer the question in the &amp;quot;Questions&amp;quot; section of this document. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Add image here&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Whenever the state is changed, print on the screen what state the system is in. The statement should only print once - not every time the state machine function is called.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
==Questions==&lt;br /&gt;
# Describe the behavior you see if you press and hold Button#1 for longer than 100ms in the SENSOR state.&lt;/div&gt;</summary>
		<author><name>Charley</name></author>	</entry>

	<entry>
		<id>http://socialledge.com/sjsu/index.php?title=ES101_-_Lesson_11_:_State_Machine_and_Board_IO&amp;diff=1793</id>
		<title>ES101 - Lesson 11 : State Machine and Board IO</title>
		<link rel="alternate" type="text/html" href="http://socialledge.com/sjsu/index.php?title=ES101_-_Lesson_11_:_State_Machine_and_Board_IO&amp;diff=1793"/>
				<updated>2012-11-02T18:33:55Z</updated>
		
		<summary type="html">&lt;p&gt;Charley: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Objective ==&lt;br /&gt;
The objective of this lab is to learn how to design a state machine.&lt;br /&gt;
&lt;br /&gt;
== State Machine Design ==&lt;br /&gt;
State machine design can be easily constructed by considering &amp;lt;i&amp;gt;what&amp;lt;/i&amp;gt; you want to do based on a certain system state. For example, you might want consider building a state machine for an obstacle-avoidance robot, which acts differently based on if it detects an obstacle or not. In other words, you might have a state to avoid an obstacle, and another state to follow a light-source if there is an obstacle in the way. This helps separate the functionality of what should happen when an obstacle is in the way, and what should happen if an obstacle is not in the way.&lt;br /&gt;
&lt;br /&gt;
Each state should contain meaningful code that should be executed in that state. Furthermore, state transition code should be put in place that provides a way out of the current state to the next state/&lt;br /&gt;
&lt;br /&gt;
&amp;lt;BR/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== ENUM ==&lt;br /&gt;
The &amp;lt;b&amp;gt;enum&amp;lt;/b&amp;gt; provides a way to assign names to a sequence of integer values. For example, if a state machine contains two unique states, you should use an &amp;lt;code&amp;gt;enum&amp;lt;/code&amp;gt; to declare the names, and the compiler guarantees that the names have a unique identifier. The alternative is to declare separate variables set to a different value, but this approach uses more unnecessary memory and is error-prone.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
// Auto-set the identifiers of the variables&lt;br /&gt;
enum{stateSTART, stateEND, stateSENSOR};&lt;br /&gt;
&lt;br /&gt;
// Manually set the identifiers of the variables&lt;br /&gt;
enum{stateSTART = 1, stateEND = 2, stateSENSOR = 3};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The enumerator variables can be automatically assigned a value, or the value can be manually chosen. In either case, unique values should be set for enumerator variables. An enumerator does not have to be used just for a state machine design, but it can be used anytime when you need to simply set unique identifiers to certain names.&lt;br /&gt;
&lt;br /&gt;
==Switch Statement==&lt;br /&gt;
&lt;br /&gt;
The C/C++ programming languages feature &amp;lt;b&amp;gt;switch/case&amp;lt;/b&amp;gt; statements. This is similar to using a series of &amp;lt;b&amp;gt;if/else&amp;lt;/b&amp;gt; statements. However, it is easier to read and write code using &amp;lt;b&amp;gt;switch/case&amp;lt;/b&amp;gt; statements in some situations such as for a state machine design.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
// Test the &amp;quot;currentState&amp;quot; variable with one of the cases given below&lt;br /&gt;
switch(currentState)&lt;br /&gt;
{  &lt;br /&gt;
   case stateSTART:&lt;br /&gt;
      i = 0;&lt;br /&gt;
      break;&lt;br /&gt;
   case stateEND:&lt;br /&gt;
      i = 0;&lt;br /&gt;
      break;&lt;br /&gt;
   case stateSENSOR:&lt;br /&gt;
      i = 2;&lt;br /&gt;
      break;&lt;br /&gt;
   default:&lt;br /&gt;
      i = 3;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As seen by the previous figure, the &amp;lt;b&amp;gt;switch/case&amp;lt;/b&amp;gt; statements loog elegant, and are easier to read compared to &amp;lt;b&amp;gt;if/else&amp;lt;/b&amp;gt; statements had they been used instead. The variable inside the round brackets &amp;lt;code&amp;gt;(currentState)&amp;lt;/code&amp;gt; is what is being compared to the other cases. Each statement has a colon (:) at the end. Also, each case ends with a break. Forgetting to include the break will force the code to go to the next state without comparison. For example, if &amp;lt;code&amp;gt;currentState&amp;lt;/code&amp;gt; was equal to &amp;lt;code&amp;gt;stateSTART&amp;lt;/code&amp;gt; and there was no &amp;lt;code&amp;gt;break&amp;lt;/code&amp;gt; statement at the end of the &amp;lt;code&amp;gt;stateSTART&amp;lt;/code&amp;gt; case, the code would continue without currentState comparison with stateEND and execute the code inside the stateEND case as well. This may appear to be a programming flaw, however, the switch/case statements were deisigned this way and some complex designs omit the break statements on purpose.&lt;br /&gt;
&lt;br /&gt;
The default case should be the last case, it is for the case if the variable currentState does not match any of the case statements, and this case does not have the break statement. The default case should be used to indicate an error state or unexpected state fault.  &lt;br /&gt;
&lt;br /&gt;
==Static Declaration of Variables==&lt;br /&gt;
&lt;br /&gt;
So far, you have learned how to locally declare variables inside a function, but sometimes, you want to declare the local variables as static so their values are preserved across multiple calls to the same function.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
void foo()&lt;br /&gt;
{&lt;br /&gt;
   int i = 0;&lt;br /&gt;
   i++;&lt;br /&gt;
   printf(&amp;quot;Value of i = %i\n&amp;quot;, i);&lt;br /&gt;
}&lt;br /&gt;
int main(void)&lt;br /&gt;
{&lt;br /&gt;
   foo();  // Prints: Value of i = 1&lt;br /&gt;
   foo();  // Prints: Value of i = 1&lt;br /&gt;
   foo();  // Prints: Value of i = 1&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Assignment== &lt;br /&gt;
Build the state machine given in the next figure. Enclose your state machine inside a function and call the function every 100ms from your &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; function. You will notice that the SENSOR state goes to IDLE state upon Button#1, and using the same button, the IDLE state transitions to SENSOR state. First, answer the question in the &amp;quot;Questions&amp;quot; section of this document. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Add image here&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Whenever the state is changed, print on the screen what state the system is in. The statement should only print once - not every time the state machine function is called.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
==Questions==&lt;br /&gt;
# Describe the behavior you see if you press and hold Button#1 for longer than 100ms in the SENSOR state.&lt;/div&gt;</summary>
		<author><name>Charley</name></author>	</entry>

	<entry>
		<id>http://socialledge.com/sjsu/index.php?title=ES101_-_Lesson_11_:_State_Machine_and_Board_IO&amp;diff=1773</id>
		<title>ES101 - Lesson 11 : State Machine and Board IO</title>
		<link rel="alternate" type="text/html" href="http://socialledge.com/sjsu/index.php?title=ES101_-_Lesson_11_:_State_Machine_and_Board_IO&amp;diff=1773"/>
				<updated>2012-11-02T02:21:27Z</updated>
		
		<summary type="html">&lt;p&gt;Charley: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Objective ==&lt;br /&gt;
The objective of this lab is to learn how to design a state machine.&lt;br /&gt;
&lt;br /&gt;
== State Machine Design ==&lt;br /&gt;
State machine design can be easily constructed by considering &amp;lt;i&amp;gt;what&amp;lt;/i&amp;gt; you want to do based on a certain system state. For example, you might want consider building a state machine for an obstacle-avoidance robot, which acts differently based on if it detects an obstacle or not. In other words, you might have a state to avoid an obstacle, and another state to follow a light-source if there is an obstacle in the way. This helps separate the functionality of what should happen when an obstacle is in the way, and what should happen if an obstacle is not in the way.&lt;br /&gt;
&lt;br /&gt;
Each state should contain meaningful code that should be executed in that state. Furthermore, state transition code should be put in place that provides a way out of the current state to the next state/&lt;br /&gt;
&lt;br /&gt;
&amp;lt;BR/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== ENUM ==&lt;br /&gt;
The &amp;lt;b&amp;gt;enum&amp;lt;/b&amp;gt; provides a way to assign names to a sequence of integer values. For example, if a state machine contains two unique states, you should use an &amp;lt;code&amp;gt;enum&amp;lt;/code&amp;gt; to declare the names, and the compiler guarantees that the names have a unique identifier. The alternative is to declare separate variables set to a different value, but this approach uses more unnecessary memory and is error-prone.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
// Auto-set the identifiers of the variables&lt;br /&gt;
enum{stateSTART, stateEND, stateSENSOR};&lt;br /&gt;
&lt;br /&gt;
// Manually set the identifiers of the variables&lt;br /&gt;
enum{stateSTART = 1, stateEND = 2, stateSENSOR = 3};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The enumerator variables can be automatically assigned a value, or the value can be manually chosen. In either case, unique values should be set for enumerator variables. An enumerator does not have to be used just for a state machine design, but it can be used anytime when you need to simply set unique identifiers to certain names.&lt;br /&gt;
&lt;br /&gt;
==Switch Statement==&lt;br /&gt;
&lt;br /&gt;
The C/C++ programming languages feature &amp;lt;b&amp;gt;switch/case&amp;lt;/b&amp;gt; statements. This is similar to using a series of &amp;lt;b&amp;gt;if/else&amp;lt;/b&amp;gt; statements. However, it is easier to read and write code using &amp;lt;b&amp;gt;switch/case&amp;lt;/b&amp;gt; statements in some situations such as for a state machine design.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
// Test the &amp;quot;currentState&amp;quot; variable with one of the cases given below&lt;br /&gt;
switch(currentState)&lt;br /&gt;
{  &lt;br /&gt;
   case stateSTART:&lt;br /&gt;
      i = 0;&lt;br /&gt;
      break;&lt;br /&gt;
   case stateEND:&lt;br /&gt;
      i = 0;&lt;br /&gt;
      break;&lt;br /&gt;
   case stateSENSOR:&lt;br /&gt;
      i = 2;&lt;br /&gt;
      break;&lt;br /&gt;
   default:&lt;br /&gt;
      i = 3;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As seen by the previous figure, the &amp;lt;b&amp;gt;switch/case&amp;lt;/b&amp;gt; statements loog elegant, and are easier to read compared to &amp;lt;b&amp;gt;if/else&amp;lt;/b&amp;gt; statements had they been used instead. The variable inside the round brackets &amp;lt;code&amp;gt;(currentState)&amp;lt;/code&amp;gt; is what is being compared to the other cases.&lt;br /&gt;
&lt;br /&gt;
==Static Declaration of Variables==&lt;br /&gt;
&lt;br /&gt;
==Assignment== &lt;br /&gt;
Build the state machine given in the next figure. Enclose your state machine inside a function and call the function every 100ms from your &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; function. You will notice that the SENSOR state goes to IDLE state upon Button#1, and using the same button, the IDLE state transitions to SENSOR state. First, answer the question in the &amp;quot;Questions&amp;quot; section of this document. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Add image here&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Whenever the state is changed, print on the screen what state the system is in. The statement should only print once - not every time the state machine function is called.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
==Questions==&lt;br /&gt;
# Describe the behavior you see if you press and hold Button#1 for longer than 100ms in the SENSOR state.&lt;/div&gt;</summary>
		<author><name>Charley</name></author>	</entry>

	<entry>
		<id>http://socialledge.com/sjsu/index.php?title=ES101_-_Lesson_11_:_State_Machine_and_Board_IO&amp;diff=1772</id>
		<title>ES101 - Lesson 11 : State Machine and Board IO</title>
		<link rel="alternate" type="text/html" href="http://socialledge.com/sjsu/index.php?title=ES101_-_Lesson_11_:_State_Machine_and_Board_IO&amp;diff=1772"/>
				<updated>2012-11-02T01:52:50Z</updated>
		
		<summary type="html">&lt;p&gt;Charley: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Objective ==&lt;br /&gt;
The objective of this lab is to learn how to design a state machine.&lt;br /&gt;
&lt;br /&gt;
== State Machine Design ==&lt;br /&gt;
State machine design can be easily constructed by considering &amp;lt;i&amp;gt;what&amp;lt;/i&amp;gt; you want to do based on a certain system state. For example, you might want consider building a state machine for an obstacle-avoidance robot, which acts differently based on if it detects an obstacle or not. In other words, you might have a state to avoid an obstacle, and another state to follow a light-source if there is an obstacle in the way. This helps separate the functionality of what should happen when an obstacle is in the way, and what should happen if an obstacle is not in the way.&lt;br /&gt;
&lt;br /&gt;
Each state should contain meaningful code that should be executed in that state. Furthermore, state transition code should be put in place that provides a way out of the current state to the next state/&lt;br /&gt;
&lt;br /&gt;
&amp;lt;BR/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== ENUM ==&lt;br /&gt;
The &amp;lt;b&amp;gt;enum&amp;lt;/b&amp;gt; provides a way to assign names to a sequence of integer values. For example, if a state machine contains two unique states, you should use an &amp;lt;code&amp;gt;enum&amp;lt;/code&amp;gt; to declare the names, and the compiler guarantees that the names have a unique identifier. The alternative is to declare separate variables set to a different value, but this approach uses more unnecessary memory and is error-prone.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
// Auto-set the identifiers of the variables&lt;br /&gt;
enum{stateSTART, stateEND, stateSENSOR};&lt;br /&gt;
&lt;br /&gt;
// Manually set the identifiers of the variables&lt;br /&gt;
enum{stateSTART = 1, stateEND = 2, stateSENSOR = 3};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The enumerator variables can be automatically assigned a value, or the value can be manually chosen. In either case, unique values should be set for enumerator variables. An enumerator does not have to be used just for a state machine design, but it can be used anytime when you need to simply set unique identifiers to certain names.&lt;br /&gt;
&lt;br /&gt;
==Switch Statement==&lt;br /&gt;
&lt;br /&gt;
The C/C++ programming languages feature &amp;lt;b&amp;gt;switch/case&amp;lt;/b&amp;gt; statements. This is similar to using a series of &amp;lt;b&amp;gt;if/else&amp;lt;/b&amp;gt; statements. However, it is easier to read and write code using &amp;lt;b&amp;gt;switch/case&amp;lt;/b&amp;gt; statements in some situations such as for a state machine design.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
// Test the &amp;quot;currentState&amp;quot; variable with one of the cases given below&lt;br /&gt;
switch(currentState)&lt;br /&gt;
{  &lt;br /&gt;
   case stateSTART:&lt;br /&gt;
      i = 0;&lt;br /&gt;
      break;&lt;br /&gt;
   case stateEND:&lt;br /&gt;
      i = 0;&lt;br /&gt;
      break;&lt;br /&gt;
   case stateSENSOR:&lt;br /&gt;
      i = 2;&lt;br /&gt;
      break;&lt;br /&gt;
   default:&lt;br /&gt;
      i = 3;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Static Declaration of Variables==&lt;br /&gt;
&lt;br /&gt;
==Assignment== &lt;br /&gt;
Build the state machine given in the next figure. Enclose your state machine inside a function and call the function every 100ms from your &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; function. You will notice that the SENSOR state goes to IDLE state upon Button#1, and using the same button, the IDLE state transitions to SENSOR state. First, answer the question in the &amp;quot;Questions&amp;quot; section of this document. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Add image here&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Whenever the state is changed, print on the screen what state the system is in. The statement should only print once - not every time the state machine function is called.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
==Questions==&lt;br /&gt;
# Describe the behavior you see if you press and hold Button#1 for longer than 100ms in the SENSOR state.&lt;/div&gt;</summary>
		<author><name>Charley</name></author>	</entry>

	<entry>
		<id>http://socialledge.com/sjsu/index.php?title=ES101_-_Lesson_11_:_State_Machine_and_Board_IO&amp;diff=1771</id>
		<title>ES101 - Lesson 11 : State Machine and Board IO</title>
		<link rel="alternate" type="text/html" href="http://socialledge.com/sjsu/index.php?title=ES101_-_Lesson_11_:_State_Machine_and_Board_IO&amp;diff=1771"/>
				<updated>2012-11-02T01:46:20Z</updated>
		
		<summary type="html">&lt;p&gt;Charley: Created page with &amp;quot;== Objective == The objective of this lab is to learn how to design a state machine.  == State Machine Design == State machine design can be easily constructed by considering ...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Objective ==&lt;br /&gt;
The objective of this lab is to learn how to design a state machine.&lt;br /&gt;
&lt;br /&gt;
== State Machine Design ==&lt;br /&gt;
State machine design can be easily constructed by considering &amp;lt;i&amp;gt;what&amp;lt;/i&amp;gt; you want to do based on a certain system state. For example, you might want consider building a state machine for an obstacle-avoidance robot, which acts differently based on if it detects an obstacle or not. In other words, you might have a state to avoid an obstacle, and another state to follow a light-source if there is an obstacle in the way. This helps separate the functionality of what should happen when an obstacle is in the way, and what should happen if an obstacle is not in the way.&lt;br /&gt;
&lt;br /&gt;
Each state should contain meaningful code that should be executed in that state. Furthermore, state transition code should be put in place that provides a way out of the current state to the next state/&lt;br /&gt;
&lt;br /&gt;
&amp;lt;BR/&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== ENUM ==&lt;br /&gt;
The &amp;lt;b&amp;gt;enum&amp;lt;/b&amp;gt; provides a way to assign names to a sequence of integer values. For example, if a state machine contains two unique states, you should use an &amp;lt;code&amp;gt;enum&amp;lt;/code&amp;gt; to declare the names, and the compiler guarantees that the names have a unique identifier. The alternative is to declare separate variables set to a different value, but this approach uses more unnecessary memory and is error-prone.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
// Auto-set the identifiers of the variables&lt;br /&gt;
enum{stateSTART, stateEND, stateSENSOR};&lt;br /&gt;
&lt;br /&gt;
// Manually set the identifiers of the variables&lt;br /&gt;
enum{stateSTART = 1, stateEND = 2, stateSENSOR = 3};&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The enumerator variables can be automatically assigned a value, or the value can be manually chosen. In either case, unique values should be set for enumerator variables. An enumerator does not have to be used just for a state machine design, but it can be used anytime when you need to simply set unique identifiers to certain names.&lt;br /&gt;
&lt;br /&gt;
==Switch Statement==&lt;br /&gt;
&lt;br /&gt;
==Static Declaration of Variables==&lt;br /&gt;
&lt;br /&gt;
==Assignment== &lt;br /&gt;
Build the state machine given in the next figure. Enclose your state machine inside a function and call the function every 100ms from your &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; function. You will notice that the SENSOR state goes to IDLE state upon Button#1, and using the same button, the IDLE state transitions to SENSOR state. First, answer the question in the &amp;quot;Questions&amp;quot; section of this document. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Add image here&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Whenever the state is changed, print on the screen what state the system is in. The statement should only print once - not every time the state machine function is called.&lt;br /&gt;
&lt;br /&gt;
 &lt;br /&gt;
&lt;br /&gt;
==Questions==&lt;br /&gt;
# Describe the behavior you see if you press and hold Button#1 for longer than 100ms in the SENSOR state.&lt;/div&gt;</summary>
		<author><name>Charley</name></author>	</entry>

	<entry>
		<id>http://socialledge.com/sjsu/index.php?title=ES101_-_Lesson_8_:_Functions_with_Pass-By-Reference&amp;diff=1577</id>
		<title>ES101 - Lesson 8 : Functions with Pass-By-Reference</title>
		<link rel="alternate" type="text/html" href="http://socialledge.com/sjsu/index.php?title=ES101_-_Lesson_8_:_Functions_with_Pass-By-Reference&amp;diff=1577"/>
				<updated>2012-10-16T21:45:09Z</updated>
		
		<summary type="html">&lt;p&gt;Charley: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Objective ==&lt;br /&gt;
You will learn the basics of pointers, which are used in combination with the functions.&lt;br /&gt;
&lt;br /&gt;
Pass-by-value type of functions pass just the value of the variables into a function, but with a simple change, you can use functions that accept pass-by-reference type of variables. This is a very powerful concept that will allow your functions to change the values outside of its scope.&lt;br /&gt;
&lt;br /&gt;
== Pass By Reference ==&lt;br /&gt;
Pass by reference provides a mean to provide input to functions by specifying a memory location, which allows the function to modify caller's variables. In other words, instead of just merely giving a value, you pass the &amp;lt;b&amp;gt;pointer&amp;lt;/b&amp;gt; of variable(s), and the pointer points to the memory address where the data is stored. The values are then accessed by using the * operator inside the function, which is called the &amp;lt;b&amp;gt;dereference&amp;lt;/b&amp;gt; operator. Students often get confused with &amp;amp; and * symbols. &amp;amp; refers to the memory address whereas as * refers to a value stored at this memory address.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
// Declare foobar() that takes pointers of int and float&lt;br /&gt;
void foobar(int *intInput, float *fpInput);&lt;br /&gt;
&lt;br /&gt;
int main(void)&lt;br /&gt;
{&lt;br /&gt;
   // We can no longer do this since we have to provide&lt;br /&gt;
   // addresses of some variable&lt;br /&gt;
   foobar(1, 1.2);        // ERROR!&lt;br /&gt;
   foobar(anInt, aFloat); // ERROR!&lt;br /&gt;
&lt;br /&gt;
   // Pass variable addresses to foobar()&lt;br /&gt;
   int anInt = 1;&lt;br /&gt;
   float aFloat = 2.3;&lt;br /&gt;
   foobar(&amp;amp;anInt, &amp;amp;aFloat);&lt;br /&gt;
&lt;br /&gt;
   // After function call to foobar(): anInt and aFloat are zero&lt;br /&gt;
   printf(&amp;quot;Variables now: %i, %f\n&amp;quot;, anInt, aFloat);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void foobar(int *intInput, float *fpInput)&lt;br /&gt;
{&lt;br /&gt;
   // intInput and fpInput are addresses, so to read/write&lt;br /&gt;
   // the values at these addresses, we must use * (dereference)&lt;br /&gt;
   // operator&lt;br /&gt;
   printf(&amp;quot;Input was: %i, %f\n&amp;quot;, *intInput, *fpInput);&lt;br /&gt;
&lt;br /&gt;
   // Modify the inputs:&lt;br /&gt;
   *intInput = 0;&lt;br /&gt;
   *fpInput  = 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similar to how you provided address to &amp;lt;code&amp;gt;scanf&amp;lt;/code&amp;gt; function, you must provide address of variables to &amp;lt;code&amp;gt;foobar()&amp;lt;/code&amp;gt;, when calling the function otherwise a compiler error will be encountered as seen by first two calls to &amp;lt;code&amp;gt;foobar()&amp;lt;/code&amp;gt; in the figure above. The third call to &amp;lt;code&amp;gt;foobar()&amp;lt;/code&amp;gt; shows correct function call, which passes addresses of &amp;lt;code&amp;gt;anInt&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;aFloat&amp;lt;/code&amp;gt; variables. The &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; function called &amp;lt;code&amp;gt;foobar()&amp;lt;/code&amp;gt; with values as 1 and 2.3, and, after &amp;lt;code&amp;gt;foobar()&amp;lt;/code&amp;gt; returns, the values of the variables are set to 0 by &amp;lt;code&amp;gt;foobar()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Passing Arrays to a Function==&lt;br /&gt;
Passing arrays to a function is not much different than passing a pointer. This is because to pass an array to a function, you actually still pass the address of its first element. Arrays usually consist of large number of elements; therefore, passing array pass-by-value would be time consuming because each elements needs to copied. To address this issue, the address of first element of the array is passed to functions (pass-by-reference). See the examples below for illustration.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
void getName(char *charArray);&lt;br /&gt;
float getAverageOfFloatingPointArray(float *floatArray, int elements);&lt;br /&gt;
&lt;br /&gt;
int main(void)&lt;br /&gt;
{&lt;br /&gt;
   char userName[50];&lt;br /&gt;
   getName(userName);&lt;br /&gt;
   printf(&amp;quot;Your name is: %s\n&amp;quot;, userName);&lt;br /&gt;
&lt;br /&gt;
   float samples[4] = {1.1, 2.2, 3.3, 4.4};&lt;br /&gt;
   float average = getAverageOfFloatingPointArray(samples, 4);&lt;br /&gt;
   printf(&amp;quot;Average = %f\n&amp;quot;, average);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void getName(char *charArray)&lt;br /&gt;
{&lt;br /&gt;
   printf(&amp;quot;Enter your name: &amp;quot;);&lt;br /&gt;
   // No &amp;amp; symbol needed since the pointer is already the address&lt;br /&gt;
   scanf(&amp;quot;%s&amp;quot;, charArray);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
float getAverageOfFloatingPointArray(float *floatArray, int elements)&lt;br /&gt;
{&lt;br /&gt;
   int i = 0;&lt;br /&gt;
   float sum = 0;&lt;br /&gt;
   for(i = 0; i &amp;lt; elements; i++)&lt;br /&gt;
   {&lt;br /&gt;
      sum += floatArray[i];&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   float average = sum / elements;&lt;br /&gt;
   return average;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that when &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; calls the &amp;lt;code&amp;gt;getName()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;getAverageOfFloatingPointArray()&amp;lt;/code&amp;gt; functions, it just passes the arrays without the &amp;amp; operator; this is because the array name itself is the address of the first element of the array. The one problem is that &amp;lt;code&amp;gt;getAverageOfFloatingPointArray()&amp;lt;/code&amp;gt; will not know how big the array is, so another parameter, &amp;lt;code&amp;gt;elements&amp;lt;/code&amp;gt;, is given, such that this function can know how many elements there are in the array. Again, this is similar to pass-by-reference, except how the arrays are passed. One last thing to note is that when the &amp;lt;code&amp;gt;getName()&amp;lt;/code&amp;gt; function tried to do &amp;lt;code&amp;gt;scanf&amp;lt;/code&amp;gt;, it did not need to pass the address of &amp;lt;code&amp;gt;charArray&amp;lt;/code&amp;gt;, because the &amp;lt;code&amp;gt;charArray&amp;lt;/code&amp;gt; is the address to begin with. Furthermore, notice that the &amp;lt;code&amp;gt;floatArray&amp;lt;/code&amp;gt; was accessed simply by using index locations.&lt;br /&gt;
&lt;br /&gt;
==Assignment== &lt;br /&gt;
# Build the following functions and use meaningful names for each function.&lt;br /&gt;
#*&amp;lt;b&amp;gt;1a.&amp;lt;/b&amp;gt;  A function asks to input an integer, scans it and returns the input data.&lt;br /&gt;
#*&amp;lt;b&amp;gt;1b.&amp;lt;/b&amp;gt;  A function asks to input a floating-point number, scans it and returns the input data.&lt;br /&gt;
#*&amp;lt;b&amp;gt;1c.&amp;lt;/b&amp;gt;  A function that takes a char pointer to a char array, asks for the user‟s first name, and stores it into the array using the pointer.&lt;br /&gt;
#*&amp;lt;b&amp;gt;1d.&amp;lt;/b&amp;gt;  A function that takes two integer pointers and swaps the numbers.&lt;br /&gt;
# Ask the user to input two integers using the function built in 1a, swap them using your swap function, and print the values on the screen.&lt;br /&gt;
# Declare an array of 5 floating point numbers, ask the user to input five floating point numbers by calling the function in 1b five times, and store the value to the five index locations of the floating-point array. Create a function that can calculate the average of floating-point array and returns the average. Using this function, get the average of the user input and print it out.&lt;br /&gt;
# Ask the user to input their name using the function built in 1c and print the name using a for loop in capital letters.&lt;br /&gt;
&lt;br /&gt;
==Questions==&lt;br /&gt;
Answer the following questions to complete the lab.&lt;br /&gt;
# Although pass-by-reference can be used to substitute all pass-by-value functions, why is pass-by-value be considered safer?&lt;br /&gt;
# When passing an integer array to a function, why do you also need to pass the number of elements in that array?&lt;/div&gt;</summary>
		<author><name>Charley</name></author>	</entry>

	<entry>
		<id>http://socialledge.com/sjsu/index.php?title=ES101_-_Lesson_8_:_Functions_with_Pass-By-Reference&amp;diff=1576</id>
		<title>ES101 - Lesson 8 : Functions with Pass-By-Reference</title>
		<link rel="alternate" type="text/html" href="http://socialledge.com/sjsu/index.php?title=ES101_-_Lesson_8_:_Functions_with_Pass-By-Reference&amp;diff=1576"/>
				<updated>2012-10-16T18:36:45Z</updated>
		
		<summary type="html">&lt;p&gt;Charley: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Objective ==&lt;br /&gt;
You will learn the basics of pointers, which are used in combination with the functions.&lt;br /&gt;
&lt;br /&gt;
Pass-by-value type of functions pass just the value of the variables into a function, but with a simple change, you can use functions that accept pass-by-reference type of variables. This is a very powerful concept that will allow your functions to change the values outside of its scope.&lt;br /&gt;
&lt;br /&gt;
== Pass By Reference ==&lt;br /&gt;
Pass by reference provides a mean to provide input to functions by specifying a memory location, which allows the function to modify caller's variables. In other words, instead of just merely giving a value, you pass the &amp;lt;b&amp;gt;pointer&amp;lt;/b&amp;gt; of variable(s), and the pointer points to the memory address where the data is stored. The values are then accessed by using the * operator inside the function, which is called the &amp;lt;b&amp;gt;dereference&amp;lt;/b&amp;gt; operator. Students often get confused with &amp;amp; and * symbols. &amp;amp; refers to the memory address whereas as * refers to a value stored at this memory address.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
// Declare foobar() that takes pointers of int and float&lt;br /&gt;
void foobar(int *intInput, float *fpInput);&lt;br /&gt;
&lt;br /&gt;
int main(void)&lt;br /&gt;
{&lt;br /&gt;
   // We can no longer do this since we have to provide&lt;br /&gt;
   // addresses of some variable&lt;br /&gt;
   foobar(1, 1.2);        // ERROR!&lt;br /&gt;
   foobar(anInt, aFloat); // ERROR!&lt;br /&gt;
&lt;br /&gt;
   // Pass variable addresses to foobar()&lt;br /&gt;
   int anInt = 1;&lt;br /&gt;
   float aFloat = 2.3;&lt;br /&gt;
   foobar(&amp;amp;anInt, &amp;amp;aFloat);&lt;br /&gt;
&lt;br /&gt;
   // After function call to foobar(): anInt and aFloat are zero&lt;br /&gt;
   printf(&amp;quot;Variables now: %i, %f\n&amp;quot;, anInt, aFloat);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void foobar(int *intInput, float *fpInput)&lt;br /&gt;
{&lt;br /&gt;
   // intInput and fpInput are addresses, so to read/write&lt;br /&gt;
   // the values at these addresses, we must use * (dereference)&lt;br /&gt;
   // operator&lt;br /&gt;
   printf(&amp;quot;Input was: %i, %f\n&amp;quot;, *intInput, *fpInput);&lt;br /&gt;
&lt;br /&gt;
   // Modify the inputs:&lt;br /&gt;
   *intInput = 0;&lt;br /&gt;
   *fpInput  = 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similar to how you provided address to &amp;lt;code&amp;gt;scanf&amp;lt;/code&amp;gt; function, you must provide address of variables to &amp;lt;code&amp;gt;foobar()&amp;lt;/code&amp;gt;, when calling the function otherwise a compiler error will be encountered as seen by first two calls to &amp;lt;code&amp;gt;foobar()&amp;lt;/code&amp;gt; in the figure above. The third call to &amp;lt;code&amp;gt;foobar()&amp;lt;/code&amp;gt; shows correct function call, which passes addresses of &amp;lt;code&amp;gt;anInt&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;aFloat&amp;lt;/code&amp;gt; variables. The &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; function called &amp;lt;code&amp;gt;foobar()&amp;lt;/code&amp;gt; with values as 1 and 2.3, and, after &amp;lt;code&amp;gt;foobar()&amp;lt;/code&amp;gt; returns, the values of the variables are set to 0 by &amp;lt;code&amp;gt;foobar()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Passing Arrays to a Function==&lt;br /&gt;
Passing arrays to a function is not much different than passing a pointer. This is because to pass an array to a function, you actually still pass the address of its first element. Arrays usually consist of large number of elements; therefore, passing array pass-by-value would be time consuming because each elements needs to copied. To address this issue, the address of first element of the array is passed to functions (pass-by-reference). See the examples below for illustration.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
void getName(char *charArray);&lt;br /&gt;
float getAverageOfFloatingPointArray(float *floatArray, int elements);&lt;br /&gt;
&lt;br /&gt;
int main(void)&lt;br /&gt;
{&lt;br /&gt;
   char userName[50];&lt;br /&gt;
   getName(userName);&lt;br /&gt;
   printf(&amp;quot;Your name is: %s\n&amp;quot;, userName);&lt;br /&gt;
&lt;br /&gt;
   float samples[4] = {1.1, 2.2, 3.3, 4.4};&lt;br /&gt;
   float average = getAverageOfFloatingPointArray(samples, 4);&lt;br /&gt;
   printf(&amp;quot;Average = %f\n&amp;quot;, average);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void getName(char *charArray)&lt;br /&gt;
{&lt;br /&gt;
   printf(&amp;quot;Enter your name: &amp;quot;);&lt;br /&gt;
   // No &amp;amp; symbol needed since the pointer is already the address&lt;br /&gt;
   scanf(&amp;quot;%s&amp;quot;, charArray);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
float getAverageOfFloatingPointArray(float *floatArray, int elements)&lt;br /&gt;
{&lt;br /&gt;
   int i = 0;&lt;br /&gt;
   float sum = 0;&lt;br /&gt;
   for(i = 0; i &amp;lt; elements; i++)&lt;br /&gt;
   {&lt;br /&gt;
      sum += floatArray[i];&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   float average = sum / elements;&lt;br /&gt;
   return average;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that when main() calls the &amp;lt;code&amp;gt;getName()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;getAverageOfFloatingPointArray()&amp;lt;/code&amp;gt; functions, it just passes the arrays without the &amp;amp; operator; this is because the array name itself is the address of the first element of the array. The one problem is that &amp;lt;code&amp;gt;getAverageOfFloatingPointArray()&amp;lt;/code&amp;gt; will not know how big the array is, so another parameter, &amp;lt;code&amp;gt;elements&amp;lt;/code&amp;gt;, is given, such that this function can know how many elements there are in the array. Again, this is similar to pass-by-reference, except how the arrays are passed. One last thing to note is that when the &amp;lt;code&amp;gt;getName()&amp;lt;/code&amp;gt; function tried to do &amp;lt;code&amp;gt;scanf&amp;lt;/code&amp;gt;, it did not need to pass the address of &amp;lt;code&amp;gt;charArray&amp;lt;/code&amp;gt;, because the &amp;lt;code&amp;gt;charArray&amp;lt;/code&amp;gt; is the address to begin with. Furthermore, notice that the &amp;lt;code&amp;gt;floatArray&amp;lt;/code&amp;gt; was accessed simply by using index locations.&lt;br /&gt;
&lt;br /&gt;
==Assignment== &lt;br /&gt;
# Build the following functions and use meaningful names for each function.&lt;br /&gt;
#* A function asks to input an integer, scans it and returns the input data.&lt;br /&gt;
#* A function asks to input a floating-point number, scans it and returns the input data.&lt;br /&gt;
#* A function that takes a char pointer to a char array, asks for the user‟s first name, and stores it into the array using the pointer.&lt;br /&gt;
#* A function that takes two integer pointers and swaps the numbers.&lt;br /&gt;
# Ask the user to input two integers using the function built in 1a, swap them using your swap function, and print the values on the screen.&lt;br /&gt;
# Declare an array of 5 floating point numbers, ask the user to input five floating point numbers by calling the function in 1b five times, and store the value to the five index locations of the floating-point array. Create a function that can calculate the average of floating-point array and returns the average. Using this function, get the average of the user input and print it out.&lt;br /&gt;
# Ask the user to input their name using the function built in 1c and print the name using a for loop in capital letters.&lt;br /&gt;
&lt;br /&gt;
==Questions==&lt;br /&gt;
Answer the following questions to complete the lab.&lt;br /&gt;
# Although pass-by-reference can be used to substitute all pass-by-value functions, why is pass-by-value be considered safer?&lt;br /&gt;
# When passing an integer array to a function, why do you also need to pass the number of elements in that array?&lt;/div&gt;</summary>
		<author><name>Charley</name></author>	</entry>

	<entry>
		<id>http://socialledge.com/sjsu/index.php?title=ES101_-_Lesson_8_:_Functions_with_Pass-By-Reference&amp;diff=1575</id>
		<title>ES101 - Lesson 8 : Functions with Pass-By-Reference</title>
		<link rel="alternate" type="text/html" href="http://socialledge.com/sjsu/index.php?title=ES101_-_Lesson_8_:_Functions_with_Pass-By-Reference&amp;diff=1575"/>
				<updated>2012-10-16T18:31:59Z</updated>
		
		<summary type="html">&lt;p&gt;Charley: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Objective ==&lt;br /&gt;
You will learn the basics of pointers, which are used in combination with the functions.&lt;br /&gt;
&lt;br /&gt;
Pass-by-value type of functions pass just the value of the variables into a function, but with a simple change, you can use functions that accept pass-by-reference type of variables. This is a very powerful concept that will allow your functions to change the values outside of its scope.&lt;br /&gt;
&lt;br /&gt;
== Pass By Reference ==&lt;br /&gt;
Pass by reference provides a mean to provide input to functions by specifying a memory location, which allows the function to modify caller's variables. In other words, instead of just merely giving a value, you pass the &amp;lt;b&amp;gt;pointer&amp;lt;/b&amp;gt; of variable(s), and the pointer points to the memory address where the data is stored. The values are then accessed by using the * operator inside the function, which is called the &amp;lt;b&amp;gt;dereference&amp;lt;/b&amp;gt; operator. Students often get confused with &amp;amp; and * symbols. &amp;amp; refers to the memory address whereas as * refers to a value stored at this memory address.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
// Declare foobar() that takes pointers of int and float&lt;br /&gt;
void foobar(int *intInput, float *fpInput);&lt;br /&gt;
&lt;br /&gt;
int main(void)&lt;br /&gt;
{&lt;br /&gt;
   // We can no longer do this since we have to provide&lt;br /&gt;
   // addresses of some variable&lt;br /&gt;
   foobar(1, 1.2);        // ERROR!&lt;br /&gt;
   foobar(anInt, aFloat); // ERROR!&lt;br /&gt;
&lt;br /&gt;
   // Pass variable addresses to foobar()&lt;br /&gt;
   int anInt = 1;&lt;br /&gt;
   float aFloat = 2.3;&lt;br /&gt;
   foobar(&amp;amp;anInt, &amp;amp;aFloat);&lt;br /&gt;
&lt;br /&gt;
   // After function call to foobar(): anInt and aFloat are zero&lt;br /&gt;
   printf(&amp;quot;Variables now: %i, %f\n&amp;quot;, anInt, aFloat);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void foobar(int *intInput, float *fpInput)&lt;br /&gt;
{&lt;br /&gt;
   // intInput and fpInput are addresses, so to read/write&lt;br /&gt;
   // the values at these addresses, we must use * (dereference)&lt;br /&gt;
   // operator&lt;br /&gt;
   printf(&amp;quot;Input was: %i, %f\n&amp;quot;, *intInput, *fpInput);&lt;br /&gt;
&lt;br /&gt;
   // Modify the inputs:&lt;br /&gt;
   *intInput = 0;&lt;br /&gt;
   *fpInput  = 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similar to how you provided address to &amp;lt;code&amp;gt;scanf&amp;lt;/code&amp;gt; function, you must provide address of variables to &amp;lt;code&amp;gt;foobar()&amp;lt;/code&amp;gt;, when calling the function otherwise a compiler error will be encountered as seen by first two calls to &amp;lt;code&amp;gt;foobar()&amp;lt;/code&amp;gt; in the figure above. The third call to &amp;lt;code&amp;gt;foobar()&amp;lt;/code&amp;gt; shows correct function call, which passes addresses of &amp;lt;code&amp;gt;anInt&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;aFloat&amp;lt;/code&amp;gt; variables. The &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; function called &amp;lt;code&amp;gt;foobar()&amp;lt;/code&amp;gt; with values as 1 and 2.3, and, after &amp;lt;code&amp;gt;foobar()&amp;lt;/code&amp;gt; returns, the values of the variables are set to 0 by &amp;lt;code&amp;gt;foobar()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Passing Arrays to a Function==&lt;br /&gt;
Passing arrays to a function is not much different than passing a pointer. This is because to pass an array to a function, you actually still pass the address of its first element. Arrays usually consist of large number of elements; therefore, passing array pass-by-value would be time consuming because each elements needs to copied. To address this issue, the address of first element of the array is passed to functions (pass-by-reference). See the examples below for illustration.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
void getName(char *charArray);&lt;br /&gt;
float getAverageOfFloatingPointArray(float *floatArray, int elements);&lt;br /&gt;
&lt;br /&gt;
int main(void)&lt;br /&gt;
{&lt;br /&gt;
   char userName[50];&lt;br /&gt;
   getName(userName);&lt;br /&gt;
   printf(&amp;quot;Your name is: %s\n&amp;quot;, userName);&lt;br /&gt;
&lt;br /&gt;
   float samples[4] = {1.1, 2.2, 3.3, 4.4};&lt;br /&gt;
   float average = getAverageOfFloatingPointArray(samples, 4);&lt;br /&gt;
   printf(&amp;quot;Average = %f\n&amp;quot;, average);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void getName(char *charArray)&lt;br /&gt;
{&lt;br /&gt;
   printf(&amp;quot;Enter your name: &amp;quot;);&lt;br /&gt;
   // No &amp;amp; symbol needed since the pointer is already the address&lt;br /&gt;
   scanf(&amp;quot;%s&amp;quot;, charArray);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
float getAverageOfFloatingPointArray(float *floatArray, int elements)&lt;br /&gt;
{&lt;br /&gt;
   int i = 0;&lt;br /&gt;
   float sum = 0;&lt;br /&gt;
   for(i = 0; i &amp;lt; elements; i++)&lt;br /&gt;
   {&lt;br /&gt;
      sum += floatArray[i];&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   float average = sum / elements;&lt;br /&gt;
   return average;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Notice that when main() calls the &amp;lt;code&amp;gt;getName()&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;getAverageOfFloatingPointArray()&amp;lt;/code&amp;gt; functions, it just passes the arrays without the &amp;amp; operator; this is because the array name itself is the address of the first element of the array. The one problem is that &amp;lt;code&amp;gt;getAverageOfFloatingPointArray()&amp;lt;/code&amp;gt; will not know how big the array is, so another parameter, &amp;lt;code&amp;gt;elements&amp;lt;/code&amp;gt;, is given, such that this function can know how many elements there are in the array. Again, this is similar to pass-by-reference, except how the arrays are passed. One last thing to note is that when the &amp;lt;code&amp;gt;getName()&amp;lt;/code&amp;gt; function tried to do &amp;lt;code&amp;gt;scanf&amp;lt;/code&amp;gt;, it did not need to pass the address of &amp;lt;code&amp;gt;charArray&amp;lt;/code&amp;gt;, because the &amp;lt;code&amp;gt;charArray&amp;lt;/code&amp;gt; is the address to begin with. Furthermore, notice that the &amp;lt;code&amp;gt;floatArray&amp;lt;/code&amp;gt; was accessed simply by using index locations.&lt;/div&gt;</summary>
		<author><name>Charley</name></author>	</entry>

	<entry>
		<id>http://socialledge.com/sjsu/index.php?title=ES101_-_Lesson_8_:_Functions_with_Pass-By-Reference&amp;diff=1574</id>
		<title>ES101 - Lesson 8 : Functions with Pass-By-Reference</title>
		<link rel="alternate" type="text/html" href="http://socialledge.com/sjsu/index.php?title=ES101_-_Lesson_8_:_Functions_with_Pass-By-Reference&amp;diff=1574"/>
				<updated>2012-10-16T18:28:22Z</updated>
		
		<summary type="html">&lt;p&gt;Charley: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Objective ==&lt;br /&gt;
You will learn the basics of pointers, which are used in combination with the functions.&lt;br /&gt;
&lt;br /&gt;
Pass-by-value type of functions pass just the value of the variables into a function, but with a simple change, you can use functions that accept pass-by-reference type of variables. This is a very powerful concept that will allow your functions to change the values outside of its scope.&lt;br /&gt;
&lt;br /&gt;
== Pass By Reference ==&lt;br /&gt;
Pass by reference provides a mean to provide input to functions by specifying a memory location, which allows the function to modify caller's variables. In other words, instead of just merely giving a value, you pass the &amp;lt;b&amp;gt;pointer&amp;lt;/b&amp;gt; of variable(s), and the pointer points to the memory address where the data is stored. The values are then accessed by using the * operator inside the function, which is called the &amp;lt;b&amp;gt;dereference&amp;lt;/b&amp;gt; operator. Students often get confused with &amp;amp; and * symbols. &amp;amp; refers to the memory address whereas as * refers to a value stored at this memory address.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
// Declare foobar() that takes pointers of int and float&lt;br /&gt;
void foobar(int *intInput, float *fpInput);&lt;br /&gt;
&lt;br /&gt;
int main(void)&lt;br /&gt;
{&lt;br /&gt;
   // We can no longer do this since we have to provide&lt;br /&gt;
   // addresses of some variable&lt;br /&gt;
   foobar(1, 1.2);        // ERROR!&lt;br /&gt;
   foobar(anInt, aFloat); // ERROR!&lt;br /&gt;
&lt;br /&gt;
   // Pass variable addresses to foobar()&lt;br /&gt;
   int anInt = 1;&lt;br /&gt;
   float aFloat = 2.3;&lt;br /&gt;
   foobar(&amp;amp;anInt, &amp;amp;aFloat);&lt;br /&gt;
&lt;br /&gt;
   // After function call to foobar(): anInt and aFloat are zero&lt;br /&gt;
   printf(&amp;quot;Variables now: %i, %f\n&amp;quot;, anInt, aFloat);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void foobar(int *intInput, float *fpInput)&lt;br /&gt;
{&lt;br /&gt;
   // intInput and fpInput are addresses, so to read/write&lt;br /&gt;
   // the values at these addresses, we must use * (dereference)&lt;br /&gt;
   // operator&lt;br /&gt;
   printf(&amp;quot;Input was: %i, %f\n&amp;quot;, *intInput, *fpInput);&lt;br /&gt;
&lt;br /&gt;
   // Modify the inputs:&lt;br /&gt;
   *intInput = 0;&lt;br /&gt;
   *fpInput  = 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similar to how you provided address to &amp;lt;code&amp;gt;scanf&amp;lt;/code&amp;gt; function, you must provide address of variables to &amp;lt;code&amp;gt;foobar()&amp;lt;/code&amp;gt;, when calling the function otherwise a compiler error will be encountered as seen by first two calls to &amp;lt;code&amp;gt;foobar()&amp;lt;/code&amp;gt; in the figure above. The third call to &amp;lt;code&amp;gt;foobar()&amp;lt;/code&amp;gt; shows correct function call, which passes addresses of &amp;lt;code&amp;gt;anInt&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;aFloat&amp;lt;/code&amp;gt; variables. The &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; function called &amp;lt;code&amp;gt;foobar()&amp;lt;/code&amp;gt; with values as 1 and 2.3, and, after &amp;lt;code&amp;gt;foobar()&amp;lt;/code&amp;gt; returns, the values of the variables are set to 0 by &amp;lt;code&amp;gt;foobar()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
==Passing Arrays to a Function==&lt;br /&gt;
Passing arrays to a function is not much different than passing a pointer. This is because to pass an array to a function, you actually still pass the address of its first element. Arrays usually consist of large number of elements; therefore, passing array pass-by-value would be time consuming because each elements needs to copied. To address this issue, the address of first element of the array is passed to functions (pass-by-reference). See the examples below for illustration.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
void getName(char *charArray);&lt;br /&gt;
float getAverageOfFloatingPointArray(float *floatArray, int elements);&lt;br /&gt;
&lt;br /&gt;
int main(void)&lt;br /&gt;
{&lt;br /&gt;
   char userName[50];&lt;br /&gt;
   getName(userName);&lt;br /&gt;
   printf(&amp;quot;Your name is: %s\n&amp;quot;, userName);&lt;br /&gt;
&lt;br /&gt;
   float samples[4] = {1.1, 2.2, 3.3, 4.4};&lt;br /&gt;
   float average = getAverageOfFloatingPointArray(samples, 4);&lt;br /&gt;
   printf(&amp;quot;Average = %f\n&amp;quot;, average);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void getName(char *charArray)&lt;br /&gt;
{&lt;br /&gt;
   printf(&amp;quot;Enter your name: &amp;quot;);&lt;br /&gt;
   // No &amp;amp; symbol needed since the pointer is already the address&lt;br /&gt;
   scanf(&amp;quot;%s&amp;quot;, charArray);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
float getAverageOfFloatingPointArray(float *floatArray, int elements)&lt;br /&gt;
{&lt;br /&gt;
   int i = 0;&lt;br /&gt;
   float sum = 0;&lt;br /&gt;
   for(i = 0; i &amp;lt; elements; i++)&lt;br /&gt;
   {&lt;br /&gt;
      sum += floatArray[i];&lt;br /&gt;
   }&lt;br /&gt;
&lt;br /&gt;
   float average = sum / elements;&lt;br /&gt;
   return average;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Charley</name></author>	</entry>

	<entry>
		<id>http://socialledge.com/sjsu/index.php?title=ES101_-_Lesson_8_:_Functions_with_Pass-By-Reference&amp;diff=1573</id>
		<title>ES101 - Lesson 8 : Functions with Pass-By-Reference</title>
		<link rel="alternate" type="text/html" href="http://socialledge.com/sjsu/index.php?title=ES101_-_Lesson_8_:_Functions_with_Pass-By-Reference&amp;diff=1573"/>
				<updated>2012-10-16T18:20:48Z</updated>
		
		<summary type="html">&lt;p&gt;Charley: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Objective ==&lt;br /&gt;
You will learn the basics of pointers, which are used in combination with the functions.&lt;br /&gt;
&lt;br /&gt;
Pass-by-value type of functions pass just the value of the variables into a function, but with a simple change, you can use functions that accept pass-by-reference type of variables. This is a very powerful concept that will allow your functions to change the values outside of its scope.&lt;br /&gt;
&lt;br /&gt;
== Pass By Reference ==&lt;br /&gt;
Pass by reference provides a mean to provide input to functions by specifying a memory location, which allows the function to modify caller's variables. In other words, instead of just merely giving a value, you pass the &amp;lt;b&amp;gt;pointer&amp;lt;/b&amp;gt; of variable(s), and the pointer points to the memory address where the data is stored. The values are then accessed by using the * operator inside the function, which is called the &amp;lt;b&amp;gt;dereference&amp;lt;/b&amp;gt; operator. Students often get confused with &amp;amp; and * symbols. &amp;amp; refers to the memory address whereas as * refers to a value stored at this memory address.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
// Declare foobar() that takes pointers of int and float&lt;br /&gt;
void foobar(int *intInput, float *fpInput);&lt;br /&gt;
&lt;br /&gt;
int main(void)&lt;br /&gt;
{&lt;br /&gt;
   // We can no longer do this since we have to provide&lt;br /&gt;
   // addresses of some variable&lt;br /&gt;
   foobar(1, 1.2);        // ERROR!&lt;br /&gt;
   foobar(anInt, aFloat); // ERROR!&lt;br /&gt;
&lt;br /&gt;
   // Pass variable addresses to foobar()&lt;br /&gt;
   int anInt = 1;&lt;br /&gt;
   float aFloat = 2.3;&lt;br /&gt;
   foobar(&amp;amp;anInt, &amp;amp;aFloat);&lt;br /&gt;
&lt;br /&gt;
   // After function call to foobar(): anInt and aFloat are zero&lt;br /&gt;
   printf(&amp;quot;Variables now: %i, %f\n&amp;quot;, anInt, aFloat);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void foobar(int *intInput, float *fpInput)&lt;br /&gt;
{&lt;br /&gt;
   // intInput and fpInput are addresses, so to read/write&lt;br /&gt;
   // the values at these addresses, we must use * (dereference)&lt;br /&gt;
   // operator&lt;br /&gt;
   printf(&amp;quot;Input was: %i, %f\n&amp;quot;, *intInput, *fpInput);&lt;br /&gt;
&lt;br /&gt;
   // Modify the inputs:&lt;br /&gt;
   *intInput = 0;&lt;br /&gt;
   *fpInput  = 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similar to how you provided address to &amp;lt;code&amp;gt;scanf&amp;lt;/code&amp;gt; function, you must provide address of variables to &amp;lt;code&amp;gt;foobar()&amp;lt;/code&amp;gt;, when calling the function otherwise a compiler error will be encountered as seen by first two calls to &amp;lt;code&amp;gt;foobar()&amp;lt;/code&amp;gt; in the figure above. The third call to &amp;lt;code&amp;gt;foobar()&amp;lt;/code&amp;gt; shows correct function call, which passes addresses of &amp;lt;code&amp;gt;anInt&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;aFloat&amp;lt;/code&amp;gt; variables. The &amp;lt;code&amp;gt;main()&amp;lt;/code&amp;gt; function called &amp;lt;code&amp;gt;foobar()&amp;lt;/code&amp;gt; with values as 1 and 2.3, and, after &amp;lt;code&amp;gt;foobar()&amp;lt;/code&amp;gt; returns, the values of the variables are set to 0 by &amp;lt;code&amp;gt;foobar()&amp;lt;/code&amp;gt;.&lt;/div&gt;</summary>
		<author><name>Charley</name></author>	</entry>

	<entry>
		<id>http://socialledge.com/sjsu/index.php?title=ES101_-_Lesson_8_:_Functions_with_Pass-By-Reference&amp;diff=1572</id>
		<title>ES101 - Lesson 8 : Functions with Pass-By-Reference</title>
		<link rel="alternate" type="text/html" href="http://socialledge.com/sjsu/index.php?title=ES101_-_Lesson_8_:_Functions_with_Pass-By-Reference&amp;diff=1572"/>
				<updated>2012-10-16T18:17:35Z</updated>
		
		<summary type="html">&lt;p&gt;Charley: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Objective ==&lt;br /&gt;
You will learn the basics of pointers, which are used in combination with the functions.&lt;br /&gt;
&lt;br /&gt;
Pass-by-value type of functions pass just the value of the variables into a function, but with a simple change, you can use functions that accept pass-by-reference type of variables. This is a very powerful concept that will allow your functions to change the values outside of its scope.&lt;br /&gt;
&lt;br /&gt;
== Pass By Reference ==&lt;br /&gt;
Pass by reference provides a mean to provide input to functions by specifying a memory location, which allows the function to modify caller's variables. In other words, instead of just merely giving a value, you pass the &amp;lt;b&amp;gt;pointer&amp;lt;/b&amp;gt; of variable(s), and the pointer points to the memory address where the data is stored. The values are then accessed by using the * operator inside the function, which is called the &amp;lt;b&amp;gt;dereference&amp;lt;/b&amp;gt; operator. Students often get confused with &amp;amp; and * symbols. &amp;amp; refers to the memory address whereas as * refers to a value stored at this memory address.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
// Declare foobar() that takes pointers of int and float&lt;br /&gt;
void foobar(int *intInput, float *fpInput);&lt;br /&gt;
&lt;br /&gt;
int main(void)&lt;br /&gt;
{&lt;br /&gt;
   // We can no longer do this since we have to provide&lt;br /&gt;
   // addresses of some variable&lt;br /&gt;
   foobar(1, 1.2);        // ERROR!&lt;br /&gt;
   foobar(anInt, aFloat); // ERROR!&lt;br /&gt;
&lt;br /&gt;
   // Pass variable addresses to foobar()&lt;br /&gt;
   int anInt = 1;&lt;br /&gt;
   float aFloat = 2.3;&lt;br /&gt;
   foobar(&amp;amp;anInt, &amp;amp;aFloat);&lt;br /&gt;
&lt;br /&gt;
   // After function call to foobar(): anInt and aFloat are zero&lt;br /&gt;
   printf(&amp;quot;Variables now: %i, %f\n&amp;quot;, anInt, aFloat);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void foobar(int *intInput, float *fpInput)&lt;br /&gt;
{&lt;br /&gt;
   // intInput and fpInput are addresses, so to read/write&lt;br /&gt;
   // the values at these addresses, we must use * (dereference)&lt;br /&gt;
   // operator&lt;br /&gt;
   printf(&amp;quot;Input was: %i, %f\n&amp;quot;, *intInput, *fpInput);&lt;br /&gt;
&lt;br /&gt;
   // Modify the inputs:&lt;br /&gt;
   *intInput = 0;&lt;br /&gt;
   *fpInput  = 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Charley</name></author>	</entry>

	<entry>
		<id>http://socialledge.com/sjsu/index.php?title=ES101_-_Lesson_8_:_Functions_with_Pass-By-Reference&amp;diff=1571</id>
		<title>ES101 - Lesson 8 : Functions with Pass-By-Reference</title>
		<link rel="alternate" type="text/html" href="http://socialledge.com/sjsu/index.php?title=ES101_-_Lesson_8_:_Functions_with_Pass-By-Reference&amp;diff=1571"/>
				<updated>2012-10-16T18:08:32Z</updated>
		
		<summary type="html">&lt;p&gt;Charley: Created page with &amp;quot;== Objective == You will learn the basics of pointers, which are used in combination with the functions.  Pass-by-value type of functions pass just the value of the variables ...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Objective ==&lt;br /&gt;
You will learn the basics of pointers, which are used in combination with the functions.&lt;br /&gt;
&lt;br /&gt;
Pass-by-value type of functions pass just the value of the variables into a function, but with a simple change, you can use functions that accept pass-by-reference type of variables. This is a very powerful concept that will allow your functions to change the values outside of its scope.&lt;br /&gt;
&lt;br /&gt;
== Pass By Reference ==&lt;br /&gt;
Pass by reference provides a mean to provide input to functions by specifying a memory location, which allows the function to modify caller's variables. In other words, instead of just merely giving a value, you pass the &amp;lt;b&amp;gt;pointer&amp;lt;/b&amp;gt; of variable(s), and the pointer points to the memory address where the data is stored. The values are then accessed by using the * operator inside the function, which is called the &amp;lt;b&amp;gt;dereference&amp;lt;/b&amp;gt; operator. Students often get confused with &amp;amp; and * symbols. &amp;amp; refers to the memory address whereas as * refers to a value stored at this memory address.&lt;/div&gt;</summary>
		<author><name>Charley</name></author>	</entry>

	</feed>