summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFranklin Wei <me@fwei.tk>2019-05-21 19:49:44 -0400
committerFranklin Wei <me@fwei.tk>2019-05-21 19:49:44 -0400
commitd1169d1a0f631e6780d9f545a8e1534d1c5fd31f (patch)
treee8b3f421dac7c605cd2dee69fefa8cde5433fbf7
downloadmultipicker-master.zip
multipicker-master.tar.gz
multipicker-master.tar.bz2
multipicker-master.tar.xz
initial commitHEADmaster
-rw-r--r--multipicker.ino299
1 files changed, 299 insertions, 0 deletions
diff --git a/multipicker.ino b/multipicker.ino
new file mode 100644
index 0000000..e3ba1fc
--- /dev/null
+++ b/multipicker.ino
@@ -0,0 +1,299 @@
+/***************************************************
+ This is an example for our Adafruit 16-channel PWM & Servo driver
+ Servo test - this will drive 8 servos, one after the other on the
+ first 8 pins of the PCA9685
+
+ Pick one up today in the adafruit shop!
+ ------> http://www.adafruit.com/products/815
+
+ These drivers use I2C to communicate, 2 pins are required to
+ interface.
+
+ Adafruit invests time and resources providing this open source code,
+ please support Adafruit and open-source hardware by purchasing
+ products from Adafruit!
+
+ Written by Limor Fried/Ladyada for Adafruit Industries.
+ BSD license, all text above must be included in any redistribution
+ ****************************************************/
+
+#include <Wire.h>
+#include <Adafruit_PWMServoDriver.h>
+
+// called this way, it uses the default address 0x40
+Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
+// you can also call it with a different address you want
+//Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x41);
+// you can also call it with a different address and I2C interface
+//Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(&Wire, 0x40);
+
+// Depending on your servo make, the pulse width min and max may vary, you
+// want these to be as small/large as possible without hitting the hard stop
+// for max range. You'll have to tweak them as necessary to match the servos you
+// have!
+#define SERVOMIN 150 // this is the 'minimum' pulse length count (out of 4096)
+#define SERVOMAX 600 // this is the 'maximum' pulse length count (out of 4096)
+
+// our servo # counter
+uint8_t servonum = 0;
+
+/* virtual number */
+const uint8_t center_pos[] = {
+ 70, // low E
+ 103,
+ 90,
+ 115,
+ 65,
+ 95 // high E
+};
+
+uint8_t down_pos[6];
+
+/* +/- pluck_step / 2 */
+const uint8_t pluck_step[] = {
+ 25,
+ 25,
+ 25,
+ 25,
+ 25,
+ 35
+};
+
+/* virtual string -> servo port num */
+const uint8_t mapping[] = {
+ 5, 2,
+ 4, 1,
+ 3, 0
+};
+
+uint8_t pluckers[6];
+
+// you can use this function if you'd like to set the pulse length in seconds
+// e.g. setServoPulse(0, 0.001) is a ~1 millisecond pulse width. its not precise!
+void setServoPulse(uint8_t n, double pulse) {
+ double pulselength;
+
+ pulselength = 1000000; // 1,000,000 us per second
+ pulselength /= 60; // 60 Hz
+ Serial.print(pulselength); Serial.println(" us per period");
+ pulselength /= 4096; // 12 bits of resolution
+ Serial.print(pulselength); Serial.println(" us per bit");
+ pulse *= 1000000; // convert to us
+ pulse /= pulselength;
+ Serial.println(pulse);
+ pwm.setPWM(n, 0, pulse);
+}
+
+
+void moveServo(uint8_t index, int degrees)
+{
+ uint16_t pulselen = map(degrees, 0, 180, SERVOMIN, SERVOMAX);
+ pwm.setPWM(index, 0, pulselen);
+}
+
+void moveString(uint8_t virt, int degrees)
+{
+ moveServo(mapping[virt], degrees);
+}
+
+void pluck(uint8_t index)
+{
+ if(index < 6)
+ {
+ uint8_t angle;
+ if(pluckers[index] == down_pos[index])
+ angle = down_pos[index] + pluck_step[index];
+ else
+ angle = down_pos[index];
+ pluckers[index] = angle;
+ moveString(index, pluckers[index]);
+ }
+}
+
+void setup() {
+ Serial.begin(9600);
+ Serial.println("8 channel Servo test!");
+ Serial.setTimeout(1000*10);
+
+ pwm.begin();
+
+ delay(100);
+ pwm.setPWMFreq(60); // Analog servos run at ~60 Hz updates
+
+ delay(100);
+
+ Serial.println("point 1");
+
+ for(uint8_t i = 0; i < 6; i++)
+ {
+
+ Serial.println("point 2");
+ down_pos[i] = center_pos[i] - pluck_step[i] / 2;
+ pluckers[i] = down_pos[i];
+ moveString(i, pluckers[i]);
+ }
+
+ delay(100);
+
+ Serial.println("point 2");
+
+ pinMode(LED_BUILTIN, OUTPUT);
+}
+
+int readInt()
+{
+ String s = Serial.readStringUntil('\n');
+ char buf[64];
+ s.toCharArray(buf, 64);
+ return atoi(buf);
+}
+
+void loop() {
+ // length between 8ths
+ int tempo = 250;
+ // Drive each servo one at a time
+
+ /*
+ Serial.println("enter string num:");
+ uint8_t servonum = readInt();
+ Serial.println("enter angle:");
+ uint8_t angle = readInt();
+ moveString(servonum, angle);
+ */
+
+ //Serial.println("enter string:");
+ //pluck(readInt());
+
+ /*
+ for(int i = 0; i < 6; i++)
+ pluck(i);
+ delay(150);
+ */
+
+ pluck(0);
+ delay(tempo*2);
+ pluck(1);
+ delay(tempo*2);
+ pluck(2);
+ delay(tempo*2*3/2);
+
+ pluck(0);
+ delay(tempo*2);
+ pluck(1);
+ delay(tempo*2);
+ pluck(3);
+ delay(tempo*1);
+ pluck(2);
+ delay(tempo*4);
+
+ pluck(0);
+ delay(tempo*2);
+ pluck(1);
+ delay(tempo*2);
+ pluck(2);
+ delay(tempo*2*3/2);
+ pluck(1);
+ delay(tempo*2);
+ pluck(0);
+ delay(tempo*1);
+ pluck(0);
+
+ delay(tempo*8);
+
+ delay(1000);
+
+ /*
+ pluck(1);
+ delay(1000);
+
+ pluck(0);
+ delay(500);
+
+ pluck(0);
+ delay(500);
+
+ pluck(1);
+ delay(1000);
+
+ pluck(0);
+ delay(1000);
+ */
+
+ /*
+ 0 0
+ 0 0
+ 0 0
+ 0 0
+ */
+
+/*
+ pluck(1);
+ pluck(4);
+ delay(200);
+ pluck(2);
+ delay(150);
+ pluck(3);
+ delay(150);
+ pluck(1);
+ delay(150);
+ pluck(4);
+ delay(150);
+ pluck(2);
+ delay(150);
+ pluck(3);
+ delay(300);
+ */
+ /*
+ * 0 0 0
+ * 0 0
+ *
+ * 0 0
+ */
+ /*
+ pluck(1);
+ pluck(4);
+ delay(150);
+ pluck(3);
+ delay(150);
+ pluck(4);
+ delay(150);
+ pluck(1);
+ delay(150);
+ pluck(4);
+ delay(150);
+ pluck(3);
+ delay(250);
+ */
+ /*
+ for(int i = 0; i < 32; i++)
+ {
+ pluck(1);
+ delay(75);
+ }
+ for(int i = 0; i < 32; i++)
+ {
+ pluck(0);
+ delay(75);
+ }
+ */
+ //pluck(5);
+ //delay(50);
+
+
+ /*
+ for(int8_t i = 5; i >= 0; i--)
+ {
+ pluck(i);
+ delay(100);
+ }
+ delay(100);
+ */
+
+ //pluck(1);
+ //delay(250);
+
+
+
+ //pluck(5);
+ //delay(100);
+}