Announcement

Collapse
No announcement yet.

4Duino Timer

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

  • 4Duino Timer

    Hello,

    i would like to make a countdown timer, but i dont know how, do you have a sample file or something?
    my project contains Winbuttons (up and down->increase and decrease leddigits number and a start button->start counting)

    Thanks for your help!
    Best
    Tamas
    Attached Files

  • #2
    can you post the code so we can verify how to make it work

    Comment


    • #3
      Code:
      %%Display%%.DefineResetLine ;
      %%Display%%.DefineDisplaySerialx ;
      #include "countdownConst.h"
      %%Display%%.IncludeSerial_4DLib ;
      #include "%%Display%%.Processor_LedDigitsDisplay.h"
      %%Display%%.IncludeSerial_Const4D ;
      %%Display%%.AssignDisplaySerialtoLibrary ;
      
      
      word hndl;
      int value;
      int state;
      
      void setup()
      {
      
        pinMode(RESETLINE, OUTPUT);
        %%Display%%.Toggle_Reset_On ;
        delay(100);
        %%Display%%.Toggle_Reset_Off ;
        delay(3000) ;
      
        %%Display%%.DisplaySerial.Begin_Speed ;
        Display.TimeLimit4D = 5000 ;
        Display.gfx_ScreenMode(PORTRAIT) ;
      
        Display.putstr("Mounting...\n");
        if (!(Display.file_Mount()))
        {
          while(!(Display.file_Mount()))
          {
            Display.putstr("Drive not mounted...");
            delay(200);
            Display.gfx_Cls();
            delay(200);
          }
          LedDigitsDisplay(Display, hndl, value, iLeddigits1+1, 56, 2, 1, 30, 0) ;  // Leddigits1
        }
      
      
        hndl = Display.file_LoadImageControl("COUNTD~1.dat", "COUNTD~1.gci", 1);
      
        Display.img_ClearAttributes(hndl, iWinbutton1, I_TOUCH_DISABLE);
        Display.img_Show(hndl, iWinbutton1);
        Display.img_ClearAttributes(hndl, iWinbutton2, I_TOUCH_DISABLE);
        Display.img_Show(hndl, iWinbutton2);
        Display.img_ClearAttributes(hndl, iWinbutton3, I_TOUCH_DISABLE);
        Display.img_Show(hndl, iWinbutton3);
      
      
      } // end Setup **do not alter, remove or duplicate this line**
      
      void loop()
      {
        Display.img_SetWord(hndl, iWinbutton1, IMAGE_INDEX, 0); // button_increase where state is 0 for up and 1 for down
        Display.img_Show(hndl,iWinbutton1) ;  // button_increase
        Display.img_SetWord(hndl, iWinbutton2, IMAGE_INDEX, 0); // button_decrease where state is 0 for up and 1 for down
        Display.img_Show(hndl,iWinbutton2) ;  // button_decrease
        Display.img_SetWord(hndl, iWinbutton3, IMAGE_INDEX, 0); // button_start where state is 0 for up and 1 for down
        Display.img_Show(hndl,iWinbutton3) ;  // button_start
        Display.img_Show(hndl, iLeddigits1);  // Leddigits1 show all digits at 0, only do this once
      
        state = Display.touch_Get(TOUCH_STATUS);
        int n = Display.img_Touched(hndl, iWinbutton1);
        int n2 = Display.img_Touched(hndl, iWinbutton2);
        int n3 = Display.img_Touched(hndl, iWinbutton3);
      
      //INCREASE BUTTON
      if (state == TOUCH_PRESSED && n == iWinbutton1)
          {
            value++;
          }
      
      //DECREASE BUTTON
      if (state == TOUCH_PRESSED && n2 == iWinbutton2)
          {
            value--;
          }
      
      //START BUTTON
      if (state == TOUCH_PRESSED && n3 == iWinbutton3)
          {
            //i would like to start the countdown if i press this button;
          }
      
      }

      Comment


      • #4
        Hi,

        This should work, it is only basic but shows how to do it.

        %%Display%%.DefineResetLine ;
        %%Display%%.DefineDisplaySerialx ;
        #include "countdownConst.h"
        %%Display%%.IncludeSerial_4DLib ;
        #include "%%Display%%.Processor_LedDigitsDisplay.h"
        %%Display%%.IncludeSerial_Const4D ;
        %%Display%%.AssignDisplaySerialtoLibrary ;


        word hndl;
        long value;
        int state;
        long timer;
        int ledvalue;

        void setup()
        {

        pinMode(RESETLINE, OUTPUT);
        %%Display%%.Toggle_Reset_On ;
        delay(100);
        %%Display%%.Toggle_Reset_Off ;
        delay(3000) ;

        %%Display%%.DisplaySerial.Begin_Speed ;
        Display.TimeLimit4D = 5000 ;
        Display.gfx_ScreenMode(PORTRAIT) ; // change manually if orientation change
        Display.putstr("Mounting...\n");
        if (!(Display.file_Mount()))
        {
        while(!(Display.file_Mount()))
        {
        Display.putstr("Drive not mounted...");
        delay(200);
        Display.gfx_Cls();
        delay(200);
        }
        }
        //LedDigitsDisplay(Display, hndl, value, iLeddigits1+1, 56, 2, 1, 30, 0) ; // Leddigits1
        // Leddigits1



        hndl = Display.file_LoadImageControl("COUNTD~1.dat", "COUNTD~1.gci", 1);

        Display.img_ClearAttributes(hndl, iWinbutton1, I_TOUCH_DISABLE);
        Display.img_Show(hndl, iWinbutton1);
        Display.img_ClearAttributes(hndl, iWinbutton2, I_TOUCH_DISABLE);
        Display.img_Show(hndl, iWinbutton2);
        Display.img_ClearAttributes(hndl, iWinbutton3, I_TOUCH_DISABLE);
        Display.img_Show(hndl, iWinbutton3);
        Display.img_Show(hndl, iLeddigits1); // Leddigits1 show all digits at 0, only do this once
        Display.touch_Set(TOUCH_ENABLE);

        //Display.img_Show(hndl, iLeddigits1); // Leddigits1 show all digits at 0, only do this once
        } // end Setup **do not alter, remove or duplicate this line**

        void loop()
        {
        // Display.img_SetWord(hndl, iWinbutton1, IMAGE_INDEX, 0); // button_increase where state is 0 for up and 1 for down
        // Display.img_Show(hndl,iWinbutton1) ; // button_increase
        // Display.img_SetWord(hndl, iWinbutton2, IMAGE_INDEX, 0); // button_decrease where state is 0 for up and 1 for down
        // Display.img_Show(hndl,iWinbutton2) ; // button_decrease
        // Display.img_SetWord(hndl, iWinbutton3, IMAGE_INDEX, 0); // button_start where state is 0 for up and 1 for down
        // Display.img_Show(hndl,iWinbutton3) ; // button_start

        state = Display.touch_Get(TOUCH_STATUS);
        int n = Display.img_Touched(hndl, -1);
        //int n2 = Display.img_Touched(hndl, iWinbutton2);
        //int n3 = Display.img_Touched(hndl, iWinbutton3);

        //INCREASE BUTTON
        if (state == TOUCH_PRESSED && n == iWinbutton1) {
        Display.img_SetWord(hndl, iWinbutton1, IMAGE_INDEX, 1); // button_increase where state is 0 for up and 1 for down
        Display.img_Show(hndl,iWinbutton1) ;
        if(value < 99){
        value++;
        }
        LedDigitsDisplay(Display, hndl, value, iLeddigits1+1, 56, 2, 1, 30, 0) ; // Leddigits1
        // LedDigitsDisplay(Display, hndl, value, iLeddigits1+1, 60, 2, 1, 62, 0) ;
        }
        //DECREASE BUTTON
        if (state == TOUCH_PRESSED && n == iWinbutton2) {
        Display.img_SetWord(hndl, iWinbutton2, IMAGE_INDEX, 1); // button_increase where state is 0 for up and 1 for down
        Display.img_Show(hndl,iWinbutton2) ;
        if(value > 0){
        value--;
        }
        LedDigitsDisplay(Display, hndl, value, iLeddigits1+1, 56, 2, 1, 30, 0) ; // Leddigits1
        // LedDigitsDisplay(Display, hndl, value, iLeddigits1+1, 60, 2, 1, 62, 0) ; ; // Leddigits1
        }
        //START BUTTON
        if (state == TOUCH_PRESSED && n == iWinbutton3) {
        Display.img_SetWord(hndl, iWinbutton3, IMAGE_INDEX, 1); // button_increase where state is 0 for up and 1 for down
        Display.img_Show(hndl,iWinbutton3) ;
        //i would like to start the countdown if i press this button;
        timer = millis() + (value * 1000);
        }

        if(timer > millis()){
        value = ((timer - millis()) / 1000);
        //long secs = ((timer - millis()) / 1000);
        ledvalue = value;
        LedDigitsDisplay(Display, hndl, ledvalue, iLeddigits1+1, 56, 2, 1, 30, 0) ; // Leddigits1
        //LedDigitsDisplay(Display, hndl, ledvalue, iLeddigits1+1, 60, 2, 1, 62, 0) ; // Leddigits1
        }
        if (state == TOUCH_RELEASED){
        Display.img_SetWord(hndl, iWinbutton1, IMAGE_INDEX, 0); // button_increase where state is 0 for up and 1 for down
        Display.img_Show(hndl,iWinbutton1) ;
        Display.img_SetWord(hndl, iWinbutton2, IMAGE_INDEX, 0); // button_increase where state is 0 for up and 1 for down
        Display.img_Show(hndl,iWinbutton2) ;
        Display.img_SetWord(hndl, iWinbutton3, IMAGE_INDEX, 0); // button_increase where state is 0 for up and 1 for down
        Display.img_Show(hndl,iWinbutton3) ;
        }
        }

        I hope it helps

        Best regards

        Paul

        Comment


        • #5
          Hello, I used uLCD 43DT and arduino nuo, could you show me how to use timer? (press START => increase timer as count up second) thank you so much.

          Here is my code:


          #include <genieArduino.h>
          #include <avr/interrupt.h>

          volatile int BDT = 0;
          volatile int second = 0;
          int state;

          Genie genie;
          #define terminator 10


          void setup()
          {
          Serial.begin(200000);
          genie.Begin(Serial);
          pinMode(13, OUTPUT);
          cli(); // tắt ngắt toàn cục

          /* Reset Timer/Counter1 */
          TCCR1A = 0;
          TCCR1B = 0;
          TIMSK1 = 0;

          /* Setup Timer/Counter1 */
          TCCR1B |= (1 << CS11) | (1 << CS10); // prescale = 64
          TCNT1 = 40536;
          TIMSK1 = (1 << TOIE1); // Overflow interrupt enable
          sei(); // cho phép ngắt toàn cục

          genie.AttachEventHandler(myGenieEventHandler);


          pinMode(4, OUTPUT); // Set D4 on Arduino to Output (4D Arduino Adaptor V2 - Display Reset)
          digitalWrite(4, 1); // Reset the Display via D4
          delay(100);
          digitalWrite(4, 0); // unReset the Display via D4

          delay (3500); //let the display start up
          }

          void loop()
          {
          genie.DoEvents();
          //genie.WriteObject(GENIE_OBJ_LED_DIGITS, 0x01, phut);
          genie.WriteObject(GENIE_OBJ_LED_DIGITS,0x00, second);
          delay(1000);

          }

          ISR (TIMER1_OVF_vect)
          {
          if(state == 0)
          {
          giay = 0;
          digitalWrite(13, HIGH);
          }
          else
          {
          digitalWrite(13, LOW);
          TCNT1 = 40536;
          BDT = BDT + 1;
          if(BDT == 10)
          {
          second = second + 1;
          if(second == 60)
          {
          second = 0;
          }
          BDT = 0;
          }
          }
          }


          void myGenieEventHandler(void)
          {
          genieFrame Event;
          genie.DequeueEvent (&Event);
          //---------------------------------------------------------------
          if (Event.reportObject.cmd == GENIE_REPORT_EVENT)
          {
          if (Event.reportObject.object == GENIE_OBJ_WINBUTTON) // If a 4DButton was pressed
          {
          if (Event.reportObject.index == 0) // If 4DButton #0 was pressed
          {
          state = !state;
          }
          }
          }
          }
          Attached Files
          Last edited by huy373; 6th December 2017, 08:25 AM.

          Comment


          • #6
            woah, no need to use interrupt timers and vectors, enabling/disabling interrupts, way overkill..

            all you need is a little millis() counter in your uno code, an example is above your post...

            Comment


            • #7
              string.pdf Hi , i tried to compiled above example but i dont have 2 libraries. Could you please give me these libraries? I will appreciate if u help me complete my interrupt timer code!!
              Thank you so much.
              Attached Files
              Last edited by huy373; 27th June 2017, 08:18 PM.

              Comment


              • #8
                the code above is for a 4duino, the libraries as well, not the same as an uno, wrong section of the forum

                All the interrupts from your code can be stripped and just use a millis() timer to count the time

                Comment


                • #9
                  you can play a little with this, untested, but it should work

                  Code:
                  #include <genieArduino.h>
                  
                  uint16_t secs;
                  uint8_t state;
                  uint32_t stateTimer = millis();
                  
                  Genie genie;
                  
                  void setup()
                  {
                    Serial.begin(200000);
                    pinMode(13, OUTPUT);
                  
                    genie.Begin(Serial);
                    genie.AttachEventHandler(myGenieEventHandler);
                  
                    pinMode(4, OUTPUT); // Set D4 on Arduino to Output (4D Arduino Adaptor V2 - Display Reset)
                    digitalWrite(4, 1); // Reset the Display via D4
                    delay(100);
                    digitalWrite(4, 0); // unReset the Display via D4
                  
                    delay (3500); //let the display start up
                  }
                  
                  void loop()
                  {
                    genie.DoEvents();
                  
                    if ( state && millis() - stateTimer >= 1000 ) {
                      secs++;
                      stateTimer = millis();
                      genie.WriteObject(GENIE_OBJ_LED_DIGITS, 0x00, secs);
                    }
                  
                    //genie.WriteObject(GENIE_OBJ_LED_DIGITS, 0x01, phut);
                    //  delay(1000); <-- why is this here? not needed.
                  }
                  
                  
                  void myGenieEventHandler(void)
                  {
                    genieFrame Event;
                    genie.DequeueEvent (&Event);
                    //---------------------------------------------------------------
                    if (Event.reportObject.cmd == GENIE_REPORT_EVENT)
                    {
                      if (Event.reportObject.object == GENIE_OBJ_WINBUTTON) // If a 4DButton was pressed
                      {
                        if (Event.reportObject.index == 0) // If 4DButton #0 was pressed
                        {
                          stateTimer = millis();
                          secs = 0;
                          state = !state;
                        }
                      }
                    }
                  }

                  Comment


                  • #10
                    WOW that was great. Thank you so much. I will try after my working time.

                    Comment


                    • #11
                      hello, i am going to use win button to ON and OFF led, everything i need is the original color of the button is white, when i press it, the button color changes to red, the led is ON, and the color changes to white, led OFF when i press it again.
                      Help me please.

                      Thanks and Best Regards,
                      ---Huy---
                      Last edited by huy373; 21st June 2017, 03:34 PM.

                      Comment


                      • #12
                        Hello,

                        For this task you would be better using a rocker switch and a seperate LED indicator. in the rocker properties turn momentary off and it will stay in the switched position until you press it again.

                        I hope this helps

                        Best regards

                        Paul

                        Comment


                        • #13
                          Hi, abcdefgyhb
                          Attached Files

                          Comment

                          Working...
                          X