Example code: Controlling Electric Motors with a controlP5 knob and Adafruit Motor Shield boards

edited December 2015 in Share Your Work

I Wanted to post this code to help out others trying to do something similar.
you need to have the controlP5 and Adafruit libraries installed. I hope my notes are helpful.

the code is a combination of the following codes:
1) https://processing.org/tutorials/electronics/ (Example 5A and 5B)
2) https://learn.adafruit.com/adafruit-motor-shield-v2-for-arduino/using-dc-motors
3) http://www.sojamo.de/libraries/controlP5/examples/controllers/ControlP5knob/ControlP5knob.pde

Processing:
// Write data to the serial port according to the status of a ControlP5 Knob controlled
// by the mouse. uses values from 0-255.

import processing.serial.*;
import controlP5.*; // import library for knob

Serial port;        // Create serial port object 
ControlP5 cp5;      // defines the type "cp5" as a variable "type" defined in ControlP5  for knob

int myColorBackground = color(0,0,0);  
int knobValue = 100;  

Knob myKnobA;  
Knob myKnobB;  

void setup() {

  port = new Serial(this, 9600); 
  size(700,400);  
  smooth();  
  noStroke(); 

  cp5 = new ControlP5(this);  //object created from library (what is this exactly?)  

  myKnobA = cp5.addKnob("knob")  // "method" is add(ControllerInterface<?> theElement) 
               .setRange(0,255) 
               .setValue(50)  
               .setPosition(100,70)  
               .setRadius(50)  // for knob
               .setDragDirection(Knob.VERTICAL)  
               ;                  
} 

void draw() {   

  background(myColorBackground); 
  fill(knobValue);   
  rect(0,height/2,width,height/2);   
  fill(0,100);   
  rect(80,40,140,320);  
} 

void knob(int theValue) {   
  myColorBackground = color(theValue);  
  println("a knob event. setting background to "+theValue); 
  port.write(theValue); 
}  

Arduino: // Read data from the serial and turn a DC motor on or off according to the value. with adafruit motor driver // board code included

#include <Wire.h>     
#include <Adafruit_MotorShield.h> 
#include "utility/Adafruit_PWMServoDriver.h"  

// Create the motor shield object with the default I2C address   
// Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 
// DONT USE THIS ONE IF RUNNING MULIPLE MOTORS   
// Or, create it with a different I2C address (say for stacking)   
Adafruit_MotorShield AFMS = Adafruit_MotorShield(0X60);  // 1st board if stacking 

// Select which 'port' M1, M2, M3 or M4. In this case, M1
Adafruit_DCMotor *myMotor1 = AFMS.getMotor(1); // 1st board, M1 

char val;             // Data received from the serial port

void setup() { 
  Serial.begin(9600);                  // Start serial communication at 9600 bps 

  AFMS.begin();  // create with the default frequency 1.6KHz, this connects to the 
             // controller- need one for each motor? nope.  
} 

void loop() { 
  if (Serial.available()) {            // If data is available,  
    val = Serial.read();               // read it and store it in val 
  } 
  myMotor1->setSpeed(val); 
  myMotor1->run(FORWARD); 
  delay(100);                          // Wait 100 milliseconds for next reading
}

Comments

  • Thank you Admins for moving this to the right section.

  • edited January 2016

    BTW:
    I used the following to write this code:
    Operating System: Windows 7 professional service pack 1
    Arduino Uno with arduino software version: 1.6.6
    Processing Software version: 3.101 32 bit

Sign In or Register to comment.