Hi guys,
i managed to send single numbers to my Arduino using Processing but sadly
when i try to send for example "1200" it wont work. Can someone help me?
Processing Code:
import processing.serial.*;
Serial ComPort;
String input[];
void setup(){
String portName = Serial.list() [0];
ComPort = new Serial(this, portName, 9600);
ComPort.bufferUntil('\n');
input = loadStrings("Website.txt");
if(input.length != 0){
String s_current = input[0];
int current = Integer.parseInt(s_current);
println(current);
delay(2000);
ComPort.write(current);
}
}
Arduino Code:
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 1);
void setup()
{
Serial.begin(9600); //set up serial input
Serial.println("Website control start");
AFMS.begin();
myMotor->setSpeed(10);
}
void loop()
{
if (Serial.available() > 0)
{
int key = Serial.read();
if(key ==1) // i wanna change this key to 1200
{
Serial.println("Motor forward");
myMotor->step(1000, FORWARD, DOUBLE);
delay(0);
}
else if(key==2) // i wanna change this key to 1205
{
Serial.println("Motor backward");
myMotor->step(1000, BACKWARD, DOUBLE);
delay(0);
}
}
}
Alan0
March 6, 2016, 12:50pm
2
Hi asgar434,
Try:
ComPort.write(current+".');
Regards Alan0
Alan0
March 6, 2016, 12:56pm
3
Try again:
ComPort.write(current+".");
Alan0
Change this on the Arduino side:
if (Serial.available() > 0)
to
if (Serial.available() > 3) // 4 bytes arrived
then read the 4 bytes and check if 1,2,0,0 or perhaps '1', '2', '0', '0' were received.
Right now you are only reading 1 byte with
int key = Serial.read();
Alan0
March 6, 2016, 1:39pm
6
Next, try this in the Arduino code:
int key = Serial.parseInt();
(with the ComPort.write(current+".")
Regards Alan0
@Crossroads (btw your name reminds me of a certain game)
I changed it but I can just run processing or view the serial monitor of the Arduino (same port).
How can check what the Arduino received?
@AlanO
int key = Serial.parseInt(); worked!
finally! thanks!
But i also try to understand this so what does this change?
int key = Serial.parseInt();
ComPort.write(current+".")
Robin2
March 6, 2016, 2:31pm
8
You may be interested in the examples in Serial Input Basics - simple reliable ways to receive data without blocking. Serial.parseInt() is a blocking function.
The technique in the 3rd example would be most reliable.
There is also a parse example.
...R
Hi Robin2,
atm iam working myself through the examples.
I love the format its really easy to follow but some stuff is quite hard for me to crasp.
Lets see if i can learn how to implement the technique CrossRoads recommended.
Greetings
Hi guys,
i managed with your help to send 4 Numbers to my arduino.
But i switched to a new pc and suddenly i doesnt work anymore. It also does not work with 1 single
Number and that was no problem at all in the past. Any clue what could be the problem?
@Robin2
So you think the reason it doesnt work is because how the code i written is not working very well
and produces that way trouble?
Here is the Code that worked on the other pc.
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 1);
void setup()
{
Serial.begin(9600); //set up serial input
Serial.println("Website control start");
AFMS.begin();
myMotor->setSpeed(20);
}
void loop()
{
if (Serial.available() > 3)
{
int key = Serial.parseInt();
if(key ==1200)
{
Serial.println("Motor forward");
myMotor->step(1000, FORWARD, DOUBLE);
delay(0);
}
else if(key==1205)
{
Serial.println("Motor backward");
myMotor->step(1000, BACKWARD, DOUBLE);
delay(0);
}
}
}
Processing:
import processing.serial.*;
Serial ComPort;
String input[];
void setup(){
String portName = Serial.list() [0];
ComPort = new Serial(this, portName, 9600);
ComPort.bufferUntil('\n');
input = loadStrings("Website");
if(input.length != 0){
String s_current = input[0];
int current = Integer.parseInt(s_current);
delay(10000);
println(current);
ComPort.write(current+".");
}
}
Robin2
March 25, 2016, 2:59pm
11
As I said before, I think you need a more reliable way to receive data.
I don't know why it should work with one PC and not another. What is the difference between the PCs or the software on them that you are using?
Have you uploaded the Arduino program from the new PC or are you still using the code that was uploaded with the first PC.
...R
I read your "serial input basics" alot but could not figure out how to make it work.
"i dont know why it should work with one PC and not another" Thats why iam massively confused
I think it would help me if i could somehow see what my Arduino recieves from Processing.
Maybe there was a Software update i missed maybe processing.
Iam still using the Code that worked on the old pc.
I installed everything on the new pc and uploaded the old code that was working(on my old pc) from my new pc to the arduino.
But nothing works. Not even 1 number. Thats what i find really strange.
I can test the motor so there is no problem.
iam quite stuck
Oh and i forgot. The only diffrence is that the old pc is a laptop and the new one a desktop pc.
Windows is same. But iam not quite sure if the arduino and processing is the same Software.
Robin2
March 25, 2016, 5:37pm
14
asgar434:
I read your "serial input basics" alot but could not figure out how to make it work.
...SNIP...
I think it would help me if i could somehow see what my Arduino recieves from Processing .
Try the 2nd example in Serial Input Basics
If you don't tell me what you could not figure out I can't help.
...R
I found the problem!
Somehow there was a COM2 active (communication port) that was blocking processing from sending stuff. Deactivating it solved my problem. So everything is working fine now on the new pc.
But i wanna learn to use your way of sending. (2nd example in Serial Input Basics)
The problem i have is that i have no clue how to change my arduino code with it and if i need to change something at the processing code. At the moment iam still using parseInt on the arduino side.
Robin2
March 26, 2016, 11:37pm
16
asgar434:
But i wanna learn to use your way of sending. (2nd example in Serial Input Basics)
The problem i have is that i have no clue how to change my arduino code with it and if i need to change something at the processing code. At the moment iam still using parseInt on the arduino side.
The best way to do that is to make backup copies of the working Arduino and Processing programs so you can always go back to them.
Then have a go at a new version including the new techniques. If you run into a problem post the code here and we will try to help.
...R
So this is my try on the Arduino side.
Doesnt work but i think there are many thinks wrong.
On the Processing side i really dont know what to change.
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_MS_PWMServoDriver.h"
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_StepperMotor *myMotor = AFMS.getStepper(200, 1);
const byte numChars = 32;
char receivedChars[numChars];
boolean newData = false;
void setup() {
Serial.begin(9600);
Serial.println("<Arduino is ready>");
AFMS.begin();
myMotor->setSpeed(20);
}
void loop() {
recvWithEndMarker();
showNewData();
{
if (Serial.available() > 3)
{
int key = Serial.read();
if(key==1200)
{
Serial.println("Motor forward");
myMotor->step(1000, FORWARD, DOUBLE);
delay(0);
}
else if(key==1205)
{
Serial.println("Motor backward");
myMotor->step(1000, BACKWARD, DOUBLE);
delay(0);
}
}
}
}
void recvWithEndMarker() {
static byte ndx = 0;
char endMarker = '\n';
char rc;
// if (Serial.available() > 0) {
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
newData = true;
}
}
}
void showNewData() {
if (newData == true) {
Serial.print("This just in ... ");
Serial.println(receivedChars);
newData = false;
}
}
Old working processing code:
import processing.serial.*;
Serial ComPort;
String input[];
void setup(){
String portName = Serial.list() [0];
ComPort = new Serial(this, portName, 9600);
ComPort.bufferUntil('\n');
input = loadStrings("http://food-bot.co.nf/1200.txt");
if(input.length != 0){
String s_current = input[0];
int current = Integer.parseInt(s_current);
delay(10000);
println(current);
ComPort.write(current+".");
}
}
Robin2
March 27, 2016, 8:20pm
18
Why do you still have code with int key = Serial.read(); when you are using the function recvWithEndMarker() to get the data?
You can't do it both ways.
Are you seeing the correct data printed out by showNewData()?
...R
Yes i see the correct data.
So recvWithEndMarker(); is replacing key = Serial.read(); ?
How do i change that?
Robin2
March 28, 2016, 8:16pm
20
asgar434:
How do i change that?
I'm tired at the moment and my best advice is to spend some time studying my examples and thinking about how you can use them.
Put very simply, use my code to put the value in the variable key (or use the place my code puts the data inplace of the the variable key
...R