Go Down

Topic: RESET TO ANGLE 0° AFTER SHUT DOWN (Read 148 times) previous topic - next topic

GHAZI30

I am in arduino world. please I need some help in this project, I would like to add and instruction to let the servo come back to 0° after shut down:

#include <Arduino.h>
#include <Servo.h>
Servo servo;

#define ABS(x) ( x >= 0 ) ? ( x ) : ( - x )

#define angles_count 3
const int angles[ angles_count ] = { 15, 90, 165 };
int angle = angles[ 0 ];
const int speed = 1;
int dir = speed;

void setup( )
{
   servo.attach( 5, 500, 2500 );
   angle = angles[ 0 ];
   servo.write( angle );
   delay( 3000 );
}

void loop( )
{
   delay( 100 );
   angle = angle + dir;
   servo.write( angle );
   if ( angle >= angles[ angles_count - 1 ] ) dir = - speed;
   if ( angle <= angles[ 0 ] ) dir = speed;
   for ( int i = 0; i < angles_count; i ++ )
   {
      // stop on each point
      if ( angle == angles[ i ] )
      {
         // but don't stop on the middle if back direction
         if ( i == 1 && dir < 0 ) break;
         delay( 3000 );
         break;
      }
   }
}

waiting to hear from you in the near future.
best Regards
Ghazi

Paul_KD7HB

Which servo would you be referring to?

Paul

GHAZI30

hi there,
thank you for the reply. it is the servo MG996

Paul_KD7HB

IS there some place in your code where "shutdown" is identified? A procedure name or even a comment would help.

Paul

Go Up