/* Built on Serial Event example, and AF motor example from Adafruit
When new serial data arrives, this sketch adds it to a String.
When a newline is received, the loop prints the string and
clears it.
A good test for this is to try it with a GPS receiver
that sends out NMEA 0183 sentences.
Created 9 May 2011
by Tom Igoe
This example code is in the public domain.
//
http://www.arduino.cc/en/Tutorial/SerialEvent
*/
// MRF 8 30 2013 try adding some motor control
//#include <AFMotor.h>
//AF_DCMotor motor(4);
//AF_DCMotor motor2(2, MOTOR12_64KHZ); // create motor #2, 64KHz pwm
//AF_DCMotor motor4(4, MOTOR12_64KHZ); // create motor #2, 64KHz pwm
// MRF 11 28 2013 add temp control - library, code and hardware from Adafruit industries
#include "Adafruit_MAX31855.h"
int thermoDO = 5;
int thermoCS = 4;
int thermoCLK = 3;
// optional setlimitF for different message or whatever
const int SetlimitF = 180;
Adafruit_MAX31855 thermocouple(thermoCLK, thermoCS, thermoDO);
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
// Or, create it with a different I2C address (say for stacking)
// Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61);
// Select which 'port' M1, M2, M3 or M4. In this case, M1
Adafruit_DCMotor *Motor1 = AFMS.getMotor(1);
// You can also make another motor on port M2
Adafruit_DCMotor *Motor2 = AFMS.getMotor(2);
Adafruit_DCMotor *Motor3 = AFMS.getMotor(3);
Adafruit_DCMotor *Motor4 = AFMS.getMotor(4);
int ledPin=13; //LED connected to pin 13 on Arduino UNO.
int ControllerNum;// first character that comes in from pass-thru program
int BtnNum;// next two numbers to come in
int ThrottleSet;// three digit number from 000-255
int LoopCounter;
int array[25];
void setup() {
// initialize serial:
// Serial.begin(9600);/// works well
// Serial.begin(14400);
// Serial.begin(19200);
// Serial.begin(57600);
Serial.begin(115200);/// this one is working fine -9/12/2013
// clear the array, might want to use an array for each gamepad to store
// status of controls, not sure. Right now only using array[1-7] as an inputbuffer
for (int i=1; i < 26 ; i++)
{
array
= 0;
}
ControllerNum=0;
BtnNum=0;
ThrottleSet=0;
AFMS.begin(); // create with the default frequency 1.6KHz
// AFMS.begin(1000); // OR with a different frequency, say 1KHz
}
void loop()
{
uint8_t i;
LoopCounter=LoopCounter+1;
if (Serial.available())
{
delay(20);
for (int i=1; i < 7 ; i++)
{
array = Serial.read()-48;
}
}// end of serial loop now whatever came in on the serial port is flushed but the
// info is in the array
// can have up to 4 XBOX type gamepads, this command came from the one indicated
// in the first position of the serial input
ControllerNum = array[1];
// controller can only be 1-4- I'm going to use 9 as a code for 'all clear' for all controllers
// if the controllernum>4 and less than 9, subtract 4 to get the 'all clear' message for 1 controller
// this saves bandwidth by sending just one byte to signify 'reset all'
// so if a '5' code comes in, the system should take that to mean that all of the controlls on gamepad 1 have beon released
BtnNum=(array[2]*10)+array[3];
// bytes 4-6 are the throttle locations (for buttons 1-10)
// they will be 001 for the rest of the buttons
ThrottleSet=(array[4]*100)+(array[5]*10)+array[6];
if (ControllerNum>0)
{
// for debugging- send back what I got
//Serial.println(ControllerNum);
//Serial.println(BtnNum);
//Serial.println(ThrottleSet);
}
// left stick is button 1, 2, 3, and 4
// button 1 is left stick X axis positive numbers-Up (000-255)
// button 2 is left stick X axis negative numbers-Down (000-255)
// button 3 is left stick Y axis positive numbers-Right (000-255)
// button 4 is left stick Y axis negative numbers-Left (000-255)
// Controller 1 will control motor2 with left stick
switch (ControllerNum)
{
case 1:
// controllerNum is the 'player number'
// I'm making controller 1 the pilot, he gets most of the controls
switch (BtnNum)
{ case 1:
Motor1->run(FORWARD);
Motor1->setSpeed(ThrottleSet);
Motor2->run(FORWARD);
Motor2->setSpeed(ThrottleSet);
Motor3->run(FORWARD);
Motor3->setSpeed(ThrottleSet);
Motor4->run(FORWARD);
Motor4->setSpeed(ThrottleSet);
//motor2.run(FORWARD);
//motor2.setSpeed(ThrottleSet);
// want to send some gamepad feedback?
Serial.print("FB");// signal that you're sending a feedback command
Serial.print(ControllerNum);// controller num
Serial.print(9);// first param is left motor (low freq- 0-9)
Serial.println(9);// Second param is right motor (high freq- 0-9)
if (ThrottleSet<20)
{
Motor1->run(RELEASE);
Motor2->run(RELEASE);
Motor3->run(RELEASE);
Motor4->run(RELEASE);
Serial.print("FB");// signal that you're sending a feedback command
Serial.print(ControllerNum);// controller num
Serial.print(0);// first param is left motor (low freq- 0-9)
Serial.println(0);// Second param is right motor (high freq- 0-9)
}
break;
case 2:
Motor1->run(BACKWARD);
Motor1->setSpeed(ThrottleSet);
Motor2->run(BACKWARD);
Motor2->setSpeed(ThrottleSet);
Motor3->run(BACKWARD);
Motor3->setSpeed(ThrottleSet);
Motor4->run(BACKWARD);
Motor4->setSpeed(ThrottleSet);
// motor2.run(BACKWARD);
// motor2.setSpeed(ThrottleSet);
// try some feedback
Serial.print("FB");// signal that you're sending a feedback command
Serial.print(ControllerNum);// controller num
Serial.print(0);// first param is left motor (low freq- 0-9)
Serial.println(3);// Second param is right motor (high freq- 0-9)
if (ThrottleSet<20)
{Motor1->run(RELEASE);
Motor2->run(RELEASE);
Motor3->run(RELEASE);
Motor4->run(RELEASE);
Serial.print("FB");// signal that you're sending a feedback command
Serial.print(ControllerNum);// controller num
Serial.print(0);// first param is left motor (low freq- 0-9)
Serial.println(0);// Second param is right motor (high freq- 0-9)
}
break;}
// add all 24 cases here for controller 1
break;
//********************Anything scoped to the first controller should be above here
case 2:// controllerNum=2
{ // controllerNum =2 can only turn on and off the LED with his left trigger
}
break;
// ControllerNum=2*********************************END*************
case 5://
// controllerNum is the 'player number'
// I'm making controller 1 the pilot, he gets most of the controls
// code '5' is the 'all clear' code for controller 1- implement whatever changes that means (retain cruise or trim for some types of controllers,
// reset all motors for others- whatever you decide should happen when the controller is released.
// this uses less bandwidth than sending a reset for any button, let alone all of them.
//motor2.run(RELEASE);
break;
case 9:
// set all to default settings, turn off all motors (unless something should retain state at this point.
// 9 means all controllers have been released completely
break;
}
// end of switch for controllerNum
delay (10);
for (int i=1; i < 7 ; i++)
{
array = 0;
}
// print the temp coming in from thermocouple
if (LoopCounter>100)// seems to be about once every second or so at 100
{
Serial.print("Temp=");
Serial.print(thermocouple.readFarenheit());
Serial.println(" deg. F");
// reset it, only print ever 100 loops?
LoopCounter=0;
}
}