diff --git a/3D_Printed_Bionic_Eye/.3D_Printed_Bionic_Eye.ino.swp b/3D_Printed_Bionic_Eye/.3D_Printed_Bionic_Eye.ino.swp new file mode 100644 index 000000000..dd8e73584 Binary files /dev/null and b/3D_Printed_Bionic_Eye/.3D_Printed_Bionic_Eye.ino.swp differ diff --git a/3D_Printed_Bionic_Eye/.3D_Printed_Bionic_Eye.py.swp b/3D_Printed_Bionic_Eye/.3D_Printed_Bionic_Eye.py.swp new file mode 100644 index 000000000..72fb4580f Binary files /dev/null and b/3D_Printed_Bionic_Eye/.3D_Printed_Bionic_Eye.py.swp differ diff --git a/3D_Printed_Bionic_Eye/3D_Printed_Bionic_Eye.ino b/3D_Printed_Bionic_Eye/3D_Printed_Bionic_Eye.ino new file mode 100644 index 000000000..6a8a6d38f --- /dev/null +++ b/3D_Printed_Bionic_Eye/3D_Printed_Bionic_Eye.ino @@ -0,0 +1,73 @@ +/******************************************************************* + Bionic Eye sketch for Adafruit Trinket. + + by Bill Earl + for Adafruit Industries + + Required library is the Adafruit_SoftServo library + available at https://github.com/adafruit/Adafruit_SoftServo + The standard Arduino IDE servo library will not work with 8 bit + AVR microcontrollers like Trinket and Gemma due to differences + in available timer hardware and programming. We simply refresh + by piggy-backing on the timer0 millis() counter + + Trinket: Bat+ Gnd Pin #0 Pin #1 + Connection: Servo+ Servo- Tilt Rotate + (Red) (Brown) Servo Servo + (Orange)(Orange) + + *******************************************************************/ + +#include // SoftwareServo (works on non PWM pins) + +#define TILTSERVOPIN 0 // Servo control line (orange) on Trinket Pin #0 +#define ROTATESERVOPIN 1 // Servo control line (orange) on Trinket Pin #1 + +Adafruit_SoftServo TiltServo, RotateServo; //create TWO servo objects + +void setup() +{ + // Set up the interrupt that will refresh the servo for us automagically + OCR0A = 0xAF; // any number is OK + TIMSK |= _BV(OCIE0A); // Turn on the compare interrupt (below!) + + TiltServo.attach(TILTSERVOPIN); // Attach the servo to pin 0 on Trinket + RotateServo.attach(ROTATESERVOPIN); // Attach the servo to pin 1 on Trinket + delay(15); // Wait 15ms for the servo to reach the position +} + +void loop() +{ + delay(100); + TiltServo.detach(); // release the servo + RotateServo.detach(); // release the servo + + if(random(100) > 80) // on average, move once every 500ms + { + TiltServo.attach(TILTSERVOPIN); // Attach the servo to pin 0 on Trinket + TiltServo.write(random(120, 180)); // Tell servo to go to position + } + if(random(100) > 90) // on average, move once every 500ms + { + RotateServo.attach(ROTATESERVOPIN); // Attach the servo to pin 1 on Trinket + RotateServo.write(random(0, 180)); // Tell servo to go to position + } +} + +// We'll take advantage of the built in millis() timer that goes off +// to keep track of time, and refresh the servo every 20 milliseconds +// The SIGNAL(TIMER0_COMPA_vect) function is the interrupt that will be +// Called by the microcontroller every 2 milliseconds +volatile uint8_t counter = 0; +SIGNAL(TIMER0_COMPA_vect) +{ + // this gets called every 2 milliseconds + counter += 2; + // every 20 milliseconds, refresh the servos! + if (counter >= 20) + { + counter = 0; + TiltServo.refresh(); + RotateServo.refresh(); + } +} \ No newline at end of file diff --git a/3D_Printed_Bionic_Eye/3D_Printed_Bionic_Eye.py b/3D_Printed_Bionic_Eye/3D_Printed_Bionic_Eye.py new file mode 100644 index 000000000..42c43a1c3 --- /dev/null +++ b/3D_Printed_Bionic_Eye/3D_Printed_Bionic_Eye.py @@ -0,0 +1,24 @@ +# Laser Dog Goggles +# https://learn.adafruit.com/laser-dog-goggles + +import time +import board +import pulseio +from adafruit_motor import servo + +# servo pin for the M0 boards: +pwm = pulseio.PWMOut(board.A2, duty_cycle=2 ** 15, frequency=50) +my_servo = servo.Servo(pwm) +speed = .04 # 40ms lower value means faster movement +max_turn = 180 # rotation range 180 degree, half a circle + +while True: + # move stepper max_turn degrees clockwise + for angle in range(0, max_turn, 1): + my_servo.angle = angle + time.sleep(speed) + + # move stepper max_turn degrees counter clockwise + for angle in range(max_turn, 0, -1): + my_servo.angle = angle + time.sleep(speed) diff --git a/3D_Printed_Bionic_Eye/README.md b/3D_Printed_Bionic_Eye/README.md new file mode 100644 index 000000000..583065dfd --- /dev/null +++ b/3D_Printed_Bionic_Eye/README.md @@ -0,0 +1,4 @@ +# 3D_Printed_Bionic_Eye + +Code to accompany this Adafruit tutorial: +https://learn.adafruit.com/3d-printed-bionic-eye