Source

ArduinoProjects / Experimental / MicroHorse / MicroHorse.ino

// Sweep
// by BARRAGAN <http://barraganstudio.com> 
// This example code is in the public domain.


#include <Servo.h> 
#include <MathTypes.h>
#include <MathUtil.h>
#include <Animation.h>

Animation* upperLegServoR;
Animation* lowerLegServoR;
Animation* upperLegServoL;
Animation* lowerLegServoL;

const Animation::AnimMode upperLegMode = Animation::AnimMode_PingPong;
const int numUpperLegAnimValues = 2;
float upperLegAnimValues[numUpperLegAnimValues] = {30,135};

const Animation::AnimMode lowerLegMode = Animation::AnimMode_Wrap;
const int numLowerLegAnimValues = 2;
float lowerLegAnimValues[numLowerLegAnimValues] = {30,180};

Servo servoUpperR;
Servo servoLowerR;

Servo servoUpperL;
Servo servoLowerL;

float lastTimeMillis = 0.0f;
void setup() 
{ 
  //Create animations
  const float overallSpeed = 1.0f;
  
  upperLegServoR = new Animation(upperLegAnimValues,numUpperLegAnimValues,upperLegMode);
  upperLegServoR->animSpeed = 2.0f * overallSpeed;
  
  lowerLegServoR = new Animation(lowerLegAnimValues,numLowerLegAnimValues,lowerLegMode);
  lowerLegServoR->animSpeed = 1.0f * overallSpeed;
  
  const float offset = 0.6f;
  
  upperLegServoL = new Animation(upperLegAnimValues,numUpperLegAnimValues,upperLegMode);
  upperLegServoL->animSpeed = 2.0f * overallSpeed;
  upperLegServoL->currT = offset;
  
  lowerLegServoL = new Animation(lowerLegAnimValues,numLowerLegAnimValues,lowerLegMode);
  lowerLegServoL->animSpeed = 1.0f * overallSpeed;
  lowerLegServoL->currT = offset;
  
  servoUpperR.attach(5);
  servoLowerR.attach(4);
  
  servoUpperL.attach(6);
  servoLowerL.attach(7);
  
  //Init time
  lastTimeMillis = millis();
} 

void loop() 
{ 
  //Get current time in seconds
  float currTimeMillis = millis();
  float timeDelta = (currTimeMillis-lastTimeMillis)/1000.0f;
  lastTimeMillis = currTimeMillis;
  
  //Update animations
  upperLegServoR->Update(timeDelta);
  lowerLegServoR->Update(timeDelta);
  
  upperLegServoL->Update(timeDelta);
  lowerLegServoL->Update(timeDelta);
  
  //Set servo values from animations
  servoUpperR.write(upperLegServoR->CalcAnimVal());
  servoLowerR.write(lowerLegServoR->CalcAnimVal());
  
  servoUpperL.write(180.0f - upperLegServoL->CalcAnimVal());
  servoLowerL.write(180.0f - lowerLegServoL->CalcAnimVal());
} 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.