Announcement

Collapse
No announcement yet.

myGenieEventHandler Error

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • myGenieEventHandler Error

    Hi there,

    Could someone please help me compile this Arduino code. I made some changes after a previous post, but still no luck. I am trying to get the touchscreen functions of the 4D systems display to interpret the numbers entered from a touchscreen keypad to be used for a stepper motor.

    It would be great if someone could read through the code to notice any issues I may have done.

    The code is across 2 posts within this topic as there is a 1000 limit unfortunately.

    Many thanks

    Code:
    /* Arduino Control Stepper with Keypad and LCD
    
    Created by Yvan / https://Brainy-Bits.com
    This code is in the public domain...
    You can: copy it, use it, modify it, share it or just plain ignore it!
    Thx!
    
    */
    
    #include "AccelStepper.h" // AccelStepper Library
    #include <Keypad.h> // Keypad Library
    #include "U8glib.h" // U8glib for Nokia LCD
    #include <genieArduino.h> /4D Display Library
    
    // Pin used for Homing stepper using Sensor
    #define home_switch 9 // Pin 9 connected to Home Switch (Microswitch)
    
    // Variables to hold entered number on Keypad
    volatile int firstnumber=99; // used to tell how many numbers were entered on keypad
    volatile int secondnumber=99;
    volatile int thirdnumber=99;
    
    // Variables to hold Distance and CurrentPosition
    int keyfullnumber=0; // used to store the final calculated distance value
    String currentposition = ""; // Used for display on Nokia LCD
    //String keyboardValue = ""; // Used for display on Nokia LCD
    
    // Keypad Setup
    const byte ROWS = 4; // Four Rows
    const byte COLS = 4; // Four Columns
    char keys[ROWS][COLS] = {
    {'1','2','3','A'},
    {'4','5','6','B'},
    {'7','8','9','C'},
    {'*','0','#','D'},
    };
    
    
    byte rowPins[ROWS] = {22, 24, 26, 28}; // Arduino pins connected to the row pins of the keypad
    byte colPins[COLS] = {31, 33, 35, 37}; // Arduino pins connected to the column pins of the keypad
    Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS ); // Keypad Library definition
    
    
    // U8glib Setup for Nokia LCD
    #define backlight_pin 11
    U8GLIB_PCD8544 u8g(3, 4, 6, 5, 7); // Arduino pins connected to Nokia pins:
    // CLK=3, DIN=4, CE=6, DC=5, RST=7
    
    
    // AccelStepper Setup
    AccelStepper stepper(1, A0, A1); // 1 = Easy Driver interface
    // Arduino A0 connected to STEP pin of Easy Driver
    // Arduino A1 connected to DIR pin of Easy Driver
    #define step_pin A0 //STEP pin connected to A0 on Arduino
    #define dir_pin A1 // DIR pin connected to A1 on Arduino
    
    
    int direction; // Variable to set Rotation (CW-CCW) of the motor
    int steps; // Used to set HOME position after Homing is complete
    
    Genie genie;
    #define RESETLINE 8 // 7Change this if you are not using an Arduino Adaptor Shield Version 2 (see code below)
    
    int winbutton3;
    int val_D = 0;
    int val_O = 0;
    int counter = 0;
    int counter1 = 0;
    int temp;
    int temp1;
    int pulsos;
    int pulsos1;
    char keyvalue[10]; // array to hold keyboard values
    char keyvalue1[10]; // array to hold keyboard values
    
    void setup(void) {
    
    Serial.begin(9600); // "defined by the genieArduino Library" ; "verify the serial begin for Diablo16"
    genie.Begin(Serial); // "defined by the genieArduino Library"
    genie.AttachEventHandler(myGenieEventHandler); // "defined by the genieArduino Library"
    
    pinMode(RESETLINE, OUTPUT); // Set D4 on Arduino to Output (4D Arduino Adaptor V2 - Display Reset)
    digitalWrite(RESETLINE, 1); // Reset the Display via D4
    delay(100);
    digitalWrite(RESETLINE, 0); // unReset the Display via D4
    
    delay (100); //let the display start up
    
    genie.WriteObject (GENIE_OBJ_FORM, 0, 0); // Select form 0 (the calculator)
    
    pinMode(home_switch, INPUT_PULLUP);
    digitalWrite(step_pin, HIGH); //Trigger stepper motor
    
    // Light up the LCD backlight LEDS
    analogWrite(backlight_pin, 0); // Set the Backlight intensity (0=Bright, 255=Dim)
    
    // AccelStepper speed and acceleration setup
    stepper.setMaxSpeed(10000); // Not to fast or you will have missed steps
    stepper.setAcceleration(12000); // Same here
    
    // Draw starting screen on Nokia LCD
    u8g.firstPage();
    do {
    u8g.drawHLine(0, 15, 84);
    u8g.drawVLine(50, 16, 38);
    u8g.drawHLine(0, 35, 84);
    u8g.setFont(u8g_font_profont11);
    u8g.drawStr(0, 10, "ENTER DISTANCE");
    u8g.drawStr(62, 29, "MM");
    u8g.drawStr(4, 46, "cur-pos");
    }
    while( u8g.nextPage() );
    
    
    
    // Start Homing procedure of Stepper Motor at startup
    genie.WriteObject (GENIE_OBJ_FORM, 2, 0); // Select form 2 (return to home screen)
    while (!digitalRead(home_switch)) { // Do this until the switch is not activated
    digitalWrite(dir_pin, HIGH); // (HIGH = anti-clockwise / LOW = clockwise)
    digitalWrite(step_pin, HIGH);
    delay(1); // Delay to slow down speed of Stepper
    digitalWrite(step_pin, LOW);
    delay(1);
    }
    
    while (digitalRead(home_switch)) { // Do this until the switch is activated
    digitalWrite(dir_pin, LOW);
    digitalWrite(step_pin, HIGH);
    delay(1); // More delay to slow even more while moving away from switch
    digitalWrite(step_pin, LOW);
    delay(1);
    }
    genie.WriteObject (GENIE_OBJ_FORM, 0, 0); // Select form 0 (home screen)
    steps=0; // Reset position variable to zero
    
    }

  • #2
    Code:
    void loop(){
    
    
    // Big loop - just wait for events from the display now
    
    genie.DoEvents();
    
    // Do other things here if required
    
    char keypressed = keypad.getKey(); // Get value of keypad button if pressed
    // char keyboardValue = GENIE_OBJ_KEYBOARD(); // Get value of keypad button if pressed
    if (keypressed != NO_KEY){ // If keypad button pressed check which key it was
    switch (keypressed) {
    
    case '1':
    //genie.WriteObject (GENIE_OBJ_KEYBOARD, 0, 1, checknumber(1));
    // touchKey (keyboardValue); // pass data to the calculatorKey function for processing
    checknumber(1);
    break;
    
    case '2':
    checknumber(2);
    break;
    
    case '3':
    checknumber(3);
    break;
    
    case '4':
    checknumber(4);
    break;
    
    case '5':
    checknumber(5);
    break;
    
    case '6':
    checknumber(6);
    break;
    
    case '7':
    checknumber(7);
    break;
    
    case '8':
    checknumber(8);
    break;
    
    case '9':
    checknumber(9);
    break;
    
    case '0':
    checknumber(0);
    break;
    
    case '*':
    deletenumber();
    break;
    
    case '#':
    calculatedistance();
    break;
    
    case 'A':
    
    checknumber(1);
    checknumber(0);
    checknumber(0);
    break;
    
    case 'B':
    checknumber(2);
    checknumber(5);
    checknumber(0);
    break;
    
    case 'C':
    checknumber(5);
    checknumber(0);
    checknumber(0);
    break;
    
    case 'D':
    checknumber(0);
    break;
    
    break;
    }
    }
    
    void checknumber(int x){
    if (firstnumber == 99) { // Check if this is the first number entered
    firstnumber=x;
    String displayvalue = String(firstnumber); // Transform int to a string for display
    genie.WriteStr (0, displayvalue); //Write to 4D Display for number entry
    drawnokiascreen(displayvalue); // Redraw Nokia lcd
    
    }
    else {
    if (secondnumber == 99) { // Check if it's the second number entered
    secondnumber=x;
    String displayvalue = (String(firstnumber) + String(secondnumber));
    genie.WriteStr (0, displayvalue); //Write to 4D Display for number entry
    drawnokiascreen(displayvalue);
    
    }
    else { // It must be the 3rd number entered
    thirdnumber=x;
    String displayvalue = (String(firstnumber) + String(secondnumber) + String(thirdnumber));
    genie.WriteStr (0, displayvalue); //Write to 4D Display for number entry
    drawnokiascreen(displayvalue);
    
    }
    }
    }
    
    
    void deletenumber() { // Used to backspace entered numbers
    if (thirdnumber !=99) {
    String displayvalue = (String(firstnumber) + String(secondnumber));
    genie.WriteStr (0, displayvalue); //Write to 4D Display for number entry
    drawnokiascreen(displayvalue);
    
    thirdnumber=99;
    }
    else {
    if (secondnumber !=99) {
    String displayvalue = String(firstnumber);
    genie.WriteStr (0, displayvalue); //Write to 4D Display for number entry
    drawnokiascreen(displayvalue);
    
    secondnumber=99;
    }
    else {
    if (firstnumber !=99) {
    String displayvalue = "";
    genie.WriteStr (0, displayvalue); //Write to 4D Display for number entry
    drawnokiascreen(displayvalue);
    
    firstnumber=99;
    }
    }
    }
    }
    
    void calculatedistance() { // Used to create a full number from entered numbers
    
    if (thirdnumber == 99 && secondnumber == 99 && firstnumber != 99) {
    keyfullnumber=firstnumber;
    movestepper(keyfullnumber);
    }
    
    if (secondnumber != 99 && thirdnumber == 99) {
    keyfullnumber=(firstnumber*10)+secondnumber;
    movestepper(keyfullnumber);
    }
    
    if (thirdnumber != 99) {
    keyfullnumber=(firstnumber*100)+(secondnumber*10)+thirdnumber;
    movestepper(keyfullnumber);
    }
    
    resetnumbers(); // Reset numbers to get ready for new entry
    }
    
    void movestepper(int z) { // Move the stepper
    
    if (z >500) {
    u8g.firstPage();
    do {
    u8g.drawHLine(0, 15, 84);
    u8g.drawVLine(50, 16, 38);
    u8g.drawHLine(0, 35, 84);
    u8g.setFont(u8g_font_profont11);
    u8g.drawStr(0, 10, "INVALID ENTRY");
    u8g.drawStr(62, 29, "MM");
    u8g.drawStr(4, 46, "cur-pos");
    
    genie.WriteObject (GENIE_OBJ_FORM, 1, 0); // Select form 1 (invalid page)
    delay (500);
    }
    while( u8g.nextPage() );
    genie.WriteObject (GENIE_OBJ_FORM, 0, 0); // Select form 0 (home screen)
    return;
    }
    
    long calculatedmove=((z*6400L)/40); // Calculate number of steps needed in mm
    stepper.runToNewPosition(calculatedmove);
    currentposition = String(z);
    u8g.firstPage();
    do {
    u8g.drawHLine(0, 15, 84);
    u8g.drawVLine(50, 16, 38);
    u8g.drawHLine(0, 35, 84);
    u8g.setFont(u8g_font_profont11);
    u8g.drawStr(0, 10, "ENTER DISTANCE");
    u8g.drawStr(62, 29, "MM");
    u8g.drawStr(4, 46, "cur-pos");
    u8g.setPrintPos(57,47);
    u8g.print(currentposition);
    }
    while( u8g.nextPage() );
    genie.WriteObject (GENIE_OBJ_FORM, 0, 0); // Select form 0 (home screen)
    genie.WriteStr (1, currentposition); //Write to 4D Display for number entry
    }
    
    void resetnumbers() { // Reset numbers for next entry
    firstnumber=99;
    secondnumber=99;
    thirdnumber=99;
    }
    
    void drawnokiascreen(String y) {
    
    u8g.firstPage();
    do {
    u8g.drawHLine(0, 15, 84);
    u8g.drawVLine(50, 16, 38);
    u8g.drawHLine(0, 35, 84);
    u8g.setFont(u8g_font_profont11);
    u8g.drawStr(0, 10, "ENTER DISTANCE");
    u8g.setPrintPos(0,29);
    u8g.print(y); // Put entered number on Nokia lcd
    u8g.drawStr(62, 29, "MM");
    u8g.drawStr(4, 46, "cur-pos");
    u8g.setPrintPos(57,47);
    u8g.print(currentposition); // Display current position of stepper
    genie.WriteStr (1, currentposition); //Write to 4D Display for number entry
    }
    while( u8g.nextPage() );
    }
    void myGenieEventHandler(void)
    {
    genieFrame Event;
    genie.DequeueEvent(&Event);
    
    //If the cmd received is from a Reported Event (Events triggered from the Events tab of Workshop4 objects)
    
    if (Event.reportObject.cmd == GENIE_REPORT_EVENT)
    {
    
    // KEYBOARDS
    
    if (Event.reportObject.object == GENIE_OBJ_KEYBOARD) // If the Reported Message is from a keyboard
    {
    if (Event.reportObject.index == 0) // If keyboard0
    {
    temp = genie.GetEventData(&Event);
    if (temp >= 48 && temp <= 57 && counter <= 9)
    {
    keyvalue[counter] = temp; // Receive the event data from the keyboard
    
    counter = counter + 1; //increment array
    }
    else if (temp == 8) //check if backspace
    {
    counter--;
    keyvalue[counter] = 0;
    }
    int pulsos = atoi(keyvalue);
    
    genie.WriteObject(GENIE_OBJ_LED_DIGITS, 0, pulsos);
    }
    
    if (Event.reportObject.index == 1) // If keyboard1
    {
    temp1 = genie.GetEventData(&Event);
    if (temp1 >= 48 && temp1 <= 57 && counter1 <= 9)
    {
    keyvalue1[counter1] = temp1; // Receive the event data from the keyboard1
    
    counter1 = counter1 + 1; //increment array
    }
    else if (temp1 == 8) //check if backspace
    {
    counter1--;
    keyvalue1[counter1] = 0;
    }
    int val_O = atoi(keyvalue1);
    
    genie.WriteObject(GENIE_OBJ_LED_DIGITS, 1, pulsos1);
    
    
    // WINBUTTONS
    
    if (Event.reportObject.object == GENIE_OBJ_WINBUTTON)
    {
    if (Event.reportObject.index == 3) // If Reported Message is from Winbutton3
    {
    winbutton3 = genie.GetEventData(&Event); // Receive the event data from the winbutton3
    /pulsos1(keyvalue);
    stepper.step(4 * val_O / 1.8);
    pulsos = pulsos1;
    }
    }
    }
    }
    }
    }
    }

    Comment


    • #3
      Hi,

      I managed to get it to compile but there are some errors that you will need to look at

      z in this command wasn't declared

      long calculatedmove=((z*6400L)/40);

      /pulsos1(keyvalue);
      stepper.step(4 * val_O / 1.8); // not sure what the problem is with this

      Best regards

      Paul
      Attached Files

      Comment


      • #4
        Hi,

        Exactly the same as your last post you made about it here https://forum.4dsystems.com.au/node/73473

        Missing } at the end of your loop() function, and one too many at the very end of your project. Also in this one, a few lines from the end the
        /pulsos1(keyvalue);
        only has one / instead of // for a comment to comment out that line. Something wrong with that stepper.step() function that Paul indicated too.

        Compiles fine when you fix the } issue, and comment out the two lines
        // pulsos1(keyvalue);
        //stepper.step(4 * (long) val_O / 1.8);
        at the end of your program. YOu will need to figure out what is wrong with that stepper line.

        Regards
        James

        Comment

        Working...
        X