Processing serial servo communication to Arduino
in
Integration and Hardware
•
2 years ago
Hey,
the code underneath is to send a value from processing to arduino (In this case angular degrees for a servo)
processing writes : port.write(spos + "a");
and Arduino gets that and uses switch... case to write it to a servo.
when you write : port.write(spos + "b");
it will write to an other servo.
I want to use this method for a large Serial communication and i need 24 "letters"
in my processing version it would look something like that:
char[] Order = {"a","b","c",..and so on ....,"v","w","x"};
and than :
port.write(pos + Order[i]);
but that doesn't work because I can't change strings into char. "a" is a string and 'a' is a char ( as far I understood)
I did the array with chars {'a','b','c'.......}; but my servos didn't even twitch......
I tried it with the simple >>>port.write(spos + "b"); <<<<< method and everything worked fine, so it needs to be something wrong with the array.
has someone any helpful suggestions?
I am really confused it think
Thank you very much !!!!
P.S. that is not my code down there, but I thought it is easier to see that little code than my gigantic and confusing code.
the code underneath is to send a value from processing to arduino (In this case angular degrees for a servo)
processing writes : port.write(spos + "a");
and Arduino gets that and uses switch... case to write it to a servo.
when you write : port.write(spos + "b");
it will write to an other servo.
I want to use this method for a large Serial communication and i need 24 "letters"
in my processing version it would look something like that:
char[] Order = {"a","b","c",..and so on ....,"v","w","x"};
and than :
port.write(pos + Order[i]);
but that doesn't work because I can't change strings into char. "a" is a string and 'a' is a char ( as far I understood)
I did the array with chars {'a','b','c'.......}; but my servos didn't even twitch......
I tried it with the simple >>>port.write(spos + "b"); <<<<< method and everything worked fine, so it needs to be something wrong with the array.
has someone any helpful suggestions?
I am really confused it think
Thank you very much !!!!
P.S. that is not my code down there, but I thought it is easier to see that little code than my gigantic and confusing code.
- /**
* Servocontrol (derived from processing Mouse 1D example.)
*
* Updated 24 November 2007
*/
// Use the included processing code serial library
import processing.serial.*;
int gx = 15;
int gy = 35;
int spos=90;
float leftColor = 0.0;
float rightColor = 0.0;
Serial port; // The serial port
void setup()
{
size(720, 720);
colorMode(RGB, 1.0);
noStroke();
rectMode(CENTER);
frameRate(100);
println(Serial.list()); // List COM-ports
//select second com-port from the list
port = new Serial(this, Serial.list()[1], 19200);
}
void draw()
{
background(0.0);
update(mouseX);
fill(mouseX/4);
rect(150, 320, gx*2, gx*2);
fill(180 - (mouseX/4));
rect(450, 320, gy*2, gy*2);
}
void update(int x)
{
//Calculate servo postion from mouseX
spos= x/4;
//Output the servo position ( from 0 to 180)
port.write(spos + "a");
// Just some graphics
leftColor = -0.002 * x/2 + 0.06;
rightColor = 0.002 * x/2 + 0.06;
gx = x/2;
gy = 100-x/2;
}
// Arduino code
//
//#include <Servo.h>
//
////initialize all servos.
//Servo servo1; Servo servo2;
//
//
//void setup() {
//
//
// servo1.attach(3);
// servo2.attach(4);
//
// servo1.write(100);
// servo2.write(170);
// delay(7000);
//
//
//
// Serial.begin(19200);
// Serial.println("Ready");
//
//}
//
//void loop() {
//
// static int v = 0;
//
// if ( Serial.available()) {
// char ch = Serial.read();
//
// switch(ch) {
// case '0'...'9':
// v = v * 10 + ch - '0';
// break;
// case 'a':
// servo1.write(v);
// //Serial.print("a = ");
// //Serial.println(v);
// v = 0;
// break;
// case 'b':
// servo2.write(180-v);
//
//
// //Serial.print("b = ");
// //Serial.println(v);
// v = 0;
// break;
// case 'c':
// // servo3.write(v);
// //Serial.print("c = ");
// //Serial.println(v);
// v = 0;
// break;
// case 'd':
// //servo4.write(v);
// //Serial.print("d = ");
// //Serial.println(v);
// v = 0;
// break;
// }
// }
//
// // Servo::refresh();
//
//}
3