Updated sketches for TEI workshop

This commit is contained in:
Bill Verplank 2013-02-09 21:38:08 +01:00
parent 9260d066ba
commit bae5b3d412
12 changed files with 396 additions and 18 deletions

View File

@ -0,0 +1,36 @@
//"Pulse" - small duration positive then negative force
// parameters: F1, T1, D1, F2, T2, D2
// pitch set from slider (B - A3)
#define BIT_DEPTH 12
#include <Motor.h>
#include <Music.h>
void setup(){
MotorA.init();
Music.init();
Music.setWaveform(SINE);
Music.enableEnvelope();
Music.setAttack(10);
Music.setDecay(10);
Music.setRelease(10);
}
void loop(){
//for (int i; i < 512; i + 100){
MotorA.torque(80);
Music.noteOn(map(analogRead(A3),0,1023,30,60));
delay (40);
Music.noteOff();
MotorA.torque(0);
delay (150);
MotorA.torque(-70);
Music.noteOn(map(analogRead(A3),0,1023,35,65));
delay (40);
Music.noteOff();
MotorA.torque(0);
delay (750);
//}
}

View File

@ -11,15 +11,17 @@ int xA, xB, foutA, foutB;
void setup(){ void setup(){
MotorA.init(); MotorA.init();
MotorB.init(); MotorB.init();
Music.init();
} }
void loop(){ void loop(){
xA = analogRead(A0); xA = analogRead(A0);
xB = analogRead(A3); xB = analogRead(A3);
foutA = 6*(xB-xA); // this will peak at x=1024/6
MotorA.torque(foutA); // 1/4 or 1/2 ? foutA = 2*(xB-xA);
MotorA.torque(foutA);
foutB = 6*(xA-xB); // this will peak at x=1024/6
foutB = 2*(xA-xB); // this will peak at x=1024/6
MotorB.torque(foutB); // 1/4 or 1/2 ? MotorB.torque(foutB); // 1/4 or 1/2 ?
} }

View File

@ -1,11 +1,7 @@
// CenterAB - both motors // CenterAB - both motors
// Freqequency1 B->Frequency2 // xA->Freqequency1, xB->Frequency2
//CenterA at xB, CenterB at xA? //CenterA at xB, CenterB at xA
//feels like "Slave" //feels like "Slave"
//position on A0, pwm:D9, dir:D8,D7
//CenterB
//position on A3, pwm:D10, dir:D11,D12
#include <Motor.h> #include <Motor.h>
#include <Music.h> #include <Music.h>
@ -20,7 +16,7 @@ void setup(){
MotorA.init(); MotorA.init();
MotorB.init(); MotorB.init();
Music.init(); Music.init();
Music.setWaveform1(0);//8bit Music.setWaveform1(0);//8bit default?
Music.setWaveform2(0); Music.setWaveform2(0);
Music.setGain1(1.0f); Music.setGain1(1.0f);
Music.setGain2(1.0f); Music.setGain2(1.0f);

View File

@ -0,0 +1,98 @@
//plucks - four bumps
//three notes (400,500,600hz)
//can't get Music.setGain1, etc to work only Music.setGain() starts all of them.
//requires FSR attached to A
#define NUM_OSCILLATORS 3
#define BIT_DEPTH 8
#include <Music.h>
#include <Motor.h>
int x, xold, xt; // input position x, output force F
float K = 20; // slope constant
byte count; //for print count-down
float fin; // scaled to be 0.0 - 1.0
float fout; // from FSR
void setup(){
Music.init();
Music.setFrequency1(200);
Music.setFrequency2(250);
Music.setFrequency3(300);
//FSR-A
pinMode(A1,INPUT);
digitalWrite(A1,HIGH); //set pull-up
pinMode(4,OUTPUT);
digitalWrite(4,LOW);
//FSR-B
pinMode(A4,INPUT);
digitalWrite(A4,HIGH); //set pull-up
pinMode(5,OUTPUT);
digitalWrite(5,LOW);
Serial.begin(9600);
MotorA.init();
Serial.begin(9600);
x = analogRead(A0); // initialize x
}
void loop(){
xold = x;
x = analogRead(A0);
// did xold - x include 125, 375, 625, 875? or x%250 = 125
if (((xold <= 125) && (x > 125)) || ((xold >= 125) && (x < 125))){
Music.setGain1(1.0);//Music.setGain1(fin*fin);
Music.setFrequency1(200);
}
if (((xold <= 375) && (x > 375)) || ((xold >= 375) && (x < 375))){
Music.setGain2(1.0);//Music.setGain2(fin*fin);
//Music.setFrequency(250);
}
if (((xold <= 625) && (x > 625)) || ((xold >= 625) && (x < 625))){
Music.setGain3(1.0);//Music.setGain3(fin*fin);
//Music.setFrequency(300);
}
if (((xold <= 875) && (x > 875)) || ((xold >= 875) && (x < 875))){
Music.setGain1(1.0);//Music.setGain1(fin*fin);
Music.setFrequency1(400);
}
else{
Music.setGain1(0.995f*Music.getGain1());
Music.setGain2(0.995f*Music.getGain2());
Music.setGain3(0.995f*Music.getGain3());
}
fin = 1000 - analogRead(A1); // invert and shift
fin = max (0, fin); // make sure it's > 0
fin = min (1023, fin); // and < 1023
fin = fin/1000; // scale 0-1.0
fin = fin*fin; //square it for more effect near 1.0.
xt = x % 250; //same force for each 250 ranage
fout = 0;
if (xt > 60) fout = - K * (xt - 60);
if (xt > 80) fout = - K * (100 - xt);
if (xt > 120) fout = K * (140 - xt);
if (xt > 140) fout = 0;
//fout = fout * fin;
fout = fout;
MotorA.torque(fout);
// print every 256 cycles
if(count++==0){
Serial.print(x);
Serial.print(" ");
Serial.print(xt);
Serial.print(" ");
Serial.print(fin);
Serial.print(" ");
Serial.println(fout);
}
}

View File

@ -18,8 +18,8 @@ void setup(){
} }
void loop(){ void loop(){
//x = analogRead(A0) - 512; x = analogRead(A0) - 512;
x = analogRead(A3) - 512; //x = analogRead(A3) - 512;
if (forward){ if (forward){
if (x <= - w/2) f = 0; if (x <= - w/2) f = 0;
if (x > -w/2 && x< w/2) f = - slope*(x + w/2); if (x > -w/2 && x< w/2) f = - slope*(x + w/2);
@ -40,8 +40,8 @@ void loop(){
} }
} }
fout = int(f); fout = int(f);
//MotorA.torque(fout); MotorA.torque(fout);
MotorB.torque(fout); //MotorB.torque(fout);
if(count++>=0){ if(count++>=0){
count=-1000; // wait 1000 loops before print count=-1000; // wait 1000 loops before print
Serial.print(x,DEC); Serial.print(x,DEC);

View File

@ -22,12 +22,13 @@ void loop(){
MotorB.torque(40); MotorB.torque(40);
Music.noteOn(map(analogRead(A3),0,1023,30,60)); Music.noteOn(map(analogRead(A3),0,1023,30,60));
delay (20); delay (20);
Music.noteOff();MotorB.torque(0); Music.noteOff();
MotorB.torque(0);
delay (150); delay (150);
MotorB.torque(-70); MotorB.torque(-70);
Music.noteOn(map(analogRead(A3),0,1023,35,65)); Music.noteOn(map(analogRead(A3),0,1023,35,65));
delay (40); delay (40);
Music.noteOff();MotorB.torque(0); Music.noteOff();
MotorB.torque(0); MotorB.torque(0);
delay (750); delay (750);
//} //}

View File

@ -0,0 +1,35 @@
//"Pulse" - small duration positive then negative force
// parameters: F1, T1, D1, F2, T2, D2
#define BIT_DEPTH 12
#include <Motor.h>
#include <Music.h>
void setup(){
MotorB.init();
Music.init();
Music.setWaveform(SINE);
Music.enableEnvelope();
Music.setAttack(10);
Music.setDecay(10);
Music.setRelease(10);
}
void loop(){
//for (int i; i < 512; i + 100){
MotorB.torque(40);
Music.noteOn(map(analogRead(A3),0,1023,30,60));
delay (20);
Music.noteOff();
MotorB.torque(0);
delay (150);
MotorB.torque(-70);
Music.noteOn(map(analogRead(A3),0,1023,35,65));
delay (40);
Music.noteOff();
MotorB.torque(0);
delay (750);
//}
}

View File

@ -0,0 +1,11 @@
//slider test
#include <Motor.h>
void setup(){
Serial.begin(9600);
MotorA.init();
}
void loop(){
MotorA.torque(analogRead(A0));
Serial.println(analogRead(A0));
}

View File

@ -0,0 +1,51 @@
// Theremin
// A: pitch, B: volume
// or visa versa ??????????
#include <Music.h>
int xA, xB; //10bit A/D gives 0-1023
float p; //midi note number 0-127
double frequency, volume;
byte c; //print every 256 cycles
void setup(){
Music.init();
Serial.begin(9600);
}
void loop(){
xA = analogRead(A3); // A position
p = float(xA)/8; //pitch (midi)
p = 80.*(p/128.)+20.;
// p = map(p, 0,127,20,100);
//linear mapping of p
//The map() function uses integer math so will
//not generate fractions, when the math might
//indicate that it should do so. Fractional
//remainders are truncated, and are not rounded
//or averaged.
//
frequency = pow(2,((p-69.)/12.))*440; //midi 69 -> 440hz
Music.setFrequency(frequency);
//Music.setFrequency(xA);
xB = analogRead(A0);
volume = -float(xB)/256.; // B position
volume = pow(10,volume);
Music.setGain(volume);
//Music.setGain(0.5);
if(c++==0){
Serial.print(xA);
Serial.print(" ");
Serial.print(p);
Serial.print(" ");
Serial.print(frequency);
Serial.print(" ");
Serial.print(xB);
Serial.print(" ");
Serial.println(volume);
}
}

View File

@ -45,7 +45,7 @@ while (Serial.available()) {
Music.setGain(1.0f); Music.setGain(1.0f);
} }
if(x>10){ if(x>10){
Music.setGain(0.998f*Music.getGainFloat()); Music.setGain(0.998f*Music.getGain());
} }
} }
} }

View File

@ -0,0 +1,148 @@
{\rtf1\ansi\ansicpg1252\cocoartf1138\cocoasubrtf510
{\fonttbl\f0\fmodern\fcharset0 Courier;}
{\colortbl;\red255\green255\blue255;}
\margl1440\margr1440\vieww10800\viewh8400\viewkind0
\deftab720
\pard\pardeftab720
\f0\fs24 \cf0 /* fscale\
Floating Point Autoscale Function V0.1\
Paul Badger 2007\
Modified from code by Greg Shakar\
\
This function will scale one set of floating point numbers (range) to another set of floating point numbers (range)\
It has a "curve" parameter so that it can be made to favor either the end of the output. (Logarithmic mapping)\
\
It takes 6 parameters\
\
originalMin - the minimum value of the original range - this MUST be less than origninalMax\
originalMax - the maximum value of the original range - this MUST be greater than orginalMin\
\
newBegin - the end of the new range which maps to orginalMin - it can be smaller, or larger, than newEnd, to facilitate inverting the ranges\
newEnd - the end of the new range which maps to originalMax - it can be larger, or smaller, than newBegin, to facilitate inverting the ranges\
\
inputValue - the variable for input that will mapped to the given ranges, this variable is constrained to originaMin <= inputValue <= originalMax\
curve - curve is the curve which can be made to favor either end of the output scale in the mapping. Parameters are from -10 to 10 with 0 being\
a linear mapping (which basically takes curve out of the equation)\
\
To understand the curve parameter do something like this: \
\
void loop()\{\
for ( j=0; j < 200; j++)\{\
scaledResult = fscale( 0, 200, 0, 200, j, -1.5);\
\
Serial.print(j, DEC); \
Serial.print(" "); \
Serial.println(scaledResult, DEC); \
\} \
\}\
\
And try some different values for the curve function - remember 0 is a neutral, linear mapping\
\
To understand the inverting ranges, do something like this:\
\
void loop()\{\
for ( j=0; j < 200; j++)\{\
scaledResult = fscale( 0, 200, 200, 0, j, 0);\
\
// Serial.print lines as above\
\
\} \
\}\
\
*/\
\
\
#include <math.h>\
\
int j;\
float scaledResult; \
\
void setup() \{\
Serial.begin(9600);\
\}\
\
void loop()\{\
for ( j=0; j < 200; j++)\{\
scaledResult = fscale( 0, 200, 0, 200, j, -1.5);\
\
Serial.print(j, DEC); \
Serial.print(" "); \
Serial.println(scaledResult , DEC); \
\} \
\}\
\
float fscale( float originalMin, float originalMax, float newBegin, float\
newEnd, float inputValue, float curve)\{\
\
float OriginalRange = 0;\
float NewRange = 0;\
float zeroRefCurVal = 0;\
float normalizedCurVal = 0;\
float rangedValue = 0;\
boolean invFlag = 0;\
\
\
// condition curve parameter\
// limit range\
\
if (curve > 10) curve = 10;\
if (curve < -10) curve = -10;\
\
curve = (curve * -.1) ; // - invert and scale - this seems more intuitive - postive numbers give more weight to high end on output \
curve = pow(10, curve); // convert linear scale into lograthimic exponent for other pow function\
\
/*\
Serial.println(curve * 100, DEC); // multply by 100 to preserve resolution \
Serial.println(); \
*/\
\
// Check for out of range inputValues\
if (inputValue < originalMin) \{\
inputValue = originalMin;\
\}\
if (inputValue > originalMax) \{\
inputValue = originalMax;\
\}\
\
// Zero Refference the values\
OriginalRange = originalMax - originalMin;\
\
if (newEnd > newBegin)\{ \
NewRange = newEnd - newBegin;\
\}\
else\
\{\
NewRange = newBegin - newEnd; \
invFlag = 1;\
\}\
\
zeroRefCurVal = inputValue - originalMin;\
normalizedCurVal = zeroRefCurVal / OriginalRange; // normalize to 0 - 1 float\
\
/*\
Serial.print(OriginalRange, DEC); \
Serial.print(" "); \
Serial.print(NewRange, DEC); \
Serial.print(" "); \
Serial.println(zeroRefCurVal, DEC); \
Serial.println(); \
*/\
\
// Check for originalMin > originalMax - the math for all other cases i.e. negative numbers seems to work out fine \
if (originalMin > originalMax ) \{\
return 0;\
\}\
\
if (invFlag == 0)\{\
rangedValue = (pow(normalizedCurVal, curve) * NewRange) + newBegin;\
\
\}\
else // invert the ranges\
\{ \
rangedValue = newBegin - (pow(normalizedCurVal, curve) * NewRange); \
\}\
\
return rangedValue;\
\}\
}