Wednesday, February 29, 2012

How to convert string to integer in Arduino?

Today just found myself having to convert a string to an integer in an Arduino. Had a code that did character to integer but not from string. So copy from here to there ended up with this function. And posted here just for anybody to use or have a starting point.
Here is the code, use it change it or don't use it. Hope this saves you some time.
 int8_t satoi( char *str, int len ){  
  int8_t r = 0;  
  for(int i=0; i<len; i++){  
   //Check if this is a number  
   if ( str[i] < 0x3a && str[i] > 0x2f){  
     // is a ASCII number, return it  
     r = r * 10;  
   r += (str[i]-0x30);  
   }else{  
    i = len; //exit!  
    r = -1;  
    break;  
   }    
  }  
  return r;   
 }  
 int8_t satoi( char *str ){  
  int8_t r = 0;  
  int len = strlen(str);  
  for(int i=0; i<len; i++){  
   //Check if this is a number  
   if ( str[i] < 0x3a && str[i] > 0x2f){  
     // is a ASCII number, return it  
     r = r * 10;  
   r += (str[i]-0x30);  
   }else{  
    i = len; //exit!  
    r = -1;  
    break;  
   }    
  }  
  return r;   
 }  

Tuesday, February 28, 2012

Progress on arduino tv remote

Today received the zener diodes needed for a virtual usb keyboard and lucky me it worked. I was able to load the virtual usb keyboard libs from the web and run a hello world.
Next step is to put it all together with the irda and ethernet code. I will continue on this tomorrow and hope to complete soon. After this would be a very nice post to share.
Building, programming and sharing would make me go full circle.


Sunday, February 26, 2012

Arming my arduino

Just a view of how looks like an Arduino with a cheap Ethernet module and an infrared led with corresponding receiver. Separated all is working. Is a matter of creating interrupts for multitasking.  If not possible then this could become a dual core arduino project. Oh and this build is missing more LEDs and an USB cable to emulate a keyboard. Oh and of course a mobile phone app.
I just want to complete this project to start using it. And write a post here about it.

Update:
Due to interrupts and clock limitation (also I tried and tried but could not make the two codes into one), the project ended up using two arduino chips. Each connected via serial. One emulating the USB Keyboard and one with the ethernet and IRDA. All this might be able to be resumed into just one arduino UNO board with a different firmware for the usb to serial chip and might look actually better and smaller.