Skip to content

Instantly share code, notes, and snippets.

@cypres
Created February 25, 2012 19:13
Show Gist options
  • Select an option

  • Save cypres/1910149 to your computer and use it in GitHub Desktop.

Select an option

Save cypres/1910149 to your computer and use it in GitHub Desktop.
Tradeshow Light Control
/*
* Frame 1
*/
// Initialize the Arduino and set up event handlers
var arduino:ArduinoControl = new ArduinoControl("127.0.0.1",5331);
arduino.addEventListener(Event.CONNECT, onArduinoConnect);
arduino.addEventListener(IOErrorEvent.IO_ERROR, reconnect);
function onArduinoConnect( event:Event ):void
{
trace("conn: activatePin 1");
arduino.activatePin(1);
}
function reconnect( event:Event ):void
{
arduino.close();
arduino.connect("127.0.0.1",5331);
}
/*
* Frame 2
*/
// For this frame, connect or reconnect
if (!arduino.connected) arduino.connect("127.0.0.1",5331);
arduino.activatePin(1);
/*
* Frame 25
*/
arduino.activatePin(2);
/*
* Frame 50
*/
arduino.activatePin(3);
/*
* Frame 75
*/
gotoAndPlay(2);
package
{
import flash.events.DataEvent;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.events.SecurityErrorEvent;
import flash.net.Socket;
public class ArduinoControl extends EventDispatcher
{
private var _host : String;
private var _socket : Socket;
public function ArduinoControl( host:String = null, port:int = 0 )
{
super();
_socket = new Socket();
_socket.addEventListener( Event.CLOSE, onClose );
_socket.addEventListener( Event.CONNECT, onConnect );
_socket.addEventListener( IOErrorEvent.IO_ERROR, onIOErrorEvent );
_socket.addEventListener( SecurityErrorEvent.SECURITY_ERROR, onSecurityError );
_socket.addEventListener( ProgressEvent.SOCKET_DATA, onSocketData );
}
public function get connected():Boolean
{
return _socket.connected;
}
public function close():void
{
if (_socket && _socket.connected) _socket.close();
}
public function connect( host:String = null, port:int = 0 ):void
{
_socket.connect( host, port );
trace( "connecting" );
}
public function activatePin( i:int):void
{
trace("Activate pin: "+new String(i));
if (_socket.connected) {
_socket.writeUTFBytes(new String(i));
_socket.writeUTF("\n");
_socket.flush();
} else {
trace("Socket not connected");
}
}
public function send( value:String ):void
{
_socket.writeUTFBytes( value );
_socket.flush();
}
private function onClose( event:Event ):void
{
trace( "onClose" );
dispatchEvent( event );
}
private function onConnect( event:Event ):void
{
trace( "onConnect" );
dispatchEvent( event );
}
private function onIOErrorEvent( event:IOErrorEvent ):void
{
trace( "onIOErrorEvent" );
dispatchEvent( event );
}
private function onSecurityError( event:SecurityErrorEvent ):void
{
trace( "onSecurityError" );
dispatchEvent( event );
}
private function onSocketData( event:ProgressEvent ):void
{
dispatchEvent( new DataEvent( DataEvent.DATA, false, false, _socket.readUTFBytes( _socket.bytesAvailable ) ) );
}
}
}
int ledPins[] = {
22,23,24,25,26,27,
28,29,30,31,32,33,
36,37,38,39,40,41,
46,47,48,49,50,51
};
int n;
int numPins=24;
void setup() {
for (int i=0;i<numPins;i++) {
pinMode(ledPins[i], OUTPUT);
}
n=0;
}
void loop() {
if (++n >= numPins) n=0;
digitalWrite(ledPins[n], HIGH);
delay(50);
digitalWrite(ledPins[n], LOW);
}
// Setup the led pin mappings
int ledPins[] = {
22,23,24,25,26,27,
28,29,30,31,32,33,
36,37,38,39,40,41,
46,47,48,49,50,51
};
int n;
int numPins=24;
// Declare som stringy params for serial input
String inputString = "";
boolean stringComplete = false;
void setup() {
// Initialize the LEDs
for (int i=0;i<numPins;i++) {
pinMode(ledPins[i], OUTPUT);
}
n=0;
// Initialize the serial
Serial.begin(9600);
inputString.reserve(100);
// Run a test of the LEDs
for (int i=0;i<numPins;i++) {
digitalWrite(ledPins[i], HIGH);
delay(300);
digitalWrite(ledPins[i], LOW);
}
}
// Main loop, simply checks if serial has any input for us
void loop() {
// If input string is marked complete, process it
if (stringComplete) {
// Convert input into integer
char pinChars[3];
inputString.toCharArray(pinChars,3);
int pin = atoi(pinChars);
// clear the string:
inputString = "";
stringComplete = false;
// If conversion was successful and it valid turn it on
if (pin == -1) {
// -1 means turn off all LEDs
for (int i=0;i<numPins;i++) {
digitalWrite(ledPins[i], LOW);
}
} else if (pin != 0 && pin<=numPins) {
// First turn off all other LEDs
for (int i=0;i<numPins;i++) {
digitalWrite(ledPins[i], LOW);
}
// Turn on requested LED
digitalWrite(ledPins[pin-1], HIGH);
}
}
}
// This will get called on all serial inputs
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
if (inChar == '\n' || inChar == '\r' || inChar == '\0') {
stringComplete = true; // mark input string as ready for processing
} else {
inputString += inChar;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment