4D Systems Trouble

Just purchased a 4D systems touch screen.
I am using the example “geniesparkdemo” for reference.
I am using a particle photon.
I have everything connected and the screen powered up with a basic program (1 winbutton0).
I have a relay shield connected to D0 and it works.
I just want to press my winbutton0 and have the relay turn on.
I’m missing something…here’s my code if anyone could try to help me. Thanks for any help!

#if defined (SPARK)
#include "VisiGenieSpark.h"
#else
#include <genieArduino.h>
#endif

Genie genie;
#define RESETLINE D4  // Change this if you are not using an Arduino Adaptor Shield Version 2 (see code below)

void setup() 
{

    Serial1.begin(200000);  // Serial1 @ 200000 (200K) Baud
    genie.Begin(Serial1);   // Use Serial0 for talking to the Genie Library, and to the 4D Systems display

    pinMode(D0, OUTPUT); 
    pinMode(D3, INPUT_PULLDOWN);
    digitalWrite(D0,LOW);

    genie.AttachEventHandler(myGenieEventHandler); // Attach the user function Event Handler for processing events

    pinMode(RESETLINE, OUTPUT);  // Set D4 on Arduino to Output (4D Arduino Adaptor V2 - Display Reset)
    digitalWrite(RESETLINE, 0);  // Reset the Display via D4
    delay(100);
    digitalWrite(RESETLINE, 1);  // unReset the Display via D4
    
    delay (3500); //let the display start up after the reset (This is important)
    
    genie.WriteContrast(1); // 1 = Display ON, 0 = Display OFF.
    
    //Write a string to the Display to show the version of the library used
    genie.WriteStr(0, GENIE_VERSION);
}

void loop() 
{
    static long waitPeriod = millis();
    genie.DoEvents(); // This calls the library each loop to process the queued responses from the display

    if (millis() >= waitPeriod)
    {
   
        // The results of this call will be available to myGenieEventHandler() after the display has responded
        // Do a manual read from the UserLEd0 object
        genie.ReadObject(GENIE_OBJ_WINBUTTON, 0x00);
    
        waitPeriod = millis() + 50; // rerun this code to update Cool Gauge and Slider in another 50ms time.
    }

}



void myGenieEventHandler(void)
{
  genieFrame Event;
  genie.DequeueEvent(&Event);

    //If the cmd received is from a Reported Object, which occurs if a Read Object (genie.ReadOject) is requested in the main code, reply processed here.
    if (Event.reportObject.cmd == GENIE_REPORT_OBJ)
    {
        if (Event.reportObject.object == GENIE_OBJ_WINBUTTON)              // If the Reported Message was from a User LED
        {
            if (Event.reportObject.index == 0)                              // If UserLed0
            {
                digitalWrite(D0, HIGH);                         // Turn Off Relay, everything is ok.*
                //bool UserLed0_val = genie.GetEventData(&Event);               // Receive the event data from the UserLed0
                //UserLed0_val = !UserLed0_val;                                 // Toggle the state of the User LED Variable
                //genie.WriteObject(GENIE_OBJ_USER_LED, 0x00, UserLed0_val);    // Write UserLed0_val value back to to UserLed0
            }
        }
    }
  }

What is the purpose of declaring waitPeriod every loop? The line waitPeriod = millis() + 50; gets set at the end of this if block but waitPeriod will get written over at the start of each loop. It would make more sense to make the declaration in the header as a global variable.

This makes more sense (at least to me):

unsigned long waitPeriod = 50;
unsigned long lastRun = 0;

void loop() {
  genie.DoEvents();
  
  if (millis() - lastRun >= waitPeriod) {
    genie.ReadObject(....);
    lastRun = millis();
  }
}

The demo uses 115200 baud not 200000 for Particle (Spark) devices (BTW, a valid baudrate would be 230400).
Also you may want to use unsigned long for waitPeriod and for unsigned arithmetic you should rewrite your code slightly

void loop() 
{
    static unsigned long waitPeriod = millis();

    if (millis() - waitPeriod >= 50)
    {
        ...
        waitPeriod = millis(); // rerun this code to update Cool Gauge and Slider in another 50ms time.
    }
}
1 Like

@ninjatill, declaring a variable static within a function instantiates it like a global but is only available within the scope of the function. So static long waitPeriod = millis(); is run only once.

2 Likes

@peekay123, Thanks. I understand what that was doing now.

1 Like

Thanks for your input. Still can’t get it working. Little on the stumped side…
Changed the baud rate and the variable but no go…should be pretty simple.

Does the unaltered library sample work for you?

I’ve yet to try that…

Also if you didn’t know 4D systems has a newly updated library they posted up a few months ago that you can try.

I’ve used the Photon + 4D Systems display libraries successfully in the past so I know they work. If you keep having trouble I’ll dig up the library code I used that did work for me.

1 Like

Thanks RWB.
I realized that in Workshop 4 I did not have report message in event handler. Testing in GTX now shows that hex codes are being sent when the button is pushed.
I still can’t get the button on my screen to work though!
Not sure what I’m doing wrong.
I appreciate your willingness to help. Press a button…click a relay…feel like an idiot!

I have a variety of button and other functions working with a 4DSystems display. I have just returned from a vacation trip but if you have not cracked it in the next 24 hours or so I will have a close look at your code.

Thanks…still can’t get it. So here is the summary that maybe will help…
I have a winbutton in 4d workshop that has report message for “on change” for the event. The program is successfully loaded to the panel. When I click on GTX and press the button I see that a string is being sent out.
The panel is connected with ground to ground, 5v to Vin, reset to pin D4, RX to TX, and TX to RX.
I apply power to the circuit and the screen comes on…goes off…and then comes on.
I press my button and nothing happens.
I have tried the baud rate of 200000 based on the sample code. 230400 based on ScruffR’s suggestion. And 115200.
Also, I had included “VisieGenieSpark” library in my first trials. I just switched it to include the “genieParticle” library and get the same results.

ALRIGHT! Got it going…for others to check with same problems.

1.) Wasn’t aware that the baud rate you in 4D workshop actually sets the baud rate that you will use in your program.
2.) The f’n cable! The 5 pin cable is printed TX and RX on the header. The board you connect the header to is reversed.

Thanks for your thoughts everyone :slight_smile:

1 Like

Great to hear. Like you I fell for the crossover between Rx and Tx on my first attempt.