Announcement

Collapse
No announcement yet.

Arduino to 4d, not waiting on next button press

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

  • Arduino to 4d, not waiting on next button press

    I am probably doing something wrong, but with the below Arduino code I am getting 1st response for 1 soft button push, then it blows through the loop without waiting for next push. Possibly this is related to below post, but post is in the legacy area. I am not a super experienced coder so any feedback is welcome. What I want code to do is take 4 presses, put them in an array, and then check that against admin[] array. Thanks much in advance for any assist, John

    Related post?
    http://4d.websitetoolbox.com/post/way-to-clear-touchbuffer-on-ulcd43pt-6063209?highlight=button+wait


    My Arduino Mega 2560 code:
    #include

    char pin_array[4];
    int pin_loop;
    int pin_count = 4;
    char admin[4] = {'1','4','1','4'}; // array to hold admin password

    void setup()
    {
    Serial.begin(9600);
    genieBegin (GENIE_SERIAL_1, 9600);
    genieAttachEventHandler(myGenieEventHandler);

    // Reset 4d display
    pinMode(23, OUTPUT);
    digitalWrite(23, LOW);
    delay(100);
    digitalWrite(23, HIGH);
    Serial.println("Setup complete");
    }

    void loop()
    {
    genieDoEvents();
    }

    void check_pin() {
    pin_loop = 0;
    Serial.println("Password entered:");
    while(pin_loop < pin_count)
    {
    Serial.print(pin_array[pin_loop]);
    pin_loop++;
    if (pin_array[0] == admin[0] &&
    pin_array[1] == admin[1] &&
    pin_array[2] == admin[2] &&
    pin_array[3] == admin[3]
    {
    Serial.println("Pin entered is correct");
    }
    else
    {
    Serial.println("Pin entered is NOT correct");
    }
    }
    }

    void myGenieEventHandler(void)
    {
    pin_loop = 0;
    genieFrame Event; // Creates instance for the event
    while(pin_loop < pin_count)
    // for (int pin_loop = 0; pin_loop < pin_count; pin_loop++)
    {

    genieDequeueEvent(&Event); // Deques event for reading by Arduino
    Serial.print("Pin_loop =");
    Serial.println(pin_loop);
    Serial.println("");
    if(Event.reportObject.cmd == GENIE_REPORT_EVENT)
    {
    if (Event.reportObject.object == GENIE_OBJ_WINBUTTON)
    {
    int button = Event.reportObject.index;
    Serial.print("Button pressed:");
    Serial.println(button);
    pin_array[pin_loop] = '0'+button;
    delay(250);
    }
    pin_loop++;
    // break;
    }
    }
    check_pin();
    }

    Output:
    Setup complete
    Pin_loop =0

    Button pressed:5
    Pin_loop =1

    Button pressed:5
    Pin_loop =2

    Button pressed:5
    Pin_loop =3

    Button pressed:5
    Password entered:
    5Pin entered is NOT correct
    5Pin entered is NOT correct
    5Pin entered is NOT correct
    5Pin entered is NOT correct
    Any spelling or grammar inconsistencies are transmission errors

  • #2


    Hi John

    Something does seem a little strange, however can you zip up your WS4 project using the Zip Project option in the File menu, and attach that, so we can see how you have that set up?

    I assume you have a bunch of winbuttons, and the index of each winbutton is the value of that button. ie you have 10 buttons, 0 to 9? and winbutton0 is your 0 button, through to winbutton9 being your 9 button?
    Have you looked at the Keyboard widget?

    Anyway, continuing with that assumption. You have a while loop at the start of the event handler function. I am not sure this is a good idea, as you are therefore assuming that each event that comes in is a button press and not some other event, such as change of form etc. Your project might be simple enough at the moment and this is OK, but something I would check if I was you. It is not a good idea to prevent the event handler from de-queuing the event. You should process it as it comes in, and if you need to wait for 4 buttons to come in, then you need to store the value in a variable and access it from your loop, for example, and evaluate it after you have received 4 of them.
    Also it is not a good idea to have delays, as this stops the arduino in its tracks completely and prevents it receiving possible serial comms, as delay is a blocking function.

    I can fully see what you are trying to do.

    If the above has helped, then great. If not, please zip up both your arduino and WS4 projects and send them in for us to look at

    Thanks
    James

    Comment


    • #3


      Hi John

      I thought I would have a crack at fixing your code.
      You had a few things which were a bit off. Please compare your code to mine so you can see what was changed.
      A number of ways to skin the cat, but here is one.
      Hope it helps.

      Oh and I commented out your reset 4D code, as mine was on different pins.

      I created a ViSi-Genie program, had 9 win buttons, named Winbutton0 through to Winbutton9, corresponding to numbers 0 to 9, each set to Report Message onChanged.

      HTML Code:
      #include 
      
      int pin_array[4];
      int admin[4] = {1,4,1,4};  // array to hold admin password
      int position = 0;
      
      void setup() 
      {
        Serial.begin(9600);
        genieBegin (GENIE_SERIAL_1, 9600);
        genieAttachEventHandler(myGenieEventHandler);
      
        // Reset 4d display  
        //pinMode(23, OUTPUT);
        //digitalWrite(23, LOW);
        //delay(100);
        //digitalWrite(23, HIGH);
        Serial.println("Setup complete");
      }
      
      void loop() 
      {
        genieDoEvents();
      
        if(position > 3)   
        {   
          Serial.println("Password entered"); 
          if (pin_array[0] == admin[0] &&
            pin_array[1] == admin[1] &&
            pin_array[2] == admin[2] &&
            pin_array[3] == admin[3] ) 
          {
            Serial.println("Pin entered is correct");
            //UNLOCK HERE
          }
          else
          {
            Serial.println("Pin entered is NOT correct");
          }
          position = 0;  
        }
      }
      
      void myGenieEventHandler(void) 
      {
        genieFrame Event;  // Creates instance for the event
        genieDequeueEvent(&Event);  // Deques event for reading by Arduino
      
        if(Event.reportObject.cmd == GENIE_REPORT_EVENT)
        {
          if (Event.reportObject.object == GENIE_OBJ_WINBUTTON)
          {
            int button = Event.reportObject.index;
            Serial.print("Button pressed:");
            Serial.println(button);
            pin_array[position] = button; 
            position++;
          }
        }
      }
      Output:

      Setup completeButton pressed:1Button pressed:2Button pressed:3Button pressed:4Password enteredPin entered is NOT correctButton pressed:1Button pressed:4Button pressed:1Button pressed:4Password enteredPin entered is correct
      If you are planning to expand the program more, and more additional winbuttons, then you will need to filter the winbuttons from 0 to 9 in your handler so other win buttons do not get detected as keypresses for your password. This is obviously just a base of the program based on what you had above.

      Regards
      James

      Comment


      • #4


        Thanks much, your fix worked great. I won't bother posting my Genie files since all is working now, and I appreciate your advice on the further expansion. I have been looking for more extensive code examples to read through but only somewhat successful. Thanks for the quick response. Now I can move on with the project. Regards, John
        Any spelling or grammar inconsistencies are transmission errors

        Comment

        Working...
        X