Code examples from today’s class

Example 1: Spin Search

//
// Spin search sumo example MSP430G2553
// Written by Ted Burke - Last modified 30-11-2016
//
// P1.0 is proximity detector
// P1.1 is colour sensor
// P2.0 is green LED
// P2.1 and P2.2 are left motor
// P2.3 and P2.4 are right motor
// 

#include <msp430.h>

void main( void )
{
    WDTCTL = WDTPW + WDTHOLD; // Disable watchdog timer
    
    // Digital i/o
    P1DIR = 0b00000000; // Sensors: P1.0 is proximity, P1.1 is colour
    P2DIR = 0b00011111; // Motors and LED
    
    while(1)
    {
        if (P1IN & BIT0) // Proximity sensor
        {
            P2OUT = 0b00010101; // Forward with LED on
        }
        else
        {
            P2OUT = 0b00001100; // LM forward, RM reverse, LED off
        }       
    }
}

Example 2: Bounce Around Table

//
// Bounce around sumo robot example MSP430G2553
// Written by Ted Burke - Last modified 30-11-2016
//
// P1.0 is proximity detector
// P1.1 is colour sensor
// P2.0 is green LED
// P2.1 and P2.2 are left motor
// P2.3 and P2.4 are right motor
// 

#include <msp430.h>

void main( void )
{
    WDTCTL = WDTPW + WDTHOLD; // Disable watchdog timer
    
    // Digital i/o
    P1DIR = 0b00000000; // Sensors: P1.0 is proximity, P1.1 is colour
    P2DIR = 0b00011111; // Motors and LED
    
    while(1)
    {
        P2OUT = 0b00010100; // Forward with LED off
        
        if (P1IN & BIT1) // Colour sensor white?
        {
            P2OUT = 0b00001101;     // LM forward, RM reverse, LED on
            __delay_cycles(750000); // 0.75 seconds
        }
    }
}

Example 3: Spin Search and Move

//
// Sumo robot example MSP430G2553
// Written by Ted Burke - Last modified 30-11-2016
//
// P1.0 is proximity detector
// P1.1 is colour sensor
// P2.0 is green LED
// P2.1 and P2.2 are left motor
// P2.3 and P2.4 are right motor
// 

#include <msp430.h>

void main( void )
{
    WDTCTL = WDTPW + WDTHOLD; // Disable watchdog timer
    
    // Digital i/o
    P1DIR = 0b00000000; // Sensors: P1.0 is proximity, P1.1 is colour
    P2DIR = 0b00011111; // Motors and LED
    
    int counter;
    
    while(1)
    {
        // Search for opponent
        counter = 0;
        while(counter < 2000)
        {
            P2OUT = 0b00001100;     // LM forward, RM reverse, LED off
            
            if (P1IN & BIT0) break; // Proximity sensor detects object?
            
            counter = counter + 1;  // increment counter
            __delay_cycles(1000);   // 1 ms delay
        }
        
        // Driving to white line
        while ((P1IN & BIT1) == 0) // While colour sensor stays black
        {
            P2OUT = 0b00010101;     // LM forward, RM reverse, LED on
        }
        
        // Turn and drive back into the centre of table
        P2OUT = 0b00010010;     // turn
        __delay_cycles(500000); // 500 ms delay
        P2OUT = 0b00010100;     // forward
        __delay_cycles(1000000);
    }
}
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