Pages: [1]   Go Down
Author Topic: Processing + Arduino  (Read 1049 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am connecting four stepper motors to arduino, controlled by processing (connected to touchOSC). I am trying to determine the best way to send information to independently control each motor.

Here is how im doing it: TouchOSC(on iphone) >> Processing >> arduino >> motors

Arduino receives a single number through serial connection! How can I distinguish different values and connect them to the proper motor?

**********Processing code**************
/**
 * TouchOSC
 *
 * Example displaying values received from
 * the "Simple" layout (Page1 only so far)
 * http://hexler.net/touchosc
 *
 */

import oscP5.*;
import netP5.*;
import processing.serial.*;

Serial arduinoPort;
OscP5 oscP5;

float v_fader1 = 0.0f;
float v_fader2 = 0.0f;
float v_fader3 = 0.0f;
float v_fader4 = 0.0f;
float v_fader5 = 0.0f;
float v_toggle1 = 0.0f;
float v_toggle2 = 0.0f;
float v_toggle3 = 0.0f;
float v_toggle4 = 0.0f;

float v_fadera = 0;
float v_faderb = 0;
float v_faderc = 0;
float v_faderd = 0;

int int_fader1;
int int_fader2;
int int_fader3;
int int_fader4;

void setup() {
  size(320,440);
  frameRate(25);
  /* start oscP5, listening for incoming messages at port 8000 */
  oscP5 = new OscP5(this,8000);
  arduinoPort=new Serial(this,Serial.list()[0],9600);
}

void oscEvent(OscMessage theOscMessage) {

  String addr = theOscMessage.addrPattern();
  float  val  = theOscMessage.get(0).floatValue();

  if(addr.equals("/1/fader1"))        {
    v_fader1 = val;
  }
  else if(addr.equals("/1/fader2"))   {
    v_fader2 = val;
  }
  else if(addr.equals("/1/fader3"))   {
    v_fader3 = val;
  }
  else if(addr.equals("/1/fader4"))   {
    v_fader4 = val;
  }
  else if(addr.equals("/1/fader5"))   {
    v_fader5 = val;
  }
  else if(addr.equals("/1/toggle1"))  {
    v_toggle1 = val;
  }
  else if(addr.equals("/1/toggle2"))  {
    v_toggle2 = val;
  }
  else if(addr.equals("/1/toggle3"))  {
    v_toggle3 = val;
  }
  else if(addr.equals("/1/toggle4"))  {
    v_toggle4 = val;
  }
}

void draw() {
  background(0);

  // fader5 + toggle 1-4 outlines
  fill(0);
  stroke(0, 196, 168); 

  rect(17,21,287,55);
  rect(17,369,60,50);
  rect(92,369,60,50);
  rect(168,369,60,50);
  rect(244,369,60,50);

  // fader5 + toggle 1-4 fills
  fill(0, 196, 168);
  rect(17,21,v_fader5*287,55);
  if(v_toggle1 == 1.0f) rect(22,374,50,40);
  if(v_toggle2 == 1.0f) rect(97,374,50,40);
  if(v_toggle3 == 1.0f) rect(173,374,50,40);
  if(v_toggle4 == 1.0f) rect(249,374,50,40);

  // fader 1-4 outlines
  fill(0);
  stroke(255, 237, 0);
  rect(17,95,60,255);
  rect(92,95,60,255);
  rect(168,95,60,255);
  rect(244,95,60,255);

  // fader 1-4 fills
  fill(255, 237, 0);
  rect(17,95 + 255 - v_fader1*255,60,v_fader1*255);
  rect(92,95 + 255 - v_fader2*255,60,v_fader2*255);
  rect(168,95 + 255 - v_fader3*255,60,v_fader3*255);
  rect(244,95 + 255 - v_fader4*255,60,v_fader4*255);

  v_fadera = map(v_fader1, 0, 1,0, 200);
  v_faderb = map(v_fader2, 0, 1,201, 400);
  v_faderc = map(v_fader3, 0, 1,401, 600);
  v_faderd = map(v_fader4, 0, 1,600, 800);

  int_fader1 = int(v_fadera);
  int_fader2 = int(v_faderb);
  int_fader3 = int(v_faderc);
  int_fader4 = int(v_faderd);

  println(int_fader4);
//  println(int_fader1);
//  println(int_fader3);
//  println(int_fader4);

 // arduinoPort.write(int_fader1);
  //arduinoPort.write(int_fader2);
  //arduinoPort.write(int_fader3);
  arduinoPort.write(int_fader4);
 
  //arduinoPort.write(int_fader2);
  //arduinoPort.write(int_fader3);
  //arduinoPort.write(int_fader4);

  //println(int_fader);
}





********arduino code***********
/*
 * MotorKnob
 *
 * A stepper motor follows the turns of a potentiometer
 * (or other sensor) on analog input 0.
 *
 * http://www.arduino.cc/en/Reference/Stepper
 * This example code is in the public domain.
 */

#include <Stepper.h>

// change this to the number of steps on your motor
#define STEPS 100

// create an instance of the stepper class, specifying
// the number of steps of the motor and the pins it's
// attached to
Stepper stepper1(STEPS, 2, 3, 4, 5);
Stepper stepper2(STEPS, 6, 7, 8, 9);
Stepper stepper3(STEPS, 10, 11, 12, 13);
Stepper stepper4(STEPS, A0, A1, A2, A3);

// the previous reading from the analog input
int previous = 0;

void setup()
{
  // set the speed of the motor to 30 RPMs
  stepper1.setSpeed(30);
  stepper2.setSpeed(30);
  stepper3.setSpeed(30);
  stepper4.setSpeed(30);
  // begin serial connection (readings)
  Serial.begin(9600);
}

void loop()
{
  if(Serial.available() > 0)
  {


    // get the sensor value
    int val = Serial.read();
   
    if (val <= 600){
      val = val-600;
    }   
   
    if (val >= 200){
      stepper1.step(val-previous);
          previous = val;
    }
   
    if (val < 200){
      stepper4.step(val-previous);
          previous = val;
     
    }
   

    //uncomment for manual testing
    //stepper.step(-90);
    //delay(500);


    // remember the previous value of the sensor
   // previous = val;
  }
}





Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26633
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ow! My scrolling finger.
Please go back and edit your post, putting each code block into a code box.
Highlight each block, then click the # icon on the editor's toolbar.

Quote
Arduino receives a single number through serial connection!
Yes, that's how serial interfaces work.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Pages: [1]   Go Up
Jump to: