Go Down

### Topic: Sending different informations to arduino with serial monitor. (Read 4219 times)previous topic - next topic

#### giacomomarelli

##### Aug 06, 2012, 01:09 pm
I'm building a robot that has to go under water. To control it from the ground, it will have a cable. My idea was to send different informations from the pc to the arduino using the serial monitor.
I wanted to send a number that identyfies  what I'm controlling and how to controll it.

For example, if I write 1030 ( intended as a decimal number) I need that the stepper motor number 1 connected to the arduino moves of 30 steps. (1030  - 1000 = 30)

If I write 2052, the stepper number 2 has to move 52 steps.

If i write 3001, the LED number 3 has  to go HIGH.

I can't figure out how to comunicate with the arduino. I tried a sketch but when I write 1030 in the serial monitor the arduino will receive four different numbers (1; 0 ; 3; 0;). Is there any way to tell the arduino that the number I am sending is the decimal number 1030?
Or maybe there's a better way to do this...?

Sorry for the grammar mistakes.

#### PaulS

#1
##### Aug 06, 2012, 01:36 pm
Quote
I tried a sketch but when I write 1030 in the serial monitor the arduino will receive four different numbers (1; 0 ; 3; 0;).

That's the way it works.

Quote
Is there any way to tell the arduino that the number I am sending is the decimal number 1030?

There are ways, but you need to send more data. How, in a stream of data that might look like "103020523001", is the Arduino supposed to know that the first number is 1030, not 103 or 10302?

You need something that defines the end of a packet, so the Arduino knows when to quit reading.

If you have start and end markers, like in "<1030><2052><3001>", then code like this:
713au
Code: [Select]
`#define SOP '<'#define EOP '>'bool started = false;bool ended = false;char inData[80];byte index;void setup(){   Serial.begin(57600);   // Other stuff...}void loop(){  // Read all serial data available, as fast as possible  while(Serial.available() > 0)  {    char inChar = Serial.read();    if(inChar == SOP)    {       index = 0;       inData[index] = '\0';       started = true;       ended = false;    }    else if(inChar == EOP)    {       ended = true;       break;    }    else    {      if(index < 79)      {        inData[index] = inChar;        index++;        inData[index] = '\0';      }    }  }  // We are here either because all pending serial  // data has been read OR because an end of  // packet marker arrived. Which is it?  if(started && ended)  {    // The end of packet marker arrived. Process the packet    // Reset for the next packet    started = false;    ended = false;    index = 0;    inData[index] = '\0';  }}`
can read and store each packet. Where the "Process the packet" comment is, make use of the data in the inData array, perhaps using atoi() to convert it to an int.

#### giacomomarelli

#2
##### Aug 08, 2012, 10:13 am
Problem solved. It works!
I found out more here: http://stackoverflow.com/questions/9012819/arduino-making-decision-according-to-a-packet-received-from-serial-port . The second solution from Fulvio works just fine.

Thanks!!

#### giacomomarelli

#3
##### Aug 08, 2012, 06:22 pm
This is what I have now:
There are two pushButton with an red LED each connected to the first arduino. When I press them the arduino serial.prints a message to the serial pins 1 and 0. The message is received from the second arduino that decides how to move the stepper motor, if I press forward the motor goes forward for 5 steps, If I press reverse the motor reverses 5 steps.

Video here http://youtu.be/LnKcP3CGFMo

Code for the Arduino n° 1 with L293D Motor shield and Stepper:
Code: [Select]
`/*Invio di comandi tramite serial monitortrovato su web + modificheGiacomo Marelli06.08.2012Controlla uno stepper. Scrivere nel serial Monitor: "<stepper,+>" o "<stepper,->".Ogni comando sposta di 5 passi (9°) in avanti (+) o indietro(-).*///inserisce le due libreri necessarie,//la prima per comunicare, la seconda per lo stepper#include <avr/pgmspace.h>#include <AFMotor.h>//variabili per comunicazione seriale#define SOP '<'#define EOP '>'bool started = false;bool ended = false;char inData[80];byte index;// Connect a stepper motor with 53 steps per revolution// to motor port #2 (M3 and M4)AF_Stepper motor(53, 2);void setup() {  Serial.begin(9600);  motor.setSpeed(300);  // 50 rpm   }void loop() {  // Read all serial data available, as fast as possible  while (Serial.available() > 0) {    char inChar = Serial.read();    if (inChar == SOP) {       index = 0;       inData[index] = '\0';       started = true;       ended = false;    } else if (inChar == EOP) {       ended = true;       break;    } else {      if (index < 79) {        inData[index] = inChar;        index++;        inData[index] = '\0';      }    }  }  // We are here either because all pending serial  // data has been read OR because an end of  // packet marker arrived. Which is it?  if (started && ended) {    // The end of packet marker arrived. Process the packet    char *cmd = strtok(inData, ",");    if (cmd) {       char *val = strtok(NULL, ",");       if (val) {          sendCommand(cmd, val);       }    }    // Reset for the next packet    started = false;    ended = false;    index = 0;    inData[index] = '\0';  }}// controllo dello stepper// usare linguaggio specifico del motorshield di ADaFRUITvoid sendCommand(char *command, char *value) {  if (strcmp(command,"stepper") == 0) {    stepper(value);  }}void stepper(char *value) {  if (strcmp(value, "+") == 0) {      motor.step(5, FORWARD, MICROSTEP);      delay(100);   }   else if (strcmp(value, "-") == 0) {      motor.step(5, BACKWARD, MICROSTEP);      delay(100);   }}`

And this is the code for Arduino n° 2, with 2 pushButtons.

Code: [Select]
`/*Giacomo Marelli06.08.2012sketch per arduino di comandoinvia tramite seriale il comando "<stepper,+>" o "<stepper,->"il secondo arduino riceve i comandi e li eseguemuovendo il motore stepper*/int ledForw = 6;int ledRev = 19;int buttonForw = 4;int buttonRev = 5;int time = 200;void setup(){  Serial.begin(9600);  pinMode( ledForw, OUTPUT);  pinMode( ledRev, OUTPUT);  pinMode( buttonForw, INPUT);  pinMode( buttonRev, INPUT);  Serial.print("<stepper,+>"); // tests the stepper  delay(200);  Serial.print("<stepper,->");  delay(1000);}void loop(){  if (digitalRead(buttonForw) == HIGH)  {    digitalWrite(ledForw, HIGH);    Serial.print("<stepper,+>");    delay(time);     }    else (digitalRead(buttonForw) == LOW);  {    digitalWrite(ledForw, LOW);  }          if (digitalRead(buttonRev) == HIGH)  {    digitalWrite(ledRev, HIGH);    Serial.print("<stepper,->");    delay(time);    }    else (digitalRead(buttonRev) == LOW);  {    digitalWrite(ledRev, LOW);  }}`

#### PaulS

#4
##### Aug 08, 2012, 06:59 pm
For the next challenge, then, change the switch actions on the 2nd Arduino to send
Serial.print("<stepper,-10>");
or
Serial.print("<stepper,+10>");
and have the first Arduino properly handle the +NN or -NN values. The atoi() function can be used.

#### bsiswoyo

#5
##### Aug 17, 2012, 12:13 pmLast Edit: Aug 17, 2012, 12:24 pm by bsiswoyo Reason: 1

I'm building a robot that has to go under water. To control it from the ground, it will have a cable. My idea was to send different informations from the pc to the arduino using the serial monitor.
I wanted to send a number that identyfies  what I'm controlling and how to controll it.

For example, if I write 1030 ( intended as a decimal number) I need that the stepper motor number 1 connected to the arduino moves of 30 steps. (1030  - 1000 = 30)

If I write 2052, the stepper number 2 has to move 52 steps.

If i write 3001, the LED number 3 has  to go HIGH.

I can't figure out how to comunicate with the arduino. I tried a sketch but when I write 1030 in the serial monitor the arduino will receive four different numbers (1; 0 ; 3; 0;). Is there any way to tell the arduino that the number I am sending is the decimal number 1030?
Or maybe there's a better way to do this...?

Sorry for the grammar mistakes.

You must send as string format from PC to Arduino. Then, the arduino board must exctract the string into you action.

OR

You can send as string from PC by comma (',') space. On arduino you can use a statemet of 'Serial.parseInt". This mean arduino will waiting the sequence string space by comma.

Similar this visit:
http://bsiswoyo.lecture.ub.ac.id/2012/08/arduino-tutorial-how-to-turn-onoff-8-led-using-visual-basic/

Go Up