Pages: [1]   Go Down
Author Topic: Sending data from Arduino to uVGA-II in GFX (4DGL) Mode  (Read 1509 times)
0 Members and 1 Guest are viewing this topic.
Andrews SC
Offline Offline
Full Member
***
Karma: 0
Posts: 240
If Arduino is not the answer, you are asking the wrong question!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm migrating our micrometer project (http://www.instructables.com/id/Interfacing-a-Digital-Micrometer-to-a-Microcontrol/), which was working very well, from SGC mode (serial) to GFX mode (4DGL), due to speed issues. I've stripped out all the uVGA code that was being generated on the Arduino, so the Arduino simply collects data from the micrometer and sends it to the uVGA-II for further processing (serial 3 on the Arduino 2560 to serial 1 on the uVGA). The problem is actually a 4DGL issue, reading the data presented by the Arduino, and stuffing it into a variable. I can display the last character the Arduino sends, but not all 5 digits.

Anyone with 4DGL experience care to help out? (I'm sure I'll get "why don't you take this to 4D tech support". Did that already.) I'm stumped.

Arduino sketch attached, 4DGL test code below:

4DGL code reading the Arduino data from Serial 1:

Code:
#platform "uVGA-II_GFX2"
/*
     - serial interface test-
      -- PICASO Platform --
*/

#inherit "4DGL_16bitColours.fnc"

func main()
    var ch;
    gfx_Cls();
    txt_Set(FONT_SIZE, FONT2);
    gfx_Rectangle(75,75,140,120,WHITE);


    // now just stay in a loop
    repeat
        gfx_MoveTo(90,100);
        ch := serin1();

        if (ch != -1)
            print( [CHR] ch );   // if a key was received from PC, print its ascii value

        endif

    forever

endfunc

* micrometerGFX.ino (1.35 KB - downloaded 14 times.)
« Last Edit: May 29, 2012, 08:05:48 am by Steve Spence - KK4HFJ » Logged

Steve Spence - ET/AT/R&D KK4HFJ

Andrews SC
Offline Offline
Full Member
***
Karma: 0
Posts: 240
If Arduino is not the answer, you are asking the wrong question!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Would it help if during sending from the Arduino, I did a

Serial3.print(num, DEC);

instead of

Serial3.println(num);

I think I need to add a "separator" so the receiving unit knows where start and stop is.

Serial3.print(num, DEC);
Serial3.print("$");


Now I'm sending the ascii values 54 55 52 55 36 (6 7 4 7 $). On the other end, I need to read in until I hit $, and save to a variable.
« Last Edit: May 29, 2012, 12:57:26 pm by Steve Spence - KK4HFJ » Logged

Steve Spence - ET/AT/R&D KK4HFJ

Global Moderator
Melbourne, Australia
Offline Offline
Brattain Member
*****
Karma: 511
Posts: 19350
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

... due to speed issues.

Quote
Code:
void setup(){
  Serial3.begin(9600);
  Serial.begin(9600);

9600 baud is not going to help with the speed issues. Can't you go faster?

Your code:

Code:
int req = 5; //mic REQ line goes to pin 5 through q1 (arduino high pulls request line low)
int dat = 2; //mic Data line goes to pin 2
int clk = 3; //mic Clock line goes to pin 3
int i = 0; int j = 0; int k = 0;
byte mydata[14];
int num;


void setup(){
  Serial3.begin(9600);
  Serial.begin(9600);
  pinMode(req, OUTPUT);
  pinMode(clk, INPUT);
  pinMode(dat, INPUT);
  digitalWrite(clk, HIGH); // enable internal pull ups
  digitalWrite(dat, HIGH); // enable internal pull ups
  digitalWrite(req,LOW); // set request at LOW
 
}

void loop(){
 
// get data from mic

  digitalWrite(req, HIGH); // generate set request

    for( i = 0; i < 13; i++ ) {

      k = 0;

      for (j = 0; j < 4; j++) {

      while( digitalRead(clk) == LOW) { } // hold until clock is high

      while( digitalRead(clk) == HIGH) { } // hold until clock is low

        bitWrite(k, j, (digitalRead(dat) & 0x1)); // read data bits, and reverse order )

      }

// extract data

      mydata[i] = k;

//      sign = mydata[4];

//      decimal = mydata[11];

//      units = mydata[12];

    }
   
// assemble measurement from bytes   

char buf[7];

for(int lp=0;lp<6;lp++)

  buf[lp]=mydata[lp+5]+'0';

buf[6]=0;

num=atol(buf); //assembled measurement, no decimal place added

 Serial3.println(num);
 Serial.println(num);
    digitalWrite(req,LOW);

   //  delay(25);

}

I don't see Serial3.available or Serial3.read in there. Are you trying to do you own software serial?  Why?
Logged

http://www.gammon.com.au/electronics

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

Andrews SC
Offline Offline
Full Member
***
Karma: 0
Posts: 240
If Arduino is not the answer, you are asking the wrong question!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The speed problems were trying to build graphics on the arduino and output them serially to the uVGA. Now all the graphics are moved to the uVGA, and all I'm sending over to the uVGA is a 5 digit value, from variable num, in ascii. I can increase the speed later once I get it all working if necessary.

Serial3 is an output, not an input. I'm not doing any reads on the Arduino. I am trying to on the uVGA, on Serial1.

Serial.println(num); was just being used for debugging (serial monitor). I've // it out in the real version.


Logged

Steve Spence - ET/AT/R&D KK4HFJ

Global Moderator
Melbourne, Australia
Offline Offline
Brattain Member
*****
Karma: 511
Posts: 19350
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Would it help if during sending from the Arduino, I did a

Serial3.print(num, DEC);

instead of

Serial3.println(num);

Yes that will help. You are sending each digit on its own line.
Logged

http://www.gammon.com.au/electronics

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

Andrews SC
Offline Offline
Full Member
***
Karma: 0
Posts: 240
If Arduino is not the answer, you are asking the wrong question!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Success! Thanks to 4DSysFan at http://4d.websitetoolbox.com/post/Converting-from-SGC-to-GFX-5858304, I now have an integer variable on the uVGA-II (result) that contains the same data as the integer variable on the Arduino (num). The print statements to the serial port had to be modified again, as the uVGA-II is looking for a $ as a start of packet, and a CR as the end of the packet:

  Serial3.print("$");
  Serial3.println(num, DEC);


Arduino sketch:

Code:
int req = 5; //mic REQ line goes to pin 5 through q1 (arduino high pulls request line low)
int dat = 2; //mic Data line goes to pin 2
int clk = 3; //mic Clock line goes to pin 3
int i = 0; int j = 0; int k = 0;
byte mydata[14];
int num;


void setup(){
  Serial3.begin(9600);
  Serial.begin(9600);
  pinMode(req, OUTPUT);
  pinMode(clk, INPUT);
  pinMode(dat, INPUT);
  digitalWrite(clk, HIGH); // enable internal pull ups
  digitalWrite(dat, HIGH); // enable internal pull ups
  digitalWrite(req,LOW); // set request at LOW
  
}

void loop(){
 
// get data from mic

  digitalWrite(req, HIGH); // generate set request

    for( i = 0; i < 13; i++ ) {

      k = 0;

      for (j = 0; j < 4; j++) {

      while( digitalRead(clk) == LOW) { } // hold until clock is high

      while( digitalRead(clk) == HIGH) { } // hold until clock is low

        bitWrite(k, j, (digitalRead(dat) & 0x1)); // read data bits, and reverse order )

      }

// extract data

      mydata[i] = k;

//      sign = mydata[4];

//      decimal = mydata[11];

//      units = mydata[12];


                

    }
    
// assemble measurement from bytes    

char buf[7];

for(int lp=0;lp<6;lp++)

  buf[lp]=mydata[lp+5]+'0';

buf[6]=0;

num=atol(buf); //assembled measurement, no decimal place added

 
  Serial3.print("$");
  Serial3.println(num, DEC);
  
  digitalWrite(req,LOW);

   //  delay(25);

}



uVGA-II 4DGL Code:

Code:
#platform "uVGA-II_GFX2"
// Micrometer GFX
#inherit "4DGL_16bitColours.fnc"
func main()
    var comvar;
    var result;
    gfx_Cls();
    txt_Set(FONT_SIZE, FONT2);
    gfx_Rectangle(75,75,150,120,WHITE);
    //print("Com test");
    //com_SetBaud(COM0,960);
    //to(COM0); print("serial input test:\n");
    repeat
        comvar := serin1();
        if(comvar == '$')
            //gfx_Cls();
            //print("$");
            result :=0;
        endif
        if(lookup8(comvar,"0123456789"))
           result:=(result*10)+(comvar-'0');
           //print("\n Char:",comvar);
        endif
        if(comvar ==13)
            txt_MoveCursor(12,10);
            print(result,"  ");
        endif
    forever
endfunc
« Last Edit: May 30, 2012, 06:52:09 am by Steve Spence - KK4HFJ » Logged

Steve Spence - ET/AT/R&D KK4HFJ

Andrews SC
Offline Offline
Full Member
***
Karma: 0
Posts: 240
If Arduino is not the answer, you are asking the wrong question!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

BTW, the uVGA 4DGL language only supports Ints (16bit) as a variable type. No floats, boolean etc.

This means it can't handle our calculations. So it's back to doing the math on the Arduino, and sending multiple data sets over to the uVGA for display. 4DSYSFAN has a very good way for doing this, which includes a ID field so the uVGA knows which variable to stuff with that particular string of data. http://4d.websitetoolbox.com/post/picking-out-data-from-a-string-5821237
« Last Edit: May 30, 2012, 03:34:16 pm by Steve Spence - KK4HFJ » Logged

Steve Spence - ET/AT/R&D KK4HFJ

Pages: [1]   Go Up
Jump to: