Wednesday, March 29, 2017

Tutorials and project resources

Arduino (and C) Code Samples and Communities

Learning Code (tutorials)


Get motors, sensors, and other stuff...


and let's not forget McMaster-Carr

Examples of Cool Projects...

Project ShapeOko
Totally Awesome Sylvia Watercolor Bot
iFetch
Balanduino
Helicopter Blimpbot
Openbuilds v-slot


Create custom parts...

Thingiverse
Ponoko
Dimension uPrint
Epilog Laser Cutter



A short list of Arduino Key Words

A short list of Arduino C Keywords

The following list is based upon the keywords found in the more commonly used Arduino Example Sketches.

Variable Types

·         int

·         float

·         long

Variable Type Modifier

·         const

·         unsigned

Serial Library

·         Serial.begin( … )

·         Serial.print( … )

·         Serial.println( … )

Sevo Library

·         #include <Servo.h>             (must be placed at beginning of sketch)

·         Servo servoName                (each servo must be given a unique identifier)

·         servoName.attach( … )      (must be attached to a PWM pin)

·         servoName.write(val)       (val must be between 0 and 180)

Modes

·         INPUT

·         OUTPUT

·         HIGH

·         LOW

·         INPUT_PULLUP

Defined Functions

·         setup()

·         loop()

·         analogRead( …   )

·         analogWrite( …   )

·         delay( … )

·         digitalWrite( … )

·         digitalRead( … )

·         millis( … )

·         pinMode( … )

Conditionals

·         if

if (someVariable > 50)

{

  // do something here

}

 

·         if  /  else

if (someVariable > 50)

{

  // action A

}

else

{

  // action B

}

 

·         while

var = 0;

while(var< 200)
{

  // do something repetitive 200 times

  var++;

}

 

·         for

for (int i=0; i <= 255; i++)

{

    Serial.println(i);

}

 

Comparison Operators

·         x == y      (x is equal to y)

·         x != y       (x is not equal to y)

·         x <  y        (x is less than y) 

·         x >  y        (x is greater than y)

·         x <= y      (x is less than or equal to y)

·         x >= y      (x is greater than or equal to y)

Boolean Operators

·         &&           and

·         ||             or

·         !                not

 

Special Items

·         void – this is only used in function declarations. It indicates that the function is expected to return no information to the function from which it was called.

Arrays

·         An array is a collection of variables that are accessed with an index number.

·         Declaring an array

int myArray[] = {2, 4, 8, 3, 6};

·         Accessing an array

myArray[0]      contains 2

myArray[4]      contains 6

myArray[5]      is invalid and will return random information

 

 

 

Digital Input

Microprocessors can be used to read signals from the outside environment.

The command that can be used to read the state of a digital pin is digitalRead.

A simple way to see how this works is to use a pushbutton to toggle the voltage of an I/O pin between +5V and ground.

The following circuit created using Frizting shows one possible way that this can be accomplished.


So what's happening here??


  1. The Arduino board is used to supply both +5V and GND to the rails of the breadboard.
  2. The left pushbutton is used to light an LED in a simple electrical circuit. 
  3. The right pushbutton is used to control the voltage state of digital I/O pin 2.
    • Pin 2 is connected to both one leg of the pushbutton and to +5V through a 10k resistor. So when the button is open, pin 2 is HIGH.
    • The other leg of the pushbutton is connected to GND. When the button is closed the voltage of Pin 2 is "pulled-down" to GND. 
  4. The state of Pin 2 is determined using digitalRead  in the program loop. When the program determines that the button has been pushed, it sends a  digitalWrite(13, HIGH)  this results in the other LED to turn on.
There a a few more things that need to be explored and these will be covered in lab.

Using the above set-up explore the behavior of the example Sketches...

Button
StateChangeDetection
Debounce

After this exploration - take a look at the following code that combines some of these ideas together.

    


Next steps...

Modify the circuit such that indicator LED's are connected to pins 9, 10, 11, and 12.


The following code is a variation of the above code where functions are used to better define what is happening.

The Loop section only contains calls to a series of user defined functions and then updates to loop variables. The functions called are

debounceButton - this checks the current state of the button
checkForChange - this checks to see if the button has switched state (HIGH to LOW or LOW to HIGH)
getButtonNumber - this will increment the button counter it a change from a HIGH to LOW button state has been detected
indicatorLight - this turns on the LED associated with the button value. Either LED 1, 2, 3, or 4, on the 5th button push all LED's are tunred off.

The code for this setup is shown below the video...




Going even further...

One of the students in the class created a project that uses a sine wave to control an array of LED's. A pushbutton allows the user to toggle between different input modes and a potentiometer is used to adjust the parameter in each mode - phase, wave speed and brightness.

This project introduces two new functions, analogWrite and analogRead. The analogWrite function names use of pulse-width modulation (PWM) to control the brightness of the LED's. The analogRead function is used to read the value of the potentiometer. You can explore these two functions using the Arduino Example sketches...

analogWrite: File... Examples... Analog... Fading
analogRead: File... Examples... Analog... AnalogInput

The code is also a very nice example of both how to use functions to make your code easier to read and the proper use of comments to provide clarity.

Here is a video of the system in action. The code used to create this set-up is listed after the video.



Tuesday, March 28, 2017

Digital Output

The digital I/O pin

The Arduino Uno can supply a digital output signal on its 14 digital input/output (I/O) pins. In most instances, the pins used to provide these signals are pins 2 through 12. Pins 0 and 1 are also used by the Uno to effect serial communication and it is good practice to not use these pins in a project unless absolutely necessary. The output of Pin 13 is connected to an on-board LED and a current limiting resistor - this pin is usually held in reserve for debugging purposes and again it is good practice to not use this pin in a project unless absolutely necessary.

The digital I/O pins 3, 5, 6, 9, 10 and 11 can be used as I/O pins but can also provide PWM output using the analogWrite() function.

The digital I/O pins 2, 4, 7, 8, 12 can be considered the most basic type of digital pin. In the most generic sense, digital I/O pins can be thought of as the output of a switch that can can be toggled between two terminals where one of the terminals is held at positive five volts (+5V) and zero volts, also referred to as ground (GND). This digital "switch" is then controlled via software, which is to say, the pin does what the program (or sketch in Arduino speak) tells it what to do. This concept of the digital pin as a switch is shown in the figure below.



The digital pins on the Arduino can be used in two modes: they can provide a digital signal - this is the OUTPUT mode, or they can be used to receive a digital signal, this is the INPUT mode. In order to use a pin, the mode of operation must be specified in the sketch - typically this is done within the setup( ) function using the defined function called pinMode( ).

As an example, the following line of code sets the digital I/O pin number 3 to OUTPUT mode.

pinMode( 3, OUTPUT);

Once the mode is set to OUTPUT, the boolean value of the digital pin can be set using the predefined function digitalWrite( ). The pin value can be set to HIGH (+5V) or LOW (GND). The following are examples of the code usage and the figures show the resulting "physical" process. Again, pin 3 is used.

digitalWrite( 3, HIGH);


digitalWrite( 3, LOW);



Using digital output

Digital pins are typically used in two ways. The first is to send digital information to another device and the second is to act as a combination power source and switch to a limited set of components such as LEDs.

Sending digital information

The Arduino is often used to control different types of motors. This is commonly done through the use of a motor control board such as the Pololu Qik 2s9v1 Dual Serial Motor Controller or the EasyDriver Stepper Motor Driver. In a typical arrangement a number of the Arduino digital I/O pins are use to send signals to the motor driver boards. The signal requirements are different for each board and are commonly controlled via libraries that are meant to be used with the boards. Nonetheless, the Arduino sketch will use the digitalWrite function with the HIGH and LOW modes to provide the required digital signal to the board. This concept is shown in the following figure.


Controlling components

The second use for the digital pins is to provide current to operate a component such as an LED. When used in this manner, the Arduino documentation states that no pin should be allowed to source more than 40 mA of current and the recommended steady current is around 20 mA.There is also a specified upper limit of 200 mA for the entire Arduino board. Thus one must be very careful when using the Arduino to provide operating current to more than a few simple components. For instance - using the Arduino to control 10 LEDs can easily exceed the 200 mA limit of the board.

When using components that draw current it is important to calculate the possible current drawn and use an in series resistor to limit the maximum current draw - this is sometimes referred to as a current limiting resistor. The typical setup for using a digital output pin to control an LED is shown in the figure below. Note that the figure explicitly shows that the Arduino requires power from an outside source which can either be a USB cable connected to a PC or some other DC power source (7-15 V) that is connected to the Arduino input power jack. The ground line of the power source is connected to the Arduino and the figure notes that this is the same ground that is used by the LED circuit.



Code example

The following complete code is an example of the use of digital output. Digital pin 3 is given a descriptive name "redLedPin", its mode is set to output and its state is toggled from high to low repeatedly in a loop. A video of the circuit setup and running is provided below the sketch

Sketch



Video