Arduino Uno & Usb Host

diego

Member
Licensed User
Longtime User
I'd like to connect an Arduino Uno by USBAccesory and read data.

Anyone could post a sample code on connecting a USBAccesory?

I don't know if this is the way:

B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   Dim accesories() As UsbAccessory
   Dim manager As UsbManager
   Dim indexArduino As Int
   Dim timer1 As Timer
End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.

   Dim Label1 As Label
   Dim Label2 As Label
   Dim Label3 As Label
   Dim Label4 As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime = True Then
      Activity.LoadLayout("main")
      manager.Initialize
      accesories = manager.GetAccessories
      For a = 0 To accesories.Length - 1
         If accesories(a).Description = "Arduino UNO" Then
            indexArduino = a
         End If
      Next
      Label1.Text = accesories(indexArduino).Description
      Label2.Text = accesories(indexArduino).Manufacturer
      Label3.Text = accesories(indexArduino).Model
      timer1.Initialize(1000)
   End If
End Sub
Sub timer1_tick
   Label4.Text = accesories(indexArduino).InputStream
End Sub


:sign0085:
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

jscoulter

Member
Licensed User
Longtime User
Wow....it must be fate !! .....I have JUST literly been looking at the Arduino Uno board becuase I saw it had android on it, and before I buy one, because I need to build an A to D circuit, and thought I would come and see if anyone has interfaced to the Arduino boards and how do you do it ?
As I say I want to build and A to D converter so I can display voltage, and I suspect I can do this with the Arduino and display it on and android app, prob a tablet, but wondered how I do that with B4A :)

Jeremy
 
Upvote 0

hdtvirl

Active Member
Licensed User
Longtime User
I used some code Andrew Graham posted for use with a serial to USB converter based on the PL2303 chip. Please view this posting for more details. http://www.basic4ppc.com/forum/addi...es/11449-pl2303-usb-rs232-adaptor-driver.html

I was using the Arduino Mega Board (Non Android) and used one of it serial ports to connect to my tablet via a MAX232 circuit I had to construct.

I was using the device to log Time and Temperature, I was polling the device every min to get the data. I had planned to use it with a Android mobile phone as a logging device.

There are some very good YouTube posting from a guy called Jeremy Blum see for more details Arduino Tutorials | JeremyBlum.com

My son gave me the Arduino Board as a Christmas Present, I must dig it out and try a few more experiments with it.

Hope this helps.

Regards

BOB
 
Upvote 0

jscoulter

Member
Licensed User
Longtime User
Thanks for the link. I am looking at it now.
I have been developing stuff for the PIC Micro for years, but recently I had to use it after moving the IDE I use from an old machine to a new one and forgot about the mental Lic. scheme it has. I have to register the new version of the IDE and then they send you a USB key dongle...about 1900's but the board I am looking at is going to save SO much time mucking around building repeatative circuits AND now I can use a color LCD :) (the screen on my phone)
I havent been this excited about electronics in a while :).....sad but true:sign0060:

Jeremy
 
Upvote 0

diego

Member
Licensed User
Longtime User
There are several problems in your code.
First you should move Activity.LoadLayout out of the FirstTime condition. See this important tutorial: http://www.basic4ppc.com/forum/basi...87-android-process-activities-life-cycle.html

Is the board recognized? Do the labels show the proper values (not Label4)?

I recommend you to use Log instead of these labels and check the logs tab.

Actually I couldn't make a good test because I was trying to connect with my Android 2.3.4 phone with a USB host cable, so I think I need to buy first a 3.1 tablet or so.

All my tests crash (java.lang.NullPointerException) and the app closes. At this moment I'm in "stand-by mode", but I don't give up...

thanks!
 
Last edited:
Upvote 0
Top