2012-11-03 14:04:45 +01:00
|
|
|
//Center
|
|
|
|
|
//uses a variable force (pwm duty)
|
|
|
|
|
//If it feels like a mountain - pushing away from center, then
|
|
|
|
|
//reverse the motor leads
|
|
|
|
|
//or for a quick fix in the code: change if(f < 0) to if (f > 0)
|
|
|
|
|
|
2013-01-24 21:14:05 +01:00
|
|
|
#include "Motor.h"
|
|
|
|
|
#include "Music.h"
|
2012-11-03 14:04:45 +01:00
|
|
|
|
2013-01-25 11:14:52 +01:00
|
|
|
int posA, posB; // position from analogRead
|
|
|
|
|
int forceA, forceB; // computed from pos and k
|
|
|
|
|
int kA, kB = 2; // spring constant
|
|
|
|
|
//int duty; // pwm duty for Timer1 (range 0 - 1023) 10-bit resolution
|
2012-11-03 14:04:45 +01:00
|
|
|
|
|
|
|
|
void setup()
|
|
|
|
|
{
|
2013-01-24 12:11:48 +01:00
|
|
|
MotorA.init();
|
2013-01-25 11:14:52 +01:00
|
|
|
MotorB.init();
|
2013-01-24 21:14:05 +01:00
|
|
|
Music.init();
|
2012-11-03 14:04:45 +01:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void loop()
|
|
|
|
|
{
|
|
|
|
|
|
2013-01-25 11:14:52 +01:00
|
|
|
posA = analogRead(A0);
|
|
|
|
|
posB = analogRead(A3);
|
2012-11-03 14:04:45 +01:00
|
|
|
|
2013-01-25 11:14:52 +01:00
|
|
|
Music.setFrequency1(posA);
|
|
|
|
|
Music.setFrequency2(posB);
|
2013-01-24 21:14:05 +01:00
|
|
|
|
2013-01-25 11:14:52 +01:00
|
|
|
forceA = - kA * (512 - posA);
|
|
|
|
|
forceB = - kB * (512 - posB);
|
2013-01-24 21:14:05 +01:00
|
|
|
//duty = abs(force);
|
|
|
|
|
//duty = min(512, duty);
|
2012-11-03 14:04:45 +01:00
|
|
|
|
2013-01-25 11:14:52 +01:00
|
|
|
MotorA.torque(forceA); //force can be -512 to +511
|
|
|
|
|
MotorB.torque(forceB); //force can be -512 to +511
|
2012-11-03 14:04:45 +01:00
|
|
|
|
2013-01-24 21:14:05 +01:00
|
|
|
//if(force < 0) MotorA.direction(FORWARD);
|
|
|
|
|
//else MotorA.direction(BACKWARD);
|
2012-11-03 14:04:45 +01:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|