|
AnalogInput
This widget connects to the analog inputs of a microcontroller and provides some processing of the sensor values as they come in. The default range for analog inputs for the Make Controller and Brainstem is 0 - 1023, but this range can be scaled by AnalogInput to any range desired by setting the min and max numbers. AnalogInput also has a fader knob which allows the user to simulate the values of a sensor coming in, so the user can test other parts of their application with no external hardware hooked up.
On Screen Features
- CONNECT: When on, reads data from the sensor at the selected port. When off, use the slider to set the value. The off mode is useful for when you don't have the sensor and/or microcontroller hooked up, but still want to test what would happen. You can simulate the operation of the sensor by dragging the knob up and down.
- SMOOTH: When on, performs a median filter on the raw sensor readings so that the processed values are less jittery. This is helpful because many sensors produce rough readings that change more than you want.
- RAW: A display of the unprocessed data coming from the sensor
- CEILING: The highest raw value processed. Values higher than this value are ignored. Useful if your application uses a limited range of the sensor.
- FLOOR: The lowest raw value processed. Note that the CEILING and FLOOR raw values will be scaled to the MAX and MIN values. So if ceiling is set to 800, floor is set to 200, max is set to 100 and min is set to 0, only the values from 800 to 200 will be scaled from 100 to 0.
- MAX: Sets the highest value output. The highest raw value is scaled to this value
- MIN: Sets the lowest value output. The lowest raw value is scaled to this value
- INVERT: Makes a high raw value produce a low output value and visa versa. Useful for reversing the effect of a sensor
- PROCESSED: The value output by the widget after all processing is applied
Parameters
- CONTROLLER: Determines which kind of controller the widget communicates with, Make or Brainstem
- CONTROLLERIP: Sets the IP address used to communicate with the Make Controller
- CONTROLLERPORT: Sets the Port used to communicate with the Make Controller
- INVISIBLE: if set to "yes", the widget will disappear when the Flash movie is run
- SENSORPORT: Sets the analog input port on the controller - starts at 0 to use the first port on the controller (note, starting with NETLabConnect version 1.0, port numbers start at 0 instead of 1 to follow the documentation of the microcontrollers)
- SMOOTHING BUFFER SIZE: Sets the size of the buffer for the smoothing. the larger the number, the more smooth the processed values will be. but the other effect is that the processed values will have a lag time from the actual sensor data. this lag time will increase with the buffer size.
netFeed(feedValue, feedID) Explanation
If you need to access the values generated by the ../AnalogInput (and ../DigitalInput) widgets in ActionScript, the widgets always call the function netFeed(feedValue, feedID) for themselves. So by creating a netFeed() function attached to the ../AnalogInput widget, you can create custom code to process the values.
The parameters for the netFeed(feedValue, feedID) function are:
- feedValue = the value output by the widget, after any processing it does. This is the value seen in the bottom text field in the widget
- feedID = the instance name of the widget as set in the property inspector
The following code example shows how two ../AnalogInput widgets (called analog1 and analog 2) can control the rotation of a movieClip (called rectangle). This code should be put on the first frame of the timeline, and is written in the more modern coding style that avoids putting code on movieClips directly.
Here is an example Flash file with this code: netFeedExample.fla.zip
// initialize the variables that keep the last values of each AnalogInput widget
lastAnalog1 = 0;
lastAnalog2 = 0;
// create a netFeed function on the AnalogInput instance named analog1
analog1.netFeed = processNetFeed;
// create a netFeed function on the AnalogInput instance named analog2
analog2.netFeed = processNetFeed;
// function to handle the values coming in from the AnalogInput widgets
function processNetFeed(feedValue, feedID) {
if (feedID == "analog1") {
_root.lastAnalog1 = feedValue;
} else if (feedID == "analog2") {
_root.lastAnalog2 = feedValue;
}
}
// create an onEnterFrame function for the rectangle to animate it
rectangle.onEnterFrame = function() {
// compare the saved values from the analogInput widgets
if (_root.lastAnalog1 > _root.lastAnalog2) {
// rotate right
this._rotation += 8;
} else if (_root.lastAnalog1 < _root.lastAnalog2) {
// rotate left
this._rotation -= 8;
}
}
setSensorValue(sensorValue) Explanation
Sometimes you need to be able to control the external world (lights, servos, etc.) through code. The ../AnalogInput (and ../DigitalInput) widget provides a mechanism for this, by allowing code to control its values as if it were an external sensor. The ../AnalogInput widget will listen to code through its setSensorValue() function if you set the "controller" parameter of the component in the Properties Inspector to "code". Since all the output widgets can listen to an ../AnalogInput or ../DigitalInput widget, your code can control any output by changing the values of these input widgets.
The parameter for the setSensorValue(sensorValue) function is:
sensorValue = the RAW value used by the ../AnalogInput widget
The following code example shows how simple code can turn a light on or off. In this example, an ../AnalogInput widget (instance name codeAnalog) is controlled by code attached to a movieClip on the stage. Pressing this movieClip (instance name lightButton) sets the sensor value of the widget to 750 and releasing the button sets the value to 0. A ../DigitalOutput widget uses the AnalogInout widget as the source of values to determine if a light (or other device) attached to port 0 should turn on or off.
Here is an example Flash file with this code: controlOutput.fla.zip
// set up functions for movieClip
lightButton.onPress = function () {
// set the value of the AnalogInput widget
_root.codeAnalog.setSensorValue(750);
}
lightButton.onRelease = function() {
// set the value of the AnalogInput widget
_root.codeAnalog.setSensorValue(0);
}