Arduino Potentiometer Rotation to Graphics Display on PC


This has two parts:
  • An Arduino Software Sketch that reads the potentiometer position and sends it to your PC
  • A "Processing" Software Sketch that runs in your PC and creates a graphics display that shows the potentiometer position as a "Radar Sweep".

You need to have the "Processing" software (Another free software system that inspired Arduino) which you can download here: http://processing.org/download/ (We suggest you get the regular Windows version that comes with Java). There are also MAC and Linux versions.

Arduino Software Sketch: (Copy/Paste into Arduino IDE)

/* YourDuino Electronic Brick Test
   Rotary Potentiometer to Processing Graphics on PC 1.20
   See BS_PotToGraphics360 (Processing Sketch)
   terry@yourduino.com */

/*-----( Declare Constants )-----*/
/*-----( Define Pin Numbers )-----*/
#define POT_PIN    A0  

/*-----( Declare Variables )-----*/

int  PotValue; 

void setup()   /*----( SETUP: RUNS ONCE )----*/
{
Serial.begin(9600);  /*----(Set up Serial Monitor Output)----*/
}/*--(end setup )---*/


void loop()   /*----( LOOP: RUNS CONSTANTLY )----*/
{
PotValue = analogRead (POT_PIN); /*----(Read the value and send it)----*/
Serial.println (PotValue, DEC );

delay(250);  /*----(Let PC plot the angle)----*/

}/* --(end main loop )-- */

/* ( THE END ) */




Processing Software Sketch that displays the Potentiometer position as a "Radar Sweep Display".
(Copy/Paste into Processing IDE)
NOTE! You MUST edit this to have the correct COM port that your Arduino is on.

/* BS_PotToGraphics360 V1.12  terry@yourduino.com
    This is a "Processing" program that runs on the PC and
    receives data sent from an Arduino.
    - Needs "Processing" installed on PC
      - Download from: http://processing.org/download/
    - NEEDS the right COM Port set below!!

*/

/*-----( Import needed libraries )-----*/
import processing.serial.*;  // Receive data from Arduino Serial Port

/*-----( Declare objects )-----*/
Serial myPort;  //(example)            // The serial port

/*-----( Declare Constants )-----*/
int bgcolor = #4080df;                // Sort of Blueish

/*-----( Declare Variables for the Graphics display )-----*/
int linelen, endx, endy;
int oursize;
int origin;
/*-----( Declare Variables for angle line draw and erase )-----*/
float anglefloat ;
int x,y, oldx, oldy = 0;
int angle, oldangle = 0;
int writeline = 0;
int linewritten = 0;
int oldwiped = 0;
int wipeonce = 0;

/*----------( SETUP: RUNS ONCE )----------*/
void setup() 
{
   
 println(Serial.list()); // List all the serial ports to the lower window
 
/*************( SET THIS TO YOUR WORKING COM PORT )********************/ 
  myPort = new Serial(this, "COM12", 9600);
 
 // wait to generate a serialEvent() until you get a newline character:
 myPort.bufferUntil('\n');
 
/*-----( Set up size of display window, background )----*/ 
  oursize = 400;
  size(oursize + 50, oursize + 50);  // Leave some space around pointer
  origin = (oursize + 50) / 2;
  linelen = oursize / 2;
  
/*-----( Set up background color, width of pointer )----*/ 
  background(bgcolor);
  strokeWeight(4); 
  
}//--(end setup )---

/*---------( DRAW: LOOPS CONSTANTLY )--------*/
void draw() 
{
  /*-----( Wipe out the old pointer )----*/ 
    if (writeline == 0 
    && oldwiped == 0
    && wipeonce == 0  )  
    {
    println("Wipe Old");    
    oldwiped = 1;  
    wipeonce = 1;
    stroke(bgcolor);
    line(origin,origin,origin + oldx, origin + oldy);   
    }
/*-----( Draw the new pointer )----*/ 
    if (writeline==1 && linewritten == 0)     //Write new line
 
    {
    println("Write new");
    stroke(255,0,0);
    line(origin,origin,origin + x, origin + y); 
    linewritten = 1;;
    wipeonce = 0;
    }

}//--(end main loop )---

/*-----( Declare User-written Functions )-----*/

/*( This is an event-driven function that receives serial data )*/ 
void serialEvent (Serial myPort) 
{
 // get the ASCII string:
 String inString = myPort.readStringUntil('\n');
 
 if (inString != null) 
 {
 // trim off any whitespace:
 inString = trim(inString);
 
 // convert to an int and map to the screen height:
 int inByte = int(inString); 

 anglefloat = map(inByte, 0, 1023, 0, 180);
 angle = int(anglefloat);
 angle = angle * 2;
 println(angle);

/*-----( Calculate X and Y Trignometry )----*/  
 if (angle != oldangle)
   {
     oldx = x;
     oldy = y;
     oldangle = angle;   
     x = int(linelen * cos(radians(angle)));
     y = int(linelen * sin(radians(angle))); 
     writeline = 0; // wipe out old line, ready 4 new
     linewritten = 0;
     oldwiped = 0;

   }
 else
   {
     writeline = 1; // Newline
   }

 //delay(100);  // Delay now done in Arduino

 }//End Not Null
  
}//End Function




Another Processing Software Sketch that displays the same data from the same Arduino Sketch as a Graph:

/* Processing code for this example  YD_PotToGraph
 Graphing sketch
 This program takes ASCII-encoded strings from the serial port at 9600 baud
 and graphs them. It expects values in therange 0 to 1023, 
 followed by a newline, or newline and carriage return.

 Created 20 Apr 2005  Updated 18 Jan 2008  by Tom Igoe
 Updated 23 March 2015 by terry@yourduino.com
 This example code is in the public domain.
*/
// Import needed libraries
 import processing.serial.*;


// Create Objects
Serial myPort;        // The serial port

// Define variables
 int xPos = 1;         // horizontal position of the graph

 void setup ()  // Runs Once
 {
 size(400, 300);  // set the window size:
 
 println(Serial.list());  // List all the available serial ports
/* Click RUN and See the com ports printed in the lower window
   See which one is your Arduino COM port, (check your Tools>Port)
   The first port shown below is 0, then 1,2 etc. enter the number in
   Serial.list()[0]  where the zero is.
*/
 
 myPort = new Serial(this, Serial.list()[1], 9600);
 
 // generate a serialEvent() when you get a newline character:
 myPort.bufferUntil('\n');
 
 background(0);   // set inital background: (0 is black)
 }// END setup
 
 void draw () //---( Runs in a loop, over and over )-------
 {
 // Does nothing in this example: everything happens in the serialEvent() 
 }

//-----( User written functions )---------------
 void serialEvent (Serial myPort) 
 {
 // get the ASCII string:
 String inString = myPort.readStringUntil('\n');

 if (inString != null) 
 {
 // trim off any whitespace:
 inString = trim(inString);
 // convert to an int and map to the screen height:
 float inByte = float(inString);
 inByte = map(inByte, 0, 1023, 0, height);

 // draw the line:
 stroke(127,34,255);
 line(xPos, height, xPos, height - inByte);

 // at the edge of the screen, go back to the beginning:
 if (xPos >= width) 
 {
 xPos = 0;
 background(0);
 }
 else 
 {
 // increment the horizontal position:
 xPos++;
 }
 
 }// END InString is not Null
 
 }// END SerialEvent