We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I have 3 motors , DC motor , servo motor and stepper motor, for this project. Everything would I run with the processing and arduino . But I really feel stuck . The main problem is I can not send data from the toggle , slider and textfield of ControlP5 to be sent to the arduino, please help me make a sketch for how to send each value of the slider toggle and the other to be accepted by the arduino . thank you your help would be greatly appreciated.
this is processing code :
import controlP5.*;
import processing.serial.*;
ControlP5 cP5a;
ControlP5 cP5b;
ControlP5 cP5c;
ControlP5 cP5d;
ControlP5 cP5e;
Serial arduino;
int MOTOR_DC_SPEED_CONTROL = 0;
boolean CHANGE_DIRECTION = false;
String STEPPER_STEP = "";
String STEPPER_SPEED_CONTROL = "";
int SERVO_ANGLE = 0;
void setup()
{
size(600, 450);
String portName = Serial.list()[0];
arduino = new Serial(this, portName, 9600);
cP5a = new ControlP5(this);
cP5a.addSlider("MOTOR_DC_SPEED_CONTROL", 0, 255, 0, 30, 95, 255, 35);
cP5b = new ControlP5(this);
cP5b.addToggle("CHANGE_DIRECTION", false, 30, 145, 35, 35);
cP5c = new ControlP5(this);
cP5c.addTextfield("STEPPER_STEP")
.setPosition(30,235)
.setSize(128,35)
.setAutoClear(false);
cP5c.addBang("clear")
.setPosition(168,235)
.setSize(80,35)
.getCaptionLabel().align(ControlP5.CENTER, ControlP5.CENTER);
cP5d = new ControlP5(this);
cP5d.addTextfield("STEPPER_SPEED_CONTROL")
.setPosition(30,295)
.setSize(128,35)
.setAutoClear(false);
cP5d.addBang("clear")
.setPosition(168,295)
.setSize(80,35)
.getCaptionLabel().align(ControlP5.CENTER, ControlP5.CENTER);
cP5e = new ControlP5(this);
cP5e.addSlider("SERVO_ANGLE", 0, 180, 0, 30, 380, 255, 35);
}
void draw()
{
background(128);
fill(0, 0, 153);
text("DC MOTOR STEPPER MOTOR & SERVO MOTOR CONTROLLER", 115, 20);
text("FIANSYAH AFANDI RAKHMAN, ELECTRICAL ENGINEERING, SILIWANGI UNIVERSITY, INDONESIA", 45,43);
text("A. DC MOTOR", 15,75);
text("B. STEPPER MOTOR", 15,215);
text("C. SERVO MOTOR", 15,365);
text("created by: Fiansyah Afandi Rakhman", 375, 442);
}
void controlEvent(ControlEvent theEvent) {
//if(theEvent.isAssignableFrom(Textfield.class))
if(theEvent.isController()) {
print("control event from : "+theEvent.controller().name());
println(", value : "+theEvent.controller().value());
println("controlEvent: accessing a string from controller '"
+theEvent.getName()+"': "
+theEvent.getStringValue());
if(theEvent.controller().name()=="MOTOR_DC_SPEED_CONTROL") {
}
if(theEvent.controller().name()=="CHANGE_DIRECTION") {
}
if(theEvent.controller().name()=="STEPPER_STEP") {
}
if(theEvent.controller().name()=="STEPPER_SPEED_CONTROL") {
}
if(theEvent.controller().name()=="SERVO_ANGLE") {
}
}
}
public void clear() {
cP5c.get(Textfield.class,"STEPPER_STEP").clear();
cP5d.get(Textfield.class,"STEPPER_SPEED_CONTROL").clear();
}
I am confused in this section :
if(theEvent.controller().name()=="MOTOR_DC_SPEED_CONTROL") {
}
if(theEvent.controller().name()=="CHANGE_DIRECTION") {
}
if(theEvent.controller().name()=="STEPPER_STEP") {
}
if(theEvent.controller().name()=="STEPPER_SPEED_CONTROL") {
}
if(theEvent.controller().name()=="SERVO_ANGLE") {
}
how to continue sending signals generated by each of the objects , and how to differentiate between each other in order to command the right target .
and this is the arduino code to receive a signal :
#include <Servo.h>
#include <Stepper.h>
Servo myservo;
int in1Pin = 5; // for stepper
int in2Pin = 6; // for stepper
int relayPin = 8; // for dcmotor change direction
int in1dcPin = 10; // for dcmotor
int in2dcPin = 9; // for dcmotor
int enablePin = 11; // for dcmotor
int switchPin = 7; // for dcmotor change direction
char val; // for relay as changer direction
int valServo; // for servo angle
Stepper motor(48, in1Pin, in2Pin);
void setup(){
pinMode(in1Pin, OUTPUT);
pinMode(in2Pin, OUTPUT);
pinMode(in1dcPin, OUTPUT);
pinMode(in2dcPin, OUTPUT);
pinMode(enablePin, OUTPUT);
pinMode(switchPin, INPUT_PULLUP);
pinMode(relayPin, OUTPUT);
myservo.attach(9);
while (!Serial);
Serial.begin(9600);
motor.setSpeed('myspeed');
}
void loop(){
boolean reverse = digitalRead(switchPin);
if (Serial.available()) {
{
int incomeByte = Serial.parseInt(); // read from motordcspeedcontrol's 1st slider
setMotor(incomeByte, reverse);
analogWrite(enablePin, incomeByte); } //
{
val = Serial.read(); // read from changedirection's togggle
if (val == '1')
{ digitalWrite(relayPin, HIGH);}
else
{digitalWrite(relayPin, LOW);}
delay(10); } // for relay changer of motor direction
{
int steps = Serial.parseInt(); // read from stepperstep's 2nd slider
motor.step(steps); } // for step of steppermotor
{
int myspeed = Serial.parseInt(); // read from stepperspeedcontrol's 3rd slider
motor.setSpeed(myspeed); } // for speed of steppermotor
static int v = 0;
{
valServo = Serial.read();
valServo = map(val, 0, 1024, 0, 180);
myservo.write(valServo);
delay(15); }
}
}
void setMotor(int speed, boolean reverse)
{
digitalWrite(in1dcPin, ! reverse);
digitalWrite(in2dcPin, reverse);
}
Answers
Hi, in response to the part where you are confused, here is an example how to check if a controlEvent was sent by a particular controller. I included some comments, I hope they are useful
to send data to a serial port and receive data on a serial device, you need to use a library that supports serial communication. I see you are already importing processing's serial library, but you are not using it to send data to the connected serial device. Have a look at the processing examples that cover Serial Communication (they can be found on the processing website or just make a google search 'processing Arduino serial communication')
since you are dealing with 3 different outputs which you want to control with the Arduino, you need to find a way to distinguish between the different values sent from processing. Have you checked if the firmata library can be useful here for your project? Otherwise I would prefix each value (sent from processing) with an identifier for example
m1=100
orm2=0
.m1=100
would be used to change the state of the motor associated with m1 andm2=0
would make changes to m2. of course the latter would require more implementation work.Thank you so much, I will try the explanation you have given . sorry if my english is not raw , because I only use " google translate " . If I find a back trouble , I will be asked back to you . Thanks so much, your help is very precious for me.
hi sojamo , have given way you can send a signal from tiap2 ( slider , textfield , etc.) , but all that was not enough resolve my problem , it seems arduino confusion read any data coming in , how to distinguish arduino sketch reception of data that each data received by the receiver which is desired. Thank You. and this is a sketch that I tried from the way you give , ( please correct any mistakes )
actually I was a little hesitant with the sketch that I made it , I ask that you vote and give a true example if it is wrong . thank you very much