Dependencies:   mbed Servo

Fork of multiServoBat by Angeline Luther

main.cpp

Committer:
aluthe3099
Date:
2019-08-01
Revision:
2:7e5d670ed9aa
Parent:
1:829cbbaeaf32
Child:
3:88de581de68d

File content as of revision 2:7e5d670ed9aa:

#include "mbed.h"
#include "Servo.h"

#define NUM_SERVOS 4
Servo servo[NUM_SERVOS]={p24,p23,p22,p21};
float range = 0.0005;
float degrees = 45.0;

void together();
void apart(int side);

int main() {     
    printf("Hello bats!\n");
    
    printf("entering loop\n"); 
    while(1) {        
        printf("entering together() call\n");
        together();
        wait(5);
        apart(0);
        wait(2);
        apart(1);
        wait(2);
    }
    
}

void together()
{
    float i; 
    int j;
    float max = 1.0, min = 0.0, incr = 0.01;
    
    for(i = min; i<max; i+=incr)
    {
        for(j = 0; j<NUM_SERVOS; j++)
        {
            servo[j] = i;
        }
        wait(0.01);
    }
    wait(1);
    for(i = max; i>min; i-=incr)
    {
        for(j = 0; j<NUM_SERVOS; j++)
        {
            servo[j] = i;
        }
        wait(0.01);
    }
    wait(1);
    printf("finished one of together\n");
}

//only moves two of the servo motors, which 2 depends on the parameter side: runs 1 and 3 when side = 1, runs 2 and 4 when side = 0   
void apart (int side)
{
    float i;
    int j;
    float max = 1.0, min = 0.0, incr = 0.01;
    for(i = min; i<max; i+=incr)
    {
        for(j = 0; j<NUM_SERVOS; j++)
        {
            if(j%2 == side)
            {
                servo[j] = i;
            }
          
                
        }
        wait(0.01);
    }
    wait(1);
    for(i = max; i>min; i-=incr)
    {
        for(j = 0; j<NUM_SERVOS; j++)
        {
            if(j%2 == side)
            {
                servo[j] = i;
            }
        }
        wait(0.01);
    }
    wait(1);
    printf("finished one of together\n");
}