Go Down

Topic: running 2 operations without using delay() on Pro Mini (Read 229 times) previous topic - next topic

nagdrn

Hi guys!

I wrote a code to control a camera shutter and a 1-axis gimbal using a servo motor.
the commands are sent from a raspberry Pi 3 to my Pro Mini using serial connection.

example of cmd for shutter #S1% i.e take a single shot
example of cmd for gimbal #G450% i.e position gimbal at 45 degrees.

In My code I am using delay(500) to control the shutter.
In the meanwhile any command sent to control the camera gimbal will not be processed as the Pro Mini pauses the whole process.
Same thing for the camera gimbal (servo motor), I am using a delay(10) to smoothly position it at the desired angle.

Can anyone suggest me a way to still process both command for shutter and gimbal without freezing each other?

My code is below.

Thank you very much

Code: [Select]
#include <Servo.h>

char cmd[6];
int Count = 0;
float angle = 0;
int cu_ms = 0;
int pr_ms = 1500;
int shutterPin = 9; // shutter connected to digital pin 9

Servo myservo;

void setup()
{
  Serial.begin(115200, SERIAL_8N1);
 
  myservo.attach(6,930, 2200);
  myservo.writeMicroseconds(1475);

  pinMode(shutterPin, OUTPUT); // sets the digital pin as output
  digitalWrite(shutterPin, HIGH); // sets the camera shutter on
}

int an2ms(float angle);
int ch2int(char *var);

void loop()
{

  while (Serial.available() > 0)
  {
    cmd[Count] = Serial.read();
    Count++;

    if ( Count == 6 && cmd[0] == 35 && cmd[1] == 71 && cmd[5] == 37 )
    {
      angle = ch2int(cmd);
      angle /= 10.0;
      cu_ms = an2ms(angle);

      if (cu_ms < pr_ms)
        for (int i = pr_ms ; i >= cu_ms ; i -= 5) {
          myservo.writeMicroseconds(i);
          delay(10);
        }
      else if (cu_ms > pr_ms)
        for (int i = pr_ms; i <= cu_ms ; i += 5) {
          myservo.writeMicroseconds(i);
          delay(10);
        }

      pr_ms = cu_ms;
      memset(cmd, 0, 6);
      Count = 0;
    }

    else if(Count==4 && strncmp(cmd, "#S1%%", 4) == 0)
    {
      digitalWrite(shutterPin, LOW); // sets the camera off
      delay(500); // waits for a second
      digitalWrite(shutterPin, HIGH); // sets the camera shutter on
      memset(cmd, 0, 4);
      Count = 0;
    }
   

  }

}

int an2ms(float angle)
{
  int ms;
  ms = 1475 + (int)(angle * 525 / 90);
  return ms;
}

int ch2int(char *var)
{
  int angle;
  angle = ((int)var[2] - 48) * 100  +  ((int)var[3] - 48) * 10  +  ((int)var[4] - 48);
  return angle;
}

pert

You will use millis() instead of delay(). See File > Examples > 02.Digital > BlinkWithoutDelay for a simple example of how to do this. The tutorial page for that example is here:
https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
This is a very important technique to learn. Once you get the basic concept it become almost as easy to use as delay() and as you start getting into more advanced applications such as what you're doing now you will always find that delay() causes you problems so start writing all your code as non-blocking from the start so you don't ever need to do rewrites.


Go Up