1

I'm working on a simple project for school. I have 4 digit 12 pin display on my breadboard, all hooked up correctly, I can set a default value in my program and it displays just fine. (or get a random nummer, increment this is all working so I don't need any advice on this.

I however need to get a long value from flash to Arduino (well it's 4 digits in actionscript this can be an int but it seems that arduino doesn't read this since it's 8 bits).

I can't however seem to find out how to transfer this to my arduino, I tried different things and I always had mixed results can anyone point out what I'm doing wrong?

Code from AS3 (which is relevant as far as I think) And arduino below

Arduino:

    //used for reading the float
//something I attempted unsuccessfully
union u_tag {
    byte b[4];
    float ival;
} u;

//segments
int a = 6;
int b = 7;
int c = 8;
int d = 9;
int e = 10;
int f = 11;
int g = 12;
int p = 13;
//digits
int d4 = 5;
int d3 = 4;
int d2 = 3;
int d1 = 2;
//other
int n = 401;//display value, this value will be displayed, can be changed
int x = 1;//use this if you want decimals e.g. 10 is 1 decimal
int del = 45;
char commandLetter;  // the delineator / command chooser
char numStr[4];      // the number characters and null
String inString;

void setup()
{
  Serial.begin(9600);           // set up Serial library at 9600 bps

  pinMode(d1, OUTPUT);
  pinMode(d2, OUTPUT);
  pinMode(d3, OUTPUT);
  pinMode(d4, OUTPUT);
  pinMode(a, OUTPUT);
  pinMode(b, OUTPUT);
  pinMode(c, OUTPUT);
  pinMode(d, OUTPUT);
  pinMode(e, OUTPUT);
  pinMode(f, OUTPUT);
  pinMode(g, OUTPUT);
  pinMode(p, OUTPUT);
}

void loop()
{
  //digit 1 Nxxx
  clearLEDs();
  pickDigit(1);
  pickNumber((n/x/1000)%10);
  delayMicroseconds(del);

  //digit 2 xNxx
  clearLEDs();
  pickDigit(2);
  pickNumber((n/x/100)%10);
  delayMicroseconds(del);

  //digit 3 xxNx
  clearLEDs();
  pickDigit(3);
  pickNumber((n/x/10)%10);
  delayMicroseconds(del);

  //digit 4 xxxN
  clearLEDs();
  pickDigit(4);
  pickNumber(n/x%10);
  delayMicroseconds(del);

  //read new data here
  serialEvent();
}

void pickDigit(int x)
{
  digitalWrite(d1, HIGH);
  digitalWrite(d2, HIGH);
  digitalWrite(d3, HIGH);
  digitalWrite(d4, HIGH);

  switch(x)
  {
    case 1: digitalWrite(d1, LOW); break;
    case 2: digitalWrite(d2, LOW); break;
    case 3: digitalWrite(d3, LOW); break;
    default: digitalWrite(d4, LOW); break;
  }
}

void pickNumber(int x)
{
  switch(x)
  {
    default: zero(); break;
    case 1: one(); break;
    case 2: two(); break;
    case 3: three(); break;
    case 4: four(); break;
    case 5: five(); break;
    case 6: six(); break;
    case 7: seven(); break;
    case 8: eight(); break;
    case 9: nine(); break;
  }
}

void dispDec(int x)
{
  digitalWrite(p, LOW);
}

void clearLEDs()
{
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, LOW);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
  digitalWrite(p, LOW);
}

void zero()
{
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, LOW);
}

void one()
{
  digitalWrite(a, LOW);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
}

void two()
{
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, LOW);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, LOW);
  digitalWrite(g, HIGH);
}

void three()
{
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, HIGH);
}

void four()
{
  digitalWrite(a, LOW);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
}

void five()
{
  digitalWrite(a, HIGH);
  digitalWrite(b, LOW);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, LOW);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
}

void six()
{
  digitalWrite(a, LOW);
  digitalWrite(b, LOW);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
}

void seven()
{
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, LOW);
  digitalWrite(g, LOW);
}

void eight()
{
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, HIGH);
  digitalWrite(e, HIGH);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
}

void nine()
{
  digitalWrite(a, HIGH);
  digitalWrite(b, HIGH);
  digitalWrite(c, HIGH);
  digitalWrite(d, LOW);
  digitalWrite(e, LOW);
  digitalWrite(f, HIGH);
  digitalWrite(g, HIGH);
}

void serialEvent() {
  while(Serial.available() > 0) {
          int c = Serial.parseInt();
          if (c > 0) {
            n = c;
          }
      }
}

Actionscript (in frame)

import flash.net.Socket;
import flash.events.Event;

    //only allow numbers, period and minus sign
    //numberInput.restrict = ".0-9\\-";

stage.addEventListener(MouseEvent.CLICK, onConnect);

var socket:Socket = new Socket()
    socket.addEventListener(Event.CONNECT, onConnect);
    socket.addEventListener(Event.CLOSE, onClose);
    socket.addEventListener( IOErrorEvent.IO_ERROR, onError );
    socket.addEventListener( SecurityErrorEvent.SECURITY_ERROR, onError );
    socket.addEventListener( ProgressEvent.SOCKET_DATA, onSocketData );

    //disable until we connect
    this.enabled = false;

    //this is important! If you dont set this to
    //little endian, then Arduino wont understand
    //the bytes
    socket.endian = Endian.LITTLE_ENDIAN;

    socket.connect("127.0.0.1", 5331);

function onConnect( evt:Event):void
{
    //get the number that the user input
    var out:Number = Number("1200");

    //write it as a float to the server.
    //this is important.
    //socket.writeByte(1200);
    socket.writeByte(109);
    socket.flush();
    //if number is too big, then it will overflow on
    //the Arduino, and probably come back as 0.00000
}

function onClose(evt:Event):void {

}

function onError(evt:Event):void {


}

function onSocketData(evt:ProgressEvent):void {
var msg:String = ""; // create a buffer
    while (socket.bytesAvailable) { // while there is byte to read
        var byte:int = socket.readByte();

            msg += String.fromCharCode(byte); // else, we add the byte to our buffer

    }        
    trace("data:" + msg);
}

I use serproxy to make these 2 communicate.

I loaded it up again now (I have this problem for a while) and now I see the default value (n) in arduino keeps resetting after I send data from flash. The data always is 1 right now.

I'd love to know why and what I'm doing wrong.

5
  • You can write long using for instance BigInteger from as3crypto, or LongInt from lodgamebox Commented Apr 19, 2015 at 17:02
  • Hm, hm, interesting topic! I've searched in three old projects, and all I see is this: _socket.writeUTFBytes(data); _socket.flush();, where data is String. To be honest - I don't remember why, but it's always a string representation, always UTF. Or you don't want to cast? Another thing I can share is the way we've used to read data - with delimiter, as sometimes only a partial of the package can arrive (but that's another topic). Commented Apr 19, 2015 at 17:12
  • I see, I know how to send the string and such, but is there any chance you have a good example how 2 read it on the arduino? Commented Apr 19, 2015 at 21:53
  • So nobody have a good arduino side example or everyone using the same. Commented Apr 22, 2015 at 6:40
  • What happens on Arduino side when you send data as a string, the way Andrey Popov suggested? Commented Apr 22, 2015 at 23:34

1 Answer 1

2
+50

Does serial speed match in serproxy and Arduino sketch?

parseInt() does not work with binary data, it expects digits as ascii characters: "1", not 1.

Try to send data by one of string methods of Socket class, e.g:

socket.writeMultiByte("125" , "us-ascii") ;
socket.flush();

and also try to input data manually into serial monitor of the following sketch to see how parseInt() works:

int n = 401;//display value, this value will be displayed, can be changed
boolean new_val = true ;

void setup() {

    Serial.begin(9600);           // set up Serial library at 9600 bps
    // This check is only needed on the Leonardo:
    while (!Serial) {
        ; // Wait for serial port to connect. Needed for Leonardo only
    }
    delay(100) ;
    Serial.println( "Started") ;
}

void loop() {
    serialEvent();

    if (new_val) {
       Serial.print( "n: " ); Serial.println( n , DEC ) ;       
       new_val = false ;
    }

    delay(200) ;
}

void serialEvent() {
    while(Serial.available() > 0) {
        int c = Serial.parseInt();
        if (c != 0) {
           n = c;
           new_val = true ;
        }
    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

I''ll try when I'm at home! makes sense I guess.
Well what if he sends 0 as a digit? :P
Good question. According to source code of Stream::parseInt : return 0; // zero returned if timeout. I do not know how to distinguish between time-out and zero as a value. It is a greeeeey area - do not touch!!! :).
Still need to check it but the locial response to Andrey would be that "0" is not equal to an actual 0 response didn't have time yet hope I can respond tonight if it worked or not.
Yeah, I hope I did not earn bounty for nothing.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.