run two loop codes at the same time / hold servo's angle

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
apllike
 
Posts: 15
Joined: Fri Jul 11, 2014 7:18 pm

run two loop codes at the same time / hold servo's angle

Post by apllike »

Hi,

I'm trying to make a something with one flora, two TSL2561 light sensors and two micro servos. Each light sensor pairs up with a single micro servo, so based on the brightness of surroundings, each servo rotates to the different angle.

Specifically, the light sensor 1 controls micro servo 1 based on its light input, and light sensor 2 controls servo 2 in the same way. I've succeeded in making it happened, but still have some problems that I can't solve. Here below are the part of the code that I wrote and further questions.

Code: Select all

void loop(void) 
{  
  // sensor 1 + servo 1 Event

  sensors_event_t event;
  tsl1.getEvent(&event);

  if (event.light)
  {
    Serial.print(event.light); Serial.println(" lux1");
  }
  else
  {
    Serial.println("Sensor overload");
  }
  
  if (event.light > 50) {
    servo1.slowmove(150, servo_speed);
    delay(2000);
  }
  
  if (event.light > 100) {
    servo1.slowmove(180, servo_speed);
    delay(2000);
  }

  else {
    servo1.write(servo_default);
  }
  
  //  sensor 2 + servo2 Event
  
  tsl2.getEvent(&event);
 
  if (event.light)
  {
    Serial.print(event.light); Serial.println(" lux2");
  }
  else
  {
    Serial.println("Sensor overload");
  }
  delay(250);
  
    if (event.light > 50) {
    servo2.slowmove(150, servo_speed);
    delay(2000);
  }
  
  if (event.light > 100) {
    servo2.slowmove(180, servo_speed);
    delay(2000);
  }

  else {
    servo2.write(servo_default);
  }

}
Q1. The pair of light sensor 1 and the servo 1 stops working while the pair of the light sensor2 and servo2 are working. It seems like the code for the pair of light sensor 2 and servo 2 is interrupted by the code for the pair of light sensor 1 and servo 1. How can I run two different sets of loop codes at the same time? In this case, I want sensors to detect surroundings simultaneously and servos to be rotated by that as well.

2. How can I pause servo on the most current position when the sensor maintains the same input?
I haven't figure out how to solve this problem for a long time, but I really want to know the solution. Even if I apply a quite long delay, servo continues to go back to the default position right after reaching to certain position. So, servo's movement looks really jumpy and unstable. I want to hold servo's angle if the input from the sensor doesn't change.

User avatar
Franklin97355
 
Posts: 23912
Joined: Mon Apr 21, 2008 2:33 pm

Re: run two loop codes at the same time / hold servo's angle

Post by Franklin97355 »

The way to get them working together is to combine the code so you:
get lux1 assign to var1
get lux2 assign to var2
set servo1
set servo2
wait
loop

User avatar
adafruit_support_bill
 
Posts: 88096
Joined: Sat Feb 07, 2009 10:11 am

Re: run two loop codes at the same time / hold servo's angle

Post by adafruit_support_bill »

The problem here is that you are relying on delay() for your timing. Delay effectively stops all processing and nothing else can be done.

If you want to do multiple things in parallel, you need to keep a schedule and go by the time. See the example in File->Examples->02.Digital->BlinkWithoutDelay

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”