// // Daniel's robot // #include <msp430.h> // function prototype int distance(); // LED and motor control function definitions void stop() {P2OUT = 0b00000000;} void forward() {P2OUT = 0b00001010;} void reverse() {P2OUT = 0b00001010;} void spinleft() {P2OUT = 0b00001001;} void spinright() {P2OUT = 0b00000110;} void leds(int n) {P1OUT = 0b00011110 + n;} // define pin constants #define GREEN_LED 0b00000001 #define RED_LED 0b10000000 #define TRIG_PIN 0b01000000 #define ECHO_PIN 0b00100000 // main function int main( void ) { // variables for distance and distance counter int d, n; // disable watchdog timer to prevent time out reset WDTCTL = WDTPW + WDTHOLD; // configure digital inputs and outputs P1OUT = 0b00000000; P1REN = 0b00011110; // enable pull-downs on P1.1-4 P1DIR = GREEN_LED + RED_LED + TRIG_PIN; P2DIR = 0b00001111; // P2.0-1 left motor, P2.2-3 right motor // main loop implements state machine while(1) { // searching while(1) { leds(RED_LED); spinleft(); __delay_cycles(150000); stop(); __delay_cycles(50000); d = distance(); if (d < 50) break; } // charging at opponent n = 5; while(n > 0) { leds(GREEN_LED); forward(); d = distance(); if (d < 50) n = 5; else n = n - 1; } } return 0; } // // This function measures distance in cm using the rangefinder. // It assumes TRIG is P1.3 and ECHO is P1.2. // int distance() { // Send 20us trigger pulse P1OUT |= TRIG_PIN; __delay_cycles(20); P1OUT &= ~TRIG_PIN; // Wait for start of echo pulse while((P1IN & ECHO_PIN) == 0); // Measure how long the pulse is int d = 0; while((P1IN & ECHO_PIN) > 0) { // The following delay was worked out by trial and error // so that d counts up in steps corresponding to 1cm __delay_cycles(30); d = d + 1; if (d >= 400) break; } // Leave 100ms for any ultrasound reflections to die away __delay_cycles(100000); // Pass the measured distance back to the calling function return d; }
What is RoboSlam?
RoboSlam is an ultra low-cost robot building workshop for beginners using real electronic components. Our aim is to create a recipe for a €10 robot that’s easy to build and easy to teach others to build.
RoboSlam by Ted Burke, Damon Berry, Shannon Chance, Frank Duignan is licensed under a Creative Commons Attribution 4.0 International License.Follow RoboSlam on Twitter
My TweetsBlog Stats
- 113,713 hits
-
Recent Comments
-
Recent Posts
- TY photos keep rolling in!
- The final moments of suspense at BioSlam
- More photos from “Engineering Your Future” 2022
- Building heart rate monitors for kicks at TU Dublin
- Engineering Your Future at TU Dublin, 2022
- Mighty Dublin Makers
- Bread Board Games!
- Do-it-yourself punch cards and other amazing feats: DIT’s Paper Programming booth at Dublin Maker 2018
- New “Bread Board Games” for Science Week in Bunclody
- Engineering Your Future–a week of adventures in engineering at DIT
- Template for laser cutting in Inkscape
- More from Engineers Week 2017
- Engineers Week 2017
- Adults love robots, too….
- Creative Techfest’s RoboSlam at Google
- CopyCat in Action
- Introducing ArcadeSlam and the CopyCat mini-arcade platform
- Spreading Joy with Robots and Pasta
- Roboslam at Dublin Maker 2016
- Photos of roboteers at Dublin Maker 2016
- RoboSlam Instructions Dublin Maker 2016
- RoboSlam robot for Dublin Maker 2016
- Living Knowledge
- RoboSlam at PAEE/ALE 2016, Guimarães Portugal
- Robotics for Transition Year
- RoboSlam at Enniscorthy
- The “twoeyes” program from Enniscorthy
- Engineer’s Week: RoboSlam with a Twist
- Alternative version of the zigzag example using motor control functions
- Luke’s Robot Show-and-Tell
- Getting ready for a busy week…
- Robot build instructions and code for Tech Week / Science Week Oct/Nov 2015
- Colour Sensor Example Code
- Workshop for Roboslam tutors at IT Blanchardstown
- A remote control bot
- Instructions, laser cutting patterns and C code for RoboSlam robots at Dublin Maker 2015
- RoboSlam @ Dublin Maker – only two days away!
- Engineer Your Future Roboslam Tournament Winners 2015
- Engineer Your Future with Electronics
- Daniel’s code
- RoboSlam Testing Code and hex file
- DIT Team Deliver RoboSlam Workshop for Tech Week
- Assembling the new laser-cut chassis
- RoboSlam Instructions for CAO Open Day 2015
- Building Robots for Engineers Week
- On the Road to Victory
- Instructions for assembling the colour sensor module
- Tip-Top Design Skills
- FOCAS on Robots
- Workshop at Kinsale Arts Festival