Texas Instruments University ProgramDaniel Oglvie
Published © GPL3+

Spooky Halloween Prank with Microcontroller

The TI LaunchPad™ development kit team decided to have some fun and use this spooky LaunchPad kit-based Halloween project to prank them.

IntermediateFull instructions provided2 hours1,847
Spooky Halloween Prank with Microcontroller

Things used in this project

Story

Read more

Code

ULN2003BOOST.cpp

C/C++
//ULN2003BOOST.cpp
/* --COPYRIGHT--,BSD_EX
 * Copyright (c) 2016, Texas Instruments Incorporated
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * *  Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * *  Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * *  Neither the name of Texas Instruments Incorporated nor the names of
 *    its contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * This product is designed as an aid for customers of Texas Instruments.  No warranties, either express
 * or implied, with respect to this software or its fitness for any particular purpose is claimed by Texas
 * Instruments or the author.  The software is licensed solely on an "as is" basis.  The entire risk as to its
 * quality and performance is with the customer.
 * --/COPYRIGHT--*/
//******************************************************************************
#include "ULN2003BOOST.h"
#include "Energia.h"

int _Period;
int SRCLK = 13; //SRCLK for SN74HC595 is set as GPIO 13 in the Lauchpad standard
int RCLK = 12;  //RCLK for SN74HC595 is set as GPIO 12 in the Lauchpad standard
int SER = 11;   //SER for SN74HC595 is set as GPIO 111 in the Lauchpad standard
char halfStep[8] = {B10001000,B11001100,B01000100,B01100110,B00100100,B00110011,B00010001,B10011001}; //Defines the step waveform for half-Step
char fullStep[4] = {B11001100, B01100110, B00110011, B10011001};                                      //Defines the step waveform for full-Step
char waveDrive[4] = {B10001000, B01000100, B00100010, B00010001};                                     //Defines the step waveform for wave-drive

ULN2003BOOST::ULN2003BOOST(int Period) {
  //Assign delay for communication through the on board SN74HC595
  pinMode(SRCLK, OUTPUT);      
  pinMode(RCLK, OUTPUT);      
  pinMode(SER, OUTPUT);      
  pinMode(8, INPUT);         //used to fix any issues with GPIO8 *to be removed*
  digitalWrite (SRCLK, LOW); //writting low just in case
  digitalWrite (RCLK, LOW);
  digitalWrite (SER, LOW);
  _Period = Period;
}

void ULN2003BOOST::start(){
  //Blink and clear outputs for board
  clearOutput();
}

void ULN2003BOOST::sendByte(char Output){
  //Send user defined byte output
  digitalWrite(RCLK, LOW);
  digitalWrite(SER, LOW);
  shiftOut(SER, SRCLK, MSBFIRST, Output);
  digitalWrite (RCLK, HIGH);
  delay(_Period);
}

void ULN2003BOOST::clearOutput(){
  //Sets outputs to low or '0'
  sendByte(0x00);
}

void ULN2003BOOST::cwStepHalf(){
  //Step clockwise using a full Half-step sequence
  for(int i = 0; i < 8; i++){ //move accross array
    sendByte(halfStep[i]);
  }
}

void ULN2003BOOST::ccwStepHalf(){
  //Step counter-clockwise using a full Half-step sequence
  for(int i = 7; i >= 0; i--){ //move accross array
    sendByte(halfStep[i]);
  }
}

void ULN2003BOOST::cwStepFull(){
  //Step clockwise using a full Full-step sequence
  for(int i = 0; i < 4; i++){ //move accross array
    sendByte(fullStep[i]);
  }
}

void ULN2003BOOST::ccwStepFull(){
  //Step counter-clockwise using a full Full-step sequence
  for(int i = 3; i >= 0; i--){ //move accross array
    sendByte(fullStep[i]);
  }
}

void ULN2003BOOST::cwStepWave(){
  //Step clockwise using a full wave-drive sequence
  for(int i = 0; i < 4; i++){ //move accross array
    sendByte(waveDrive[i]);
  }
}

void ULN2003BOOST::ccwStepWave(){
  //Step counter-clockwise using a full wave-drive sequence
  for(int i = 3; i >= 0; i--){ //move accross array
    sendByte(waveDrive[i]);
  }
}

ULN2003BOOST.h

C Header File
//ULN2003BOOST.h
/* --COPYRIGHT--,BSD_EX
 * Copyright (c) 2016, Texas Instruments Incorporated
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * *  Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * *  Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * *  Neither the name of Texas Instruments Incorporated nor the names of
 *    its contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * This product is designed as an aid for customers of Texas Instruments.  No warranties, either express
 * or implied, with respect to this software or its fitness for any particular purpose is claimed by Texas
 * Instruments or the author.  The software is licensed solely on an "as is" basis.  The entire risk as to its
 * quality and performance is with the customer.
 * --/COPYRIGHT--*/
//******************************************************************************

extern char halfStep[8];
extern char fullStep[4];
extern char waveDrive[4];

class ULN2003BOOST {
  public: 
  ULN2003BOOST(int Period); //Initiate board with Pin assignments for serial data mode
  void start(); //Initiate board, clear and blink LEDs to signify ready to transmit
  void sendByte(char Output); //serialize out byte information entered by user or readout
  void clearOutput(); //set all outputs to low or '0'
  void cwStepHalf(); //completes a Half-Step sequence clockwise
  void cwStepFull(); //completes a Full-Step sequence clockwise
  void cwStepWave(); //completes a Wave-Drive sequence clockwise
  void ccwStepHalf(); //completes a Half-Step sequence counter-clockwise
  void ccwStepFull(); //completes a Full-Step sequence counter-clockwise
  void ccwStepWave(); //completes a Half-Step sequence counter-clockwise
};

pumpkin.ino

Arduino
#include "ULN2003BOOST.h"                      // Attach needed library for board functions
#include <BLE.h>

int period = 5;                              // Time defined in milliseconds
ULN2003BOOST myBoard = ULN2003BOOST(period);   // Create instance of ULN2003BOOST board
String bleString = "state";

void setup(){
  Serial.begin(115200);// Set pins and clear outputs
  ble.setLogLevel(BLE_LOG_ALL);
  ble.begin();
  ble.serial();
  ble.setAdvertName("Energia Serial");
  ble.startAdvert();
}

void loop(){
  if (ble.available())
  {
    bleString = ble.readString();
    Serial.println(ble.readString());
    // Turn on the motor. Note: conflicts with BLE?
    myBoard.start(); 
    int i = 300;
    if (bleString == "boo!")
    {
      // Drop the spider!
      while(i>0)
      {
        myBoard.cwStepFull();
        i--;
      }
    }
    if (bleString == "up")
    {
      while(i>0)
      {
        myBoard.ccwStepFull();
        i--;
      }
    }
  }
}

Github

Credits

Daniel Ogilvie

Comments