Flash LED Dimmer

Hello everybody,

For testing purposes i am trying to make a LED dimmer (with Flash) : according to the x position of a flash movie which is 255 pixels width.

This test above is actually an adaptation of the LED dimmer with Processing described here: [u]http://arduino.cc/en/Tutorial/Dimmer[/u]

With processing this is working great. My tests with Flash - using serproxy - are working crappy: the LED blinks somehow randomly.

If you permit, i paste my code below. I don't know exactly what i am doing wrong.... can someone take a look at this?


This is the arduino code:


int ledPin = 9;

void setup()
{
  // begin the serial communication
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
}

void loop()
{
  byte val;

  // check if data has been sent from the computer
  if (Serial.available()) {
    // read the most recent byte (which will be from 0 to 255)
    val = Serial.read();
          
    // set the brightness of the LED
    analogWrite(ledPin, val);
  }
}

And this is the flash code:


var _value:Number = 0;
var myInterval:Number;

updateValue = function()
      {
            debug_txt.text = _root._xmouse;
            _value = _root._xmouse;    // sets the value of the x position of the mouse
      };


looper = function()
      {
            serialServer.send(_value);
      };

openBtn.onRelease = function() {  // button to create the socket
      createSocket();
};

closeBtn.onRelease = function() {  // button to close the socket
      
      clearInterval(myInterval);
      serialServer.close();
      connectionStateTxt.text = "Connection closed...";
};

function createSocket() {
      
      serialServer = new XMLSocket();
      connectionStateTxt.text = "made it"+serialServer;
      serialServer.connect("127.0.0.1", 5331);
      serialServer.onConnect = function(success) {
            
            _root.onMouseMove = updateValue ;  // everytime mouse moves this function is called
            myInterval = setInterval(looper,30);  // the looper function is called every 30ms (the frame rate of the movie is also 30fps 
            
      };
      
      serialServer.onClose = function() {
            connectionStateTxt.text = "closed";
      };
      
      
}

Hi all,

after some tests I managed to get a satisfying solution to make the dimmer in flash (AS 2.0) Hope it helps someone.

FLASH CODE InfoObj.as

class InfoObj {
      
      public var valid:Boolean;
      private var info:String = null;
      
      public function InfoObj(){
            valid = false;
            
            }
      public function setInfo(s:String){
            
                  this.info = s;
            };
      public function getInfo():String{
            return this.info ; 
            
            };
            
      
      }

code in the .FLA

// flash dimmer based on X position of the mouse pointer
// the swf shoud be 240 pixel wide


var _value:Number = 0;
var myInterval:Number;   // a reference of the interval
var infoObj:InfoObj = new InfoObj();  // an object that holds the data
var startString = "*";  // a 'start' character that we will sent whenever we sent data to serproxy

var numberOfIntervals:Number = 60;
var maxValue:Number = 240;
var lastInfoSent:String = null ;  // a reference that will hold the last info sent to serproxy




this.startPulseBtn.onRelease = function(){
      
      // execute the my pulser function each 25 ms
      myInterval = setInterval(myPulser,25);
      
      
      // update the value of the _value variable whenever mouse is moved
      _root.onMouseMove = updateValue ;
      
      }

this.stopPulseBtn.onRelease = function(){

      // stop the my pulser function
      clearInterval(myInterval)
      }

      

updateValue = function()
      {
            
            // calculation of the 'x-area'
            _value = Math.floor((_root._xmouse/maxValue)*numberOfIntervals);
            
            // set the value of the infoObj
            infoObj.setInfo(String(_value));//
      
      };



openBtn.onRelease = function() {  
      
      // button to create the socket
      
      createSocket();
};

closeBtn.onRelease = function() {  

// button to close the socket
      
      serialServer.close();
      
};




function createSocket() {
      
      // we all agree on this :)
      
      serialServer = new XMLSocket();
      connectionStateTxt.text = "made it"+serialServer;
      serialServer.connect("127.0.0.1", 5331);
      
      
      
      
}

function sendTheInfo(s:String){
      
      // send the corresponding ASCII value to the arduino (we start from 'A' (=65)
      // we also send a startString '*' to arduino as a control-method
      serialServer.send(startString + String.fromCharCode(65+Number(s)));
      
      };


function myPulser(){
      
      
      // this function is executed every 25 millis
      
      
      // checks if the last info sent to the arduino differs from the actual info,
      // if so the infoObj contains invalid info and will not be sent again.
      
      if (lastInfoSent==infoObj.getInfo()){
            
            infoObj.valid = false;
            
            } else {
            
            infoObj.valid = true;
                  
            }
            
      // if you want to debug...
      //trace("* " +  infoObj.getInfo() + "..." + infoObj.valid + "\n");
      
      if(infoObj.valid == true){
            
            // if the info is valid then the info will be sent to the arduino
            
            sendTheInfo(infoObj.getInfo())
            
            // lastInfo variable will be stuffed with the actual info,
            // and the infoobj'sis not valid anymore
            
            lastInfoSent = infoObj.getInfo();
            infoObj.valid = false;
            }
else {
      
      // do nothing, forget about it
      
      }
      
      };

code on the arduino

// this program reads continously the serial data
// when it receives a '*' character it knows that the next byte will be 
// 'interesting' data  :)

byte zeroByte, temp; 
boolean readyToReceive;

void setup() 
{ 
 
  zeroByte = 0;
  readyToReceive = false;
  pinMode(9, OUTPUT);
  Serial.begin(9600);
  

}

void loop() {      
if ((Serial.available() > 0)&& (readyToReceive == false)) 
    { 
      // we first check if the available byte is a startByte, if so.. arduino is ready to receive
      
      temp = Serial.read();
      if (temp =='*')
        {
           readyToReceive = true;
          // Serial.println("I received a S" + zeroByte);
        } else {
        
        readyToReceive = false;
        }
    
} else if ((readyToReceive == true)&&(Serial.available() > 0)){     

            {  
                 temp = Serial.read();
                   
                     // if arduino is ready to receive we check if the next byte is between the right range    
                     
                    if ((temp >= 65)&&(temp<=126))     // if temp = {'A','B',...,'D','~'} the decimal value of 'A' is 65 we multiply this by 60 the get a range from 0 to 240
                              {
                                   temp = 4 * (temp - 65);
                                   
                                   // if so, we recalculate the value so that the value is between 0 and 240 (= 4*60 )
                                   
                                   analogWrite(9, temp);
                                   
                                   // now arduino has to wait for another startByte
                                   
                                  readyToReceive = false ; 
                              }             
           }
     }
}

Hi Wimber, Thanks for sharing.

If you want, you can simplify the Arduino code by taking advantage of the fact that multiple logical expressions are always evaluated in the order they are written and the evaluaton stops as soon as the expression becomes false. So you can say:
if ((Serial.available() > 1) && (Serial.read() == ‘’) )
and the code will only evaluate true when there are at least two characters and the first is a '
’’. If there are not two or more chars available, the Serial.read is not executed. This allows you to dispense with all the code relating to the global variable readyToReceive.

void setup() 
{ 
  pinMode(9, OUTPUT);
  Serial.begin(9600);
}

void loop() {   
  // test if there are two chars available and the first is a '*'
  if ((Serial.available() > 1) && (Serial.read() == '*') )   { 
    int temp = Serial.read();               
    // check if the byte after the '*' is between the right range                 
    if ((temp >= 65)&&(temp<=126)){     // if temp = {'A','B',...,'D','~'} the decimal value of 'A' is 65 we multiply this by 60 the get a range from 0 to 240   
      temp = 4 * (temp - 65);  // we recalculate the value so that the value is between 0 and 240 (= 4*60 )
      analogWrite(9, temp);
    }             
  }
}