Arduino won't print out values [solved]

Hey guys!

I'm having a problem with a part of my Arduino project. So in my project, 2 ultrasonic sensors are scanning the area and the values of every scan are inserted into an Array, but somehow whatever I try It won't show me the values. It's like Arduino can't see the "for" that should do the trick. Can someone explain me what I'm doing wrong? I'm still fairly new with Arduino, so I can't see what's wrong with it.

Here's the code:

#include <Servo.h>
#include <HCSR04.h>
#include <Wire.h>

Servo servoL; 
Servo servoR;  
HCSR04 leftSensor(11, 10); 
HCSR04 rightSensor(6, 9);

int posLeft = 0;    
int posRight = 0;
int EchoL = 10;
int TrigL = 11;
int EchoR = 9;
int TrigR = 6;
int cmL; 
long timeL; 
int cmR; 
long timeR; 

int myDistance[28];

void leftScan(int i);
void rightScan(int i);

void setup() {

  Serial.begin(9600);
  servoL.attach (5);  // Przypisanie pinu 5 do objektu servoL
  servoR.attach (3);  // Przypisanie pinu 3 do objektu servoR

  pinMode(EchoL, INPUT);
  pinMode(EchoR, INPUT);
  pinMode(TrigL, OUTPUT);
  pinMode(TrigR, OUTPUT);

}

void loop() {
  //Serial.println( leftSensor.dist() );

  int i = 0;
  int j = 0;

  for (posLeft = 0; posLeft < 90; posLeft += 5) {

    servoL.write(posLeft);              
    delay(300);   
    leftScan(i);
    i++;



    if (posRight > 0 ) {
      posRight -= 5;
      servoR.write(posRight);
    }

  }




  for (posRight = 0; posRight < 90; posRight += 5) {

    servoR.write(posRight);              
    delay(300);                        
    rightScan(i);
    i++;



    if (posLeft > 0 ) {
      posLeft -= 5;
      servoL.write(posLeft);
    }
  }

  //Serial.print("test"); // this worked 
  for(j = 0; j < 28; j++) {
    //Serial.availableForWrite();
    Serial.println("Distance:");
    Serial.println(myDistance[j]);
     Serial.print("test");
    }
//delay(1000);

}

void leftScan(int i) {
  digitalWrite(TrigL, LOW);
  delayMicroseconds(2);
  digitalWrite(TrigL, HIGH);
  delayMicroseconds(15);
  digitalWrite(EchoL, HIGH);
  timeL = pulseIn(EchoL, HIGH);
  cmL = timeL / 58;
  myDistance[i] = cmL;

}

void rightScan(int i) {
  digitalWrite(TrigR, LOW);
  delayMicroseconds(2);
  digitalWrite(TrigR, HIGH);
  delayMicroseconds(15);
  digitalWrite(EchoR, HIGH);
  timeR = pulseIn(EchoR, HIGH);
  cmR = timeR / 58;
  myDistance[i] = cmR;

}

I appreciate any hints :smiley:

How many 5 degree steps in 90 degree (twice)?
How many elements in your array?

Okay, I just realized, how dumb I was, because, it scans each step starting from 0 degrees and finishing on the scan at 90 degrees. so in total there's 19 steps. Since I want to insert the values from both sensors into 1 array, which leaves me at 38 and just after your question, I realized, that I made a typo and the for works!

So edit the thread title and add [solved].

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.