Announcement

Collapse
No announcement yet.

Keyboard values to an integer variable.

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

  • Keyboard values to an integer variable.

    Hi there.


    I’m working with a project, and I need to convert an array[4] to a single integer variable, I have no problem doing this with C , the only thing normally do is int= atoi(array), but I’m a newbie with this and I don’t have a clue how to do it.
    As an example:
    Array[0]=3
    Array[1]=4
    Array[2]=7
    Array[3]=5
    Int=3475
    Regards



  • #2
    atoi converts an ASCII string into an integer, I've never seen it used to convert an arrary of integers into a 'larger' integer. Also, based on the heading I think you want something more like atoi, rather than what your 'example' shows.

    Have you been putting individual characters from the keyboard into an array? If so they will be in a word array, not a string.

    If you have built a string,then you could use either of the following methods to convert into an integer

    [CODE
    var str[10] ;
    func main()
    var i, j, k ;
    to(str) ;
    print(12345) ;

    i := str_Ptr(str) ; // quick way, very little error checking
    str_GetW(&i, &j);
    print("GetW output ", j, "\n") ;

    i := str_Ptr(str) ; // quick way, very little error checking
    k := 0 ;
    for (j := 0; j < 5; j++)
    k := k * 10 + (str_GetByte(i+j) & 0x0f) ;
    next
    print("GetByte loop output ", k, "\n") ;

    repeat // maybe replace
    forever // this as well

    endfunc[/CODE]

    The second method could be easily adapted for a word array of characters.
    Mark

    Comment


    • #3
      Thank you for your answer
      You’re right there are strings sorry for the example, it’s an array of 4 strings only, I use a counter… array [counter] 1-0 keys ,enter key and clear key…and then I use that integer in some output pins.
      This is how I made it in Arduino..but I want to eliminate the Arduino and only use the ulc 43PT
      Sorry if the code isn’t the optimal option.
      if ((Event.reportObject.cmd == GENIE_REPORT_EVENT))
      {

      if (Event.reportObject.object == GENIE_OBJ_KEYBOARD) // If the Reported Message was from a Keyboard
      {
      if ((Event.reportObject.index == 0)&&(cont2<=4)) // If keyboard0
      {

      key[cont]=genie.GetEventData(&Event); //genie.GetEventData(&Event); // Receive the event data from the Slider0

      if(key[cont]==101) //ENTER
      {genie.WriteObject(GENIE_OBJ_LED_DIGITS, 0x00,8888);
      delay(2000);
      enter=1;
      }
      if(key[cont]==99) //CLEAR
      {genie.WriteObject(GENIE_OBJ_LED_DIGITS, 0x00,0000);

      Clear=1;
      cont=0;
      cont2=0;
      key[0]=0;
      key[1]=0;
      key[2]=0;
      key[3]=0;
      key[4]=0;
      }
      if((enter!=1)&&(Clear!=1)&&(key[4]!=101))
      {
      genie.WriteObject(GENIE_OBJ_LED_DIGITS, 0x00,atoi(key)); // Write Slider0 value to to LED Digits 0
      cont=cont+1;
      cont2=cont2+1;
      }
      Clear=0;
      if(enter==1)
      {
      key[4]=0;
      y=atoi(key);
      genie.WriteObject(GENIE_OBJ_LED_DIGITS, 0x00,y);

      do
      {
      if(y>=500)
      {
      currentMillis = millis();
      digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
      delayMicroseconds((1000000/y)/2); // wait for a second
      digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
      delayMicroseconds((1000000/y)/2);
      previousmillisvar=currentMillis;
      }
      Else
      {
      currentMillis = millis();
      digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
      delay((1000/y)/2); // wait for a second
      digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
      delay((1000/y)/2);
      previousmillisvar=currentMillis;
      }

      Comment


      • #4
        Have a look at the Picaso ViSi, KBFunc example, that might be the easiest thing to 'adapt'.
        Mark

        Comment


        • Payin
          Payin commented
          Editing a comment
          Already have everything working in the VISI except for the array into an integer value.
          Sorry but I don’t understand yet all the strings functions in 4dgl, and I would appreciate if you help me a little bit more.
          So if I already have my array with the values array[4]:=[‘3’,’5’,’6’,’9’,];
          var str[4]:=[‘3’,’5’,’6’,’9’];
          func main()
          var i, j, k ;
          to(str) ;
          print(12345) ;

          //metod 1
          /*
          i := str_Ptr(str) ; // quick way, very little error checking
          str_GetW(&i, &j);
          print("GetW output ", j, "\n") ;
          */
          //metod 2

          i := str_Ptr(str) ; // quick way, very little error checking
          k := 0 ;
          for (j := 0; j < 5; j++)
          k := k * 10 + (str_GetByte(i+j) & 0x0f) ;
          next
          print("GetByte loop output ", k, "\n") ;

          //("GetByte loop output "…..which variable is this??? And k is my integer value?...because I cant print anything

          repeat // maybe replace
          forever // this as well

          endfunc[/CODE]


          VERY thankful for your time

      • #5
        Have a look at this app note, it explains strings http://www.4dsystems.com.au/appnote/4D-AN-00056/

        For your 'str' you would need something like

        Code:
            k := 0 ;
            for (j := 0; j < 4; j++)
                k := k * 10 + (strx[j] & 0x0f) ;
            next
            print("'Getword' loop output ", k, "\n") ;
        The app note should make the 'why' more obvious
        Mark

        Comment


        • Payin
          Payin commented
          Editing a comment
          Hi Mark thank you for your help.

          I have another question, which function allows me to use the clock like in arduino:

          unsigned long currentMillis = millis();

      • #6
        sys_T() and sys_T_HI
        Mark

        Comment


        • Payin
          Payin commented
          Editing a comment
          Hope this is my last question

          what is the function for pause in microseconds like delayMicroseconds()

          cheers

      • #7
        The only delay function is for milliseconds.

        For microseconds you will need to do some 'time chewing code'
        Mark

        Comment


        • #8
          Hi
          I emigrate from picaso to diablo, and I use the same program I did with picaso and worked fine, but with the 70” screen it crashed in the middle of the routine, I need to turn on/off like 10 times for mounting the program, im using the Arduino due with the shield and a sandisk mSD.

          Comment


          • #9
            Is the PmmC and Driver on the 70DT up to date?

            Can you give is a copy or your Workshop project along with details about how to recreate the issue (in GTX if possible)
            Mark

            Comment


            • #10
              reading the post New user with 70DT getting started issues

              Reading that post, i had the same problem, but when I connected the programming cable in the Arduino shield the problem stops, and I don’t know why but now it just work with the Arduino shield without the programming cable.
              My problem right now is that always takes a lot of time for mounting the program, and sometimes it crashed and the screen get freeze.
              My comm speed is 9600 I try with higher but doesn’t respond at all.

              i have the driver and PmmC update
              Attached Files

              Comment


              • #11
                Hi Payin,

                So you have an Arduino Due, a 4D Adaptor Shield, and a uLCD-70DT. The uLCD-70DT runs the Genie program you posted and you have programmed the Due with the ViSi-Genie-Arduino library, correct?

                1. Kindly try using separate power supply modules for the uLCD-70DT and for the Due. The section Power the Arduino Host and the Display Separately of the app note ViSi-Genie Connecting a 4D Display to an Arduino Host may help you just in case.

                http://www.4dsystems.com.au/appnote/4D-AN-00017/

                2. Can you post the sketch for the Arduino host?
                3. Can you also post a video of the problem/s you are encountering? Please confirm if the problems occur when the display is or is not connected to the Due, or in both cases.

                Regards.
                Doff

                Comment


                • #12
                  #include <STDIO.h>

                  #include <genieArduino.h>

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

                  char key[4];
                  int cont=0;
                  int cont2=0;
                  int y;
                  int enter=0;
                  int Clear=0;
                  char rutina[1];
                  int enterrutina=0;
                  int contrut=0;
                  int contrut2=0;
                  int led=9;
                  long previousmillisvar=0;
                  long previousmillisvar2=120000;
                  int miliseg=0;
                  int microseg=0;
                  unsigned long currentMillis;
                  int t=0;
                  int timer;


                  void setup()
                  {
                  Serial.begin(9600);
                  genie.Begin(Serial);
                  pinMode(led, OUTPUT);
                  genie.AttachEventHandler(myGenieEventHandler);
                  pinMode(RESETLINE, OUTPUT);
                  digitalWrite(RESETLINE, 1);
                  delay(100);
                  digitalWrite(RESETLINE, 0);
                  delay (3500);
                  }
                  void loop()
                  {
                  genie.DoEvents();
                  }
                  void myGenieEventHandler(void)
                  {
                  genieFrame Event;
                  genie.DequeueEvent(&Event);
                  if ((Event.reportObject.cmd == GENIE_REPORT_EVENT))
                  {
                  if (Event.reportObject.object == GENIE_OBJ_KEYBOARD)
                  {
                  if ((Event.reportObject.index == 0)&&(cont2<=4))
                  {
                  key[cont]=genie.GetEventData(&Event);
                  if(key[0]==101)
                  {
                  key[0]=0;
                  }
                  if(key[cont]==101) //ENTER
                  {genie.WriteObject(GENIE_OBJ_LED_DIGITS, 0x00,8888);
                  delay(2000);
                  enter=1;
                  }
                  if(key[cont]==99) //CLEAR
                  {genie.WriteObject(GENIE_OBJ_LED_DIGITS, 0x00,0000);
                  Clear=1;
                  cont=0;
                  cont2=0;
                  key[0]=0;
                  key[1]=0;
                  key[2]=0;
                  key[3]=0;
                  key[4]=0;
                  }
                  if((enter!=1)&&(Clear!=1)&&(key[4]!=101))
                  {
                  genie.WriteObject(GENIE_OBJ_LED_DIGITS, 0x00,atoi(key));
                  cont=cont+1;
                  cont2=cont2+1;
                  }
                  Clear=0;
                  if(enter==1)
                  {
                  key[4]=0;
                  y=atoi(key);

                  Comment


                  • #13
                    Hi Payin,

                    Sorry for the delayed reply. Your sketch seems ok at first glance. How about items 1 and 3 above?
                    Doff

                    Comment

                    Working...
                    X