Code for letting the robot start the loop

Hello, is there any way you can code so that when something happen the robot starts read the program(the loop) but before it the robot just stands still? (So it starts the loop)

Example: the robot stands still, then it recognizing an object and the rest of the program (in the loop) starts running.

I have been thinking if the code could be something like this:

boolean run,stop;

void setup()

{
Run=False

Stop=False
      
while ((buzzerOn)  // or while (distance>x) 
    
Run=True

}

void loop() 
{

if (Run=True)

// rest of the code

}

Hello, is there any way you can code so that when something happen the robot starts read the program(the loop) but before it the robot just stands still? (So it starts the loop)

At the end of setup(), use a while loop to read the state of a switch. As long as the state is not pressed, stay in the while loop. If the state is pressed, use break; to exit the loop, allowing setup() to end and loop() to be called in a loop.

void setup()
{
  while (!startCondition)
  {
    //test for start condition and set startCondition to true when it occurs
  }
}

void loop()
{
  //main code goes here
}

PaulS:
At the end of setup(), use a while loop to read the state of a switch. As long as the state is not pressed, stay in the while loop. If the state is pressed, use break; to exit the loop, allowing setup() to end and loop() to be called in a loop.

could you give a quick example of the program?

could you give a quick example of the program?

Sure. Which pin is the switch attached to? How is the switch wired?

If the switch is connected to digital pin 7 and ground:

   // Add this to the end of setup()
   pinMode(7, INPUT_PULLUP);

   while(digitalRead(7) == HIGH)
   {
      // Do nothing
   }

If you use a different pin, change both 7s. If you use a different wiring scheme, you are on your own.

PaulS:
Sure. Which pin is the switch attached to? How is the switch wired?

If the switch is connected to digital pin 7 and ground:

   // Add this to the end of setup()

pinMode(7, INPUT_PULLUP);

while(digitalRead(7) == HIGH)
  {
      // Do nothing
  }




If you use a different pin, change both 7s. If you use a different wiring scheme, you are on your own.

What I have is a distance sensor on port 3 and on the arduino board a buzzer (dont know if I can use that for this problem?)

But I tried this code, can you see what is wrong with it? Now when I start the program both motors start running immediately even though the distance have not been less then 40 witch is my while condition.( I dont have these pins because the board is from makeblock and you just plug in rj25 contact but i set sensor on port 3) here is the board:
http://www.makeblock.cc/me-orion-base-on-arduino-uno/

MeDCMotor MotorR(M1);  

MeDCMotor MotorL(M2);

MeUltrasonicSensor UltrasonicSensor(PORT_3);

boolean kor,stanna;

int distance=0;

void setup() {
  
// put your setup code here, to run once:
kor=false;
stanna=false;
distance = UltrasonicSensor.distanceCm();

while (distance<40) 
{
  kor=true;}

}

void loop() {
  
// put your main code here, to run repeatedly:
 if (kor=true)
 {
 MotorL.run(50);
  MotorR.run(100);
 }
}
while (distance<40)
{
  kor=true;}

}

If the value in distance is less than 40, this while loop will never end. You MUST read the distance inside the while body if you expect the while loop to ever end.

while (distance<40) 
{
  distance = UltrasonicSensor.distanceCm();
  kor=true;
  }
}

I put that distance inside but when i start the robot the motors still start running, even though the distance is more the 40.

I want the robot to be still when I start it, and when the sensor measures the distance to be less then 40, kor becomes true (and then kor is true forever) and when kor is true the void loop begins. and the motor starts running.

Please post your complete program so that we can see how variables are declared.
Have you tried printing distance to that you can see the value returned by UltrasonicSensor.distanceCm();

 if (kor=true)

= is an assignment operator.

== is the equality operator.

UKHeliBob:
Please post your complete program so that we can see how variables are declared.
Have you tried printing distance to that you can see the value returned by UltrasonicSensor.distanceCm();

This is the complete code:

#include <Makeblock.h>
#include <Arduino.h>
#include <SoftwareSerial.h>
#include <Wire.h>

MeDCMotor MotorR(M1);  
MeDCMotor MotorL(M2);
MeUltrasonicSensor UltrasonicSensor(PORT_3);
boolean kor,stanna;
int distance=0;

void setup() {
  // put your setup code here, to run once:
kor=false;
stanna=false;
distance = UltrasonicSensor.distanceCm();
while (distance<40) 
{
  distance = UltrasonicSensor.distanceCm();
  kor==true;
  }

}

void loop() {
  // put your main code here, to run repeatedly:
 if (kor==true)
 {
 MotorL.run(50);
  MotorR.run(100);
 }
}

PaulS:

 if (kor=true)

= is an assignment operator.

== is the equality operator.

I changed in the while so kor == and in the if as you can se below but now the robot just stands still all the time

this is my complete program now:

#include <Makeblock.h>
#include <Arduino.h>
#include <SoftwareSerial.h>
#include <Wire.h>

MeDCMotor MotorR(M1);  
MeDCMotor MotorL(M2);
MeUltrasonicSensor UltrasonicSensor(PORT_3);
boolean kor,stanna;
int distance=0;

void setup() {
  // put your setup code here, to run once:
kor=false;
stanna=false;
distance = UltrasonicSensor.distanceCm();
while (distance<40) 
{
  distance = UltrasonicSensor.distanceCm();
  kor==true;
  }

}

void loop() {
  // put your main code here, to run repeatedly:
 if (kor==true)
 {
 MotorL.run(50);
  MotorR.run(100);
 }
}

  distance = UltrasonicSensor.distanceCm();I would be interested in seeing the value of distance printed so that it could be checked.

  kor==true;

That should be

  kor = true;

PaulS:

  kor==true;

That should be

  kor = true;

yeah I think it works now but it only works sometimes for some reason, sometime when i reset it wont notice anything and stand still so you have to reset the robot again :S

Why do you need the kor variable ?
Once the program gets into the loop() function kor will always be true so why bother testing its value ?

UKHeliBob:

  distance = UltrasonicSensor.distanceCm();

I would be interested in seeing the value of distance printed so that it could be checked.

I think the code work now, but it would be interesting to see it, how can I see how it measures?

how can I see how it measures?

Serial.print("distance measured: ");
Serial.println(distance);

PaulS:

  kor==true;

That should be

  kor = true;

Paul could you take a quick look at this image with the full code:
Imgur

It just seems to work sometimes with the while loop.

could you take a quick look at this image with the full code

No, for two reasons. First is that you did not post a link. I'm tired of the gyrations needed to look at a URL that is not posted properly. It is really simple to use the link icon to post a link. It's as simple as using the code icon to post code.

Second, I can't access most image hosting web sites since they might contain porn. Or viruses.

It just seems to work sometimes with the while loop.

I recognized each and every word. The sentence makes no sense without the code, though.