Category Archives: PicBasic Pro

Serial Call-and-Response (Microcontroller side)

If the incoming value is a valid byte (i.e. 0 – 255), the program then reads two analog inputs and one digital input. It divides the anaog inputs by 4 to convert the ranges to 0 = 255, and sends all three sensor values out serially. Continue reading

Posted in arduino/wiring, BX-24, PicBasic Pro | Comments Off on Serial Call-and-Response (Microcontroller side)

Stepper motor controller from analog input

This program controls a stepper motor from an analog input. Continue reading

Posted in PicBasic Pro | Comments Off on Stepper motor controller from analog input

Xport to SQL

This program connects a microcontroller to a mySQL database via a Lantronix Xport and a PHP script.The PHP script it uses is the SQL_datalog.php script in the Network Data Logging Suite.
Continue reading

Posted in Lantronix, PicBasic Pro | Comments Off on Xport to SQL

Network Data Logging Suite

This suite of programs takes data from a sensor and saves it to a text file on a network. Each sensor reading is time stamped. The suite illustrates the basic principles involved in sending sensor data to a networked file or database.
Continue reading

Posted in arduino/wiring, PHP, PicBasic Pro, Processing | Comments Off on Network Data Logging Suite

Linearizing Code for Sharp GP2D120 Infrared Ranger

Arduino/ATMega8 hardware connections: A0/PC5: potentiometer on analog in 1 D0/PD0/RX0: Serial input from the PC via MAX232 or hex inverter D1/PD1/TX0: Serial output to PC via MAX232 or hex inverter Distance ranging formula comes from Acroname, http://www.acroname.com: http://www.acroname.com/robotics/info/articles/irlinear/irlinear.html Created 6 Oct. 2005 Updated 25 Oct. 2005 */int val; // outgoing ADC valueint distance = 0;//function prototype:void blink(int howManyTimes);void setup(){ // start serial port at 9600 bps: beginSerial(9600); blink(3);}void loop(){ // read analog input: val = analogRead(0); // send analog value out: printString(“Analog Value =\t”); // Calculate linear slope of reading (thanks, Acroname!): distance = (2914 / (val + 5)) – 1; printInteger(distance); printString(“\n\r”); // wait 10ms for ADC to reset before next reading: delay(10); }// Blink the reset LED:void blink(int howManyTimes) { int i; for (i=0; i< howManyTimes; i++) { digitalWrite(13, HIGH); delay(200); digitalWrite(13, LOW); delay(200); }}Written for the BX-24: ‘GP2D120 sensor example ‘by Tom Igoe ‘ Reads a changing voltage from a GP2D120 IR ranging sensor ‘ on analog input 0 and sends the result out in ASCII-encoded ‘ decimal numbers. ‘ Analog in is on pin 15 of the BX-24 ‘ Distance ranging formula comes from Acroname, http://www.acroname.com:’ http://www.acroname.com/robotics/info/articles/irlinear/irlinear.html ‘ created 6 Oct 2005’ Updated 25 Oct. 2005 dim val as integer ‘ outgoing ADC valuedim distance as integersub main () call blink(3)do’ read analog input: val = getADC(15) ‘ Calculate linear slope of reading (thanks, Acroname!): distance = (2914 / (val + 5)) – 1 ‘ send analog value out: debug.print “Distance = “; cStr(distance) ‘ wait 10ms for ADC to reset before next reading: call delay(0.01) loopend sub’ Blink the reset LED:sub blink(byVal howManyTimes as integer) dim i as integer for i=0 to howManyTimes call putPin(26, 1) call delay(0.2) call putPin(26,0) call delay(0.2) nextend subWritten in PicBasic Pro, tested on a PIC18F252:’ GP2D120 sensor example’ by Tom Igoe ‘ Reads a changing voltage from a GP2D120 IR ranging sensor’ on analog input 0 and sends the result out in ASCII-encoded’ decimal numbers. Continue reading

Posted in arduino/wiring, BX-24, PicBasic Pro | Comments Off on Linearizing Code for Sharp GP2D120 Infrared Ranger

Averaging and Finding the Median

Created 17 October 2005 Updated 7 August 2007 */int numReadings = 9; // number of samples to takeint median = 0; // median of the sorted samplesint readingNumber; // counter for the sample array// variables for subroutines: byte i = 0;byte j = 0;byte position = 0;int analogValues[9]; //function prototypes:void bubbleSort(); int averageArray();void setup() { Serial.begin(9600);}void loop() { for (readingNumber = 0; readingNumber < numReadings; readingNumber++) { //get the reading: analogValues[readingNumber] = analogRead(0); // increment the counter: readingNumber++; } // sort the array using a bubble sort: bubbleSort(); // get the middle element: median = analogValues[numReadings / 2]; // print the results: // print the array, nicely ASCII-formatted: Serial.print(“Array: [“); for (j = 0; j < numReadings; j++) { Serial.print(analogValues[j], DEC); Serial.print (“, “); } Serial.print(“]\r\n”); // average the array: Serial.print(” Average = “); Serial.print(averageArray(), DEC); Serial.print(“\tMedian = “); Serial.print(median, DEC); Serial.print(“\r\n”);}// average the values in the array: int averageArray() { int total = 0; int average = 0; for (i = 0; i< numReadings; i++) { total = total + analogValues[i]; } average = total/(numReadings + 1); return average;}void bubbleSort() { int out, in, swapper; for(out=0 ; out < numReadings; out++) { // outer loop for(in=out; in<numReadings; in++) { // inner loop if( analogValues[in] > analogValues[in+1] ) { // out of order?

…’ Set sampling time in uSTRISA = %11111111 ‘ Set PORTA to all inputADCON1 = %10000010 ‘ Set PORTA analog and right justify resultnumReadings con 9 ‘ number of samples to takemedian var byte ‘ median of the sorted samplesreadingNumber var byte ‘ counter for the sample array’ serial variables and constants:tx var portc.6rx var portc.7inv9600 con 16468′ variables for subroutines: i var byte j var byte position var byte analogValues var word[numReadings] total var word average var word out var byte in var byte swapper var wordmain: for readingNumber = 0 to (numReadings – 1) ‘ get the reading: adcin 0,analogValues[readingNumber] ‘ increment the counter: readingNumber = readingNumber + 1 next ‘ sort the array using a bubble sort: gosub bubbleSort ‘ get the middle element: median = analogValues[numReadings / 2] ‘ print the results: ‘ print the array, nicely ASCII-formatted: serout2 tx, inv9600,[“Array: [“] for j = 0 to (numReadings – 1) serout2 tx, inv9600, [DEC analogValues[j], “, “] next serout2 tx, inv9600,[“]”, 10, 13] ‘ average the array: gosub averageArray serout2 tx, inv9600, [“Average: “, DEC average, 10, 13] serout2 tx, inv9600, [“Median: “, DEC median, 10, 13]GOTO MAIN’ average the values in the array: averageArray: total = 0 average = 0 for i = 0 to (numReadings – 1) total = total + analogValues[i] next average = total/numReadingsRETURNbubbleSort: for out = 0 to (numReadings – 2) ‘ outer loop for in = out+1 to (numReadings – 1) ‘ inner loop if analogValues[out] > analogValues[in] then ‘out of order? Continue reading

Posted in arduino/wiring, PicBasic Pro | Comments Off on Averaging and Finding the Median

Weighted averaging of an analog input (Smoothing)

/* Analog smoothing using a weighted average filter by Tom Igoe Based on notes by David Schultz, at http://home.earthlink.net/~david.schultz/rnd/2002/KalmanApogee.pdf This program reads an analog input and smooths out the result using a weighted average filter…. When the trimmer pot value is low, the average is weighted in favor of the previous readings, and the current reading affects the average very little. Continue reading

Posted in arduino/wiring, PicBasic Pro | Comments Off on Weighted averaging of an analog input (Smoothing)

Peak Finding

It assumes the sensor is moving in a simple curve.The program checks to see that the current value is above a given threshold, then checks to see if the value is greater than the previous value…. When the current value goes below the threshold again, it outputs the last peak value recorded.Wiring/Arduino:/*Peak Finder This example finds the peak value of an analog sensor over time. Continue reading

Posted in arduino/wiring, PicBasic Pro | Comments Off on Peak Finding

Qprox Capacitive Touch Sensors

These sensors work best as touch sensors, with the conductor embedded just below the surface of the object — for example, with the sensor underneath a cloth covering, cardboard covering, etc. The closer you can put the conductor to the chip that reads it, the better…. For this example, I used a QT113, but you can use the QT113H with the same circuit.Note: Digikey carries these sensors in the US, so there is no need to order directly from England.The wiring for the QT sensors is as follows: The connecting wire between the sensor and the pin is sensitive to touch as well. Continue reading

Posted in arduino/wiring, BX-24, PicBasic Pro | Comments Off on Qprox Capacitive Touch Sensors

Edge Detection

Most of the time, you don’t need to know whether a switch is on or off so much as you need to know when it changed…. The basic idea is that you check not only what the state of the switch is, but what the state was the last time you checked, and compare the two. Continue reading

Posted in arduino/wiring, PicBasic Pro | Comments Off on Edge Detection