loading
 
Get 2 variables from Serial to arduino?
Anonymous
4 years
Hey guys,

Need some help, i got my robot to move using COGX to track left and right movement. Now i need to use Box_SIZE to know how far away the item is so the robot will move until the box_size hits a certain number. Right now im using the serial module to send the serial to my arduino which controller my motors using a motor driver. I need to know how to send 2 variables now. both cog_x and box_size. Here is my code so far.

int ENA=5;//connected to Arduino's port 5(output pwm)
int IN1=10;//connected to Arduino's port 2
int IN2=3;//connected to Arduino's port 3
int ENB=6;//connected to Arduino's port 6(output pwm)
int IN3=11;//connected to Arduino's port 4
int IN4=9;//connected to Arduino's port 7
int rled = 13;
int lled = 12;

byte distance;
void setup(void)
{
Serial.begin(9600); // begin serial communication
pinMode(rled,OUTPUT);
pinMode(lled,OUTPUT);
pinMode(ENA,OUTPUT);//output
pinMode(ENB,OUTPUT);
pinMode(IN1,OUTPUT);
pinMode(IN2,OUTPUT);
pinMode(IN3,OUTPUT);
pinMode(IN4,OUTPUT);
digitalWrite(ENA,LOW);
digitalWrite(ENB,LOW);//stop driving
digitalWrite(IN1,HIGH);
digitalWrite(IN2,LOW);//setting motorA's directon
digitalWrite(IN3,HIGH);
digitalWrite(IN4,LOW);//setting motorB's directon
}

void loop(void)
{
   if (Serial.available()>0)
   {
      while(Serial.available()>0)
      {
         distance = Serial.read(); // read in the next byte
  
          if(distance > 0 && distance < 50)
          {
            left();
          }
          else if (distance > 100)
          {
            right();
          }
          else if(distance > 49 && distance < 101)
         {
           brake();
         }
         else if (distance == 0 || distance == -1)
         {
          brake();
         }
      }
   }

}

void brake()
{
     digitalWrite(lled,HIGH);
   digitalWrite(rled,HIGH);
analogWrite(ENA,LOW);
analogWrite(ENB,LOW);
}


void left()
{
     digitalWrite(lled,HIGH);
   digitalWrite(rled,LOW);
  analogWrite(ENA,150);
}

void right()
{
     digitalWrite(rled,HIGH);
   digitalWrite(lled,LOW);
  analogWrite(ENB,150);
}
Steven Gentner from United States  [1370 posts] 4 years
You can do this several ways, the easiest is just to use an IF_Statement module in RR to set your distance to zero based on the size of the BOX_SIZE variable ... then when 0 gets sent to the MCU it will stop.

The other possibility is to modify the Serial module to send both variables and instead use

distance = Serial.read(); // read in the next byte
box = Serial.read(); // read in the next byte

which will work as long as the box size is < 256. If it can get larger, you can use \\[COG_BOX_SIZE] in the serial module and

box1 = Serial.read(); // read in the next byte
box2 = Serial.read(); // read in the next byte
box3 = Serial.read(); // read in the next byte
box4 = Serial.read(); // read in the next byte
box = box1 | (box2<<8) | (box3<<16) | (box4<<24)

assuming you declared box as long. Alternatively, just use

\[COG_BOX_SIZE/4]

which would reduce the allowed range by a factor of four since you probably don't need the resolution. This would then compact 0 to 1024 into the range of 0 to 255. Just be sure to *4 on the MCU size if you want the actual size.

You can download the Sketch in the Sparkfun Arduino or Communicator modules to see more processing programming as examples.

STeven.
Anonymous 4 years
steven thank you so much, your a very big help.

Really appreciate it

This forum thread has been closed due to inactivity (more than 4 months) or number of replies (more than 50 messages). Please start a New Post and enter a new forum thread with the appropriate title.

 New Post   Forum Index