Category Archives: PicBasic Pro

Debouncing a digital input

For the fraction of s second before the contact is complete, the circuit may make contact and be broken several times. When you read this input on a microcontroller, you’ll see a rapid switching from 0 to 1 over a few milliseconds until the contact is final. Continue reading

Posted in arduino/wiring, PicBasic Pro | Comments Off on Debouncing a digital input

DC Motor Control with a TIP120 Transistor

Here’s the schematic for the capacitors and the regulator:The simplest program for this would be as follows (on the BX-24): Sub main() call delay(0.5) ‘ start program with a half-second delay do call putPin(13,1) call delay(1.0) call putPin(13,0) call delay(0.5) loopend sub In PicBasic Pro, the code would be as follows: pause 500 ‘ start program with a half-second delay main: high PORTD.2 pause 1000 low PORTD.2 pause 1000goto mainFor pBasic and mBasic, use the PicBasic Pro example and change the pin names.If your power supply for the microcontroller is compatible with your motor, you can wire the motor supply in parallel with the 5V regulator. For example, I use a 12V DC 1000 mA power adaptor, so I can use a 12V motor, if the power from the motor is wired in parallel with the 5V regulator’s input, like so:Note that the motor and the BX24 need a common ground (in our case, they get it through the transistor’s base; see above schematic).A motor controlled like this can only be turned in one direction. Continue reading

Posted in BX-24, pBasic (Basic stamp), PicBasic Pro | Comments Off on DC Motor Control with a TIP120 Transistor

Simple PWM in PicBasic Pro

This is an example of pulsewidth modulation for the PIC. The value received from an analog input is used to dim an LED, using the PWM command. The LED is on RD2, and the analog in is on RA0. The … Continue reading

Posted in code, PicBasic Pro | 2 Comments

Lantronix Device Connection with serial feedback

The Lantronix Device has to have its connect mode set to verbose, so that it will send ASCII N when not connected, ASCII D when disconnecting, and ASCII C when a connection is made.’ connection to the Lantronix device through a hex inverter:TX var portd.0RX var portd.1′ serial to PC for debugging:debugTX var portc.6′ some indicator LED’s for later use:yellowLED var portb.2greenLED var portb.7redLED var portb.3′ two switches:switch1 var portb.0switch2 var portb.1′ data coming in the serial port:inbyte var byte’ set the switch pins to input mode:input switch1input switch2′ clear all variables:clearmain: ‘ the Lantronix device is set to connect mode D5, meaning that ‘ it will return a byte to the PIC to indicate ‘ the connection status.
Continue reading

Posted in Lantronix, PicBasic Pro | Comments Off on Lantronix Device Connection with serial feedback

Analog Smoothing Algorithm

// divide by 4 to print the result as a byte: Serial.print(smoothed/4, BYTE); // delay 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); }}// Smooth out an analog reading:void smoothValue(int rawValue) { if (rawValue > smoothed) { smoothed = smoothed + (rawValue – smoothed)/alpha; } else { smoothed = smoothed – (smoothed – rawValue)/alpha; }}Written in PicBasic Pro, tested on a PIC 18F252:’ Analog smoothing algorithm’ by Tom Igoe ‘ This program reads an analog input and smooths out the result by averaging ‘ the result with past values of the analog input.’

…This needs to be a global variablebyteVar var byte ‘ a byte variable to send out seriallyalpha var byte ‘ the number of past samples to average bytrimPotValue var word ‘ the trimmer pot input’ serial variables and constants:tx var portc.6rx var portc.7inv9600 con 16468′ Variables for subroutines:i var byteLEDPin var portb.7gosub blinkmain: ‘ read the trim pot to determine alpha between 1 and 10: adcin 1, trimPotValue alpha = (trimPotValue / 114) + 1 ‘ get an analog reading: adcin 0, analogVal ‘ smooth it: gosub smoothValue ‘ to see the difference, try outputting analogVal ‘ instead of smoothed here, and graph the difference. Continue reading

Posted in arduino/wiring, PicBasic Pro | Comments Off on Analog Smoothing Algorithm

Intro to MIDI using PicBasic Pro

This page covers only the details of MIDI communication on the PIC using PicBasic Pro.

Posted in circuits, PicBasic Pro | Comments Off on Intro to MIDI using PicBasic Pro

Random Numbers and Physical Computing

That can take up lots of processing time, so it’s usually the first function to go when writing a microprocessor language.In fact, most of what you do in programming physical computing projects is to figure out how to deal with the world’s natural randomness and make it look smooth…. Your consciousness is a great leveller for the sensors that are your eyes, ears, skin, nose, and taste buds When you move a photoresistor from one room to another, your readings will be totally different, and all of a sudden, you have to re-calculate what is "average" and what constitutes the lighting change that you want. Continue reading

Posted in arduino/wiring, AVR, BX-24, misc, pBasic (Basic stamp), PIC, PicBasic Pro, XBee | Tagged , , | Leave a comment

Analog in using RCTIME

The RCTIME command is used to obtain a varying number from the charge or discharge of a capacitor in a resistor-capacitor circuit. The PWM command produces a modulated pulse on an oputput pin to simulate a varying voltage. Continue reading

Posted in PicBasic Pro | Comments Off on Analog in using RCTIME

Stepper Motor Control

for i = 1 to 100 thisStep = i mod 4 call stepMotor(thisStep) next ‘ move motor backward for i = 100 to 1 step -1 thisStep = i mod 4 call stepMotor(thisStep) next loopEnd Subsub stepMotor(byref whatStep as integer) ‘ sets the value of the eight pins of port c to whatStep register.portc = motorStep(whatStep) call delay (0.1) ‘ vary this delay as needed to make your stepper step.end subPicBasic Pro code:start: High PORTB.0′ set variables:x VAR BYTEsteps VAR WORD stepArray VAR BYTE(4)clearTRISD = %11110000PORTD = 255input portb.4Pause 1000stepArray[0] = %00001010stepArray[1] = %00000110stepArray[2] =%00000101stepArray[3] = %00001001main: if portb.4 = 1 then steps = steps + 1 else steps = steps – 1 endif portD = stepArray[steps //4] pause 2 GoTo main pBasic (Basic Stamp 2) code:’ set variables:x var bytestepper var nibsteps var word’ set pins 8 – 10 as outputs, using DIRS to do so:dirs.highbyte = %00001111main: steps = 200 gosub clockStep pause 1000 gosub counterClockStep pause 1000goto mainclockStep: debug “counter” , cr for x = 0 to steps lookup x//4, [%1010,%1001,%0101,%0110], stepper outs.highbyte.lownib = stepper pause 2 nextreturncounterclockStep: debug “clockwise”, cr for x = 0 to steps lookup x//4, [%0110,%0101,%1001,%1010], stepper outs.highbyte.lownib = stepper pause 2 nextreturnWiring Code (for Arduino board):This example uses the Stepper library for Wiring/Arduino…. You can use// any digital I/O pins.#include <Stepper.h>#define motorSteps 200 // change this depending on the number of steps // per revolution of your motor#define motorPin1 8#define motorPin2 9#define ledPin 13// initialize of the Stepper library:Stepper myStepper(motorSteps, motorPin1,motorPin2); void setup() { // set the motor speed at 60 RPMS: myStepper.setSpeed(60); // Initialize the Serial port: Serial.begin(9600); // set up the LED pin: pinMode(ledPin, OUTPUT); // blink the LED: blink(3);}void loop() { // Step forward 100 steps: Serial.println(“Forward”); myStepper.step(100); delay(500); // Step backward 100 steps: Serial.println(“Backward”); myStepper.step(-100); delay(500); }// Blink the reset LED:void blink(int howManyTimes) { int i; for (i=0; iFor more on steppers, see the DC motor notes on this site. Continue reading

Posted in arduino/wiring, BX-24, PicBasic Pro | Comments Off on Stepper Motor Control

PicBasic Pro Debug Statement

You have to define the debug port and pin, the baud rate, and the mode at the top of your program, and you can’t change any of these settings within the progam…. Here’s a photo of the connections: And here’s the schematic for the DB-9 connector (note: the above diagram does not include the 22Kohm resistor shown in this schematic): Note the different modes you can use to display a variable. Continue reading

Posted in PicBasic Pro | Comments Off on PicBasic Pro Debug Statement