Sign in to follow this  
LariSan

Chipkit Cmod To Control Processing Sketch-- Skeptical Penguin

Recommended Posts

This is a project that lets you potentially replace your keyboard/mouse interface with the computer, using Processing + chipKIT Cmod and an Analog Pot. 
 
Here's the video:

http://youtu.be/VR2rtftFbqg

 

To re-create this, you will need to have

  • MPIDE downloaded. 
  • Processing downloaded
  • the image files (attached to this thread)
  • chipKIT Cmod + a 10K pot + wires needed
  • USB Micro cable

STEP 1:
Wire up your breadboard + Cmod
I connected my analog pin on the pot to pin 26 on the Cmod
 






16237688020_1005172c45_c.jpgchipKIT Cmod + Analog Pot by laraswanland, on Flickr


STEP 2:
Check your analog pot is reading well. 
Connect your USB to the chipKIT then to your computer, open MPIDE, select the serial port, select the board, then copy and paste this code into your sketch.







const int analogInPin = 26; // Analog input pin that the potentiometer is attached to
const int analogOutPin = 12; // Analog output pin that the LED is attached to

int sensorValue = 0; // value read from the pot
int outputValue = 0; // value output to the PWM (analog out)

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
}

void loop() {
  // read the analog in value:
  sensorValue = analogRead(analogInPin);
  // map it to the range of the analog out:
  outputValue = map(sensorValue, 0, 1023, 0, 8);
  // change the analog out value:
  analogWrite(analogOutPin, outputValue);

  // print the results to the serial monitor:
  Serial.println(outputValue);
  //Serial.write(outputValue);
  delay(500);
}


Hit run. 
Then, hit the serial monitor to see the values coming through.
 

16238900629_765cebd23e_c.jpgScreen Shot 2015-02-01 at 10.02.06 PM by laraswanland, on Flickr


once you verify that the pot works, close the Serial monitor.
Change the code to the below (switching some of the commenting).
Upload to the board.
 

const int analogInPin = 26; // Analog input pin that the potentiometer is attached to
const int analogOutPin = 12; // Analog output pin that the LED is attached to

int sensorValue = 0; // value read from the pot
int outputValue = 0; // value output to the PWM (analog out)

void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
}

void loop() {
  // read the analog in value:
  sensorValue = analogRead(analogInPin);
  // map it to the range of the analog out:
  outputValue = map(sensorValue, 0, 1023, 0, 8);
  // change the analog out value:
  analogWrite(analogOutPin, outputValue);

  // print the results to the serial monitor:
  //-----> comment this out Serial.println(outputValue);
  Serial.write(outputValue);
  delay(500);
}

 
 
STEP3:
Don't close MPIDE (keep it open).
Now you will set up your Processing sketch. 
Make sure to have downloaded Processing. 
 
Test out a demo sketch by copy and pasting this and hitting run:
(you can also go to File->Examples->Input->MousePress)
 

 * Mouse Press. 
 * 
 * Move the mouse to position the shape. 
 * Press the mouse button to invert the color. 
 */


void setup() {
  size(640, 360);
  noSmooth();
  fill(126);
  background(102);
}

void draw() {
  if (mousePressed) {
    stroke(255);
  } else {
    stroke(0);
  }
  line(mouseX-66, mouseY, mouseX+66, mouseY);
  line(mouseX, mouseY-66, mouseX, mouseY+66); 
}

If you are able to click a cross and have it change from white to black and draw across the screen, your sketch worked. 
Close the sketch box out. 
Download the attached file to this thread. 
potreadhuh.zip
 
Once you open the file-- you should see this sketch.
(advise to save this under where your Processing folder sketches are saved). 
 

/*Larissa Swanland for the Digilent MakerSpace
reads a analog serial value from chipKIT and this makes a 
penguin turn it's head, you must have the 
chipKIT sketch open at the same time to have this work*/

import processing.serial.*;
Serial myPort;


int numFrames = 8;  // The number of frames in the animation
int frame = 0;
int count= 0; 
PImage[] images = new PImage[numFrames];
    
void setup() {
  size(800, 600);
  
  images[0]  = loadImage("huh-00.png");
  images[1]  = loadImage("huh-01.png");
  images[2]  = loadImage("huh-02.png");
  images[3]  = loadImage("huh-03.png"); 
  images[4]  = loadImage("huh-04.png");
  images[5]  = loadImage("huh-05.png"); 
  images[6]  = loadImage("huh-06.png");
  images[7]  = loadImage("huh-07.png");
  
  println ("Available serial ports:");
  println(Serial.list());
  myPort= new Serial(this, Serial.list()[3],9600);
}

void draw() { 
  background(0);
  image(images[frame],0,0); 
  
  if(myPort.available()>0) {
    frame= myPort.read(); 
  }
}
    
 

The images need to be saved to the same folder (images are the stages of the penguin's head). 
 
If you have the circuit connected to the chipKIT Cmod, the chipKIT Cmod connected to USB, the chipKIT code running, the images downloaded to the right folder, the Processing sketch...
Then you should be able to hit (RUN). 
and when you change the pot value, the penguin's head will move. 
 
... 
Troubleshooting:
 
1. if you don't get a penguin head to come up (just a blank black box)... 
check your serial port. 

  println ("Available serial ports:");
  println(Serial.list());
  myPort= new Serial(this, Serial.list()[3],9600);

Change this value from 0-4... until you find the right serial port. 
 
2. Re-boot the sketch on MPIDE if the head isn't moving-- the serial connection may be lagging. 
(change delay (500) to delay (1000) (this slows it down, it may help). 
 
3. If you see this error [ARRAYEXCEPTIONOUTOFBOUNDS]

16399242396_83659d91f2.jpg
Screen Shot 2015-02-01 at 10.20.36 PM by laraswanland, on Flickr


This means that the array Processing is using to determine which Frame you are in has been overflowed. Just hit the STOP button (next to run in processing)and re-start.
Let the penguin's head find the current value first before turning the pot...
or move slowly (too fast also causes the values to read messily).
 

4. MPIDE must be running at the same time. You will get the out of bounds error in #3 also if you don't have it opened. 

 

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this