Skip to main content
guest
Join
|
Help
|
Sign In
Liberty BASIC Programmer's Encyc
Home
guest
|
Join
|
Help
|
Sign In
Wiki Home
Recent Changes
Pages and Files
Members
Home
General Tutorials
Advanced Tutorials
GUI Programming
Graphics and Games
Strings and Text
Numbers and Math
Using Files
Windows API
Communications
Programmer's Tools
Articles by Date
FAQs
Rosetta Code
General Articles
Newsletters Contents
Table of Contents
Demos
Submit Articles
TOS and License
Fun with the Arduino
Edit
27
…
1
Tags
communications
edit
Save
Cancel
Notify
RSS
Backlinks
Source
Print
Export (PDF)
<h1>What is the Arduino?</h1> <em>rodbird</em><br /> <img id="wikitext@@toc@@normal" class="WikiMedia WikiMediaToc" title="Table of Contents" src="/site/embedthumbnail/toc/normal?w=225&h=100"/><img src="/file/view/arduino.png/573558263/arduino.png" alt="" title="" /><br /> The <a class="wiki_link_ext" href="https://www.arduino.cc/" rel="nofollow">Arduino</a> is a much loved general purpose input and output board. It has a huge following and lots of support. This article is about how you can interface the board via the serial port and so give Liberty BASIC access to all that the board has to offer.<br /> <br /> <br /> <hr /> <h1>Start simply</h1> If this is your first step into microprocessor electronics my advice would be to start simply. Purchase a Uno Kit. There are several easily obtainable kits, each provides the board, electronic components and usually a booklet to get you up and running quickly.<br /> <br /> Some kits use soldered components, others offer plug in breadboard connection or ready made plug and socket connectors. Probably best to opt for plug in breadboard style which lets you build circuits quickly while you play and learn.<br /> <br /> This is all the kit you need to get started:<br /> <img src="/file/view/arduino3.png/573655513/arduino3.png" alt="" title="" /> <img src="/file/view/arduino.png/573558263/arduino.png" alt="" title="" /> <img src="/file/view/arduino2.png/573561809/arduino2.png" alt="" title="" /><br /> <br /> Purchase this kind of kit and play with it for a day or so, follow the tutorials available online or use the booklet. Get to the point where you have downloaded your own sketch to make the Arduino's onboard LED flash. Now you are ready to unleash Liberty BASIC.<br /> <br /> <h1>The Sketch</h1> You will now know that the Arduino uses a BASIC like programming language but there are significant differences to Liberty BASIC. These programs are called Sketches and you write them on your PC and download them to the Arduino which fires up and runs autonomously. You can go and learn this language, and you probably will over time, but right now we are going to use a general purpose Sketch written by the Liberty BASIC community. This Sketch allows simple serial messages to control and receive info from the Arduino. So the Arduino is running autonomously but it is dedicated to interpreting and actioning our control requests. It has become a slave to Liberty BASIC.<br /> <br /> Here is the Sketch, you will need to download it to your Arduino, follow the booklet instructions on how to do that.<br /> <br /> <br /> <pre class="text">#include <Stepper.h><br/>// initialize 100 step stepper on pins 8&9:<br/>// alter these parameters to suit your stepper<br/>Stepper myStepper(100, 8, 9);<br/><br/>#include <Servo.h><br/>// set up 14 servo objects in an array<br/>Servo myservos[14] ;<br/><br/>void setup() {<br/> Serial.begin(9600);<br/> Serial.println("Com Open");<br/>}<br/>void loop() {<br/><br/> // you can call your own functions here<br/> // lbRun() is only called by interrupt<br/> // when there is serial data available<br/><br/>}<br/><br/>// serialEvent checks to see if data is available on serial port<br/>// when a message comes we just invoke our lbRun() function<br/><br/><br/>void serialEvent() {<br/> // check if data available, if so call the lbRun() routine<br/> if ( Serial.available()) {<br/> lbRun();<br/> }<br/>}<br/><br/><br/>void lbRun()<br/>{<br/> while (Serial.available() > 10)<br/> {<br/> // look for the next valid integer in the incoming serial stream:<br/> int cmd = Serial.parseInt();<br/> // do it again:<br/> int pin = Serial.parseInt();<br/> // do it again:<br/> int val = Serial.parseInt();<br/> // look for the "*", That's the end of our command string<br/> if (Serial.read() == '*')<br/> {<br/> //now select the command to run and use the pin and val argument<br/> if ( cmd == 0)//add servo<br/> {<br/> myservos[pin].attach(pin);<br/> }<br/><br/> if ( cmd == 1)//getdigital<br/> {<br/> pinMode(pin, INPUT);//set to input<br/> digitalWrite(pin, HIGH);//turn on pullup resistor<br/> Serial.print(pin);<br/> Serial.print(",");<br/> Serial.print(digitalRead(pin));<br/> Serial.print("*");<br/> }<br/><br/> if ( cmd == 2)//getanalog<br/> {<br/> analogRead(pin);<br/> delay(10);<br/> Serial.print(pin);<br/> Serial.print(",");<br/> Serial.print(analogRead(pin));<br/> Serial.print("*");<br/> }<br/><br/> if ( cmd == 3)//getpulse<br/> {<br/> Serial.print(pin);<br/> Serial.print(",");<br/> Serial.print(pulseIn(pin, val));<br/> Serial.print("*");<br/> }<br/><br/> if ( cmd == 4)//setdigital<br/> {<br/> pinMode(pin, OUTPUT);//set to ouput<br/> digitalWrite(pin, val);<br/> }<br/><br/> if ( cmd == 5)//setanalog<br/> {<br/> analogWrite(pin, val);<br/> }<br/><br/> if ( cmd == 6)//setservo<br/> {<br/> myservos[pin].write(val) ;<br/> }<br/><br/> if ( cmd == 7)//settone<br/> {<br/> if (val == 0)<br/> {<br/> noTone(pin);<br/> }<br/> else<br/> {<br/> tone(pin, val);<br/> }<br/> }<br/><br/> if ( cmd == 8)//ping<br/> {<br/><br/> // establish variables for duration of the ping,<br/> long duration;<br/><br/> // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.<br/> // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:<br/> pinMode(pin, OUTPUT);<br/> digitalWrite(pin, LOW);<br/> delayMicroseconds(2);<br/> digitalWrite(pin, HIGH);<br/> delayMicroseconds(5);<br/> digitalWrite(pin, LOW);<br/><br/> // The same pin is used to read the signal from the PING))): a HIGH<br/> // pulse whose duration is the time (in microseconds) from the sending<br/> // of the ping to the reception of its echo off of an object.<br/> pinMode(pin, INPUT);<br/> duration = pulseIn(pin, HIGH);<br/> Serial.print(pin);<br/> Serial.print(",");<br/> Serial.print(duration);<br/> Serial.print("*");<br/> }<br/><br/> if ( cmd == 9)//detach servo<br/> {<br/> myservos[pin].detach();<br/> }<br/> <br/> if ( cmd == 10)//set the stepper speed<br/> {<br/> myStepper.setSpeed(val);<br/> }<br/> <br/> if (cmd == 11)//set the stepper in motion +<br/> // this is a blocking call, the stepper will run at <br/> // the setSpeed value till steps are complete<br/> {<br/> myStepper.step(val);<br/> Serial.print("99,0,*");<br/><br/> }<br/> if (cmd == 12)//set the stepper in motion -<br/> {<br/> myStepper.step(-val);<br/> Serial.print("99,0,*");<br/> }<br/> }<br/> }<br/>} // end or lbRun()<br/><br/>// add your own functions here<br/><br/></pre> <br /> Very briefly this Sketch loads the servo control and stepper library then starts polling the serial buffer, looking for complete messages that we have sent from Liberty BASIC. When it finds a complete message it interprets or parses that message to establish what we are asking it to do. Then on a simple select case basis it actions that request returning results if needed. We will pick out and explain what each part of the Sketch achieves as we discuss the commands that can be sent.<br /> <br /> <h1>Our Liberty BASIC GUI</h1> This is the GUI we will build to allow us to converse with the Arduino. This is a very simple GUI that connects with the Arduino over the USB port and allows us to format and send requests and receive responses. It will allow you to play with circuits quickly and easily.<br /> <br /> Now we could write really simple code in the mainwin. It is perfectly possible to open the com port and send and receive messages from there. However there is likely to be a lot of user interaction so a GUI interface is abetter solution.<br /> <br /> <img src="/file/view/ArduinoI.png/573895881/ArduinoI.png" alt="" title="" /><br /> <br /> <h1>The message format</h1> A Liberty BASIC message is sent in a comma delimited string with fixed length numeric values which have leading zeros:<br /> <pre class="text">"CC,PP,VVVV*"</pre> Where:<br /> CC = a two digit number specifying which command to action<br /> PP = a two digit number specifying which pin to act on<br /> VVVV = optionally, a four digit number passing any value to apply<br /> followed by a terminating "*"<br /> <br /> Responses, if issued, are received in a variable length message, comma delimited, in the following format:<br /> <pre class="text">"PP,VVVVn*"</pre> Where:<br /> PP = up to two digits specifying the pin producing the result<br /> VVVVn = the numeric result<br /> followed by a terminating "*"<br /> <br /> <h1>Current command set</h1> AddServo, You can attach servos to a pin then send rotation angle messages<br /> GetDigital, You can read the digital state of a pin 0v=Low 5v=High<br /> GetAnalog, You can read the analog state of a pin returning 0-1023 depending on the voltage at the pin<br /> GetPWM, You can read the high or low timing in microseconds of pulsed pins<br /> SetDigital, You can set the pin state to High or Low<br /> SetPWM, You can set the duty cycle of a pulse train at a pin between 0% and 100%<br /> SetServo, You can set the servo angle between 0o and 180o<br /> SetTone, A pin can output a continuous tone at a specified frequency<br /> Ping, You can measure the distance to and object via an ultrasound ping<br /> DelServo, Servos need detached after use<br /> SetSpeed, You can set stepper motor rotation speed in RPM<br /> SetStepR, You can move the stepper motor right n steps<br /> SetStepL, You can move the stepper motor left n steps<br /> <br /> <h1>Making the connection</h1> The first thing we need to do is open the serial link and connect to the Arduino. If you have downloaded the Sketch all you do is plug in the Arduino, Windows will open a Com port and the Arduino will start checking for messages. All we need do in Liberty BASIC is find the port number and start conversing.<br /> <br /> Here is some code that will find the Com port and allow it to be opened. It uses a combobox, the combobox is filled with available ports and you then pick which one you want to talk to. So you may have more than one Arduino connected but most often we will be dealing with one port and one board and the code should automatically select that port.<br /> <br /> <pre class="lb"> 'Subs to handle the com port ==============================================<br/> sub portClick h$<br/> 'take com port combobox input, open choosen com port<br/> #main.cbport "selection? p$"<br/> if Port then close #port<br/> if p$<>"" then<br/> 'open p$;":9600,n,8,1,ds0,cs0,rs" for random as #port<br/> Port=1<br/> call delay 500<br/> end if<br/> end sub<br/><br/> sub getPorts<br/> 'test first 32 ports and load combobox list for valid serial ports<br/> index=1<br/> for p = 1 to 32<br/> oncomerror [trap]<br/> open "Com";str$(p);":9600,n,8,1,ds0,cs0,rs" for random as #com<br/> port$(index)="Com";str$(p)<br/> index=index+1<br/> close #com<br/><br/> [trap]<br/> oncomerror<br/> next<br/> #main.cbport, "reload"<br/> 'now if there is only one port open it<br/> if port$(1)<>"" and port$(2)="" then<br/> open port$(1);":9600,n,8,1,ds0,cs0,rs" for random as #port<br/> Port=1<br/> #main.cbport, "selectindex 1"<br/> call delay 500<br/> else<br/> #main.cbport, "selectindex 0"<br/> end if<br/> end sub</pre> <br /> <h1>Starting the conversation</h1> I say conversation because you will be sending and receiving messages. You might change my example code and do this on a timed basis, regularly graphing and displaying the results. The system is quite fast. Obviously the serial link and message passing is a bottleneck compared with how fast the Arduino would run unfettered but you will be able to get many readings per second.<br /> <br /> Lets look at some code that will manage the conversation. We need a send routine and a receive routine The send routine is pretty straight forwards because we initiate a single message though you might change the code to do this repetitively. The receiving is a little more complex because we may be receiving a stream of messages and we need to parse them out.<br /> <br /> <h1>Send Request</h1> <pre class="lb"> 'send the request if the button is clicked<br/> sub send h$<br/> #main.tbarg "!contents? v$"<br/> Val=val(v$)<br/> #main.tbresp ""<br/> msg$=str$(Cmd)+","<br/> msg$=msg$+right$("00"+str$(Pin),2)+","<br/> msg$=msg$+right$("0000"+str$(Val),4)+"*"<br/> #main.tbrequ msg$<br/> #port msg$<br/> end sub<br/></pre> <br /> <h1>Get Response</h1> <pre class="lb"> sub getresponse<br/> if Port then<br/> if lof(#port)>0 then<br/> Buffer$=Buffer$+input$(#port, lof(#port))<br/> endofdata=instr(Buffer$,"*",1)<br/> [loop]<br/> if endofdata>0 then<br/> 'we have a valid end<br/> dat$=left$(Buffer$,endofdata-1)<br/> Buffer$=right$(Buffer$,len(Buffer$)-endofdata)<br/> pin=val(word$(dat$,1,","))<br/> dat=val(word$(dat$,2,","))<br/> #main.tbresp pin;",";dat<br/> endofdata=instr(Buffer$,"*",1)<br/> if endofdata>0 then [loop]<br/> end if<br/> end if<br/> end if<br/> end sub<br/></pre> <br /> <br /> <br /> <h1>The complete GUI</h1> <br /> <pre class="lb"> nomainwin<br/> 'We need an array to store the Com port names in<br/> dim port$(256)<br/> 'since we plan to use Subs we need a few globals<br/> 'I tend to identify global variables with a capital letter<br/> global Port,Cmd,Pin,Val,Buffer$<br/> Port=0<br/> dim cmd$(14)<br/> cmd$(1)="AddServo" 'servos need attached to a pin before sending turn commands<br/> cmd$(2)="GetDigital" 'this gets the state of any digital pin, just ground it for low<br/> cmd$(3)="GetAnalog" 'this gets the analog value of the voltage on any analog pin 0v-5v<br/> cmd$(4)="GetPWM" 'this gets the pulse width in microseconds on any digital pin<br/> cmd$(5)="SetDigital" 'this sets any digital pin High or Low<br/> cmd$(6)="SetPWM" 'this sets the pulse width modulation 0%-100% (analog out)<br/> cmd$(7)="SetServo" 'sets preattached servo in degerees 0o-180o<br/> cmd$(8)="SetTone" 'sets a tone on any digital pin send 0 to silence<br/> cmd$(9)="Ping" 'pings an ultrasonic transducer to measure distance in air<br/> cmd$(10)="DelServo" 'detaches a servo from its named pin<br/> cmd$(11)="SetSpeed" 'sets the speed in RPM that a stepper will move<br/> cmd$(12)="SetStepR" 'sets the stepper moving right n steps will respond 0,99 when done<br/> cmd$(13)="SetStepL" 'sets the stepper moving left n steps will respond 0,99 when done<br/><br/> dim pin$(20)<br/> for n= 0 to 13<br/> pin$(n+1)=str$(n)<br/> next<br/><br/> WindowWidth = 550<br/> WindowHeight = 195<br/> UpperLeftX=int((DisplayWidth-WindowWidth)/2)<br/> UpperLeftY=int((DisplayHeight-WindowHeight)/2)<br/> combobox #main.cbport, port$(, portClick, 5, 80, 50, 100<br/> statictext #main.stport, "Com Port", 5, 60, 60, 20<br/> combobox #main.cbcmd, cmd$(, cmdClick, 60, 80, 90, 100<br/> statictext #main.stcmd, "Command", 60, 60, 60, 20<br/> combobox #main.cbpin, pin$(, pinClick, 155, 80, 40, 100<br/> statictext #main.stpin, "Pin", 155, 60, 20, 20<br/> textbox #main.tbarg, 200, 80, 80, 20<br/> statictext #main.starg, "Argument", 200, 60, 90, 20<br/> button #main.send,"Send",send, UL, 290, 78, 75, 25<br/> textbox #main.tbrequ, 375, 80, 150, 25<br/> statictext #main.strequ, "Request", 375, 60, 60, 20<br/> textbox #main.tbresp, 375, 125, 150, 25<br/> statictext #main.stresp, "Response", 375, 105, 60, 20<br/> open "Arduino simple interface" for window as #main<br/> #main "trapclose quit"<br/><br/> 'find out what com ports are available and load the combobox<br/> call getPorts<br/><br/> 'we need an endless loop to clear out the serial buffer Arduino only stores 64 bytes<br/> while 1<br/> scan<br/> call getresponse<br/> wend<br/><br/><br/><br/><br/> 'Subs to handle comboboxes=================================================<br/><br/> 'what command did the user click,if it needs no argument set val to 0<br/> sub cmdClick h$<br/> #main.cbcmd "selectionindex? i"<br/> 'since our combobox array is numbered 1-13 deduct 1 to get 0-12<br/> Cmd=i-1<br/> if (Cmd>=0 and Cmd<=3) or (Cmd>=8 and Cmd<=9) then #main.tbarg "0"<br/> if (Cmd>=10 and Cmd<=12) then #main.cbpin "selectindex 1" : Pin=0<br/> end sub<br/><br/> 'what pin was selected<br/> sub pinClick h$<br/> #main.cbpin "selectionindex? i"<br/> Pin=i-1<br/> end sub<br/><br/><br/> 'send the request if the button is clicked<br/> 'we need to make it a fixed length string<br/> 'because thats how the Arduino knows it<br/> 'has a complete message<br/> sub send h$<br/> #main.tbarg "!contents? v$"<br/> Val=val(v$)<br/> #main.tbresp ""<br/> msg$=right$("00"+str$(Cmd),2)+","<br/> msg$=msg$+right$("00"+str$(Pin),2)+","<br/> msg$=msg$+right$("0000"+str$(Val),4)+"*"<br/> #main.tbrequ msg$<br/> #port msg$<br/> end sub<br/><br/> 'suck the input buffer dry, keep the remnants of the message if the whole<br/> 'message has not been received, thats why Buffer$ is global<br/> sub getresponse<br/> if Port then<br/> if lof(#port)>0 then<br/> Buffer$=Buffer$+input$(#port, lof(#port))<br/> endofdata=instr(Buffer$,"*",1)<br/> [loop]<br/> if endofdata>0 then<br/> 'we have a valid end<br/> dat$=left$(Buffer$,endofdata-1)<br/> Buffer$=right$(Buffer$,len(Buffer$)-endofdata)<br/> pin=val(word$(dat$,1,","))<br/> dat=val(word$(dat$,2,","))<br/> #main.tbresp pin;",";dat<br/> endofdata=instr(Buffer$,"*",1)<br/> if endofdata>0 then [loop]<br/> end if<br/> end if<br/> end if<br/> end sub<br/><br/> 'Subs to handle the com port ==============================================<br/> sub portClick h$<br/> 'take com port combobox input, open choosen com port<br/> #main.cbport "selection? p$"<br/> if Port then close #port<br/> if p$<>"" then<br/> 'open p$;":9600,n,8,1,ds0,cs0,rs" for random as #port<br/> Port=1<br/> call delay 500<br/> end if<br/> end sub<br/><br/> sub getPorts<br/> 'test first 32 ports and load combobox list for valid serial ports<br/> index=1<br/> for p = 1 to 32<br/> oncomerror [trap]<br/> open "Com";str$(p);":9600,n,8,1,ds0,cs0,rs" for random as #com<br/> port$(index)="Com";str$(p)<br/> index=index+1<br/> close #com<br/><br/> [trap]<br/> oncomerror<br/> next<br/> #main.cbport, "reload"<br/> 'now if there is only one port open it<br/> if port$(1)<>"" and port$(2)="" then<br/> open port$(1);":9600,n,8,1,ds0,cs0,rs" for random as #port<br/> Port=1<br/> #main.cbport, "selectindex 1"<br/> call delay 500<br/> else<br/> #main.cbport, "selectindex 0"<br/> end if<br/> end sub<br/><br/> sub delay m<br/> CallDLL #kernel32, "Sleep", m As ulong, Sleep As void<br/> end sub<br/><br/> sub quit h$<br/> close #main<br/> if Port then close #port<br/> end<br/> end sub<br/><br/></pre> <h1>The command list</h1> <hr /> <br /> <h2>AddServo</h2> Command = 0 , Pin Range = 0-13, Argument = Null, Response = Null<br /> Servos must first be assigned a pin, use of the library disables PWM on pins 9 and 10<br /> <pre class="text">if ( cmd == 0)//add servo<br/>{<br/>myservos[pin].attach(pin);<br/>}</pre> <br /> <h2>GetDigital</h2> Command = 1, Pin Range = 0-13, Argument = Null, Response = pin,0 or 1<br /> <br /> The pin is set to input and the pullup resistor set high,ground the pin via a switch/sensor to pull it low.<br /> If the pin is low 0 will be returned if high 1 will be returned.<br /> <pre class="text">if ( cmd == 1)//getdigital<br/>{<br/>pinMode(pin, INPUT);//set to input<br/>digitalWrite(pin, HIGH);//turn on pullup resistor<br/>Serial.print(pin);<br/>Serial.print(",");<br/>Serial.print(digitalRead(pin));<br/>Serial.print("*");<br/>}</pre> <br /> <h2>GetAnalog</h2> Command = 2, Pin Range 0-5 (analog), Argument = Null, Response = pin,0-1023<br /> <br /> The analog pins 0-5 return 0-1023 measuring 0-5v on the pin, connect<br /> pots, LDRs, Thermistors or any resistance based sensor. The pin is read twice, the delay settles the reading<br /> and improves accuracy.<br /> <pre class="text">if ( cmd == 2)//getanalog<br/>{<br/>analogRead(pin);<br/>delay(10);<br/>Serial.print(pin);<br/>Serial.print(",");<br/>Serial.print(analogRead(pin));<br/>Serial.print("*");<br/>}</pre> <br /> <h2>GetPWM</h2> Command = 3, Pin Range 0-13, Argument = 1 or 0, Response = pin,microseconds<br /> <br /> Sending 0 as the Argument will measure the low pulse 1 will measure the high pulse,<br /> connect gyros and accelerometers that provide PWM output.<br /> <pre class="text">if ( cmd == 3)//getpulse<br/>{<br/>Serial.print(pin);<br/>Serial.print(",");<br/>Serial.print(pulseIn(pin, val));<br/>Serial.print("*");<br/>}</pre> <br /> <h2>SetDigital</h2> Command = 4, Pin Range = 0-13, Argument = 0 or 1, Response = Null<br /> <br /> This will set the pin HIGH or LOW use with LEDs or driver electronics to switch relays or pulse motors.<br /> <pre class="text">if ( cmd == 4)//setdigital//<br/>{<br/>pinMode(pin, OUTPUT);//set to ouput<br/>digitalWrite(pin, val);<br/>}</pre> <br /> <h2>SetPWM</h2> Command = 5, Pin Range = 3,5,6,9,10,11 , Argument = 0-255, Response = Null<br /> <br /> This sets the PWM ratio on any of the legal pins from 0% HIGH to 100% HIGH<br /> connect LEDs to fade or brighten or with driver electronics control motor speed.<br /> <pre class="text">if ( cmd == 5)//setPWM<br/>{<br/>analogWrite(pin, val);<br/>}</pre> <br /> <h2>SetServo</h2> Command = 6 , Pin Range = 0-13, Argument = 0o to 180o, Response = Null<br /> <br /> This sets the servo angle in degrees. But many electronic motor control (ESCs) and gyro<br /> gadgets use this form of PWM output. Forward and reverse motor speed control and gyro<br /> stabilised servo control are all possible.<br /> <pre class="text">if ( cmd == 6)//setservo<br/>{<br/>myservos[pin].write(val) ;<br/>}</pre> <br /> <h2>SetTone</h2> Command = 7 , Pin Range = 0-13, Argument = Hz 31 - 4978, Response = Null<br /> <br /> Only one pin can output a tone at any time, sending 0 silences the tone.<br /> A tone between 31Hz and 4978Hz can be specified it plays till silenced.<br /> <pre class="text">if ( cmd == 7)//settone<br/>{<br/>if (val == 0)<br/>{<br/>noTone(pin);<br/>}<br/>else<br/>{<br/>tone(pin, val);<br/>}<br/>}</pre> <br /> <h2>Ping</h2> Command = 8, Pin Range = 0-13, Argument = Null, Response = pin,microseconds<br /> <br /> This pings an ultrasonic transducer, divide the result by 29 then 2 to get cm distance.<br /> 29 is the number of cm sound will travel in a microsecond, we divide by 2 because<br /> the sound is bounced out and back.<br /> <pre class="text">if ( cmd == 8)//ping<br/>{<br/><br/>// establish variables for duration of the ping,<br/>long duration;<br/><br/>// The PING))) is triggered by a HIGH pulse of 2 or more microseconds.<br/>// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:<br/>pinMode(pin, OUTPUT);<br/>digitalWrite(pin, LOW);<br/>delayMicroseconds(2);<br/>digitalWrite(pin, HIGH);<br/>delayMicroseconds(5);<br/>digitalWrite(pin, LOW);<br/><br/>// The same pin is used to read the signal from the PING))): a HIGH<br/>// pulse whose duration is the time (in microseconds) from the sending<br/>// of the ping to the reception of its echo off of an object.<br/>pinMode(pin, INPUT);<br/>duration = pulseIn(pin, HIGH);<br/>Serial.print(pin);<br/>Serial.print(",");<br/>Serial.print(duration);<br/>Serial.print("*");<br/>}<br/></pre> <br /> <h2>DelServo</h2> Command = 9 , Pin Range = 0-13, Argument = Null, Response = Null<br /> <br /> Servos must be detached for the pin to be reused, when all are detached pin 9 and 10<br /> are reenabled for PWM<br /> <pre class="text">if ( cmd == 9)//detach servo<br/>{<br/>myservos[pin].detach();<br/>}</pre> <br /> <h2>SetSpeed</h2> Command = 10 , Pin Range = Null, Argument = RPM, Response = Null<br /> <br /> This uses the stepper library and sets the rotational speed of the stepper motor. The Sketch contains starting<br /> parameters for the stepper 100,8,9 that is 100 steps is one complete revolution. This has to be the correct<br /> step value for the RPM value to work correctly<br /> <pre class="text"> if ( cmd == 10)//set the stepper speed<br/> {<br/> myStepper.setSpeed(val);<br/> }</pre> <br /> <h2>SetStepR</h2> Command = 11 , Pin Range = Null, Argument = N Steps, Response = 0,99 when complete<br /> <br /> This sets the stepper motor in motion and it will complete N steps to the right and then respond. The stepper control is blocking, the Sketch will handle nothing else till it is complete. So use high seed and short movements to keep things flexible.<br /> <pre class="text"> if (cmd == 11)//set the stepper in motion +<br/> // this is a blocking call, the stepper will run at <br/> // the setSpeed value till steps are complete<br/> {<br/> myStepper.step(val);<br/> Serial.print("99,0,*");<br/><br/> }</pre> <br /> <h2>SetStepL</h2> Command = 12 , Pin Range = Null, Argument = N Steps, Response = 0,99 when complete<br /> <br /> As for SetStepR only the motor moves in the opposite direction.<br /> <hr /> <img id="wikitext@@toc@@flat" class="WikiMedia WikiMediaTocFlat" title="Table of Contents" src="/site/embedthumbnail/toc/flat?w=100&h=16"/>
Javascript Required
You need to enable Javascript in your browser to edit pages.
help on how to format text
Turn off "Getting Started"
Home
...
Loading...