=<span style="font-size: 1.4em; line-height: 1.5;">Using an Arduino as a slave board</span>= <span style="font-size: 1.4em; line-height: 1.5;">Arduino serial interfacing and basic function library </span> [[toc]] //By Mike Molianri// [[image:mmiscool.png]] <span style="font-size: 1.4em; line-height: 1.5;">**Home automation, Robots, Vending machines. What do they have in common?**</span> ---- The answer is computers controlling hardware. Making lights go on and off. Controlling motors, relays and other electronic components. Liberty Basic for windows makes programming simple but lacks the ability to directly control or interface with hardware (except the parallel port). To make hardware interface coding simpler a set of functions is required to facilitate the communication with external hardware. Because the arduino is the most widely used hobby micro controller in the world and has so much support it is the obvious choice make a set of simple functions for LB users to take advantage of this great platform for there projects. The things covered in this article are limited to simple port I/O (Input and output). * <span style="line-height: 1.5;">Test if pin is high or low</span> ** <span style="line-height: 1.5;">Get input from buttons and other sensors connected to the arduino</span> * <span style="line-height: 1.5;">Set a pin high or low</span> ** <span style="line-height: 1.5;">Turn things on and off like lights, buzzers, motors, ect.</span> * PWM (Pulse width modulation) output ** Dim lights, Control servo motors, interface with variable speed motor controllers * PWM <span style="line-height: 1.5;">(Pulse width modulation) input</span> ** <span style="line-height: 1.5;">Get input from things like sliders and position information</span> ---- =Getting Started with the functions= ||~ Code ||~ Example Circuit || || ===Initialization code=== <span style="line-height: 19.5px;">In order to use the functions there are 2 things that need to be done at the beginning of the program.</span> <span style="line-height: 19.5px;">The AD.start$() function must be called and the ad.pinstatus() array must be initialized.</span> <span style="line-height: 19.5px;">There is no function to open the com port. This will be taken care of the first time the com port is used.</span> [[code]] print AD.start() dim ad.pinstatus(10,100) [[code]] || || || ===Setting a pin Status=== To set a pin high or low you can call the following function. The com port must be specified as a number (To address com3 you would use 3) The pin must be a number of a pin present on the arduino. The value to send must be a 1 or 0 (1 = high, 0 = low) The return value will be a 1 if successful and 0 if not. [[code]] return.value = AD.Set(Com.port, Pin, Val.To.Send) [[code]] || [[image:ArduinoAsSlaveBoard-LED.png width="426" height="320"]] || || ===Pulling the input status of a pin=== This function will retrieve a 1 or 0 depending on what the status of a sensor connected to the arduino is. The com port must be specified as a number (To address com3 you would use 3) The pin must be a number of a pin present on the arduino. Will return a 1 or a 0 depending on if the input pin is pulled high or low. [[code]] return.value = AD.Get(Com.port.Selection, Pin.Selection) [[code]] || [[image:ArduinoAsSlaveBoard-button_schem.png width="333" height="287"]] || || ===Setting a PWM output=== PWM Will allow you to control a servo, dim a light, ect. The com port must be specified as a number (To address com3 you would use 3) The pin must be a number of a pin present on the arduino. The value must be a number between 0 and 254. [[code]] return.value = AD.PWM.Out(Com.port.Selection, Pin.Selection, Val.To.Send) [[code]] || [[image:dimmer-circuit3 (1).png width="446" height="445"]] || || ===Pulling the PWM input status of a pin=== The com port must be specified as a number (To address com3 you would use 3) The pin must be a number of a pin present on the arduino. The return value will be number between 0 and 254. [[code]] return.value = AD.PWM.In(Com.port.Selection, Pin.Selection) [[code]] || || || ===Finding what you previously set a pin to=== To find out what a pin was set to previously you can look up what it was set to. This is a simple array that holds what a pin was set to and can be used like any other array. [[code]] status.pin.was.set.to = ad.pinstatus(Com.port, Pin) [[code]] || || || ===Setting a servo output=== PWM Will allow you to control a servo. The com port must be specified as a number (To address com3 you would use 3) The pin must be a number of a pin present on the arduino. The value must be a number between 0 and 180 and will ruffly be the number of degrees the servo will rotate to. [[code]] return.value = AD.Servo(Com.port.Selection, Pin.Selection, Val.To.Send) [[code]] || [[image:ArduinoAsSlaveBoard-servo.png]] || ---- =The Arduino Code= To make liberty basic talk with an arduino they have to speak the same language. In this case a string is used and send over to the device followed by a carriage return character CHR$(13) The only thing you really have to understand about the arduino code is that it must be download to the arduino board using the arduino IDE. This can be download from the following link. http://arduino.cc/en/Main/Software **Note:** This code is done in arduinos special form of c and is not basic code. [[code]] #include <Servo.h> // Buffer to store incoming commands from serial port String inData; String inData; //declare each of the servoes or some thing Servo LBServo1; Servo LBServo2; Servo LBServo3; Servo LBServo4; Servo LBServo5; Servo LBServo6; Servo LBServo7; Servo LBServo8; Servo LBServo9; Servo LBServo10; Servo LBServo11; Servo LBServo12; Servo LBServo1; Servo LBServo2; Servo LBServo3; Servo LBServo4; Servo LBServo5; Servo LBServo6; Servo LBServo7; Servo LBServo8; Servo LBServo9; Servo LBServo10; Servo LBServo11; Servo LBServo12; void setup() { Serial.begin(9600); Serial.println("Serial conection started, waiting for instructions..."); } Serial.begin(9600); Serial.println("Serial conection started, waiting for instructions..."); } void loop() { while (Serial.available() > 0) { char recieved = Serial.read(); inData += recieved; while (Serial.available() > 0) // Process message when new line character is recieved if (recieved == '\n') { String Param0 = getValue(inData, ' ', 0); String Param1 = getValue(inData, ' ', 1); String Param2 = getValue(inData, ' ', 2); String Param3 = getValue(inData, ' ', 3); String Param4 = getValue(inData, ' ', 4); String Param5 = getValue(inData, ' ', 5); { inData = ""; char recieved int valParam0 = Serial.read();Param0.toInt(); int valParam1 = Param1.toInt(); int valParam2 = Param2.toInt(); int valParam3 = Param3.toInt(); int valParam4 = Param4.toInt(); int valParam5 = Param5.toInt(); inData += recieved; // Process message when new line character is recieved if (recieved == '\n') //Serial.print(valParam0); //Serial.print(valParam1); //Serial.print(valParam2); //Serial.print(valParam3); //Serial.print(valParam4); //Serial.print(valParam5); { String Param0 = getValue(inData, ' ', 0); String Param1 = getValue(inData, ' ', 1); String Param2 = getValue(inData, ' ', 2); if ( Param0 == "get") { pinMode(valParam1, INPUT); Serial.println(digitalRead(valParam1)); String Param3 = getValue(inData, ' ', 3); String Param4 = getValue(inData, ' ', 4); } String Param5 = getValue(inData, ' ', 5); inData = ""; int valParam0 = Param0.toInt(); if ( Param0 == "set") { pinMode(valParam1, OUTPUT); digitalWrite(valParam1, valParam2); Serial.println("1"); int valParam1 = Param1.toInt(); int valParam2 = Param2.toInt(); } int valParam3 = Param3.toInt(); int valParam4 = Param4.toInt(); if ( Param0 == "pwm.out") { analogWrite(valParam1, valParam2); Serial.println("1"); int valParam5 = Param5.toInt(); } //Serial.print(valParam0); //Serial.print(valParam1); //Serial.print(valParam2); if ( Param0 == "pwm.in") { Serial.println(analogRead(valParam1)); } //Serial.print(valParam3); //Serial.print(valParam4); //Serial.print(valParam5); if if ( Param0 == "get") {"servo") { switch (valParam1) { case 1: LBServo1.attach(1); LBServo1.write(valParam2); Serial.println("1"); break; case 2: LBServo2.attach(2); LBServo2.write(valParam2); Serial.println("1"); break; case 3: LBServo3.attach(3); LBServo3.write(valParam2); Serial.println("1"); break; case 4: LBServo4.attach(4); LBServo4.write(valParam2); Serial.println("1"); break; case 5: LBServo5.attach(5); LBServo5.write(valParam2); Serial.println("1"); break; case 6: LBServo6.attach(6); LBServo6.write(valParam2); Serial.println("1"); break; case 7: LBServo5.attach(7); LBServo7.write(valParam2); Serial.println("1"); break; case 8: LBServo8.attach(8); LBServo8.write(valParam2); Serial.println("1"); break; case 9: LBServo9.attach(9); LBServo9.write(valParam2); Serial.println("1"); break; case 10: LBServo10.attach(10); LBServo10.write(valParam2); Serial.println("1"); break; case 11: LBServo11.attach(11); LBServo11.write(valParam2); Serial.println("1"); break; case 12: LBServo12.attach(12); LBServo12.write(valParam2); Serial.println("1"); break; } pinMode(valParam1, INPUT); Serial.println(digitalRead(valParam1)); } } } } if ( Param0 == "set") { pinMode(valParam1, OUTPUT); digitalWrite(valParam1, valParam2); Serial.println("1"); } if ( Param0 == "pwm.out") { analogWrite(valParam1, valParam2); Serial.println("1"); } if ( Param0 == "pwm.in") { Serial.println(analogRead(valParam1)); } if ( Param0 == "servo") { switch (valParam1) { case 1: LBServo1.attach(1); LBServo1.write(valParam2); case 2: LBServo2.attach(2); LBServo2.write(valParam2); case 3: LBServo3.attach(3); LBServo3.write(valParam2); case 4: LBServo4.attach(4); LBServo4.write(valParam2); case 5: LBServo5.attach(5); LBServo5.write(valParam2); case 6: LBServo6.attach(6); LBServo6.write(valParam2); case 7: LBServo5.attach(7); LBServo7.write(valParam2); case 8: LBServo8.attach(8); LBServo8.write(valParam2); case 9: LBServo9.attach(9); LBServo9.write(valParam2); case 10: LBServo10.attach(10); LBServo10.write(valParam2); case 11: LBServo11.attach(11); LBServo11.write(valParam2); case 12: LBServo12.attach(12); LBServo12.write(valParam2); } Serial.println("1"); } } } } String getValue(String data, char separator, int index) { int int found = 0; int int strIndex[] = { 0, 0, -1 }; int }; int maxIndex = data.length() - 1; for for (int i = 0; i <= maxIndex && found <= index; i++) { if if (data.charAt(i) == separator || i == maxIndex) { found++; strIndex[0] found++; strIndex[0] = strIndex[1] + 1; strIndex[1] strIndex[1] = (i == maxIndex) ? i + 1 : i; } } return found > index ? data.substring(strIndex[0], strIndex[1]) : ""; } }[[code]] return found > index ? data.substring(strIndex[0], strIndex[1]) : ""; } [[code]] ---- =The Liberty Basic Demo= ||< <span style="line-height: 19.5px;">The demo program shows off the basic functionality of the library.</span> <span style="line-height: 19.5px;">The Com port that the arduino is on must be placed in the com port number text box.</span> <span style="line-height: 19.5px;">A pin must be selected and a value to send must be entered.</span> <span style="line-height: 19.5px;">The 4 buttons on the right are used to execute the functions.</span> <span style="line-height: 19.5px;">The return value is populated by the what is returned by the arduino. </span> ||< [[image:ArduinoAsSlaveBoard-Screen shot.png]] || [[code]] dim ad.pinstatus(10,100) dim ad.pinSetting$(10,100) for x = 0 to 10 for y = 0 to 100 ad.pinSetting$(x,y) = "Un-Set" next y dim ad.pinstatus(10,100) dim ad.pinSetting$(10,100) next for x = 0 to 10 for y = 0 to 100 ad.pinSetting$(x,y) = "Un-Set" next y next x 'Populate pin list dim pins$(100) for 'Populate pin list dim pins$(100) for x = 0 to 13 pins$(x) = str$(x) next x pins$(x) = str$(x) next x 'Populate com port drop down dim Comm.Ports$(10) for x = 0 to 10 Comm.Ports$(x) = str$(x) next x for x = 0 to 10 Comm.Ports$(x) 'nomainwin WindowWidth = str$(x)450 WindowHeight = 445 UpperLeftX=int((DisplayWidth-WindowWidth)/2) UpperLeftY=int((DisplayHeight-WindowHeight)/2) next x listbox #main.PinSelector, pins$(, [listbox1DoubleClick], 10, 82, 40, 320 listbox #main.PinSetting, pin.Settings$(, [set.pin.settings], 50, 82, 40+70, 320 listbox #main.PinStatuses, pin.statuses$(, [set.pin.output], 90+70, 82, 50, 320 statictext #main.statictext2, "Pin", 10, 62, 40, 20 statictext #main.statictext50, "Setting", 50, 62, 40, 20 statictext #main.statictext55, "Value", 90+70, 62, 40, 20 'nomainwin WindowWidth = 450 textbox #main.ValToSend, 165+70, 32, 170, 25 statictext #main.statictext4, "Value To Send", 165+70, 12, 170, 20 statictext #main.statictext5, "Return Value", 165+70, 62, 175, 20 textbox #main.ReturnValue, 165+70, 82, 170, 25 button #main.button7,"Set Pin High/Low Status",[Set.Pin], UL, 165+70, 117, 170, 25 button #main.button8,"Get Pin High/Low Status",[Get.Pin], UL, 165+70, 152, 170, 25 button #main.button9,"PWM Output",[PWM.Out], UL, 165+70, 187, 170, 25 button #main.button10,"PWM Input",[PWM.In], UL, 165+70, 222, 170, 25 button #main.button11,"Servo Out",[Servo.out], UL, 165+70, 222+35, 170, 25 WindowHeight = 445 button #main.button11,"Update All Pins",[Update.Pins], UL, 165+70, 300, 170, 25 statictext #main.statictext11, "Com Port Number", 10, 12, 145+70, 20 combobox #main.ComPortNo, Comm.Ports$(), [update.pin.info], 10, 32, 130+70, 25 UpperLeftX=int((DisplayWidth-WindowWidth)/2) open "LB Ultra Simple Arduino Demo" for dialog as #main print #main, "trapclose [quit.main]" wait UpperLeftY=int((DisplayHeight-WindowHeight)/2) listbox #main.PinSelector, pins$(, [listbox1DoubleClick], 10, 82, 40, 320 listbox #main.PinSetting, pin.Settings$(, [set.pin.settings], 50, 82, 40+70, 320 listbox #main.PinStatuses, pin.statuses$(, [set.pin.output], 90+70, 82, 50, 320[set.pin.settings] gosub [Get.The.Selctions.And.Textboxes.From.Main.Win] statictext #main.statictext2, "Pin", 10, 62, 40, 20 print #main.PinSetting, "selectionindex? index" index = index - 1 statictext #main.statictext50, "Setting", 50, 62, 40, 20 statictext #main.statictext55, "Value", 90+70, 62, 40, 20 Dim pin.setting.options$(100) pin.setting.options$(1) = "set" pin.setting.options$(2) = "get" pin.setting.options$(3) = "pwm.out" pin.setting.options$(4) = "pwm.in" pin.setting.options$(5) = "servo" textbox #main.ValToSend, 165+70, 32, 170, 25 statictext #main.statictext4, "Value To Send", 165+70, 12, 170, 20 WindowWidth = 195 WindowHeight = 120 UpperLeftX=int((DisplayWidth-WindowWidth)/2) UpperLeftY=int((DisplayHeight-WindowHeight)/2) statictext #main.statictext5, "Return Value", 165+70, 62, 175, 20 textbox #main.ReturnValue, 165+70, 82, 170, 25 button #main.button7,"Set combobox #PinSetting.Selection, pin.setting.options$(, [Pin.Setting.Done] , 15, 27, 160, 125 statictext #PinSetting.statictext2, "Select Pin High/Low Status",[Set.Pin], UL, 165+70, 117,Setting", 15, 7, 170, 25 button #main.button8,"Get Pin High/Low Status",[Get.Pin], 20 button #PinSetting.button3,"Done",[Pin.Setting.Done], UL, 165+70, 152, 170, 25 button #main.button9,"PWM Output (Servos)",[PWM.Out], UL, 165+70, 187, 170, 25 button #main.button10,"PWM Input",[PWM.In], UL, 165+70, 222, 170, 25 button #main.button11,"Update All Pins",[Update.Pins], UL, 165+70, 300, 170, 25 statictext #main.statictext11, "Com Port Number", 10, 12, 145+70, 20 combobox #main.ComPortNo, Comm.Ports$(), [update.pin.info], 10, 32, 130+70, 25 open "LB Ultra Simple Arduino Demo" for dialog as #main print #main, "trapclose [quit.main]" 15, 57, 160, 25 open "Pin Setting" for dialog as #PinSetting print #PinSetting, "font ms_sans_serif 10" print #PinSetting, "trapclose [Pin.Setting.Done]" wait [set.pin.settings] gosub [Get.The.Selctions.And.Textboxes.From.Main.Win] print #main.PinSetting, "selectionindex? index" index = index - 1 Dim pin.setting.options$(100) pin.setting.options$(1) = "set" pin.setting.options$(2) = "get" pin.setting.options$(3) = "pwm.out" pin.setting.options$(4) = "pwm.in" pin.setting.options$(5) = "servo" WindowWidth = 195 WindowHeight = 120 UpperLeftX=int((DisplayWidth-WindowWidth)/2) UpperLeftY=int((DisplayHeight-WindowHeight)/2) combobox #PinSetting.Selection, pin.setting.options$(, [Pin.Setting.Done] , 15, 27, 160, 125 statictext #PinSetting.statictext2, "Select Pin Setting", 15, 7, 170, 20 button #PinSetting.button3,"Done",[Pin.Setting.Done], UL, 15, 57, 160, 25 open "Pin Setting" for dialog as #PinSetting print #PinSetting, "font ms_sans_serif 10" print #PinSetting, "trapclose [Pin.Setting.Done]" wait [Pin.Setting.Done] print #PinSetting.Selection, "contents? text$" ad.pinSetting$(Com.port.Selection, index) = text$ close #PinSetting goto [Update.Pins] print #PinSetting.Selection, "contents? text$" ad.pinSetting$(Com.port.Selection, index) = text$ close #PinSetting goto [Update.Pins] [set.pin.output] gosub [Get.The.Selctions.And.Textboxes.From.Main.Win] print #main.PinStatuses, "selectionindex? index" index = index - 1 gosub [Get.The.Selctions.And.Textboxes.From.Main.Win] print #main.PinStatuses, "selectionindex? index" if ad.pinSetting$(Com.port.Selection, index) = "set" then 'this code will flip the curent pin status from true to false and vice versa if ad.pinstatus(Com.port.Selection, index) = 1 then ad.pinstatus(Com.port.Selection, index) = 0 else ad.pinstatus(Com.port.Selection, index) = 1 end if end if index if ad.pinSetting$(Com.port.Selection, index) = index - 1"pwm.out" then PROMPT "Value to set and send"; value ad.pinstatus(Com.port.Selection, index) = value end if if if ad.pinSetting$(Com.port.Selection, index) = "set""servo" then PROMPT "Value to set and send"; value ad.pinstatus(Com.port.Selection, index) = value end if goto [Update.Pins] 'this code will flip the curent pin status from true to false and vice versa if ad.pinstatus(Com.port.Selection, index) = 1 then ad.pinstatus(Com.port.Selection, index) = 0 else ad.pinstatus(Com.port.Selection, index) = 1 end if end if if ad.pinSetting$(Com.port.Selection, index) = "pwm.out" then PROMPT "Value to set and send"; value ad.pinstatus(Com.port.Selection, index) = value end if if ad.pinSetting$(Com.port.Selection, index) = "servo" then PROMPT "Value to set and send"; value ad.pinstatus(Com.port.Selection, index) = value end if goto [Update.Pins] [Set.Pin] gosub [Get.The.Selctions.And.Textboxes.From.Main.Win] return.value = AD.Set(Com.port.Selection, Pin.Selection, Val.To.Send) print #main.ReturnValue, return.value goto [update.pin.info] gosub [Get.The.Selctions.And.Textboxes.From.Main.Win] return.value = AD.Set(Com.port.Selection, Pin.Selection, Val.To.Send) print #main.ReturnValue, return.value goto [update.pin.info] [Get.Pin] gosub [Get.The.Selctions.And.Textboxes.From.Main.Win] return.value = AD.Get(Com.port.Selection, Pin.Selection) print #main.ReturnValue, return.value goto [update.pin.info] gosub [Get.The.Selctions.And.Textboxes.From.Main.Win] return.value = AD.Get(Com.port.Selection, Pin.Selection) print #main.ReturnValue, return.value goto [update.pin.info] [PWM.Out] gosub [Get.The.Selctions.And.Textboxes.From.Main.Win] return.value = AD.PWM.Out(Com.port.Selection, Pin.Selection, Val.To.Send) print #main.ReturnValue, return.value goto [update.pin.info] gosub [Get.The.Selctions.And.Textboxes.From.Main.Win] return.value = AD.PWM.Out(Com.port.Selection, Pin.Selection, Val.To.Send) print[PWM.In] gosub [Get.The.Selctions.And.Textboxes.From.Main.Win] return.value = AD.PWM.In(Com.port.Selection, Pin.Selection) print #main.ReturnValue, return.value goto [update.pin.info] goto [update.pin.info] [PWM.In][Servo.out] gosub [Get.The.Selctions.And.Textboxes.From.Main.Win] return.value = AD.Servo(Com.port.Selection, Pin.Selection, Val.To.Send) print #main.ReturnValue, return.value goto [update.pin.info] gosub [Get.The.Selctions.And.Textboxes.From.Main.Win] return.value = AD.PWM.In(Com.port.Selection, Pin.Selection) print #main.ReturnValue, return.value goto [update.pin.info] [Update.Pins] gosub [Get.The.Selctions.And.Textboxes.From.Main.Win] return.value = AD.Update.All() print #main.ReturnValue, return.value goto [update.pin.info] gosub [Get.The.Selctions.And.Textboxes.From.Main.Win] return.value = AD.Update.All() print #main.ReturnValue, return.value goto [update.pin.info] [quit.main] close #main bla = AD.close.all.the.com.ports() end close #main bla = AD.close.all.the.com.ports() end [Get.The.Selctions.And.Textboxes.From.Main.Win] print #main.ValToSend, "!contents? Val.To.Send"; print #main.ComPortNo, "contents? Com.port.Selection"; print #main.PinSelector, "selection? Pin.Selection" return print #main.ValToSend, "!contents? Val.To.Send"; print #main.ComPortNo, "contents? Com.port.Selection"; print #main.PinSelector, "selection? Pin.Selection"[update.pin.info] gosub [Get.The.Selctions.And.Textboxes.From.Main.Win] dim pin.statuses$(13) return dim pin.Settings$(13) [update.pin.info] for x = 0 to 13 pin.statuses$(x) = str$(ad.pinstatus(Com.port.Selection,x)) pin.Settings$(x) = ad.pinSetting$(Com.port.Selection,x) next x gosub [Get.The.Selctions.And.Textboxes.From.Main.Win] dim pin.statuses$(13) dim pin.Settings$(13) for x = 0 to 13 print #main.PinSetting, "reload" print #main.PinStatuses, "reload" wait pin.statuses$(x) = str$(ad.pinstatus(Com.port.Selection,x)) pin.Settings$(x) = ad.pinSetting$(Com.port.Selection,x) next x print #main.PinSetting, "reload" print #main.PinStatuses, "reload" wait 'Begin Arduino Code here ------------------------------------------------------- 'These functions can be added to any program to provide arduini power input and output 'Descriptions for functions are in there comments 'these are the funtions you call to get the arduino to do your bidding 'these are the funtions you call to get the arduino to do your bidding function AD.Set(com.port, PinNo, value) 'Set an Arduino pin high or low, 'Will return 1 one if successful, 0 if not AD.Set = val(AD.talk.to.device$(com.port,"set ";PinNo;" ";value)) ad.pinstatus(com.port,PinNo) = value ad.pinSetting$(com.port,PinNo) = "set" end function AD.Set = val(AD.talk.to.device$(com.port,"set ";PinNo;" ";value)) ad.pinstatus(com.port,PinNo) = value ad.pinSetting$(com.port,PinNo) = "set" end function function AD.Get(com.port, PinNo) 'Will return the current state of the input pin '1 for high, 0 for low AD.Get = val(AD.talk.to.device$(com.port,"get ";PinNo)) ad.pinstatus(com.port,PinNo) = AD.Get ad.pinSetting$(com.port,PinNo) = "get" end function AD.Get = val(AD.talk.to.device$(com.port,"get ";PinNo)) ad.pinstatus(com.port,PinNo) = AD.Get ad.pinSetting$(com.port,PinNo) = "get" end function function AD.PWM.Out(com.port, PinNo, value) 'Set the PWM output on the select pin to the value 'Will return a 1 for success and 0 for failure. AD.PWM.Out = val(AD.talk.to.device$(com.port,"pwm.out ";PinNo;" ";value)) ad.pinstatus(com.port,PinNo) = value ad.pinSetting$(com.port,PinNo) = "pwm.out" end function AD.PWM.Out = val(AD.talk.to.device$(com.port,"pwm.out ";PinNo;" ";value)) ad.pinstatus(com.port,PinNo) = value ad.pinSetting$(com.port,PinNo) = "pwm.out" end function function AD.PWM.In(com.port, PinNo) 'Get the PWM input from the pin 'Will return a value of 0 to 1023 AD.PWM.In = val(AD.talk.to.device$(com.port,"pwm.in ";PinNo)) ad.pinSetting$(com.port,PinNo) = "pwm.in" end function AD.PWM.In = val(AD.talk.to.device$(com.port,"pwm.in ";PinNo)) ad.pinSetting$(com.port,PinNo) = "pwm.in" end function function AD.Servo(com.port, PinNo, value) 'Get the PWM input from the pin 'Will return a value of 0 to 1023 AD.Servo = val(AD.talk.to.device$(com.port,"servo ";PinNo;" ";value)) ad.pinstatus(com.port,PinNo) = value ad.pinSetting$(com.port,PinNo) = "servo" end function AD.Servo = val(AD.talk.to.device$(com.port,"servo ";PinNo;" ";value)) ad.pinstatus(com.port,PinNo) = value ad.pinSetting$(com.port,PinNo) = "servo" function AD.Update.All() for x = 0 to 10 for y = 0 to 13 if ad.pinSetting$(x,y) = "set" then bla = AD.Set(x, y, ad.pinstatus(x,y)) if ad.pinSetting$(x,y) = "get" then ad.pinstatus(x,y) = AD.Get(x, y) if ad.pinSetting$(x,y) = "pwm.out" then bla = AD.PWM.Out(x, y, ad.pinstatus(x,y)) if ad.pinSetting$(x,y) = "pwm.in" then ad.pinstatus(x,y) = AD.PWM.In(x, y) if ad.pinSetting$(x,y) = "servo" then bla = AD.Servo(x, y, ad.pinstatus(x,y)) next y next x end function function AD.Update.All() for x = 0'end of function to 10call to have the arduino do your bidding for y = 0 to 13 if ad.pinSetting$(x,y) = "set" then bla = AD.Set(x, y, ad.pinstatus(x,y)) if ad.pinSetting$(x,y) = "get" then ad.pinstatus(x,y) = AD.Get(x, y) if ad.pinSetting$(x,y) = "pwm.out" then bla = AD.PWM.Out(x, y, ad.pinstatus(x,y)) if ad.pinSetting$(x,y) = "pwm.in" then ad.pinstatus(x,y) = AD.PWM.In(x, y) if ad.pinSetting$(x,y) = "servo" then bla = AD.Servo(x, y, ad.pinstatus(x,y)) next y next x end function 'end of function to call to have the arduino do your bidding Function AD.talk.to.device$(port,msg$) 'Send a Message to the arduino and return the result sent back by the device 'this function is called by the other functions above if msg$ <> "" then 'Open the com port select case port case 1 if ad.com.ports(1) <> 1 then open "COM" ; port ; ":9600,n,8,1,ds0,cs0,rs" for random as #ArduinoCOMPort1 ad.com.ports(1) = 1 end if case 2 if ad.com.ports(2) <> 1 then open "COM" ; port ; ":9600,n,8,1,ds0,cs0,rs" for random as #ArduinoCOMPort2 ad.com.ports(2) = 1 end if case 3 if ad.com.ports(3) <> 1 then open "COM" ; port ; ":9600,n,8,1,ds0,cs0,rs" for random as #ArduinoCOMPort3 ad.com.ports(3) = 1 end if case 4 if ad.com.ports(4) <> 1 then open "COM" ; port ; ":9600,n,8,1,ds0,cs0,rs" for random as #ArduinoCOMPort4 ad.com.ports(4) = 1 end if case 5 if ad.com.ports(5) <> 1 then open "COM" ; port ; ":9600,n,8,1,ds0,cs0,rs" for random as #ArduinoCOMPort5 ad.com.ports(5) = 1 end if case 6 if ad.com.ports(6) <> 1 then open "COM" ; port ; ":9600,n,8,1,ds0,cs0,rs" for random as #ArduinoCOMPort6 ad.com.ports(6) = 1 end if case 7 if ad.com.ports(7) <> 1 then open "COM" ; port ; ":9600,n,8,1,ds0,cs0,rs" for random as #ArduinoCOMPort7 ad.com.ports(7) = 1 end if case 8 if ad.com.ports(8) <> 1 then open "COM" ; port ; ":9600,n,8,1,ds0,cs0,rs" for random as #ArduinoCOMPort8 ad.com.ports(8) = 1 end if case 9 if ad.com.ports(9) <> 1 then open "COM" ; port ; ":9600,n,8,1,ds0,cs0,rs" for random as #ArduinoCOMPort9 ad.com.ports(9) = 1 end if case 10 if ad.com.ports(10) <> 1 then open "COM" ; port ; ":9600,n,8,1,ds0,cs0,rs" for random as #ArduinoCOMPort10 ad.com.ports(10) = 1 end if end select if msg$ <> "" then 'Open the com port 'Send message to device select select case port case 1 print #ArduinoCOMPort1, msg$ case 1 'Wait until there is a carriage return while foundLF = 0 while lof(#ArduinoCOMPort1) > 0 temp$ = input$(#ArduinoCOMPort1, 1) buffer$ = buffer$ + temp$ if ad.com.ports(1) <> 1 if temp$ = chr$(13) then foundLF = 1 exit while end if wend wend case 2 print #ArduinoCOMPort2, msg$ open "COM" ; port ; ":9600,n,8,1" for random as #ArduinoCOMPort1 'Wait until there is a carriage return while foundLF = 0 while lof(#ArduinoCOMPort2) > 0 temp$ = input$(#ArduinoCOMPort2, 1) buffer$ = buffer$ + temp$ ad.com.ports(1) if temp$ = chr$(13) then foundLF = 1 exit while end if wend wend case 3 print #ArduinoCOMPort3, msg$ end if 'Wait until there is a carriage return while foundLF = 0 while lof(#ArduinoCOMPort3) > 0 temp$ = input$(#ArduinoCOMPort3, 1) buffer$ = buffer$ + temp$ case 2 if temp$ = chr$(13) then foundLF = 1 exit while end if wend wend case 4 print #ArduinoCOMPort4, msg$ if ad.com.ports(2) <> 1 then 'Wait until there is a carriage return while foundLF = 0 while lof(#ArduinoCOMPort4) > 0 temp$ = input$(#ArduinoCOMPort4, 1) buffer$ = buffer$ + temp$ open "COM" ; port ; ":9600,n,8,1" for random as #ArduinoCOMPort2 if temp$ = chr$(13) then foundLF = 1 exit while end if wend wend case 5 print #ArduinoCOMPort5, msg$ ad.com.ports(2) 'Wait until there is a carriage return while foundLF = 10 while lof(#ArduinoCOMPort5) > 0 temp$ = input$(#ArduinoCOMPort5, 1) buffer$ = buffer$ + temp$ end if temp$ = chr$(13) then foundLF = 1 exit while end if wend wend case 6 print #ArduinoCOMPort6, msg$ case 3 'Wait until there is a carriage return while foundLF = 0 while lof(#ArduinoCOMPort6) > 0 temp$ = input$(#ArduinoCOMPort6, 1) buffer$ = buffer$ + temp$ if ad.com.ports(3) <> 1 if temp$ = chr$(13) then foundLF = 1 exit while end if wend wend case 7 print #ArduinoCOMPort7, msg$ open "COM" ; port ; ":9600,n,8,1" for random as #ArduinoCOMPort3 'Wait until there is a carriage return while foundLF = 0 while lof(#ArduinoCOMPort7) > 0 temp$ = input$(#ArduinoCOMPort7, 1) buffer$ = buffer$ + temp$ ad.com.ports(3) if temp$ = chr$(13) then foundLF = 1 exit while end if wend wend case 8 print #ArduinoCOMPort8, msg$ end if 'Wait until there is a carriage return while foundLF = 0 while lof(#ArduinoCOMPort8) > 0 temp$ = input$(#ArduinoCOMPort8, 1) buffer$ = buffer$ + temp$ case 4 if temp$ = chr$(13) then foundLF = 1 exit while end if wend wend case 9 print #ArduinoCOMPort9, msg$ if ad.com.ports(4) <> 1 then 'Wait until there is a carriage return while foundLF = 0 while lof(#ArduinoCOMPort9) > 0 temp$ = input$(#ArduinoCOMPort9, 1) buffer$ = buffer$ + temp$ open "COM" ; port ; ":9600,n,8,1" for random as #ArduinoCOMPort4 if temp$ = chr$(13) then foundLF = 1 exit while end if wend wend case 10 print #ArduinoCOMPort10, msg$ ad.com.ports(4) 'Wait until there is a carriage return while foundLF = 10 while lof(#ArduinoCOMPort10) > 0 temp$ = input$(#ArduinoCOMPort10, 1) buffer$ = buffer$ + temp$ end if temp$ = chr$(13) then foundLF = 1 exit while end if wend wend case 5 if ad.com.ports(5) <> 1 then open "COM" ; port ; ":9600,n,8,1" for random as #ArduinoCOMPort5 end select ad.com.ports(5) 'Place the returned text in to AD.talk.to.device$ AD.talk.to.device$ = 1buffer$ else Notice "Improper Message Received, Must contain a command" end if End function end if case 6 if ad.com.ports(6) <> 1 then open "COM" ; port ; ":9600,n,8,1" for random as #ArduinoCOMPort6 ad.com.ports(6) = 1 end if case 7 if ad.com.ports(7) <> 1 then open "COM" ; port ; ":9600,n,8,1" for random as #ArduinoCOMPort7 ad.com.ports(7) = 1 end if case 8 if ad.com.ports(8) <> 1 then open "COM" ; port ; ":9600,n,8,1" for random as #ArduinoCOMPort8 ad.com.ports(8) = 1 end if case 9 if ad.com.ports(9) <> 1 then open "COM" ; port ; ":9600,n,8,1" for random as #ArduinoCOMPort9 ad.com.ports(9) = 1 end if case 10 if ad.com.ports(10) <> 1 then open "COM" ; port ; ":9600,n,8,1" for random as #ArduinoCOMPort10 ad.com.ports(10) = 1 end if end select 'Send message to device select case port case 1 print #ArduinoCOMPort1, msg$ 'Wait until there is a carriage return while foundLF = 0 while lof(#ArduinoCOMPort1) > 0 temp$ = input$(#ArduinoCOMPort1, 1) buffer$ = buffer$ + temp$ if temp$ = chr$(13) then foundLF = 1 exit while end if wend wend case 2 print #ArduinoCOMPort2, msg$ 'Wait until there is a carriage return while foundLF = 0 while lof(#ArduinoCOMPort2) > 0 temp$ = input$(#ArduinoCOMPort2, 1) buffer$ = buffer$ + temp$ if temp$ = chr$(13) then foundLF = 1 exit while end if wend wend case 3 print #ArduinoCOMPort3, msg$ 'Wait until there is a carriage return while foundLF = 0 while lof(#ArduinoCOMPort3) > 0 temp$ = input$(#ArduinoCOMPort3, 1) buffer$ = buffer$ + temp$ if temp$ = chr$(13) then foundLF = 1 exit while end if wend wend case 4 print #ArduinoCOMPort4, msg$ 'Wait until there is a carriage return while foundLF = 0 while lof(#ArduinoCOMPort4) > 0 temp$ = input$(#ArduinoCOMPort4, 1) buffer$ = buffer$ + temp$ if temp$ = chr$(13) then foundLF = 1 exit while end if wend wend case 5 print #ArduinoCOMPort5, msg$ 'Wait until there is a carriage return while foundLF = 0 while lof(#ArduinoCOMPort5) > 0 temp$ = input$(#ArduinoCOMPort5, 1) buffer$ = buffer$ + temp$ if temp$ = chr$(13) then foundLF = 1 exit while end if wend wend case 6 print #ArduinoCOMPort6, msg$ 'Wait until there is a carriage return while foundLF = 0 while lof(#ArduinoCOMPort6) > 0 temp$ = input$(#ArduinoCOMPort6, 1) buffer$ = buffer$ + temp$ if temp$ = chr$(13) then foundLF = 1 exit while end if wend wend case 7 print #ArduinoCOMPort7, msg$ 'Wait until there is a carriage return while foundLF = 0 while lof(#ArduinoCOMPort7) > 0 temp$ = input$(#ArduinoCOMPort7, 1) buffer$ = buffer$ + temp$ if temp$ = chr$(13) then foundLF = 1 exit while end if wend wend case 8 print #ArduinoCOMPort8, msg$ 'Wait until there is a carriage return while foundLF = 0 while lof(#ArduinoCOMPort8) > 0 temp$ = input$(#ArduinoCOMPort8, 1) buffer$ = buffer$ + temp$ if temp$ = chr$(13) then foundLF = 1 exit while end if wend wend case 9 print #ArduinoCOMPort9, msg$ 'Wait until there is a carriage return while foundLF = 0 while lof(#ArduinoCOMPort9) > 0 temp$ = input$(#ArduinoCOMPort9, 1) buffer$ = buffer$ + temp$ if temp$ = chr$(13) then foundLF = 1 exit while end if wend wend case 10 print #ArduinoCOMPort10, msg$ 'Wait until there is a carriage return while foundLF = 0 while lof(#ArduinoCOMPort10) > 0 temp$ = input$(#ArduinoCOMPort10, 1) buffer$ = buffer$ + temp$ if temp$ = chr$(13) then foundLF = 1 exit while end if wend wend end select 'Place the returned text in to AD.talk.to.device$ AD.talk.to.device$ = buffer$ else Notice "Improper Message Received, Must contain a command" end if End function function AD.close.all.the.com.ports() 'a function to call on program exit to close all the com ports gracefully if ad.com.ports(1) = 1 then close #ArduinoCOMPort1 end if if ad.com.ports(1) if ad.com.ports(2) = 1 then close #ArduinoCOMPort2 end if close #ArduinoCOMPort1 if ad.com.ports(3) = 1 then close #ArduinoCOMPort3 end if end if if ad.com.ports(2) if ad.com.ports(4) = 1 then close #ArduinoCOMPort4 end if close #ArduinoCOMPort2 if ad.com.ports(5) = 1 then close #ArduinoCOMPort5 end if end if if ad.com.ports(3) if ad.com.ports(6) = 1 then close #ArduinoCOMPort6 end if close #ArduinoCOMPort3 if ad.com.ports(7) = 1 then close #ArduinoCOMPort7 end if end if if ad.com.ports(4) if ad.com.ports(8) = 1 then close #ArduinoCOMPort8 end if close #ArduinoCOMPort4 if ad.com.ports(9) = 1 then close #ArduinoCOMPort9 end if end if if ad.com.ports(5) if ad.com.ports(10) = 1 then close #ArduinoCOMPort10 end if close #ArduinoCOMPort5end function end if if ad.com.ports(6) = 1 then close #ArduinoCOMPort6 end if if ad.com.ports(7) = 1 then close #ArduinoCOMPort7 end if if ad.com.ports(8) = 1 then close #ArduinoCOMPort8 end if if ad.com.ports(9) = 1 then close #ArduinoCOMPort9 end if if ad.com.ports(10) = 1 then close #ArduinoCOMPort10 end if end function [[code]] ---- =Just the Library and function descriptions= The bare naked functions are below with commented explanations for what they do. [[code]] 'Begin Arduino Code here ------------------------------------------------------- 'These functions can be added to any program to provide arduini power input and output 'Descriptions for functions are in there comments 'these are the funtions you call to get the arduino to do your bidding function AD.Set(com.port, PinNo, value) 'Set an Arduino pin high or low, 'Will return 1 one if successful, 0 if not AD.Set = val(AD.talk.to.device$(com.port,"set ";PinNo;" ";value)) ad.pinstatus(com.port,PinNo) = value end function function AD.Get(com.port, PinNo) 'Will return the current state of the input pin '1 for high, 0 for low AD.Get = val(AD.talk.to.device$(com.port,"get ";PinNo)) end function function AD.PWM.Out(com.port, PinNo, value) 'Set the PWM output on the select pin to the value 'Will return a 1 for success and 0 for failure. AD.PWM.Out = val(AD.talk.to.device$(com.port,"pwm.out ";PinNo;" ";value)) ad.pinstatus(com.port,PinNo) = value end function function AD.PWM.In(com.port, PinNo) 'Get the PWM input from the pin 'Will return a value of 0 to 1023 AD.PWM.In = val(AD.talk.to.device$(com.port,"pwm.in ";PinNo)) end function 'end of function to call to have the arduino do your bidding Function AD.talk.to.device$(port,msg$) 'Send a Message to the arduino and return the result sent back by the device 'this function is called by the other functions above if msg$ <> "" then 'Open the com port select case port case 1 if ad.com.ports(1) <> 1 then open "COM" ; port ; ":9600,n,8,1" for random as #ArduinoCOMPort1 ad.com.ports(1) = 1 end if case 2 if ad.com.ports(2) <> 1 then open "COM" ; port ; ":9600,n,8,1" for random as #ArduinoCOMPort2 ad.com.ports(2) = 1 end if case 3 if ad.com.ports(3) <> 1 then open "COM" ; port ; ":9600,n,8,1" for random as #ArduinoCOMPort3 ad.com.ports(3) = 1 end if case 4 if ad.com.ports(4) <> 1 then open "COM" ; port ; ":9600,n,8,1" for random as #ArduinoCOMPort4 ad.com.ports(4) = 1 end if case 5 if ad.com.ports(5) <> 1 then open "COM" ; port ; ":9600,n,8,1" for random as #ArduinoCOMPort5 ad.com.ports(5) = 1 end if case 6 if ad.com.ports(6) <> 1 then open "COM" ; port ; ":9600,n,8,1" for random as #ArduinoCOMPort6 ad.com.ports(6) = 1 end if case 7 if ad.com.ports(7) <> 1 then open "COM" ; port ; ":9600,n,8,1" for random as #ArduinoCOMPort7 ad.com.ports(7) = 1 end if case 8 if ad.com.ports(8) <> 1 then open "COM" ; port ; ":9600,n,8,1" for random as #ArduinoCOMPort8 ad.com.ports(8) = 1 end if case 9 if ad.com.ports(9) <> 1 then open "COM" ; port ; ":9600,n,8,1" for random as #ArduinoCOMPort9 ad.com.ports(9) = 1 end if case 10 if ad.com.ports(10) <> 1 then open "COM" ; port ; ":9600,n,8,1" for random as #ArduinoCOMPort10 ad.com.ports(10) = 1 end if end select 'Send message to device select case port case 1 print #ArduinoCOMPort1, msg$ 'Wait until there is a carriage return while foundLF = 0 while lof(#ArduinoCOMPort1) > 0 temp$ = input$(#ArduinoCOMPort1, 1) buffer$ = buffer$ + temp$ if temp$ = chr$(13) then foundLF = 1 exit while end if wend wend case 2 print #ArduinoCOMPort2, msg$ 'Wait until there is a carriage return while foundLF = 0 while lof(#ArduinoCOMPort2) > 0 temp$ = input$(#ArduinoCOMPort2, 1) buffer$ = buffer$ + temp$ if temp$ = chr$(13) then foundLF = 1 exit while end if wend wend case 3 print #ArduinoCOMPort3, msg$ 'Wait until there is a carriage return while foundLF = 0 while lof(#ArduinoCOMPort3) > 0 temp$ = input$(#ArduinoCOMPort3, 1) buffer$ = buffer$ + temp$ if temp$ = chr$(13) then foundLF = 1 exit while end if wend wend case 4 print #ArduinoCOMPort4, msg$ 'Wait until there is a carriage return while foundLF = 0 while lof(#ArduinoCOMPort4) > 0 temp$ = input$(#ArduinoCOMPort4, 1) buffer$ = buffer$ + temp$ if temp$ = chr$(13) then foundLF = 1 exit while end if wend wend case 5 print #ArduinoCOMPort5, msg$ 'Wait until there is a carriage return while foundLF = 0 while lof(#ArduinoCOMPort5) > 0 temp$ = input$(#ArduinoCOMPort5, 1) buffer$ = buffer$ + temp$ if temp$ = chr$(13) then foundLF = 1 exit while end if wend wend case 6 print #ArduinoCOMPort6, msg$ 'Wait until there is a carriage return while foundLF = 0 while lof(#ArduinoCOMPort6) > 0 temp$ = input$(#ArduinoCOMPort6, 1) buffer$ = buffer$ + temp$ if temp$ = chr$(13) then foundLF = 1 exit while end if wend wend case 7 print #ArduinoCOMPort7, msg$ 'Wait until there is a carriage return while foundLF = 0 while lof(#ArduinoCOMPort7) > 0 temp$ = input$(#ArduinoCOMPort7, 1) buffer$ = buffer$ + temp$ if temp$ = chr$(13) then foundLF = 1 exit while end if wend wend case 8 print #ArduinoCOMPort8, msg$ 'Wait until there is a carriage return while foundLF = 0 while lof(#ArduinoCOMPort8) > 0 temp$ = input$(#ArduinoCOMPort8, 1) buffer$ = buffer$ + temp$ if temp$ = chr$(13) then foundLF = 1 exit while end if wend wend case 9 print #ArduinoCOMPort9, msg$ 'Wait until there is a carriage return while foundLF = 0 while lof(#ArduinoCOMPort9) > 0 temp$ = input$(#ArduinoCOMPort9, 1) buffer$ = buffer$ + temp$ if temp$ = chr$(13) then foundLF = 1 exit while end if wend wend case 10 print #ArduinoCOMPort10, msg$ 'Wait until there is a carriage return while foundLF = 0 while lof(#ArduinoCOMPort10) > 0 temp$ = input$(#ArduinoCOMPort10, 1) buffer$ = buffer$ + temp$ if temp$ = chr$(13) then foundLF = 1 exit while end if wend wend end select 'Place the returned text in to AD.talk.to.device$ AD.talk.to.device$ = buffer$ else Notice "Improper Message Received, Must contain a command" end if End function function AD.start() 'initialises some variables to track information sent to the arduino and what com ports are being used dim ad.pinstatus(10,100) dim ad.com.ports(10) end function function AD.close.all.the.com.ports() 'a function to call on program exit to close all the com ports gracefully if ad.com.ports(1) = 1 then close #ArduinoCOMPort1 end if if ad.com.ports(2) = 1 then close #ArduinoCOMPort2 end if if ad.com.ports(3) = 1 then close #ArduinoCOMPort3 end if if ad.com.ports(4) = 1 then close #ArduinoCOMPort4 end if if ad.com.ports(5) = 1 then close #ArduinoCOMPort5 end if if ad.com.ports(6) = 1 then close #ArduinoCOMPort6 end if if ad.com.ports(7) = 1 then close #ArduinoCOMPort7 end if if ad.com.ports(8) = 1 then close #ArduinoCOMPort8 end if if ad.com.ports(9) = 1 then close #ArduinoCOMPort9 end if if ad.com.ports(10) = 1 then close #ArduinoCOMPort10 end if end function [[code]] ---- =Credits and thank yous= With out the folks listed below the foundation of this project would not exist and serial communications would still be a mystery to me. I appreciate the time an effort and frustration these people had to deal with getting this project going. [[http://libertybasic.conforums.com/index.cgi?action=viewprofile&username=thedarkfreak|**Chris Iverson**]] [[http://libertybasic.conforums.com/index.cgi?action=viewprofile&username=colinmac|**Colin McMurchie**]] [[http://libertybasic.conforums.com/index.cgi?action=viewprofile&username=rodbird|**Rod**]] [[http://libertybasic.conforums.com/index.cgi?action=viewprofile&username=metro|**metro**]] ---- [[toc]]