Arduino and Matlab

Hi there,

I originally took the project from internet (Ultrasonic map-maker), but it was using 1 ultrasonic sensor and I aim to use 2 ultrasonic sensors and therefore scan 360 degrees. Here is the arduino code

#include <Servo.h>
#include <NewPing.h>

#define TRIGGER_PIN  12 
#define ECHO_PIN     13  
#define TRIGGER_PIN1 5  
#define ECHO_PIN1    4  
#define MAX_DISTANCE 350 

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
NewPing sonar1(TRIGGER_PIN1, ECHO_PIN1, MAX_DISTANCE);
Servo myservo;  

int pos = 0;   
int times;
int servoPin = 11;

void setup() {
  myservo.attach(servoPin); 
  Serial.begin(9600);
  delay(2000);
}

void loop() {
  int times = 0;
  int a = 0;
  int b = 0;

  
  for (int i = 0; i<180; i++){
    unsigned int uS = sonar.ping();
    unsigned int uS1 = sonar1.ping();
    
    myservo.write(i);
    delay(5);
    
    times = 10;
    for (int j=0; j<times; j++){
      uS = sonar.ping();
      a = uS/US_ROUNDTRIP_CM + a;
      delay(10);
      
      uS1 = sonar1.ping();
      b = uS1/US_ROUNDTRIP_CM + b;
      delay(10);
    }
    //take average
    a = a/times;
    if(a>350){  a=350;  }
    if(a<3){  a=3;  }
    
    b = b/times;
    if(b>350){  b=350;  }
    if(b<3){  b=3;   }
       
    Serial.println(a); 
    delay(30);
    Serial.println(b); 
  }

  delay(5000);
    
}

I have a problem with matlab it is giving an errors which are;

Subscript indices must either be real positive integers or logicals.
Error in pro (line 12)

  • num1(i) = str2num(A);*

Here is my matlab code.

delete(instrfindall);

theta = 0:(pi/180):pi;

sPort = serial('COM9','BaudRate',9600,'DataBits',7);

fopen(sPort);

i = 0;
while (i<180)
   A = fgets(sPort);
   num1(i) = str2num(A);
   B = fgets(sPort);
   num2(i) = str2num(B); 
   i = i+1;
end
fclose(sPort);
delete(sPort);

j = 0;
unit = 1;
while j<180
    
    tab1(j,1) = (j)*unit
    tab1(j,2) = num1(j)
    tab1(j,3) = num1(j)*cosd((j)*unit)
    tab1(j,4) = num1(j)*sind((j)*unit)
    
    tab2(j,1) = (j)*unit    
    tab2(j,2) = num2(j)    
    tab2(j,3) = num2(j)*cosd((j)*unit)    
    tab2(j,4) = num2(j)*sind((j)*unit)
    
    j = j+1

end


plot(tab1(:,3),tab1(:,4))
plot(tab2(:,3),tab2(:,4))

I have a problem with matlab

Asking questions about your bicycle at a car dealer is usually pointless. Why are you asking about Matlab here?