Created
June 13, 2015 03:29
-
-
Save sulram/fc859e7719b3c1bbbe0c to your computer and use it in GitHub Desktop.
<desafiando a gravidade> chuva
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import oscP5.*; | |
import netP5.*; | |
import processing.serial.*; | |
Serial myPort; | |
Serial myPort2; | |
int val; | |
OscP5 oscP5; | |
NetAddress myRemoteLocation; | |
float x = 0; | |
float y = 0; | |
float A, B, C, D; | |
int count = 0; | |
void setup() { | |
size(400, 400); | |
frameRate(20); | |
String portName1 = Serial.list()[0]; | |
String portName2 = Serial.list()[1]; | |
println(Serial.list()); | |
myPort = new Serial(this, portName1, 9600); | |
myPort2 = new Serial(this, portName2, 9600); | |
oscP5 = new OscP5(this, 22244); | |
myRemoteLocation = new NetAddress("127.0.0.1", 22243); | |
} | |
void draw() { | |
background(0); | |
fill(255); | |
rect(0, 0, A, 50); | |
rect(0, 50, B, 50); | |
rect(0, 100, C, 50); | |
rect(0, 150, D, 50); | |
count = (count + 1) % 20; | |
if(count == 0){ | |
myPort.write("A "+ int(A)+" "+int(B)); | |
myPort.write(13); | |
myPort2.write("A "+int(C)+" "+int(D)); | |
myPort2.write(13); | |
print(int(A)); | |
print(" "); | |
print(int(B)); | |
print(" "); | |
print(int(C)); | |
print(" "); | |
print(int(D)); | |
println(" "); | |
} | |
} | |
/* incoming osc message are forwarded to the oscEvent method. */ | |
void oscEvent(OscMessage msg) { | |
/* print the address pattern and the typetag of the received OscMessage */ | |
print("### received an osc message."); | |
print(" addrpattern: "+msg.addrPattern()); | |
print(" typetag: "+msg.typetag()); | |
println(" val: "+msg.get(0).stringValue()); | |
String addr = msg.addrPattern(); | |
float val = float(msg.get(0).stringValue()); | |
if (addr.equals("/0/A")) { | |
A = val; | |
} | |
else if (addr.equals("/0/B")) { | |
B = val; | |
} | |
else if (addr.equals("/0/C")) { | |
C = val; | |
} | |
else if (addr.equals("/0/D")) { | |
D = val; | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SoftwareSerial.h> | |
#include <SerialCommand.h> | |
#include <AFMotor.h> | |
#include <AccelStepper.h> | |
SerialCommand serial_command; | |
AF_Stepper motor1(200, 1); | |
AF_Stepper motor2(200, 2); | |
long m1 = 0; | |
long m2 = 0; | |
long m1target = 0; | |
long m2target = 0; | |
boolean dir = false; | |
void forwardstep1() { | |
motor1.onestep(FORWARD, SINGLE); | |
} | |
void backwardstep1() { | |
motor1.onestep(BACKWARD, SINGLE); | |
} | |
// wrappers for the second motor! | |
void forwardstep2() { | |
motor2.onestep(FORWARD, SINGLE); | |
} | |
void backwardstep2() { | |
motor2.onestep(BACKWARD, SINGLE); | |
} | |
AccelStepper stepper1(forwardstep1, backwardstep1); | |
AccelStepper stepper2(forwardstep2, backwardstep2); | |
void setup(){ | |
Serial.begin(9600); | |
serial_command.addCommand("steps", stepTo); | |
serial_command.addDefaultHandler(unrecognized); | |
stepper1.setMaxSpeed(600.0); | |
stepper1.setAcceleration(60.0); | |
stepper2.setMaxSpeed(600.0); | |
stepper2.setAcceleration(60.0); | |
} | |
void loop(){ | |
// SERIAL COMMAND PROCESS | |
serial_command.readSerial(); | |
stepper1.run(); | |
stepper2.run(); | |
} | |
// STEP SERIAL COMMAND | |
void stepTo(){ | |
char *arg; | |
int val; | |
Serial.println("processing command..."); | |
arg = scom.next(); | |
if (arg != NULL) | |
{ | |
val = atoi(arg); // Converts a char string to an integer | |
Serial.print("motor #1 to: "); | |
Serial.println(val); | |
m1target = val; | |
stepper1.moveTo(m1target); | |
} | |
else { | |
Serial.println("no arguments"); | |
} | |
arg = scom.next(); | |
if (arg != NULL) | |
{ | |
val = atoi(arg); // Converts a char string to an integer | |
Serial.print("motor #2 to: "); | |
Serial.println(val); | |
m2target = val; | |
stepper2.moveTo(m2target); | |
} | |
else { | |
Serial.println("no arguments for motor #2"); | |
} | |
} | |
// DEFAULT SERIAL COMMAND | |
void unrecognized() | |
{ | |
Serial.println("What?"); | |
} | |
// MOTOR STEPS | |
//void step1() { | |
// motor1.onestep(FORWARD, SINGLE); | |
// m1++; | |
//} | |
//void backstep1() { | |
// motor1.onestep(BACKWARD, SINGLE); | |
// m1--; | |
//} | |
//void step2() { | |
// motor2.onestep(FORWARD, SINGLE); | |
// m2++; | |
//} | |
//void backstep2() { | |
// motor2.onestep(BACKWARD, SINGLE); | |
// m2--; | |
//} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment