Rangefinder code examples from today’s RoboSumo lecture

Example 1:

//
// Rangefinder example for MSP430G2452
// Written by Ted Burke - Last modified 13-4-2016
//
 
#include <msp430.h>
 
void main()
{
    WDTCTL = WDTPW + WDTHOLD; // Disable watchdog timer
     
    P1DIR = 0b00001001; // P1.0 is LED, P1.3 is trigger, P1.4 is echo
    
    int robot_detected = 0;
    
    // Main loop repeats forever
    while(1)
    {
        // Send trigger pulse
        P1OUT |= BIT3;      // Set P1.3 high
        __delay_cycles(20); // Delay for 20us
        P1OUT &= ~BIT3;     // Set P1.3 low
        
        // Wait for start of echo pulse
        while ((P1IN & BIT4) == 0);
        
        // Check echo state after 3ms
        __delay_cycles(3000); // Delay for 3ms
        if ((P1IN & BIT4) > 0) robot_detected = 0;
        else                   robot_detected = 1;
        
        // Wait for echo pulse to end (if necessary)
        while ((P1IN & BIT4) > 0);
        
        // Light LED if object is detected
        if (robot_detected == 1) P1OUT = 0b00000001; // LED on
        else                     P1OUT = 0b00000000; // LED off
        
        __delay_cycles(50000); // 50ms delay to let reflections fade away
    }
}

Example 2:

//
// Rangefinder example for MSP430G2452
// Written by Ted Burke - Last modified 13-4-2016
//
 
#include <msp430.h>

int rangefinder();
 
void main()
{
    int x;
    
    WDTCTL = WDTPW + WDTHOLD; // Disable watchdog timer
     
    P1DIR = 0b00001001; // P1.0 is LED, P1.3 is trigger, P1.4 is echo
    
    // Main loop repeats forever
    while(1)
    {
        x = rangefinder();
        if (x == 1) P1OUT = BIT0; // LED on
        else P1OUT = 0;           // LED off
    }
}

int rangefinder()
{
    int robot_detected;
    
    // Send trigger pulse
    P1OUT |= BIT3;      // Set P1.3 high
    __delay_cycles(20); // Delay for 20us
    P1OUT &= ~BIT3;     // Set P1.3 low
    
    // Wait for start of echo pulse
    while ((P1IN & BIT4) == 0);
    
    // Check echo state after 3ms
    __delay_cycles(3000); // Delay for 3ms
    if ((P1IN & BIT4) > 0) robot_detected = 0;
    else                   robot_detected = 1;
    
    // Wait for echo pulse to end (if necessary)
    while ((P1IN & BIT4) > 0);
    
    // Light LED if object is detected
    if (robot_detected == 1) return 1;
    else                     return 0;
}

Example 3:

//
// Rangefinder example for MSP430G2452
// Written by Ted Burke - Last modified 13-4-2016
//
 
#include <msp430.h>

int rangefinder();

void main()
{
    int x;
    int counter = 0;
    
    WDTCTL = WDTPW + WDTHOLD; // Disable watchdog timer
     
    P1DIR = 0b00001001; // P1.0 is LED, P1.3 is trigger, P1.4 is echo
    
    // Main loop repeats forever
    while(1)
    {
        x = rangefinder();
        
        if (x == 1) counter = 10;
        else if (counter > 0) counter = counter - 1;
        
        if (counter > 0) P1OUT = BIT0; // LED on
        else             P1OUT = 0;    // LED off
    }
}

int rangefinder()
{
    int robot_detected;
    
    // Send trigger pulse
    P1OUT |= BIT3;      // Set P1.3 high
    __delay_cycles(20); // Delay for 20us
    P1OUT &= ~BIT3;     // Set P1.3 low
    
    // Wait for start of echo pulse
    while ((P1IN & BIT4) == 0);
    
    // Check echo state after 3ms
    __delay_cycles(3000); // Delay for 3ms
    if ((P1IN & BIT4) > 0) robot_detected = 0;
    else                   robot_detected = 1;
    
    // Wait for echo pulse to end (if necessary)
    while ((P1IN & BIT4) > 0);
    
    // Light LED if object is detected
    if (robot_detected == 1) return 1;
    else                     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