Let’s get moving! You can use the code here to animate the robots Tadro and Sparki.
Cycloptic Tadro
Get started building your own Tadro! Nick Livingston and I wrote this code for our light-seeking cycloptic (one-eyed) Tadro. The program runs on an Arduino Uno. You can learn how to build the Tadro if you tune into my educational program, “Robotics,” through The Great Courses (tentative release date: June, 2015). Just copy and paste this program (below, in blue) into an open sketch in the Arduino IDE (integrated development environment, which you can download for free).
/**************** Light-seeking Cycloptic (one-eyed) Tadro. This program is a HACK, combining two sample programs that you can find Ardunio.com — Program 1: Sweep by BARRAGAN <http://barraganstudio.com> This example code is in the public domain. Program 2: Knob Controlling a servo position using a potentiometer (variable resistor) by Michal Rinott <http://people.interaction-ivrea.it/m.rinott> On 23 Dec 2014, John Long and Nick Livingston combined and modified these two programs in order create this program to run a ONE-EYED TADRO. Changing light intensities at the light-dependent photoresistor changes beta, the turning angle of the flapping tail. Phototaxis results from embodiment. This code was created for "Robotics," a program produced by The Great Courses. ******************/ #include <Servo.h> // loading library code to drive servo motor Servo myservo; // create servo object to control a servo // a maximum of eight servo objects can be created int pos = 0; // variable to store the servo position int alpha = 30; // amplitude (not double amplitude) int beta = 0; // variable to store turning int shift = 90; // makes zero degrees at 12 o'clock int LDRpin = 0; // variable to read the light-dependent photoresistor int eye1; // variable to hold the light intensity of the first eye void setup() { myservo.attach(9); // attaches the servo on pin 9 to the servo object pos = shift; // assigns the shift value to the servo position for set-up before swim myservo.write(pos); // moves the servo to the "zero" position to allow tail alignment delay(5000); // pause of 5 sec to allow adjustments } void loop() { int minpos; // local variable to set limit on servo motion to one side int maxpos; // local variable to set limit on servo motion to other side eye1 = analogRead(LDRpin); // reading light intensity beta = map(eye1, 0, 1023, -90+alpha, 90-alpha); // scale light value & calculate tail offset beta minpos = shift+beta-alpha; // set limit to servo (part of amplitude) maxpos = shift+beta+alpha; // set limit to servo (part of amplitude) for(pos = minpos; pos < maxpos; pos += 1) // steps through FOR loop from minpos to maxpos { // in steps of 1 degree; to increase frequency change degree myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } for(pos = maxpos; pos>=minpos+1; pos-=1) // steps through FOR loop from maxpos to minpos { myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } }
Safe-driving Sparki
Own a Sparki robot? Then try out the safe-driver program, a partial-autonomy program inspired by the great idea of Peter Staten. You can learn more about Sparki and this code if you tune into my educational program, “Robotics,” through The Great Courses (tentative release date: June, 2015). Just copy and paste this program (below, in blue) into an open sketch in the SparkiDuino IDE (integrated development environment, which you can download for free).
/******************************************* IR Remote + Edge_Avoidance Peter Staten's idea was the Safe Driver Program: have Sparki let you, the human, have control until you, the human, do something stupid, like try to drive it off the table. Then Sparki takes over, stops the forward motion, and turns. Peter and I combined the following programs available as sample code from Arcbotics.com: remote control and edge avoidance. 7 March 2015. ********************************************/ #include <Sparki.h> // include the sparki library void setup() { sparki.clearLCD(); } void loop() { int threshold = 100; // If sensor reading is below this value, then no surface underneath --> edge! int code = sparki.readIR(); int edgeLeft = sparki.edgeLeft(); // Read the left edge IR sensor. int edgeRight = sparki.edgeRight(); // Read the right edge IR sensor. if (edgeLeft < threshold) // If no surface underneath left sensor... { sparki.moveRight(20); // ... then turn right. } if (edgeRight < threshold) // If no surface underneath right sensor... { sparki.moveLeft(20); // ... then turn left/ } //If no edge is detected then you, the driver, have remote control for 0.5 seconds. if(code != -1){ sparki.print("Received code: "); sparki.println(code); } switch(code){ // Movement buttons (these are clear when you have the remote control in hand) case 70: sparki.moveForward(); break; case 21: sparki.moveBackward(); break; case 67: case 71: sparki.moveRight(); break; case 68: case 69: sparki.moveLeft(); break; case 64: sparki.moveStop(); sparki.gripperStop(); break; // Gripper Buttons case 9: sparki.gripperOpen(); break; case 7: sparki.gripperClose(); break; // buzzer case 74: sparki.beep(); break; // Servo case 90: sparki.servo(SERVO_LEFT); break; case 28: sparki.servo(SERVO_CENTER); break; case 8: sparki.servo(SERVO_RIGHT); break; // RGB LED case 25: sparki.RGB(RGB_OFF); break; case 12: sparki.RGB(RGB_RED); break; case 24: sparki.RGB(RGB_GREEN); break; case 94: sparki.RGB(RGB_BLUE); break; default: break; } sparki.updateLCD(); delay(200); // wait 0.5 seconds }