by Michael J. Horan

PicBasic Pro Code for Rotary Pot

This PicBasic Pro code reads a SpectraSymbol SoftPot and lights a series of LEDs like a VU meter in response.

Back to SoftPot Report.

define OSC 20
DEFINE  ADC_BITS        10
DEFINE  ADC_CLOCK       3
DEFINE  ADC_SAMPLEUS    20


SensorValue var Byte
SensorValueP var Byte


tx var portc.6
rx var portc.7


n9600 con 16468


TRISA = %11111111
ADCON1 = %10000010


output portb.7
output portb.6
output portb.5
output portb.4
output portb.3
output portb.2
output portb.1
output portb.0



Main:
    ADCin 0, SensorValue
    'adcin 1, sensorvaluep
    sensorvalueP = sensorvalue
    'serout2 tx, n9600, [dec sensorvalue, 13,10]
    serout2 tx, n9600, [SensorValueP]

    If sensorvalue >0 && sensorvalue<32 then
        high portb.7
    else
        low portb.7
    endif

    If sensorvalue>=32 && sensorvalue<64 then
        high portb.6 
    else
        low portb.6        
    endif

    If sensorvalue>=64 && sensorvalue<96 then
        high portb.5 
    else
        low portb.5
    endif

    If sensorvalue>=96 && sensorvalue<128 then
        high portb.4
    else
        low portb.4
    endif

    If sensorvalue>=128 && sensorvalue<160 then
        high portb.3
    else
        low portb.3
    endif

    If sensorvalue>=160 && sensorvalue<192 then
        high portb.2
    else
        low portb.2
    endif

    If sensorvalue>=192 && sensorvalue<224 then
        high portb.1
    else
        low portb.1
    endif

    If sensorvalue>=224 && sensorvalue<255 then
        high portb.0
    else
        low portb.0
    endif

    pause 50
Goto main 




Processing Code for Rotary Potentiometer. This code reads a single byte value from 0 to 255 from the serial port and uses the value to change the size of an ellipse on the screen.

Back to SoftPot Report.


import processing.serial.*;\\
Serial myPort;\\
int serial = 1; // data from serial port\\


void setup () {
  size(300, 300);
  println(Serial.list());
  myPort = new Serial(this, Serial.list()[0], 9600);
  background(255);
  noFill();
  stroke (200);
  ellipse (150, 150, 50, 50);
  ellipse (150, 150, 100, 100);
  ellipse (150, 150, 150, 150);
  ellipse (150, 150, 200, 200);
  ellipse (150, 150, 250, 250);
}


void draw () {
  if (myPort.available() > 0) {
    serial = myPort.read();
    serialEvent();
  }
}


void serialEvent () {
  background(255);
  fill(serial, serial/3, 100);
  stroke (0);
  ellipse(150, 150, serial, serial);
  noFill();
  stroke (200);
  ellipse (150, 150, 50, 50);
  ellipse (150, 150, 100, 100);
  ellipse (150, 150, 150, 150);
  ellipse (150, 150, 200, 200);
  ellipse (150, 150, 250, 250);
  println (serial);
}