Hi, i' m trying to simulate a robot' s movement on a 2d array. In my case, robot goes forward firstly, if it sees a engel, it turns left and continues its movement. The problem is that, i could not present turning left and moving on left-direction on the initial matrix.
Here' s my code:
#include <Servo.h>
#define x 7 //Matrix dimensions
#define y 7
const int trigPin = 12;
const int echoPin = 11;
Servo myServo; //variable definations
Servo myServo2;
int pos = 0;
int counter;
int counter1 = 0;
int counter2;
int counter3;
int integer;
int integer2;
int integer4;
long duration;
long distance;
void setup() {
Serial.begin(9600);
myServo.attach(9);
myServo2.attach(10);
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
}
void loop() {
int my2dArray[x][y] = { // initial matrix
{1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 0, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1, 1},
};
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
int robot = my2dArray[3][3]; // first place of robot
if (counter1 > 0 ) {
for (integer = 0; integer < counter1; integer += 1) {
if (distance < 10) { //seeing the object
for (pos = 0; pos <= 180 ; pos +=1){ //turning left
myServo.write(pos);
}
for (pos = 0; pos <= 180 ; pos += 1) { //moving on after turning
myServo.write(pos);
myServo2.write(pos);
if (pos == 180) {
counter3 += 1;
}
}
Serial.println(integer);
for (integer4 = 0; integer4 < counter3; integer4 += 1){ //modifying matrix if there is an object
my2dArray[3 + integer][3+integer4] = robot * my2dArray[3 + integer][3+integer4];
}
Serial.println("Break condition.");
break;
}
else {
my2dArray[3 + integer][3] = robot * my2dArray[3 + integer][3]; //modifying matrix in case of there is no object
}
}
}
for (integer2 = 0; integer2 < 7; integer2++) { //displaying matrix
Serial.print(my2dArray[integer2][0]); Serial.print("|");
Serial.print(my2dArray[integer2][1]); Serial.print("|");
Serial.print(my2dArray[integer2][2]); Serial.print("|");
Serial.print(my2dArray[integer2][3]); Serial.print("|");
Serial.print(my2dArray[integer2][4]); Serial.print("|");
Serial.print(my2dArray[integer2][5]); Serial.print("|");
Serial.println(my2dArray[integer2][6]);
}
Serial.println();
for (pos = 0; pos <= 180 ; pos += 1) { // moving forward as first movement
myServo.write(pos);
myServo2.write(pos);
if (pos == 180) {
counter1 += 1;
}
}
delay(1000);
}
The problem part is:
for (integer4 = 0; integer4 < counter3; integer4 += 1){ //modifying matrix if there is an object
my2dArray[3 + integer][3+integer4] = robot * my2dArray[3 + integer][3+integer4];
}
"integer" value does not change in the problem part, it' s always 0 in despite of the first for loop. How can i pass the "integer" to the problem part properly?