manipulation// ------------------------------------------------------------------------ \\// [INI] Speedvoid detectChangeSpeed() { if (changingSpeed) return; if (!(lastSpeed < analogSpeed + 6 && lastSpeed > analogSpeed - 6)) { showGraderSpeed(); }}void showGraderSpeed() { changingSpeed = true; digitalWrite(DEBUG_PIN, 1); lcd.clear(); writeInLcd("Speed:", LCD_COLUMNS); // Create display velocity showSpeed: for (unsigned int i = 0; i < 10; i++) { int charsSpeed = map(analogSpeed, 0, 1023, 0, LCD_COLUMNS); int percentSpeed = map(analogSpeed, 0, 1023, 0, 100); char* percentString; itoa(100 - percentSpeed, percentString, 10); percentString = strcat(percentString, "%"); lcd.setCursor(7, 0); writeInLcd(percentString, 4); charsSpeed = LCD_COLUMNS - charsSpeed; lcd.setCursor(0, 1); for (unsigned int x = 1; x < LCD_COLUMNS + 1; x++) { if (x <= charsSpeed) { lcd.print(">"); } else { lcd.print(" "); } } lastSpeed = analogSpeed; smartDelay(40); } // Verify for (unsigned int i = 0; i < 10; i++) { if (!(lastSpeed < analogSpeed + 6 && lastSpeed > analogSpeed - 6)) { goto showSpeed; } smartDelay(40); } // Finalize lcd.clear(); changingSpeed = false; digitalWrite(DEBUG_PIN, 0);}long applySpeed (unsigned int value) { return map(analogSpeed, 0, 1023, 0, value);}// [END] Speed// ------------------------------------------------------------------------ \\// [INI] Milliseconds controllervoid smartDelay(int milliseconds) { if (milliseconds < 2) { noDelayFunctions(); delay(milliseconds); return; } do { delay(1); milliseconds -= noDelayFunctions(); milliseconds -= 1; } while (milliseconds > 0);}int noDelayFunctions() { time = millis(); // [INI] No delay functions here refreshAnalogVars(); detectChangeSpeed(); detectCommand(); detectSerialMessage(); // [END] No delay functions here return millis() - time;}// [END] Milliseconds controllerActionScript (usando Serialproxy):package br.com.bsoares.air.airtwitterhardware{ import flash.events.Event; import flash.events.EventDispatcher; import flash.events.IOErrorEvent; import flash.events.ProgressEvent; import flash.net.Socket; /** * Message Control * Control messages between the Arduino and in AIR * Application using Serial Proxy. * * @author Bruno Soares * @link http://www.bsoares.com.br */ public class MessageControl extends EventDispatcher { // Properties private var _host:String; private var _port:uint; private var _socket:Socket; private var _messages:Array; private var _currentMessage:uint; private static var _instance:MessageControl; private static var _allowInstantiation:Boolean = false; // Constructor public function MessageControl() { if (!_allowInstantiation) throw new Error("Use instance property (this is a Singleton Class)."); init(); } // Logic private function init():void { _host = "127.0.0.1"; // COM2 _port = 5332; _messages = [ ]; _currentMessage = 0; socketConnect(); } private function socketConnect():void { _socket = new Socket(); _socket.addEventListener(ProgressEvent.SOCKET_DATA, onSocketData); _socket.addEventListener(Event.CLOSE, onSocketClose); _socket.addEventListener(IOErrorEvent.IO_ERROR, onSocketIoError); _socket.connect(_host, _port); } private function processData(data:String):void { trace("Arduino command:", data); switch (true) { // Send next message case data == "2" || data == "0": { sendNextMessage(); break; } // Send previous message case data == "1": { sendPreviousMessage(); break; } } } private function sendMessage(message:String):void { if (message == null || message == "") return; trace("Message:", message); // MESSAGE_START _socket.writeUTFBytes("^"); _socket.flush(); // MESSAGE for (var i:uint = 0; i < message.length; i++) { _socket.writeUTFBytes(message.charAt(i)); } _socket.flush(); // MESSAGE_END _socket.writeUTFBytes("~"); _socket.flush(); dispatchEvent(new Event(Event.CHANGE)); } private function sendNextMessage():void { _currentMessage = _currentMessage == _messages.length - 1 ? 0 : _currentMessage + 1; sendMessage(_messages[_currentMessage]); } private function sendPreviousMessage():void { _currentMessage = _currentMessage == 0 ? _messages.length - 1 : _currentMessage - 1; sendMessage(_messages[_currentMessage]); } // Events private function onSocketData(event:ProgressEvent):void { while (_socket.bytesAvailable > 0) processData(_socket.readUTFBytes(1)); } private function onSocketClose(event:Event):void { socketConnect(); } private function onSocketIoError(event:IOErrorEvent):void { trace(event.text); } // Getters and Setters public