Tuesday, May 23, 2017

804% Slower: Sonic the Hedgehog - Special Stage

Friday, May 19, 2017

One Bit Polyphonic Synth with Sixteen Voices

One bit polyphonic synth with sixteen voices, no interrupts and no audio library.

Download the code here: http://milkcrate.com.au/_other/downloads/arduino/One_Bit_Poly_Synth/

Wednesday, May 17, 2017

Teensy 3.6 Basics - Using DC Motors

DC motors rotate when a voltage is applied across the terminals. The speed of the motor is related to the voltage.

When using a microcontroller to control a DC motor a PWM (or pulse width modulated) signal is used. The Teensy forms a pulse wave, whereby the duty cycle is proportional to the average, equivalent over time. The duty cycle refers to the percentage of the waveform that the pulse wave is high as opposed to low.

A PWM output with a duty cycle of 50% will result in an average of half the voltage of the digital pin, a duty cycle of 25% in one quarter and so on. More information can be found about PWM signals here.

For instance, given a 3V digital pin with a PWM wave of 50%, this would provide the equivalent voltage of 1.5V over time. Thus, the digital PWM outputs on a Teensy can change the speed of a DC motor, not just turn it off and on.

The default frequency for PWM signals on the Teensy 3.6 is 488.28 Hz. Digital outputs 2, 3, 4, 5, 6, 7, 8, 9, 10, 14, 16, 17, 20, 21, 22, 23, 29, 30, 35, 36, 37, 38 are all PWM-capable pins. More information about the PWM functionality of the Teensy can be found here.

When using a PWM signal to control a motor, a certain amount of current is needed to power the motor. The outputs of the Teensy cannot provide sufficient current for the DC motor, and attempting to do so may damage the Teensy.

Instead, a Darlington transistor array in a convenient chip form can manage power source, current and control input and act as mediator between the teensy, the USB power and the DC motor. Note that anything more heavy duty than a very light motor will require an external power supply.

The Darlington transistor array comes packaged in an 8-channel version (ULN2803AN) and 7-channel version (ULN2003AN). In both cases, multiple channels can be combined to control larger motors. Each channel can provide up to 500mA of current, if a sufficient power supply is used.

In the example shown below, a single channel of a ULN2803AN is used to drive a single DC motor using 3V from the Teensy USB power supply.

Hardware Setup

Ground from the Teensy is connected to pin 9 of the ULN2803AN. 3V from the Teensy is connected to pin 10 of the ULN2803AN. Digital pin 2 of the Teensy is connected to pin 1 of the ULN2803AN. Pin 18 of the ULN2803AN is connected to one terminal of the motor. 3V from the Teensy is connected to the other terminal of the motor.

Software Setup
To control the motor, make sure that the motor is connected to a PWM-capable pin. Digital outputs 2, 3, 4, 5, 6, 7, 8, 9, 10, 14, 16, 17, 20, 21, 22, 23, 29, 30, 35, 36, 37, 38 are all PWM-capable pins.

Use analogWrite(pin, value) to set the speed of the motor. The pin is the pin number. The value is a range of 0 - 255.

There is no need to set the pin as an output pin in setup().

Example 1 - Turning the DC Motor On and Off
The motor is turned on and off.

Example 2 - Gradually Turning the DC Motor On Over Time

Download here: http://milkcrate.com.au/_other/downloads/arduino/teensy_3_6_basics/Using_DC_Motors_Example_2/

DC motors can be easily controlled via analogWrite(), however they require additional hardware to work. Combined with MIDI control, there can be used in projects of various types.

Shaving My Head for Charity

Teensy 3.6 Basics - Using Servo Motors

A servo motor is a physical actuator that usually rotates for half a circle or less, or moves forwards and backwards by a set amount. Arduino add-on includes a servo library, which can be used to easily control a servo motor.

A simple, lightweight servo motor can be connected and powered by the Teensy 3.6. Note that anything more heavy duty than a very light motor will require an external power supply.

A servo motor has a control input, which expects a pulse wave with a duty cycle of around 1 - 2 ms and a period length of 20 ms. The duty cycle determines the angle or position of the servo. A duty cycle of 1 ms sets the minimum rotation or position. A duty cycle of 2 ms sets the maximum rotation or position.

Hardware Setup
The servo motor has three connections. Black or brown is usually ground, and should be connected to Teensy ground. Red is usually the positive power terminal, and should be connected to Teensy 3.3V. Orange or yellow is usually the control input, and should be connected to a Teensy digital pin, in this case pin 0.

Note that if a more heavy-duty servo motor is used, an external power supply is required. In this case, the ground of the motor, teensy and power supply should be connected. The positive terminal of the power supply should be connected to the positive terminal of the servo. The control input should be connected to a Teensy digital pin.

Software Setup 
The servo library must be included at the start of the code. A servo object with a unique name can then be created by using the Servo structure.

The servo object can then be attached to a digital pin of the Teensy. This can be any digital pin. Two optional arguments as part of attaching the pin set the minimum and maximum duty cycle time periods in microseconds. A suggestion is to start with 1000 microseconds and 2000 microseconds, and adjust from there.

To control the servo motor, the write command is used with the Servo object. The value is in degrees, from 0 - 180.

Example 1 - Rotating the Servo
In this example, a servo is connected to the Teensy. The servo is then simply rotated and delayed on loop via the servo library.

Download here: http://milkcrate.com.au/_other/downloads/arduino/teensy_3_6_basics/Using_Servos_Example_1/

Example 2 - Controlling the Servo via MIDI
In this example, the servo is controlled via a MIDI control message on channel 1, controller 1. The range of 0 - 127 is mapped to 0 - 180 degrees.

Download here: http://milkcrate.com.au/_other/downloads/arduino/teensy_3_6_basics/Using_Servos_Example_2/

The servo motor is an example of physical control via the Teensy. MIDI data can be used to control the movement of the motor.