| |
Code to handle Arduino Analog Input Events
private function onReceiveAnalogData(e:ArduinoEvent):void {
if (analog_input1 == "on" && SlideShowRunning ==0 && idle_start_playing == 1 && initial_values_done ==1 && video_playing ==0) { //input pin must be turned on and slideshow not running
if (a.getAnalogData(analog_input1_pin) == 0) { //change this back to 0 when the phidget sensor is hooked up
Touch1PressedTimer.start(); //start this timer to make sure its been pressed for 100ms
}
}
if (analog_input2 == "on" && SlideShowRunning ==0 && idle_start_playing == 1 && initial_values_done ==1 && video_playing ==0) { //only if on to avoid ghost readings
if (a.getAnalogData(analog_input2_pin) == 0) {
Touch2PressedTimer.start(); //start this timer to make sure its at 0 for a while
}
}
if (analog_input3 == "on" && SlideShowRunning ==0 && idle_start_playing == 1 && initial_values_done ==1 && video_playing ==0) { //wait for idle video before listening for inputs
if (a.getAnalogData(analog_input3_pin) == 0) { //this is the third touch sensor, plays video1
Touch3PressedTimer.start();
}
}
if (proximity_sensor_on == "on" && SlideShowRunning ==0 && idle_start_playing == 1) { //only if set to on from config file
raw_distance.text = String("Proximity Raw Distance= " + a.getAnalogData(proximity_sensor_pin));
proximity_readout.text = String(a.getAnalogData(proximity_sensor_pin));
if (a.getAnalogData(proximity_sensor_pin) > proximity_sensor_trigger1_distance_lower && a.getAnalogData(proximity_sensor_pin) < proximity_sensor_trigger1_distance_upper ) { //make sure subject is standing in the right spot for a while
proximity_counter++;
if (LEDOffFlag == 1) { //then the LED has been off for long enough so turn it on
LEDOffFlag = 0; //the led is on so turn the flag on, cannot come back until after the LED has been turned off and delay reached
a.writeDigitalPin(AboveFireLed_pin, Arduino.HIGH); //turn on the LED and then use the timer to turn it off
LEDTimer.start(); //start this timer, goes off in 50ms
}
//now let's set a time which will then turn it off, essentially a blink
//trace (proximity_counter);
if (verbose == "yes") {SensorText.text = String("Proximity Match= " + proximity_counter)}; //show the counter in the text box
}
if (proximity_counter == proximity_sensor_trigger1_time && proximity1_do_not_play_again ==0) { //now play the first proximity clip to come closer
FirstProxVideoPlaying = 1; //it gets reset to 0 on clip done routine
a.writeDigitalPin(AboveFireLed_pin, Arduino.HIGH); //turn on the green led above the fire, do not turn off until coming closer
if (x10_commands == "on") {X10_OFF(x10_address1)};
myVid.play(proximity1_clip);
}
if (proximity_flag ==1) { // if close enough, then play the second proxity clip
if (a.getAnalogData(proximity_sensor_pin) < proximity_sensor_trigger2_distance) {
a.writeDigitalPin(AboveFireLed_pin, Arduino.LOW); //they came closer so turn it off now
if (x10_commands == "on") {
X10_OFF(x10_address1);
X10_OFF(x10_address1);
}
if (proximity_flag3 == 0) {
myVid.play(proximity2_clip);
proximity_flag3 = 1;
}
else {
myVid.play(proximity3_clip);
proximity_flag3 = 0;
}
//proximity_counter = 0; //reset all the counters //
proximity_flag = 0; //set this to zero right away so we don't repeat it, the first clip has to play first
}
}
}
if (hardware_mode_select == "on" && SlideShowRunning ==0) {
var num:int;
modeSelect.text = String("Character Select Potentiometer=" + a.getAnalogData(hardware_mode_select_pin)); //pin 4
hardware_mode_select_value = a.getAnalogData(hardware_mode_select_pin); //use this for hardware mode select
num = (initial_hardware_mode_select_value - a.getAnalogData(hardware_mode_select_pin));
var numAbsolute:int = Math.abs(num);
if (numAbsolute > 75) { // was 75, if the change is greater than 175, then check to see if need to change mode
initial_hardware_mode_select_value = a.getAnalogData(hardware_mode_select_pin); //reset the initial value
if (verbose == "yes") {zero_analog.text = String(numAbsolute)};
ModeSelectTimer.start(); //start the timer for 1/2 second
}
}
//} //end if for initial done
} //************************************ end onreceive analog function
private function Touch1PressedTimerEvent(e:TimerEvent):void { //weather
if (a.getAnalogData(analog_input1_pin) == 0) { //now check if its still 0, it had to have been zero for 100ms
if (x10_commands == "on") {
X10_OFF(x10_address1);
}
if (verbose == "yes") {
one_analog.text = "Touched";
touch_counter_text.text = "Touch Counter: " + String(touch_counter);
Touch1Timer.start()//start timer for three seconds and then reset the label
}
touch_counter++; //this tracks how many touches have happened
video_playing = 1; //set the video playing flag
weatherUpdate();
}
}
private function Touch2PressedTimerEvent(e:TimerEvent):void { //stock
if (a.getAnalogData(analog_input2_pin) == 0) { //now check if its still 0, it had to have been zero for 100ms
if (x10_commands == "on") {
X10_OFF(x10_address1);
}
if (verbose == "yes") {
two_analog.text = "Touched";
touch_counter_text.text = "Touch Counter: " + String(touch_counter);
Touch2Timer.start()//start timer for three seconds and then reset the label
}
touch_counter++;
video_playing = 1; //set the video playing flag
stockUpdate();
}
}
private function Touch3PressedTimerEvent(e:TimerEvent):void { //x10 on off
if (a.getAnalogData(analog_input3_pin) == 0) { //now check if its still 0, it had to have been zero for 100ms
if (verbose == "yes") {
three_analog.text = "Touched";
touch_counter_text.text = "Touch Counter: " + String(touch_counter);
Touch3Timer.start()//start timer for three seconds and then reset the label
}
touch_counter++;
video_playing = 1; //set the video playing flag
if (x10_on_off_flag == 0) { //play this one, set the flag which will play the alternative for next time and then back to this
myVid.play(x10_on_clip); //call the x10 commands from the onclip function
x10_on_off_flag = 1;
}
else {
myVid.play(x10_off_clip);
x10_on_off_flag = 0; //reset flag so toggles back on next play
}
}
}
private function Touch1TimerEvent(e:TimerEvent):void {
one_analog.text = "Touch Sensor 1";
Touch1Timer.reset(); //reset the timer
}
private function Touch2TimerEvent(e:TimerEvent):void {
two_analog.text = "Touch Sensor 2";
Touch2Timer.reset(); //reset the timer
}
private function Touch3TimerEvent(e:TimerEvent):void {
three_analog.text = "Touch Sensor 3";
Touch3Timer.reset(); //reset the timer
}
private function ModeSelectTimerEvent(e:TimerEvent):void { //hardware mode select timer function
switch_mode(); //call the switch mode function to change modes
ModeSelectTimer.reset(); //reset the timer
}
private function LEDoff(e:TimerEvent):void { //turns the LED off
if (FirstProxVideoPlaying == 0) { //don't allow turn off if the first proximity video is playing
a.writeDigitalPin(AboveFireLed_pin, Arduino.LOW); //turn LED off
}
LEDTimer.reset(); //reset the timer
LEDTimerOff.start(); //start the other time to control how long the LED is off before turning back on
}
private function LEDon(e:TimerEvent):void { //how long to wait before the LED can be turned back on
LEDOffFlag = 1; //the LED has been off for long enough so now set flag that its ok to turn the LED back on again
LEDTimerOff.reset(); //reset the timer and then its called again when needed above
} |
| |
X-10 commands to interface with the X-10 CM17A private function X10_ON(address:String):void {
var x10_on_address = address;
x10_last = address;
Get_X10_Array(x10_on_address); //pass the x10 on address to the x10 get array function
x10_array = X10_ON_ARRAY; //the x10 array function ran and now we can assign the array
x10_array.push (1,0,1,0,1,1,0,1); //add these to the end of the array, its the last x10 byte
x10_array.unshift(1,1,0,1,0,1,0,1,1,0,1,0,1,0,1,0); //add these to the beginning of the array
X10(); //call the actual x10 function using the x10_array that was obtained from the get x10 array function call
x10_label.text = String("Last X10 Command: " + x10_last + " ON");
trace ("Last x10 Command: " + address + " ON");
trace ("X10 ON ARRAY: " + X10_ON_ARRAY);
trace ("x10_array: " + x10_array);
} //end function
private function X10_OFF(address:String):void {
var x10_off_address = address;
x10_last = address;
Get_X10_Array(x10_off_address); //pass the x10 on address to the x10 get array function
x10_array = X10_OFF_ARRAY; //the x10 array function ran and now we can assign the array
x10_array.push (1,0,1,0,1,1,0,1); //add these to the end of the array, its the last x10 byte
x10_array.unshift(1,1,0,1,0,1,0,1,1,0,1,0,1,0,1,0); //add these to the beginning of the array
X10(); //call the actual x10 function using the x10_array that was obtained from the get x10 array function call
x10_label.text = String("Last X10 Command: " + x10_last + " OFF");
trace ("Last x10 Command: " + address + " OFF");
trace ("X10 OFF ARRAY: " + X10_OFF_ARRAY);
trace ("x10_array: " + x10_array);
} //end function
private function X10_BRIGHT(address:String):void {
var x10_bright_address = address;
x10_last = address;
Get_X10_Array_BrightDim(x10_bright_address); //pass the x10 on address to the x10 get array function
x10_array = X10_BRIGHT_ARRAY; //the x10 array function ran and now we can assign the array
x10_array.push (1,0,1,0,1,1,0,1); //add these to the end of the array, its the last x10 byte
x10_array.unshift(1,1,0,1,0,1,0,1,1,0,1,0,1,0,1,0); //add these to the beginning of the array
X10(); //call the actual x10 function using the x10_array that was obtained from the get x10 array function call
x10_label.text = String("Last X10 Command: " + x10_last + " BRIGHT");
trace ("Last x10 Command: " + address + " ON");
trace ("X10 BRIGHT ARRAY: " + X10_BRIGHT_ARRAY);
trace ("x10_array: " + x10_array);
} //end function
private function X10_DIM(address:String):void {
var x10_dim_address = address;
x10_last = address;
Get_X10_Array_BrightDim(x10_dim_address); //pass the x10 on address to the x10 get array function
x10_array = X10_DIM_ARRAY; //the x10 array function ran and now we can assign the array
x10_array.push (1,0,1,0,1,1,0,1); //add these to the end of the array, its the last x10 byte
x10_array.unshift(1,1,0,1,0,1,0,1,1,0,1,0,1,0,1,0); //add these to the beginning of the array
trace ("X10 DIM ARRAY: " + X10_DIM_ARRAY);
X10(); //call the actual x10 function using the x10_array that was obtained from the get x10 array function call
x10_label.text = String("Last X10 Command: " + x10_last + " DIM");
} //end function
private function X10():void {
x10_in_progress = 1; //set this flag so nothing else happens until this function is done
x10_counter = 0; //make sure to set this to zero to start
a.writeDigitalPin(DTR_pin,Arduino.LOW);
a.writeDigitalPin(RTS_pin,Arduino.LOW);
x10_sleep_initial.start(); //sleep for 1 ms and then set to high
} //end function
///*************** x10 timer functions
private function x10_sleep_initial_event(e:TimerEvent):void {
//***** Put firecracker into standby mode *******
a.writeDigitalPin(DTR_pin,Arduino.HIGH);
a.writeDigitalPin(RTS_pin,Arduino.HIGH);
x10_sleep_initial.reset(); //reset the timer
x10_sleep35.start();
}
private function x10_sleep35_event(e:TimerEvent):void { //delay 35ms before sending the packet
x10_sleep35.reset();
x10_main(); //call the x10 main function to start writing bits
}
private function x10_main():void {
if (x10_counter < 40) {
if (x10_array[x10_counter]==1) {
a.writeDigitalPin(DTR_pin, Arduino.LOW); // 1 = RTS HIGH/DTR-LOW
x10_counter++;
x10_done1.start();
}
else {
a.writeDigitalPin(RTS_pin, Arduino.LOW);
x10_counter++;
x10_done1.start();
}
}
else {
x10_sleep1000.start(); //done with the 40 bits so now wait a second
}
} //end x10 main function
private function x10_done1_event(e:TimerEvent):void {
a.writeDigitalPin(DTR_pin,Arduino.HIGH);
a.writeDigitalPin(RTS_pin,Arduino.HIGH);
x10_done1.reset();
x10_done2.start();
}
private function x10_done2_event(e:TimerEvent):void {
x10_done2.reset();
x10_main();
}
private function x10_sleep1000_event(e:TimerEvent):void {
x10_in_progress = 0; // now clear this so other videos can play
x10_sleep1000.reset();
} |