Offline
Newbie
Karma: 0
Posts: 6
|
 |
« on: February 22, 2011, 07:00:29 am » |
Hey guys, I am making a project for school as a thesis. The idea is to use the TouchOsc App op my ipod to control the speed and direction of the two dc motors with the arduino Mega.Basically processing receives the data from touchosc app and sends for example port.write('1'). In my arduino code i have made a switchcase whereby i do a serial.read and uses this decide where button was pushed(like move forward/backwards). Which is working smoothly. My main problem is i have to send in addition to the port.write('1') also my pwmMotorA with is an integer from one the sliders on my touchOsc app.Is there a way to send them both in like port.write( '1',pwmMotorA)? when i do that arduino just sees it as an ASCII code..I have searched almost whole google for this..with no results. I have included my arduino and processing sketch.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 316
Posts: 35593
Seattle, WA USA
|
 |
« Reply #1 on: February 22, 2011, 08:00:32 am » |
Is there a way to send them both in like port.write( '1',pwmMotorA)? Not like that, no. port.write('1'); port.write(pwmMotorA); port.write('2'); port.write(pwmMotorB); This will write 4 bytes to the serial port. Just the 1st two lines will write the two values you asked about.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 6
|
 |
« Reply #2 on: February 23, 2011, 07:06:21 am » |
thx for the fast reply Paul. But it still isnt working. I tried your code: port.write('1'); port.write(pwmMotorA); port.write('2'); port.write(pwmMotorB); by sending my variables one by one.To slow reaction on the arduino.The first line port.write('1') is actually a variabele i use to detect in arduino in which mode the motor is in(like forward/back/stop). Am considering is using seperators like i read somewhere. ie a port.write(<variabele,pwmMotorA>); Isnt this going to send all of them as a string? Thx in advance.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 316
Posts: 35593
Seattle, WA USA
|
 |
« Reply #3 on: February 23, 2011, 08:07:47 am » |
The port.write() function sends 1 byte to the serial port. IF one were to create an overloaded method that took multiple bytes as input, the overload would end up calling the existing method in a loop to send all the bytes.
The "delay" with that function is NOT with having to call it multiple times.
It's time to dispense with the academic questions, and post all of your Processing and Arduino code. There may be things that you are doing (unknowingly) that limit the speed that the data is transferred at. We can't tell that without seeing all of the code.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 6
|
 |
« Reply #4 on: February 24, 2011, 07:12:35 pm » |
hey guys, sry to say i still havent figured it out.I ll just go ahead and repost the code of what u v got so far :p Arduino sketch int EN1 = 8; //pwm motorA int EN2 = 9; //pwm motorB int IN1 = 10; //direction motorA int IN2 = 11; //direction motorB
int laag = 0; int hoog= 255; int pwmMotorA=0; int pwmMotorB=0; int dirA,dirB=0 ;
void setup() { Serial.begin(9600);
}
void loop() {
if (Serial.available() > 0) { // Check if there is a new message dirA= Serial.read(); // Put the serial input into the message dirB= Serial.read(); // Put the serial input into the message pwmMotorA = Serial.read(); pwmMotorB = Serial.read(); } switch(dirA) { case '1': //Move ahead M1 Motor1(pwmMotorA,true); //You can change the speed, such as Motor(50,true) break; case '2': //move back M1 Motor1(pwmMotorA,false); break; case '3': //stop M1 Motor1(0,false); break; default: Motor1(pwmMotorA,true); break; } switch(dirB) { case '1': //Move ahead M2 Motor2(pwmMotorB,true);//You can change the speed, such as Motor(50,true) break; case '2': //move back M2 Motor2(pwmMotorB,false); break; case '3': //stop M2 Motor2(0,false); break; default: Motor2(pwmMotorB,true); break; }
}
void Motor1(int pwmMotorA, boolean reverse) //handelen van motorA { analogWrite(EN1,pwmMotorA); //set pwm control, 0 for stop, and 255 for maximum speed if(reverse) { analogWrite(IN1,hoog); } else { analogWrite(IN1,laag); } } void Motor2(int pwmMotorB, boolean reverse)//handelen van motorB { analogWrite(EN2,pwmMotorB); if(reverse) { analogWrite(IN2,hoog); } else { analogWrite(IN2,laag); } }
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 6
|
 |
« Reply #5 on: February 24, 2011, 07:16:36 pm » |
the 2nd part... Processing sketch /* // importeer de volgende libraries serial,oscP5 en arduino classes voor communicatie met de arduino,IPOD en seriele verbinding. import oscP5.*; import netP5.*; import processing.serial.*; import cc.arduino.*;
OscP5 oscP5; NetAddress myRemoteLocation; Serial arduinoPort; Arduino arduino;
int [] Toggle1 = new int [8]; int [] Toggle2 = new int [8]; int [] sliderStrip = new int [5]; float [] fader1 = new float [8]; float [] fader2 = new float [8]; boolean sliderNeedsRedraw = true;
int pwmMotorA,pwmMotorB = 0 ; int EN1 = 12; int EN2 = 11; //PWM uitgangen int IN1 = 10; int IN2 = 9; //Draairichting
int dirA,dirB= 0;
int laag =0; int hoog =255; int myMessage1 = 0; int myMessage2 = 0; int myMessage3 = 0; int myMessage4 = 0; int myMessage5 = 0; int myMessage6 = 0;
void setup() { size(290,360); frameRate(25); background(0); fader1[1] = 0.5; fader2[1] = 0.5;
/* start oscP5, listening for incoming messages at port 8000 */ oscP5 = new OscP5(this,8000); myRemoteLocation = new NetAddress("192.168.62.55",9000); //IP address for outgoing data arduinoPort = new Serial(this, "COM4", 9600); // Set arduino to 9600 baud }
void oscEvent(OscMessage theOscMessage) { OscMessage myMessage1 = new OscMessage("/1/led1"); //used to store status of the motor OscMessage myMessage2 = new OscMessage("/1/led2"); OscMessage myMessage3 = new OscMessage("/1/led3"); OscMessage myMessage4 = new OscMessage("/2/led1"); OscMessage myMessage5 = new OscMessage("/2/led2"); OscMessage myMessage6 = new OscMessage("/2/led3"); String addr = theOscMessage.addrPattern(); // println(addr); // uncomment for seeing the raw meshsage //float val = theOscMessage.get(0).floatValue(); if(addr.indexOf("/1/fader") !=-1) { // one of the faders String list[] = split(addr,'/'); int xfader = int(list[2].charAt(5) - 0x30); fader1[xfader] = theOscMessage.get(0).floatValue(); // println(" x = "+fader[xfader]); // uncomment to see x values sliderNeedsRedraw = true; pwmMotorA=int(255*(fader1[1])); } if(addr.indexOf("/2/fader") !=-1) { // one of the faders String list[] = split(addr,'/'); int xfader = int(list[2].charAt(5) - 0x30); fader2[xfader] = theOscMessage.get(0).floatValue(); // println(" x = "+fader[xfader]); // uncomment to see x values sliderNeedsRedraw = true; pwmMotorB= int((255*(fader2[1]))); }
if(addr.indexOf("/1/toggle") !=-1)// the strip at the bottom { int i = int((addr.charAt(9) )) - 0x30; // retrns the ASCII number so convert into a real number by subtracting 0x30 String list[] = split(addr,'/'); Toggle1[i] = int(theOscMessage.get(0).floatValue()); // println(" i = "+i); // uncomment to see index value sliderNeedsRedraw = true; } if(addr.indexOf("/2/toggle") !=-1)// the strip at the bottom { int i = int((addr.charAt(9) )) - 0x30; // retrns the ASCII number so convert into a real number by subtracting 0x30 String list[] = split(addr,'/'); Toggle2[i] = int(theOscMessage.get(0).floatValue()); // println(" i = "+i); // uncomment to see index value sliderNeedsRedraw = true; }
//bestuderen van de druktoetsen if (Toggle1[2]==1) { dirA= '3'; myMessage1.add(0); /* add an int to the osc message */ myMessage2.add(0); /* add an int to the osc message */ myMessage3.add(1); /* add an int to the osc message */ /* send the message */ oscP5.send(myMessage1, myRemoteLocation); oscP5.send(myMessage2, myRemoteLocation); oscP5.send(myMessage3, myRemoteLocation); } else { if(Toggle1[1] == 1) { dirA = '2';
myMessage1.add(0); /* add an int to the osc message */ myMessage2.add(1); /* add an int to the osc message */ myMessage3.add(0); /* add an int to the osc message */
/* send the message */ oscP5.send(myMessage1, myRemoteLocation); oscP5.send(myMessage2, myRemoteLocation); oscP5.send(myMessage3, myRemoteLocation); } else { dirA= '1'; myMessage1.add(1); /* add an int to the osc message */ myMessage2.add(0); /* add an int to the osc message */ myMessage3.add(0); /* add an int to the osc message */ /* send the message */ oscP5.send(myMessage1, myRemoteLocation); oscP5.send(myMessage2, myRemoteLocation); oscP5.send(myMessage3, myRemoteLocation); } }
if (Toggle2[2]==1) { dirB= '3'; myMessage4.add(0); /* add an int to the osc message */ myMessage5.add(0); /* add an int to the osc message */ myMessage6.add(1); /* add an int to the osc message */ /* send the message */ oscP5.send(myMessage4, myRemoteLocation); oscP5.send(myMessage5, myRemoteLocation); oscP5.send(myMessage6, myRemoteLocation); } else { if(Toggle2[1] == 1) { dirB= '2'; myMessage4.add(0); /* add an int to the osc message */ myMessage5.add(1); /* add an int to the osc message */ myMessage6.add(0); /* add an int to the osc message */
/* send the message */ oscP5.send(myMessage4, myRemoteLocation); oscP5.send(myMessage5, myRemoteLocation); oscP5.send(myMessage6, myRemoteLocation); } else { dirB= '1'; myMessage4.add(1); /* add an int to the osc message */ myMessage5.add(0); /* add an int to the osc message */ myMessage6.add(0); /* add an int to the osc message */ /* send the message */ oscP5.send(myMessage4, myRemoteLocation); oscP5.send(myMessage5, myRemoteLocation); oscP5.send(myMessage6, myRemoteLocation); } } arduinoPort.write(dirA); //send direction onto serialport arduinoPort.write(dirB); arduinoPort.write(pwmMotorA); arduinoPort.write(pwmMotorB);
}
//here follows the code for drawing the slider and toggle buttons on the screen but i deleted it else couldnt post the sketch
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 316
Posts: 35593
Seattle, WA USA
|
 |
« Reply #6 on: February 24, 2011, 08:09:09 pm » |
if (Serial.available() > 0) { // Check if there is a new message dirA= Serial.read(); // Put the serial input into the message dirB= Serial.read(); // Put the serial input into the message pwmMotorA = Serial.read(); pwmMotorB = Serial.read(); } Suppose Serial.available() returns 2, as in there are 2 bytes in the serial input buffer. What is going to happen when you try to read the 3rd and 4th values?
|
|
|
|
« Last Edit: February 24, 2011, 08:18:24 pm by PaulS »
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 6
|
 |
« Reply #7 on: February 25, 2011, 03:52:40 am » |
What is going to happen when you try to read the 3rd and 4th values? I dont really get what you are trying to tell me? I think its just going to put a zeros in the 3rd & 4th serial.read line? Greetings ELOLEDBALL
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 6
|
 |
« Reply #8 on: February 25, 2011, 06:41:59 am » |
hehe its me again.Thx alot Paul for the guidance. I finally got it all working smoothly!!!!. only thing i did was to start reading the serial port after 4 bytes. if (Serial.available() >= 4) { // Check if there is a new message dirA= Serial.read(); // Put the serial input into the message dirB= Serial.read(); // Put the serial input into the message pwmMotorA = Serial.read(); pwmMotorB = Serial.read();
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 316
Posts: 35593
Seattle, WA USA
|
 |
« Reply #9 on: February 25, 2011, 08:48:25 am » |
only thing i did was to start reading the serial port after 4 bytes. Yep. That's the key. Don't try to read data that isn't there. Doing so will get you all out of sync. The next step is to change the 4 values being sent to 6 values - including a start of packet marker and an end of packet marker. With these, you can tell if a packet is complete. A start of packet marker followed by 4 value that are not end of packet markers followed by an end of packet marker defines a good packet. Anything else, and that data just gets dumped.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 6
|
 |
« Reply #10 on: December 08, 2012, 12:56:23 pm » |
i also facing the same problem to let my LCD display the data
the part of the processing is
port.write('R'); port.write(RED);
port.write('G'); port.write(GREEN);
port.write('B'); port.write(BLUE);
arduino is
// call the returned value from GetFromSerial() function switch(GetFromSerial()) { case 'R': lcd.setCursor(1,0); lcd.print("R :"+ GetFromSerial());
break; case 'G'
.... .... break; ... case 'B' ... break } }
// read the serial port int GetFromSerial() { while (Serial.available()<=0) { } return Serial.read(); } the proble for me is the LCD did not display in proper ...if i remove the GetFromSerial for lcd.print("R :"+ GetFromSerial()); not do not have any problem, can i know any solution?? i need my LCD to display the RED,GREEN,BLUE value from processing thank you
|
|
|
|
« Last Edit: December 08, 2012, 12:58:42 pm by sataman »
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 316
Posts: 35593
Seattle, WA USA
|
 |
« Reply #11 on: December 08, 2012, 12:59:17 pm » |
lcd.print("R :"+ GetFromSerial()); Suppose that GetFromSerial() returns the value 12. What, exactly, do you expect adding 12 to "R :" to result in? + is NOT a concatenation operator. It is an addition operator.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 6
|
 |
« Reply #12 on: December 08, 2012, 01:07:36 pm » |
No .i put + is just to let lcd continue print the variable (RED) i want my LCD to display the data from the Processing ..'which is RED,GREEN ,and BLUE...i have no idea how LCD and display 3 in same time?
port.write('R'); port.write(RED);
port.write('G'); port.write(GREEN);
port.write('B'); port.write(BLUE);
the RED is an interger value
if work perfect in case 'R': analogWrite(9, GetFromSerial());//arduino pin 9 for LED break; the intensity of the light will change according to the RED value but i do not know why it can not work in lcd to display out the RED value i till keep give me the random char
|
|
|
|
« Last Edit: December 08, 2012, 01:19:16 pm by sataman »
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 316
Posts: 35593
Seattle, WA USA
|
 |
« Reply #13 on: December 08, 2012, 08:04:05 pm » |
but i do not know why it can not work in lcd to display out the RED value It can. You MUST use TWO statements to print the "R :" part and the value. You can NOT use the addition operator HOPING that somehow it will magically be interpreted as a concatenation operator, just because that is what you want.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 6
|
 |
« Reply #14 on: December 08, 2012, 09:07:55 pm » |
but i do not know why it can not work in lcd to display out the RED value It can. You MUST use TWO statements to print the "R :" part and the value. You can NOT use the addition operator HOPING that somehow it will magically be interpreted as a concatenation operator, just because that is what you want. thank you very much i have change the code to lcd.setCursor(1,0); lcd.print("R :"); lcd.print(GetFromSerial()); it work !!
|
|
|
|
|
Logged
|
|
|
|
|
|