Goldsmiths

This commit is contained in:
dviid
2013-03-18 10:51:33 +00:00
parent d938b31e91
commit 05fc0c94af
38 changed files with 152 additions and 1544 deletions
@@ -7,7 +7,7 @@
#include <Music.h>
void setup(){
MotorB.init();
MotorA.init();
Music.init();
Music.setWaveform(SINE);
@@ -18,17 +18,15 @@ void setup(){
}
void loop(){
//for (int i; i < 512; i + 100){
MotorB.torque(40);
MotorA.torque(200);
Music.noteOn(map(analogRead(A3),0,1023,30,60));
delay (20);
Music.noteOff();MotorB.torque(0);
Music.noteOff();MotorA.torque(0);
delay (150);
MotorB.torque(-70);
MotorA.torque(-200);
Music.noteOn(map(analogRead(A3),0,1023,35,65));
delay (40);
Music.noteOff();MotorB.torque(0);
MotorB.torque(0);
Music.noteOff();MotorA.torque(0);
MotorA.torque(0);
delay (750);
//}
}
@@ -0,0 +1,25 @@
// Center - both motors A and B
// feels like "Double Toggle"!
#include <Motor.h>
int duty, count, fout;
int xA, xB, foutA, foutB;
void setup(){
Serial.begin(9600);
MotorA.init();
MotorB.init();
}
void loop(){
xA = analogRead(A0);
xB = analogRead(A3);
foutA = 6*(xB-512); // this will peak at x=1024/6
MotorA.torque(foutA); // 1/4 or 1/2 ?
foutB = 6*(xA-512); // this will peak at x=1024/6
MotorB.torque(foutB); // 1/4 or 1/2 ?
}
@@ -0,0 +1,41 @@
// CenterAB - both motors
// xA->Freqequency1, xB->Frequency2
//CenterA at xB, CenterB at xA
//feels like "Slave"
#include <Motor.h>
#include <Music.h>
#define BIT_DEPTH 8
#define NUM_OSCILLATORS 2
int duty, count, fout;
int xA, xB, foutA, foutB;
void setup(){
Serial.begin(9600);
MotorA.init();
MotorB.init();
Music.init();
Music.setWaveform1(0);//8bit default?
Music.setWaveform2(0);
Music.setGain1(1.0f);
Music.setGain2(1.0f);
}
void loop(){
xA = analogRead(A0);
Music.setFrequency1(map (xA, 0, 1023, 40, 2000));
xB = analogRead(A3);
Music.setFrequency2(map (xB, 0, 1023, 40, 2000));
foutA = -6*(xA-xB); // this will peak at x=1024/6
MotorA.torque(foutA); // 1/4 or 1/2 ?
foutB = -6*(xB-xA); // this will peak at x=1024/6
MotorB.torque(foutB); // 1/4 or 1/2 ?
Music.setGain(float(abs(xA-xB))/1024.0f);
}
@@ -0,0 +1,36 @@
// Theremin
// A: pitch, B: volume
#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);
xB = analogRead(A0);
volume = -float(xB)/256.; // B position
volume = pow(10,volume);
Music.setGain(volume);
}
@@ -0,0 +1,48 @@
//Pluck: one pluck at x=512
int x, fout, count;
float f;
float w = 50; //width of pluck
float h = 500; //height of pluck
float slope = h/w;
boolean forward = true;
#include <Motor.h>
void setup(){
MotorA.init();
MotorB.init();
Serial.begin(9600);
}
void loop(){
x = analogRead(A0) - 512;
if (forward){
if (x <= - w/2) f = 0;
if (x > -w/2 && x< w/2) f = - slope*(x + w/2);
if (x > w/2){
f = 0;
forward = false;
Serial.println("pluck forward");
}
}
else
{
if (x > w/2) f = 0;
if (x < w/2 && x > -w/2) f = - slope*(x - w/2);
if (x < -w/2){
f = 0;
forward = true;
Serial.println("pluck back");
}
}
fout = int(f);
MotorA.torque(fout);
if(count++>=0){
count=-1000; // wait 1000 loops before print
Serial.print(x,DEC);
Serial.print(" ");
Serial.println(fout,DEC);
}
}
@@ -0,0 +1,66 @@
//Plucks - four bumps / three notes (400,500,600hz)
#define NUM_OSCILLATORS 3
#include <Music.h>
#include <Motor.h>
int x, xold, xt, F; // input position x, output force F
int K = 10; // slope constant
byte count; //for print count-down
void setup(){
Music.init();
Music.setFrequency1(200);
Music.setFrequency2(250);
Music.setFrequency3(300);
MotorA.init();
Serial.begin(9600);
x = analogRead(A0); // initialize x
}
void loop(){
xold = x;
x = analogRead(A0);
if (((xold <= 125) && (x > 125)) || ((xold >= 125) && (x < 125))){
Music.setGain1(1.0f);
}
if (((xold <= 375) && (x > 375)) || ((xold >= 375) && (x < 375))){
Music.setGain2(1.0f);
}
if (((xold <= 625) && (x > 625)) || ((xold >= 625) && (x < 625))){
Music.setGain3(1.0f);
}
if (((xold <= 875) && (x > 875)) || ((xold >= 875) && (x < 875))){
Music.setGain1(1.0f);
}
else{
Music.setGain1(0.995f*Music.getGain1());
Music.setGain2(0.995f*Music.getGain2());
Music.setGain3(0.995f*Music.getGain3());
}
xt = x % 250; //same force for each 250 ranage
F = 0;
if (xt > 60) F = - K * (xt - 60);
if (xt > 80) F = - K * (100 - xt);
if (xt > 120) F = K * (140 - xt);
if (xt > 140) F = 0;
MotorA.torque(F);
// print every 256 cycles
if(count++==0){
Serial.print(x);
Serial.print(" ");
Serial.print(xt);
Serial.print(" ");
Serial.println(F);
}
}
@@ -0,0 +1,86 @@
/*
This sketch runs with Processing sketch Oscilloscope.pde
*/
#include <Motion.h>
#include <Motor.h>
#include <Music.h>
char buf[16] = "";
char b = 'x';
String inputString = "";
boolean stringComplete = false;
float k, m, d;
int cnt = 0;
void setup() {
// MOTOR
MotorA.init();
// MUSIC
Music.init();
// MOTION
MotionA.init(INPUTA0);
MotionA.k = 5.2f; // spring
MotionA.m = 1.0f; // mass
MotionA.d = 8.02f; // damping
// Serial
Serial.begin(9600);
}
void loop() {
MotionA.update_mass_spring_damper();
MotorA.torque(MotionA.F);
int f = map(abs(MotionA.F), 0, 512, 64, 76);
Music.noteOn(f);
cnt++;
if(cnt == 10) {
sprintf(buf, "%d %d %d %d", (int)MotionA.F, (int)MotionA.V, (int)MotionA.X, (int)MotionA.Xin);
Serial.println(buf);
cnt = 0;
}
if(stringComplete) {
if(b == 'k') {
MotionA.k = convertToFloat(inputString);
} else if(b == 'm') {
MotionA.m = convertToFloat(inputString);
} else if(b == 'd') {
MotionA.d = convertToFloat(inputString);
}
b = 'x';
stringComplete = false;
inputString = "";
}
}
void serialEvent() {
while (Serial.available()) {
char inChar = (char)Serial.read();
if(inChar == 'k' || inChar == 'm' || inChar == 'd') {
b = inChar;
} else {
if (inChar == ';') {
stringComplete = true;
} else
inputString += inChar;
}
}
}
float convertToFloat(String str) {
char buf[16] = "";
str.toCharArray(buf, str.length() + 1);
return atof(buf);
}
@@ -0,0 +1,30 @@
//FM synthes of a sort
//hang on! (it's unstable and wants to limit cycle)
#include <Music.h>
#define BIT_DEPTH 8 // gives us 16 Waveforms
#include <Motor.h>
byte cnt;
float xf, vf; //
float k = 10.0; // increase FM frequency
float b = 0.40; // increase
float Tf = .030 ; //integration time
void setup(){
Serial.begin(9600);
Music.init();
Music.setWaveform(0);
Music.setGain(1.0f);
MotorA.init();
}
void loop(){
xf += vf * Tf;
vf += (k * (analogRead(A0) - xf) - b*vf) * Tf;
Music.setFrequency(100+vf);
Music.setGain(.001*abs(vf));
MotorA.torque(-500+xf);
//if(cnt++==0)Serial.println(.001*abs(vf));
}
@@ -0,0 +1,12 @@
#include <Motor.h>
void setup() {
MotorA.init();
}
void loop() {
MotorA.torque(500);
delay(2000);
MotorA.torque(-500);
delay(2000);
}
@@ -0,0 +1,67 @@
#include <Motion.h>
#include <Motor.h>
#define NEW '!'
#define OK '*'
#define PRINT '&'
int table[800];
int len = 800;
char buf[32] = "";
int cnt = 0;
void setup() {
Serial.begin(9600);
MotorA.init();
MotionA.init(INPUTA0);
}
void loop() {
MotionA.update_position();
int i = (int) (((float) MotionA.Xin / 1023.0) * len);
int f = table[i];
MotorA.torque(f);
// TODO - send position back to interface
/*
cnt++;
if(cnt == 1000) {
sprintf(buf, "%d %d %d", MotionA.Xin, i, f);
Serial.println(buf);
cnt = 0;
}
*/
}
void serialEvent() {
if(Serial.available()) {
char in = (char)Serial.read();
if(in == NEW) {
Serial.print(OK);
len = serial_read();
for(int i = 0; i < len; i++) {
table[i] = serial_read();
}
} else if(in == PRINT) {
Serial.println(len);
for(int i = 0; i < len; i++) {
Serial.print(table[i]);
Serial.print(';');
}
Serial.print('#');
}
}
}
int serial_read() {
while(Serial.available() < 2);
int l = Serial.read();
int h = Serial.read();
Serial.write(OK);
return (h * 256) + l;
}
+11 -1
View File
@@ -7,7 +7,10 @@
#######################################
Music KEYWORD1
Motor KEYWORD1
MotorA KEYWORD1
MotorB KEYWORD1
MotionA KEYWORD1
MotionB KEYWORD1
Midi KEYWORD1
#######################################
@@ -49,6 +52,13 @@ setAttack KEYWORD2
setDecay KEYWORD2
setSustain KEYWORD2
setRelease KEYWORD2
update_position KEYWORD2
update_mass_spring_damper KEYWORD2
torque KEYWORD2
direction KEYWORD2
stop KEYWORD2
start KEYWORD2
restart KEYWORD2
#######################################