Hello, i am having an issue with my code, i have the 1.5.6 software and an uno r3, when i run this code it should print out a 2d array that is completely zeros, but for some reason i get 17 rows of 0’s, than a row of 457, and it goes back and forth like that, once you are like 10 lines form the end it makes completely random numbers.
After that i try to make the part of the array [0][1 ] equal to 9, it makes the whole row 9, any suggestions on ho to fix these 2 problems?
#include <SPI.h>
#include <Servo.h>
const int ultra3 = 4;
const int ultra = 7;
const int ultra2 = 6;
const int in = 5;
const int in2 = 3;
const int in3 = 8;
Servo myservo;
const int MAX_PAGENAME_LEN = 8;
char buffer[MAX_PAGENAME_LEN + 1];
unsigned int depth = 0;
unsigned int width = 0;
unsigned int height = 0;
int array[0][0];
void setup()
{
randomSeed(analogRead(0));
pinMode(in, INPUT);
pinMode(in2, INPUT);
pinMode(ultra, OUTPUT);
pinMode(ultra2, OUTPUT);
Serial.begin(9600);
myservo.attach(9);
delay(1000);
createroom();
Serial.println(width);
Serial.println(height);
zero(width, height); //zeros out array
}
void loop()
{
zero(width, height);
zero(width, height);
Serial.println("starting");
for (int p = 0; p < height; p++) {
for (int i = 0; i < width; i++) {
Serial.print(array[i][p]);
Serial.print("|");
}
Serial.println();
}
array[0][1] = 9;
Serial.println("start mid");
for (int p = 0; p < height; p++) {
for (int i = 0; i < width; i++) {
Serial.print(array[i][p]);
Serial.print("|");
}
Serial.println();
}
}
void zero(int j, int k) {
array[width][height];
for (int i = 0; i < height; i++) {
int u = 0;
for (int p = 0; p < width; p++) {
array[i][p] = 0;
}
}
}
void createroom() { //test
int j = random(1, 50); //pointing left
int i = random(1, 50); //pointing straight
int u = random(1, 50); //pointing up
int q = random(1, 50); //pointing down
int l = random(1, 50); //pointing straight
int h = random(1, 50); //pointing right
depth = u + h;
height = i + l;
width = j + h;
}
void createRoom() { //creates the data neccesary for to form the room
int j = irRead(in, ultra); //pointing left
int i = irRead(in2, ultra2); //pointing straight
int u = irRead(in3, ultra3); //pointing up
int q = irRead(in3, ultra3); //pointing down
myservo.write(0);
int l = irRead(in, ultra); //pointing straight
int h = irRead(in2, ultra2); //pointing right
depth = u + h;
height = i + l;
width = j + h;
myservo.write(90);
}
int irRead(int readPin, int triggerPin)
{
int halfPeriod = 13; //one period at 38.5khZ is aproximately 26 microseconds
int cycles = 38; //26 microseconds * 38 is more or less 1 millisecond
int i;
for (i = 0; i <= cycles; i++)
{
digitalWrite(triggerPin, HIGH);
delayMicroseconds(halfPeriod);
digitalWrite(triggerPin, LOW);
delayMicroseconds(halfPeriod - 1); // - 1 to make up for digitaWrite overhead
}
return digitalRead(readPin);
}