Running two loops (processes) at once?

How can I run two loops or processes at the same time? I'm trying to separate two different processes so they don't interfere with each other. Here's an example:

void loop()
{
  Serial.println(analogRead(A1));
  Serial.println(press);
}

void loop()
{
  if (analogRead(A1)==1023)
  {
    press=press+1;
    delay(100);
  }
}

I'm not sure as to how I can have both of these two commands function properly at the same time, as the arduino doesn't seem to like the fact that there are two 'void loop()' statements. Thanks for the help! :)

Have a look at the blink without delay example in your IDE.

Edit: typo

  1. See AWOL’s suggestion…
  2. Understand that the Arduino has nothing like threads or processes…

What's wrong with just including the contents of both loop() functions into one:

void loop()
{
   Serial.println(analogRead(A1));
   Serial.println(press);
   if (analogRead(A1)==1023)
    {
      press=press+1;
      delay(100);
    }
}

These statements don't 'interfere' or conflict with each other, they all just excute in their turn and when done start all over.

Lefty

These statements don't 'interfere' or conflict with each other

Well, that delay() will interfere with anything else that needs to happen while it's wasting time.

Ban delay() now!

Ban delay() now!

Poor delay function, so misunderstood and misused, it just wants to have a place in the tool box for those that might better appreciate it. It’s like the old saying if all you have (or understand) is a hammer it’s amazing how everything tends to look like a nail. :wink:

Lefty

Poor delay function, so misunderstood and misused, it just wants to have a place in the tool box for those that might better appreciate it. It’s like the old saying if all you have (or understand) is a hammer it’s amazing how everything tends to look like a nail.

Can’t argue with anything there. My only comment would be that it would be nice to see the blink without delay improved a bit, and presented BEFORE the blink with delay example. It’s the first tools that are presented/learned/acquired that are the most used. Knowing first how NOT to use delay() would make people aware that delay() has side effects that can, and often must, be avoided.

PaulS and retrolefty: I took on your ‘challenge’: http://arduino.cc/forum/index.php/topic,65216.0.html

tbaldwin102 - you’re welcome to look too :slight_smile:

@Msquare: I looked. Nice job. Get it to the playground.

Hello all ,
I just wrote a bit of code that solves your problem directly. You can see this right on the Arduino reference section. But it is rather hard to find. So I can’t blame you. And I’m not. But think about making a variable.
Here’s a link to the page, and a snippet of my code.
http://www.arduino.cc/en/Reference/FunctionDeclaration

Code:

#include <Servo.h>

Servo DriveR;
Servo DriveL;
const int forwardThreshold = 30;// 12 inches

void setup()
{
Serial.begin(9600); //for debugging
DriveR.attach(9);
DriveL.attach(11);
}

void loop()
// note that this is the most basic of navigation systems… it uses a simplistic method for navigation…. needs improvement.
{
int sensor = AquireDist();
DriveR.write(360);
DriveL.write(-360);
if(sensor < forwardThreshold)
{
DriveR.write(-360);
DriveL.write(360);
delay(2500);
DriveR.write(360);
DriveL.write(360);
delay(3000);
}
}

int AquireDist() // this can be used with any digital in/out sonar type distance sensor (PWM).
{
const int sensor = 7;
int duration;
pinMode(sensor, INPUT);
//ping…(wait for the pong)
pinMode(sensor, OUTPUT);
digitalWrite(sensor, LOW);
delayMicroseconds(2);
digitalWrite(sensor, HIGH);
delayMicroseconds(5);
digitalWrite(sensor, LOW);
//listening for pong now
pinMode(sensor, INPUT);
duration = pulseIn(sensor, HIGH);//PpPpPOoOoOnnnnnnng(hopefully)
Serial.println(duration / 29 / 2 ); // for debugging
return duration / 29 / 2 ;//convert to a human number with a unit…
}

I hope this helps!

Robot_Nav_V1.pde (1.16 KB)

 DriveL.write(-360);

Is an odd way of achieving DriveL.write(0);
because

void Servo::write(int value)
{  
  if(value < MIN_PULSE_WIDTH)
  {  // treat values less than 544 as angles in degrees (valid values in microseconds are handled as microseconds)
    if(value < 0) value = 0;
    if(value > 180) value = 180;
    value = map(value, 0, 180, SERVO_MIN(),  SERVO_MAX());      
  }
  this->writeMicroseconds(value);
}

Judging from this poster’s recent posts, I think we have either:
a) a bot (unlikely)
b) a spammer
c) an idiot.
d) someone trying to bump their post stats.

It's a continuous rotation servo. The only way I know of to reverse it is to write a negative value to it. Also, that is not the point that I attempted to get across. My point was to show that you can run two functions simultaneously and continuously without the use of for( , , ). Sorry if that wasn't clear.

My point was to show that you can run two functions simultaneously and continuously without the use of for( , , ). Sorry if that wasn't clear

It still isn't clear how what you posted represents two separate tasks.

The link I showed uses this method, where int AquireDist() is the other function. It's like naming an integer/ variable, except that is the form of value it returns. I don't know if you checked the link I provided, but that was supposed to explain the code example.

The link I showed uses this method, where int AquireDist() is the other function

One of us is missing the point of what the OP was trying to do.

My take was that they were trying to run two tasks or processes concurrently (not simultaneously - that would be impossible on a single core processor), your take seems to be how to call a function consecutively.

Oh I see what you mean. Sorry about the confusion. You are absolutely correct. I am calling a function, while this post was about running another concurrent process. Now I see my mistake.