arduino & tfduino whit FB ?

General FreeBASIC programming questions.
Post Reply
bluatigro
Posts: 660
Joined: Apr 25, 2012 10:35
Location: netherlands

arduino & tfduino whit FB ?

Post by bluatigro »

is there here anyone whit experiance
whit controling the arduino and ftduino
whit FB

how do i l;ink a DIY c++ lib to FB ?
how do i create a c++ lib that can be linked to FB ?

a verry simple example wil be good
bluatigro
Posts: 660
Joined: Apr 25, 2012 10:35
Location: netherlands

Re: arduino & tfduino whit FB ?

Post by bluatigro »

update :
i font the folowing on the libertyB site :
who helps to translate this to FB ?


WARNING : this is libertyB code

Code: Select all



' Command Pin Range Value to send Response
'====================================================================
' GetDigital 0-13 - pin,0 or 1
' The port is set to input and the pullup resistor set high,
' ground the pin via a switch/sensor to pull it low.

' GetAnalog 0-5 (analog) - pin,0-1023 0-5v
' The analog pins 0-5 return 0-1023 measuring 0-5v on the pin, connect
' pots, LDRs, Thermistors or any resistance based sensor.

' GetPWM 0-13 0 or 1 pin,microseconds
' Sending 0 will measure the low pulse 1 will measure the high pulse,
' connect gyros and accelerometers that provide PWM output.

' SetDigital 0-13 0 or 1 -
' This will set the pin HIGH or LOW use with LEDs or driver electronics
' to switch relays or pulse motors.

' SetPWM(analog) 3,5,6,9,10,11 0-255 -
' This sets the PWM ratio on any of the legal pins from 0% HIGH to 100% HIGH
' connect LEDs or with driver electronics control motot speed.

' AddServo 0-13 - -
' Servos must first be assigned a pin, this disables PWM on pins 9 and 10

' SetServo 0-13 0-180 -
' This sets the servo angle in degrees. But many electronic motor control (ESCs) and gyro
' gadgets use this form of PWM output. Forward and reverse motor speed control and gyro
' stabilised servo control are all possible.

' DelServo 0-13 - -
' Servos must be detached for the pin to be reused, when all are detached pin 9 and 10
' are reenabled for PWM

' SetTone 0-13 Hz 31-4978 -
' Only one pin can output a tone at any time, sending 0 silences the tone.

' Ping 0-13 - pin,microseconds
' This pings an ultrasonic transducer, divide the result by 29 then 2 to get cm distance





nomainwin
dim port$( 256 ) 'com port name list
dim cmd$( 10 )
cmd$( 1 ) = "AddServo"
cmd$( 2 ) = "GetDigital"
cmd$( 3 ) = "GetAnalog"
cmd$( 4 ) = "GetPWM"
cmd$( 5 ) = "SetDigital"
cmd$( 6 ) = "SetPWM"
cmd$( 7 ) = "SetServo"
cmd$( 8 ) = "SetTone"
cmd$( 9 ) = "Ping"
cmd$( 10 ) = "DelServo"

dim pin$( 20 )
for n = 0 to 13
  pin$( n + 1 ) = str$( n )
next

dim val$( 20 )
val$( 1 ) = "AsLOW" 'value of 0 sent
val$( 2 ) = "AsHIGH" 'value of 1 sent
val$( 3 ) = "AsSlider" 'value taken from slider, 0-255 for PWM 0-180 for servo
val$( 4 ) = "AsNumeric" 'value taken fro textbox
val$( 5 ) = "None" 'value of 0 sent

global Port , Cmd , Pin , Val , Buffer$ , Slider

Port = 0
Slider = 128

WindowWidth = 550
WindowHeight = 195
UpperLeftX = int( ( DisplayWidth - WindowWidth ) / 2 )
UpperLeftY = int( ( DisplayHeight - WindowHeight ) / 2 )

combobox #main.cbport, port$(, portClick, 5, 80, 50, 100
statictext #main.stport, "Com Port", 5, 60, 60, 20
combobox #main.cbcmd, cmd$(, cmdClick, 60, 80, 90, 100
statictext #main.stcmd, "Command", 60, 60, 60, 20
combobox #main.cbpin, pin$(, pinClick, 155, 80, 40, 100
statictext #main.stpin, "Pin", 155, 60, 20, 20
combobox #main.cbval, val$(, valClick, 200, 80, 80, 20
statictext #main.starg, "Argument", 200, 60, 135, 20
textbox #main.tbvalue, 290,80,75,25
statictext #main.stval, "Value", 290, 60, 135, 20
button #main.send,"Send",send, UL, 290, 125, 75, 25
textbox #main.tbrequest, 375, 80, 150, 25
statictext #main.strequest, "Request", 375, 60, 60, 20
textbox #main.tbresponse, 375, 125, 150, 25
statictext #main.stresponse, "Response", 375, 105, 60, 20
graphicbox #main.gbslider, 10, 125, 256, 19
statictext #main.st01, "0", 5, 105, 10, 15
statictext #main.st02, "50", 130, 105, 20, 15
statictext #main.st03, "100", 250, 105, 20, 15



open "Arduino simple interface" for window as #main
#main "trapclose quit"
#main.gbslider "down"
#main.gbslider "fill lightgray"
#main.gbslider "backcolor black"
#main.gbslider "line 0 8 256 8"
#main.gbslider "place ";Slider-4;" 0 ; boxfilled ";Slider+4;" 17"
#main.gbslider "when leftButtonMove mouse"
#main.gbslider "when leftButtonDown mouse"
#main.gbslider "when characterInput key"

'find out what com ports are available and load the combobox
call getPorts
'we need an endless loop to clear out the serial buffer Arduino only stores 64 bytes
while 1
  scan
  call getresponse
wend

'left and right arrow keys nudge the slider
sub key h$,k$
  key=asc(right$(k$,1))
  if key=_VK_LEFT then Slider=Slider-1
  if key=_VK_RIGHT then Slider=Slider+1
  call display h$
end sub

'mouse moves the slider
sub mouse h$,x,y
  Slider = x
  call display h$
end sub

'update the slider display, map the value and put it in the textbox
'then send the change request
sub display h$
  if Slider > 255 then Slider = 255
  if Slider < 0 then Slider = 0
  #main.st02, str$(Slider)
  #main.gbslider "fill lightgray"
  #main.gbslider "line 0 8 256 8"
  #main.gbslider "place ";Slider-4;" 0 ; boxfilled ";Slider+4;" 17"
  #main.cbval "selectionindex? i"
  if Cmd >= 5 and Cmd <=7 and i=3 then
  if Cmd=6 then
    #main.tbvalue int(Slider/1.42)'0-180
  else
    #main.tbvalue Slider'0-255
  end if
  call send h$
end if
end sub

'Subs to handle comboboxes=================================================

'what command did the user click,if it needs no argument set val to 0
sub cmdClick h$
  #main.cbcmd "selectionindex? i"
  Cmd=i-1
  if (Cmd>=0 and Cmd<=3) or (Cmd>=8 and Cmd<=9) then
    #main.cbval "selectindex 5"
    #main.tbvalue 0
   end if
end sub

'what pin was selected
sub pinClick h$
  #main.cbpin "selectionindex? i"
  Pin=i-1
end sub

'where have we to take the value from, slider,textbox or pre defined
sub valClick h$
  #main.cbval "selectionindex? i"
  Val=i-1
  if Val=0 or Val=1 then #main.tbvalue Val
  if Val=2 then #main.tbvalue Slider
  if Val=3 or Val=4 then #main.tbvalue 0
  if Val=3 then #main.tbvalue "!setfocus"
end sub

'send the request if the button is clicked
sub send h$
  #main.tbvalue "!contents? v$"
  Val=val(v$)
  #main.tbresponse ""
  msg$=str$(Cmd)+","
  msg$=msg$+right$("00"+str$(Pin),2)+","
  msg$=msg$+right$("0000"+str$(Val),4)+"*"
  #main.tbrequest msg$
  #port msg$
end sub

'suck the input buffer dry, keep the remnants of the message if the whole
'message has not been received
sub getresponse
  if Port then
    if lof(#port)>0 then
      Buffer$=Buffer$+input$(#port, lof(#port))
      endofdata=instr(Buffer$,"*",1)
[loop]
      if endofdata>0 then
      'we have a valid end
        dat$=left$(Buffer$,endofdata-1)
        Buffer$=right$(Buffer$,len(Buffer$)-endofdata)
        pin=val(word$(dat$,1,","))
        dat=val(word$(dat$,2,","))
        #main.tbresponse pin;" ";dat
        endofdata=instr(Buffer$,"*",1)
        if endofdata>0 then [loop]
      end if
    end if
  end if
end sub

'Subs to handle the com port ==============================================
sub portClick h$
  'take com port combobox input, open choosen com port
   #main.cbport "selection? p$"
   if Port then close #port
   if p$<>"" then
     open p$;":9600,n,8,1,ds0,cs0,rs" for random as #port
       Port=1
       call delay 500
   end if
end sub

sub getPorts
  'test first 32 ports and load combobox list for valid serial ports
  index=1
  for p = 1 to 32
    oncomerror [trap]
    open "Com";str$(p);":9600,n,8,1,ds0,cs0,rs" for random as #com
      port$(index)="Com";str$(p)
      index=index+1
    close #com
    [trap]
    oncomerror
  next
  #main.cbport, "reload"
  'now if there is only one port open it
  if port$(1)<>"" and port$(2)="" then
  open port$(1);":9600,n,8,1,ds0,cs0,rs" for random as #port
    Port=1
    #main.cbport, "selectindex 1"
    call delay 500
  else
    #main.cbport, "selectindex 0"
  end if
end sub

sub delay m
  CallDLL #kernel32, "Sleep", m As ulong, Sleep As void
end sub

sub quit h$
  close #main
  if Port then close #port
  end
end sub
arduino scetch :

Code: Select all

#include <Servo.h>
// set up 14 servo objects in an array
Servo myservos[14] ;

void setup() {
  Serial.begin(9600);
  Serial.println("Com Open");
}
void loop() {
// checkPot() uses pin 4 (or pin 18 in LB rpog ) and pin 13
// do not change settings on these in the lbRun() function
  checkPot() ;
}
// serialEvent checks to see if data is available on serial port
// when a message comes we just invoke our LB function
// unlike LB functions can be used without assigning a return value
// an initial * is needed for each message as it is consumed my serialEvent()

void serialEvent()
{
// check if data available, if so call the LB routine
  if ( Serial.available() )
  {
    lbRun() ;
  }
}

// all the functionality from previous LB sketch is now wrapped up here
// this is called automatically from serialEvent() when a new message is detected
void lbRun()
{
  while ( Serial.available() > 0 )
  {
    // look for the next valid integer in the incoming serial stream:
    int cmd = Serial.parseInt();
    // do it again:
    int pin = Serial.parseInt();
    // do it again:
    int val = Serial.parseInt();
    // look for the newline. That's the end of your
    // sentence:
    if ( Serial.read() == '*' )
    {
      if ( cmd == 0 ) //pin mode set
      {
        if ( val == 0 ) //input
        {
          pinMode( pin , INPUT ) ;
          digitalWrite( pin , HIGH ) ;
        }
        if ( val == 1 ) //output
        {
          pinMode( pin , OUTPUT ) ;
        }
        if ( val == 2 ) //attach servo
        {
          myservos[ pin ].attach( pin ) ;
        }
        if ( val == 3 ) //detach servo
        {
          myservos[ pin ].detach() ;
        }
      }
      if ( cmd == 1 ) //getdigital
      {
        Serial.print( pin ) ;
        Serial.print( "," ) ;
        Serial.print( digitalRead( pin ) ) ;
        Serial.print( "*" ) ;
      }
      if ( cmd == 4 ) //setdigital
      {
        digitalWrite( pin , val ) ;
      }
      if ( cmd == 5 ) //setanalog
      {
        analogWrite( pin , val ) ;
      }
      if ( cmd == 2 ) //getanalog
      {
        analogRead( pin ) ;
        delay( 10 ) ;
        Serial.print( pin + 14 ) ;
        Serial.print( "," ) ;
        Serial.print( analogRead( pin ) ) ;
        Serial.print( "*" ) ;
      }
      if ( cmd == 6 ) //setservo
      {
        myservos[ pin ].write( val ) ;
        delay( 15 ) ;
      }
      if ( cmd == 7 ) //settone
      {
        if ( val == 0 )
        {
          noTone( pin ) ;
        }
        else
        {
          tone( pin , val ) ;
        }
      }
      if ( cmd == 3 ) //getpulse
      {
        Serial.print( pin ) ;
        Serial.print( "," ) ;
        Serial.print( pulseIn( pin , val ) ) ;
        Serial.print( "*" ) ;
      }
    }
  }
} // end or lbRun()

void checkPot()
{
  if ( analogRead( 4 ) > 500 ) // check for value too high
  {
    Serial.print( "help Brian , he is far too Hot !!" ) ;
    // a numeric alternative could be Serial.print("10000,10000,*");
    // or anything that the LB program checks for in serial messages
    Serial.print( "*" ) ;
    pinMode( 13 , OUTPUT ) ;
    for( int i = 0 ; i < 5 ; i++ )
    {
      digitalWrite( 13 , HIGH ) ;
      delay( 200 ) ;
      digitalWrite( 13 , LOW ) ;
      delay( 200 ) ;
    } // end of loop
  } // end of if clause
} // end of checkpot()
Post Reply