Pages: [1]   Go Down
Author Topic: Sending different informations to arduino with serial monitor.  (Read 1692 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49365
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!!
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
/*
Invio di comandi tramite serial monitor
trovato su web + modifiche
Giacomo Marelli
06.08.2012

Controlla 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 ADaFRUIT
void 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:
/*
Giacomo Marelli
06.08.2012

sketch per arduino di comando
invia tramite seriale il comando "<stepper,+>" o "<stepper,->"
il secondo arduino riceve i comandi e li esegue
muovendo 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);
  }
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 614
Posts: 49365
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

Indonesia
Offline Offline
Newbie
*
Karma: 0
Posts: 4
willing to share knowledge to anyone is an expression of love for all mankind
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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/
« Last Edit: August 17, 2012, 05:24:13 am by bsiswoyo » Logged

Bambang Siswoyo

Pages: [1]   Go Up
Jump to: