Seth Bucher
Published

Card Dealing Machine

This machine, built around a MSP430G2ET, uses a DC motor to deal out a card when motion is detected within 4 inches of an ultrasonic sensor.

IntermediateShowcase (no instructions)3
Card Dealing Machine

Things used in this project

Hardware components

MSP-EXP430G2ET Value Line MSP430 LaunchPad™ Development Kit
Texas Instruments MSP-EXP430G2ET Value Line MSP430 LaunchPad™ Development Kit
×1
DC Motor, 12 V
DC Motor, 12 V
×1
Stepper Motor
Digilent Stepper Motor
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
Dual H-Bridge motor drivers L298
SparkFun Dual H-Bridge motor drivers L298
×1

Hand tools and fabrication machines

3D Printer (generic)
3D Printer (generic)

Story

Read more

Schematics

Project Report

Code

Card Dealing Machine

C/C++
//---------------------------------------------------------------------------------
// Final Code – Card Dealing Machine
//---------------------------------------------------------------------------------

#include <msp430g2553.h>

// Definitions to Improve Code Readability
#define L1 BIT1
#define L2 BIT2
#define L3 BIT4
#define L4 BIT5
#define delay 10000
#define Button BIT3
#define ECHO BIT6
#define MOTOR BIT7
#define GreenLED BIT0
#define Player_Distance 4
#define TRUE 1
#define FALSE 0

volatile int pulse_time[2];             // Used to store time between sensor rising 
    // and falling edge
volatile float time;                    // Used to store ultrasonic sensor time
volatile float distance;                // Used to store distance to object
volatile unsigned int i=0;              // Used in a for loop
int position = 0;                       // Used to keep track of stepper position

void Spin_Twelfth ();                   // Spins the stepper motor 30 degrees
void Flick_Card ();                     // Briefly spins the DC motor
int Player_Detected (float dst);        // Returns true if a player is detected

void main(void)
{
  WDTCTL = WDTPW + WDTHOLD;             // Stop watchdog timer

  P1DIR |= L1 + L2 + L3 + L4;           // Configure P1.0,1,2,4 as Output
  P2DIR |= MOTOR;                       // Configure P2.6 as Output for DC motor
  P2OUT &= ~MOTOR;                      // Turn DC motor off initially
  P1REN = Button;                       // Enable pull-up resistor
  P1OUT = Button;                       // Sets button initially unpressed
  P1IE = Button;                        // Enable button interrupt

  BCSCTL1= CALBC1_1MHZ;                 // Setting Clock frequency to 1 MHz
  DCOCTL = CALDCO_1MHZ;                 // Setting Clock frequency to 1 MHz
  P1DIR = ECHO;                         // P1.6 set as output
  P2SEL = BIT1;                         // P2.1 Set as timer
  P1SEL = ECHO;                         // Ultrasonic echo pin set as timer

  P1DIR |= GreenLED;                    // Configure Green LED pin as output

  TA0CTL = TASSEL_2|MC_1 ;              // SMclock selected, up mode, compare mode
  TA0CCR0 = 0xFFFF;                     // Used for period of sensor (65.535 ms)
  TA0CCR1 = 0x000A;                     // Used to send 0.01 ms trigger pulse
  TA0CCTL1 = OUTMOD_7;                  // Used for PWM
  TA1CTL = TASSEL_2|MC_2 ;              // SMclock, continuous mode
  TA1CCTL1 = CAP | CCIE | CCIS_0 | CM_3 | SCS ;       // Capture mode, interrupts 
// enabled, capture compare

  __enable_interrupt();

    while (1)                           // Loop forever
    {
        distance = time/148;            // Dividing by 148 converts into seconds
        Player_Detected(distance);      // Run player detection function

        if (Player_Detected(distance))  // If a player is detected
        {
            Flick_Card();               // Flicks a card when a player is found
        }
    }
}


void Spin_Twelfth ()                    // Spins the stepper motor 30 degrees
{                                       // Motor is driven in half step mode
    P1OUT |= L1;                        // Set Line 1 High
    P1OUT &= ~L2;                       // Set Line 2 Low
    P1OUT &= ~L3;                       // Set Line 3 Low
    P1OUT &= ~L4;                       // Set Line 4 Low
    __delay_cycles(delay);              // Delay for 10 ms

    P1OUT |= L1;                        // Set Line 1 High
    P1OUT |= L2;                        // Set Line 2 High
    P1OUT &= ~L3;                       // Set Line 3 Low
    P1OUT &= ~L4;                       // Set Line 4 Low
    __delay_cycles(delay);              // Delay for 10 ms

    P1OUT &= ~L1;                       // Set Line 1 Low
    P1OUT |= L2;                        // Set Line 2 High
    P1OUT &= ~L3;                       // Set Line 3 Low
    P1OUT &= ~L4;                       // Set Line 4 Low
    __delay_cycles(delay);              // Delay for 10 ms

    P1OUT &= ~L1;                       // Set Line 1 Low
    P1OUT |= L2;                        // Set Line 2 High
    P1OUT |= L3;                        // Set Line 3 High
    P1OUT &= ~L4;                       // Set Line 4 Low
    __delay_cycles(delay);              // Delay for 10 ms

    P1OUT &= ~L1;                       // Set Line 1 Low
    P1OUT &= ~L2;                       // Set Line 2 Low
    P1OUT |= L3;                        // Set Line 3 High
    P1OUT &= ~L4;                       // Set Line 4 Low
    __delay_cycles(delay);              // Delay for 10 ms

    P1OUT &= ~L1;                       // Set Line 1 Low
    P1OUT &= ~L2;                       // Set Line 2 Low
    P1OUT |= L3;                        // Set Line 3 High
    P1OUT |= L4;                        // Set Line 4 High
    __delay_cycles(delay);              // Delay for 10 ms

    P1OUT &= ~L1;                       // Set Line 1 Low
    P1OUT &= ~L2;                       // Set Line 2 Low
    P1OUT &= ~L3;                       // Set Line 3 Low
    P1OUT |= L4;                        // Set Line 4 High
    __delay_cycles(delay);              // Delay for 10 ms

    P1OUT |= L1;                        // Set Line 1 High
    P1OUT &= ~L2;                       // Set Line 2 Low
    P1OUT &= ~L3;                       // Set Line 3 Low
    P1OUT |= L4;                        // Set Line 4 High
    __delay_cycles(delay);              // Delay for 10 ms
}

#pragma vector=PORT1_VECTOR             // Button interrupt, spins stepper motor
__interrupt void Button_Interrupt(void)
{
    Spin_Twelfth();                     // Spin stepper 30 degrees
    P1IFG = ~Button;                    // Clear interrupt flag for next time
}

void Flick_Card ()                      // Spins the DC motor to deal one card
{
    int flick_duration;                 // Length motor is driven
    for (flick_duration = 10; flick_duration > 0; flick_duration--)
    {
        P2OUT |= MOTOR;                 // Turns on the motor
        __delay_cycles(12500);          // Delay for 12.5 ms
        P2OUT &= ~MOTOR;                // Turns off the motor
        __delay_cycles(500);            // Delay for 0.5 ms
    }
    __delay_cycles(250000);             // Delay to avoid dealing multiple cards 
    // for one wave
}

#pragma vector = TIMER1_A1_VECTOR       // Finds time of signal which is used to 
    // find distance
__interrupt void Timer_A_Ultrasonic_Sensor(void)
{
        pulse_time[i] = TA1CCR1;        // Stores rising edge time
        i += 1;
        TA1CCTL1 &= ~CCIFG ;            // Clear interrupt flag
        if (i==2)                       // On second interrupt (falling edge)
        {
            time=pulse_time[i-1]-pulse_time[i-2];   // Find time between interrupts
            i=0;                        // Set to 0 for next repetition
        }
}

int Player_Detected (float dst)         // Returns TRUE if player detected within 
    // acceptable range
{
    if (dst <= Player_Distance)         // If object detected within given range
    {
        P1OUT |= GreenLED;              // Used for debugging, turn on GreenLED
        return TRUE;                    // Return true if player is detected
    }
    else                                // If object is not within acceptable range
    {
        P1OUT &= ~GreenLED;             // Used for debugging, turn off GreenLED
        return FALSE;                   // Return false if no player is detected
    }
}
//---------------------------------------------------------------------------------

Credits

Seth Bucher

Seth Bucher

0 projects • 0 followers

Comments

Add projectSign up / Login