Robot build instructions and code for Tech Week / Science Week Oct/Nov 2015

20151028_051718

//
// RoboSlam pre-programming code: Navigate by ground colour
// Written 28-10-2015 for Tech Week RoboSlam
// Code is for MSP430G2452 or MSP430G2553
//

#include <msp430.h>
#include <stdlib.h>
 
int main( void )
{
    // stop watchdog timer to prevent time out reset
    WDTCTL = WDTPW + WDTHOLD;
    
    // P2.0 and P2.1 control right motor
    // P2.4 and P2.5 control left motor
    // P1.0 controls the green LED
    P2DIR = 0b00110011; 
    P1DIR = 0b00000001;
    
    int rnd_delay, i;
     
    while(1)
    {
        // check colour by looking at digital signal on P1.7
        if (P1IN & BIT7)
        {
            // white sensed
            P2OUT = 0b00100001; // spin left
            P1OUT = 0b00000001; // LED on
            rnd_delay = rand() % 1000;
            for (i=0 ; i<rnd_delay ; i++)
            {
                __delay_cycles(1000);
            }
        }
        else
        {
            // black sensed
            P2OUT = 0b00010001; // drive forward
            P1OUT = 0b00000000; // LED off
        }
    }
    
    return 0;
}
Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s