Doing few things at the same time

Here is my code:

#include <Servo.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
#include <robo.h>
#define led7 7
#define trigPin 6
#define echoPin 5
#define trigPin2 11
#define echoPin2 10
int turnnum = 0;
int turnvalue = 0;
Servo right; 
Servo left; 
int pos = 0; 
robo mrobo;
void setup ()
{
    Serial.begin(9600);
  lcd.begin(16,2);
  lcd.setCursor(0,0);
  lcd.print("----Loading-----");
  delay(1);
  lcd.setCursor(0,1);
  lcd.print("=");  
  delay(300);  
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("---->Start!<----");  
  lcd.setCursor(0,1);
  delay(700);
  lcd.print("----Arduino$----");  
  Serial.begin (5600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(trigPin2, OUTPUT);
  pinMode(echoPin2, INPUT);
  pinMode(led7, OUTPUT);
  mrobo.begin();
  randomSeed(analogRead(3));
  right.attach(2);
  left.attach(4);
}

void loop ()
{
  /** ------Explore------ **/
    {
    if (Serial.available()) {
      delay(100);
      while (Serial.available() > 0){
      }
      }
    }
  mrobo.goforward(0.1, 400);
  if (mrobo.proximity() < 28)
  {
    turnnum = random(2);
    turnvalue = random(150, 450);
    if (turnnum == 0)
    {
      mrobo.turnright(turnvalue);
    }
    if (turnnum == 1)
    {
      mrobo.turnleft(turnvalue);
    }
  }
    {
      
  /** ------Distance------ **/
      
   long duration, distance;
  digitalWrite(trigPin, LOW); 
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  Serial.print(distance); 
    if (distance > 20) {
    mrobo.goback(1, 650);
    digitalWrite(led7,HIGH);
    delay(300);
    mrobo.turnright(350);
}
  else {
    digitalWrite(led7,LOW);
  }
}
{
  
  /** ------Grabber------ **/
    
   long duration2, distance2;
  digitalWrite(trigPin2, LOW); 
  delayMicroseconds(2);
  digitalWrite(trigPin2, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin2, LOW);
  duration2 = pulseIn(echoPin2, HIGH);
  distance2 = (duration2/2) / 29.1;
  Serial.print(distance2); 
   if (distance2 < 2) {
   right.write(50);  
   left.write(180);
   }
  else {
   right.write(105); 
   left.write(130);
   delay(3000);
   right.write(50);  
   left.write(180);
  }
}
}

It’s not very tidy, so please ignore that. I need to make “Grabber” part to work at the same time as the “Explore”. Before I added “Grabber” part everything was working fine (“Explore” and “Distance” at the same time), but now works only “Distance” and “Grabber”, can’t get the “Explore” to work. How can I make all of them work at the same time? Is it because of the Delay();? Thanks in advance!

Several things at the same time

Delay() tells the arduino to sit there busy waiting and doing nothing else until the delay time is over. It's a blocking delay, as they say.

This is probably the most common programming question here and there are countless guides on how to replace delay with use of millis to track time in order to do multiple things at once. See the guide http://forum.arduino.cc/index.php?topic=223286.0 on doing multiple things at once, linked to from the useful posts thread pinned to top of this section.

UKHeliBob:
Several things at the same time

I was looking at that example, but it looks really complicated, and I'm not sure how to make it work with my code.

EDIT: ok, I found it pretty clear on this dinosaur age website: Gammon Forum : Electronics : Microprocessors : How to do multiple things at once ... like cook bacon and eggs
didn't knew this problem was so common although I google before posting. Anyway thanks for your answers.

Matasst3:
...on this dinosaur age website...

28 October 2016

Going out of your way to insult the creator of a site that was last updated yesterday where you found help strikes me as extremely childish. I feel compelled to return the favour with an equally childish act. Enjoy your timeout.

Matasst3:
I was looking at that example, but it looks really complicated,

If you explain what you find confusing or difficult I will try to help.

...R

Robin2:
If you explain what you find confusing or difficult I will try to help.

...R

I'd give it a few days. No rush.

Matasst3:
I found it pretty clear on this dinosaur age website: Gammon Forum : Electronics : Microprocessors : How to do multiple things at once ... like cook bacon and eggs

I'll take that as a compliment because the dinosaurs lasted a great deal longer than humans are likely to. :slight_smile:

Oh come on - the post on that site is 2011. The OP would have been in pre-school at the time! Why, in 2011 the (insert flavour-of-the-month language/platform/html feature) thing hadn't even been invented yet!

Kids, eh?

"I'll take that as a compliment because the dinosaurs lasted a great deal longer than humans are likely to. :)"

Time to get a new avatar.

.

Hello guys, sorry if it will be a silly question (or maybe one already asked many times); following the very useful Nick Gammon example code “blink” i see that the declaration of the functions toggleGreenLED() and toggleRedLED() are inside the setup() function, I always thought the setup function run only once at the boot time… My question is, how it can keep track of greenLEDtimer value of millis() or redLEDtimer if it run only once? Is not the loop() function the right place where to declare the toggleLED() functions?
Best regards
Marco

LarryD:
"I'll take that as a compliment because the dinosaurs lasted a great deal longer than humans are likely to. :)"

Time to get a new avatar.

.

That's exactly how I look!

alphagamma:
Hello guys, sorry if it will be a silly question (or maybe one already asked many times); following the very useful Nick Gammon example code "blink" i see that the declaration of the functions toggleGreenLED() and toggleRedLED() are inside the setup() function, =

No they are not. This is what is on my page:

void setup () 
  {
  pinMode (greenLED, OUTPUT);
  pinMode (redLED, OUTPUT);
  greenLEDtimer = millis ();
  redLEDtimer = millis ();
  }  // end of setup

void toggleGreenLED ()
  {
   if (digitalRead (greenLED) == LOW)
      digitalWrite (greenLED, HIGH);
   else
      digitalWrite (greenLED, LOW);

  // remember when we toggled it
  greenLEDtimer = millis ();  
  }  // end of toggleGreenLED

void toggleRedLED ()
  {
   if (digitalRead (redLED) == LOW)
      digitalWrite (redLED, HIGH);
   else
      digitalWrite (redLED, LOW);

  // remember when we toggled it
  redLEDtimer = millis ();  
  }  // end of toggleRedLED

They are not inside setup. I even have a comment: "// end of setup"

Ooops... I'm sorry, i hope this not offended you, it wasn't my intention.
Marco

I'm not offended. I'm just pointing out that the functions you mention are not "inside" any other functions.