Find file
Fetching contributors…
Cannot retrieve contributors at this time
87 lines (63 sloc) 1.85 KB
/* This demo will see the Train Overlord moving back and forth,
with forth having the laser/servos on, and back having them off
back and forth being triggered by the IR sensor on the markers
#include <AFMotor.h>
#include <Servo.h>
#include <math.h>
// DC hobby servo
Servo LaserX;
Servo LaserY;
boolean trainDir = false;
#define DEBUG 0
#define IRSensor A0
#define MarkerIRThreshhold 580
#define DropLength 8000
#define SaneResenseTime 2000
#define WheelMotorSpeed 255
#define WheelForwardDirection FORWARD
#define WheelBackwardDirection BACKWARD
AF_DCMotor wheelMotor(1, MOTOR12_8KHZ);
unsigned long senseTimer; //Used to ride past the marker so we don't sense the same one twice.
int angle = 0;
int LaserPin = 13;
int readIR() {
return analogRead(IRSensor);
void setup() {
#if DEBUG == 1
Serial.println("DEMO -1- BEGINS");
pinMode(IRSensor, INPUT);
//Setup motors.
pinMode(LaserPin, OUTPUT);
digitalWrite(LaserPin, HIGH);
void loop() {
int irReading;
// move the train (this sometimes causes it to flip direction); trainDir ? WheelBackwardDirection : WheelForwardDirection );
// calculate the new laser positions and set the servos
if (trainDir){
if (angle > 360) angle = 0;
// check the IR sensor to see if we're hit the barrier
// if so, switch the train direction
irReading = readIR();
if(irReading >= MarkerIRThreshhold && (millis() - senseTimer >= SaneResenseTime)) {
trainDir = !trainDir;
senseTimer = millis();
digitalWrite(LaserPin, trainDir);