Final Demonstration Code
Posted on Friday, 18 January 2013
|
No Comments
In this post is all the Arduino source code for each robot, and the games master Processing code. See the individual posts for a walk-through of each section and it's function.
Final Arduino Code for Robot 1
#include <Servo.h> #include "avr/interrupt.h" char instruction [10]; //instruction sent to arduino int i = 0; //index for instruction array int j = 0; //index for for loop String stringXCoord = ""; int intXCoord; //x coordinate for servo String stringYCoord = ""; int intYCoord; //y coordinate for servo Servo servoX; //servo x Servo servoY; //servo y //message to send on to Robot 2 String resendMessage = ""; //string to print to contact games master - in the following form: //G (games master) 2 (robot 2's score) 10 (how much to increment score) String GMscoreLR = "[G205]"; String GMscoreB = "[G210]"; int laser = 13; //pin for laser int ldrLR = 2; //pin for LDR interrupt 1 int ldrB = 3; //pin for LDR interrupt 2 int headLights = 4; //pin for front lights int leftLights = 8; //pin for left lights int rightLights = 7; //pin for right lights //pin numbers for the motor on the robot int leftMotor1 = 11; int leftMotor2 = 12; int rightMotor1 = 10; int rightMotor2 = 9; //variables for delay for hit interrupt int interval = 5000; long lastBlinkTime = 0; //variables for delay for flashing lights int flashInterval = 100; long lastFlashTime = 0; int timesFlashed = 0; //variable for flashing lights boolean hit = false; void setup() { Serial.begin(9600);//init Serial library (make sure Processing is sending data at the same baud rate) servoX.attach(6); servoY.attach(5); //set pins as either an input or an output pinMode(ldrLR, INPUT); pinMode(ldrB, INPUT); pinMode(laser, OUTPUT); pinMode(headLights, OUTPUT); pinMode(leftLights, OUTPUT); pinMode(rightLights, OUTPUT); pinMode(leftMotor1, OUTPUT); pinMode(leftMotor2, OUTPUT); pinMode(rightMotor1, OUTPUT); pinMode(rightMotor2, OUTPUT); sei(); //Enable global interrupts EIMSK |= (1 << INT0); //Enable external interrupt INT0 EIMSK |= (1 << INT1); //Enable external interrupt INT1 EICRA |= (1 << ISC01); //Trigger INT0 on falling edge digitalWrite(laser, HIGH); //default laser off digitalWrite(headLights, HIGH); //default headlights on digitalWrite(leftLights, LOW); //default leftlights off digitalWrite(rightLights, LOW); //default rightlights off //default values for servos servoX.write(30); servoY.write(30); } void loop() { while(Serial.available() > 0) { //look for Serial data int incoming = Serial.read(); //read and store the value instruction[i] = incoming; //store the instruction in the array //loop around the instruction array (expecting 10 characters) for (j=0; j<10; j++) { //check that message is of form <XXXXXXXX> if ((instruction[j] == '<') && (instruction[j+9] == '>')) { //check that message is for robot 1 <1....> if (instruction[j+1] == '1') { //concatenate the two strings for x coordinates together stringXCoord += instruction[j+3]; stringXCoord += instruction[j+4]; //make the string an integer intXCoord = stringXCoord.toInt(); servoX.write(intXCoord); //write to the servo //concatenate the two strings for y coordinates together stringYCoord += instruction[j+5]; stringYCoord += instruction[j+6]; //make the string an integer intYCoord = stringYCoord.toInt(); servoY.write(intYCoord); //write to the servo //turn laser on if (instruction[j+2] == 'F') { digitalWrite(laser, LOW); } //turn laser off if (instruction[j+2] == 'f') { digitalWrite(laser, HIGH); } //control motors on robot - left motor forward if (instruction[j+7] == 'F') { digitalWrite(leftMotor1, LOW); digitalWrite(leftMotor2, HIGH); } //control motors on robot - left motor stop if (instruction[j+7] == 'S') { digitalWrite(leftMotor1, LOW); digitalWrite(leftMotor2, LOW); } //control motors on robot - left motor backward if (instruction[j+7] == 'B') { digitalWrite(leftMotor1, HIGH); digitalWrite(leftMotor2, LOW); } //control motors on robot - right motor forward if (instruction[j+8] == 'F') { digitalWrite(rightMotor1, HIGH); digitalWrite(rightMotor2, LOW); } //control motors on robot - right motor stop if (instruction[j+8] == 'S') { digitalWrite(rightMotor1, LOW); digitalWrite(rightMotor2, LOW); } //control motors on robot - right motor backward if (instruction[j+8] == 'B') { digitalWrite(rightMotor1, LOW); digitalWrite(rightMotor2, HIGH); } } //else check is message is for robot 2 <2...> else if (instruction[j+1] == '2') { //reset resend message to blank resendMessage = ""; int k = 0; //loop around the instruction message for (k = j; k<(j+10); k++) { //concatenate all characters of message resendMessage += instruction[k]; } Serial.print(0); //flush the serial port Serial.print(resendMessage); //send the message onto Robot 2 } //reset variable for instruction array i = -1; } } //if an ldr has been hit if (hit == true) { unsigned long flashMillis = millis(); //if delay between flashes has passed if ((flashMillis - lastFlashTime) > flashInterval) { lastFlashTime = flashMillis; if ((timesFlashed == 0) || (timesFlashed == 2) || (timesFlashed == 4) || (timesFlashed == 6) || (timesFlashed == 8) || (timesFlashed == 10) || (timesFlashed == 12) || (timesFlashed == 14) || (timesFlashed == 16) || (timesFlashed == 18)) { digitalWrite(leftLights, LOW); digitalWrite(rightLights, HIGH); } else if ((timesFlashed == 1) || (timesFlashed == 3) || (timesFlashed == 5) || (timesFlashed == 7) || (timesFlashed == 9) || (timesFlashed == 11) || (timesFlashed == 13) || (timesFlashed == 15) || (timesFlashed == 17) || (timesFlashed == 19)) { digitalWrite(leftLights, HIGH); digitalWrite(rightLights, LOW); } //turn lights back off else if ((timesFlashed == 20)) { digitalWrite(leftLights, LOW); digitalWrite(rightLights, LOW); hit = false; timesFlashed = -1; } timesFlashed++; } } //increment counter for instruction array i++; //reset variables used stringXCoord = ""; stringYCoord = ""; intXCoord = 0; intYCoord = 0; } //if an ldr has been hit if (hit == true) { unsigned long flashMillis = millis(); //if delay between flashed has passed if ((flashMillis - lastFlashTime) > flashInterval) { lastFlashTime = flashMillis; if ((timesFlashed == 0) || (timesFlashed == 2) || (timesFlashed == 4) || (timesFlashed == 6) || (timesFlashed == 8) || (timesFlashed == 10) || (timesFlashed == 12) || (timesFlashed == 14) || (timesFlashed == 16) || (timesFlashed == 18)) { digitalWrite(leftLights, LOW); digitalWrite(rightLights, HIGH); } else if ((timesFlashed == 1) || (timesFlashed == 3) || (timesFlashed == 5) || (timesFlashed == 7) || (timesFlashed == 9) || (timesFlashed == 11) || (timesFlashed == 13) || (timesFlashed == 15) || (timesFlashed == 17) || (timesFlashed == 19)) { digitalWrite(leftLights, HIGH); digitalWrite(rightLights, LOW); } //turn lights back off else if ((timesFlashed == 20)) { digitalWrite(leftLights, LOW); digitalWrite(rightLights, LOW); hit = false; timesFlashed = -1; } timesFlashed++; } } } //interrupt service routine for when the left/right LDR is hit ISR(INT0_vect) { unsigned long hitMillis = millis(); //if (delayBetweenHits > 5) if ((hitMillis - lastBlinkTime) > interval) { lastBlinkTime = hitMillis; Serial.print(0);//flush the serial port //print message to GM to increment Robot 2's score Serial.println(GMscoreLR); } //variable to flash lights hit = true; } //interrupt service routine for when the back LDR is hit ISR(INT1_vect) { unsigned long hitMillis = millis(); //if (delayBetweenHits > 5) if ((hitMillis - lastBlinkTime) > interval) { lastBlinkTime = hitMillis; Serial.print(0);//flush the serial port //print message to GM to increment Robot 2's score Serial.println(GMscoreB); } //variable to flash lights hit = true; }
Final Arduino Code for Robot 2
#include <Servo.h>
#include "avr/interrupt.h"
char instruction [10]; //instruction sent to arduino
int i = 0; //index for instruction array
int j = 0; //index for for loop
String stringXCoord = "";
int intXCoord; //x coordinate for servo
String stringYCoord = "";
int intYCoord; //y coordinate for servo
Servo servoX; //servo x
Servo servoY; //servo y
//message to send on to games master
String resendMessage = "";
//string to print to contact games master - in the following form:
//[ (start of message) G (games master) 1 (robot 1's score) 10 (how much to increment score) ] (end message)
String GMscoreLR = "[G105]";
String GMscoreB = "[G110]";
int laser = 13; //pin for laser
int ldrLR = 2; //pin for LDR interrupt 1
int ldrB = 3; //pin for LDR interrupt 2
int headLights = 4; //pin for front lights
int leftLights = 8; //pin for left lights
int rightLights = 7; //pin for right lights
//pin numbers for the motor on the robot
int leftMotor1 = 12;
int leftMotor2 = 11;
int rightMotor1 = 10;
int rightMotor2 = 9;
//variables for delay for hit interrupt
int interval = 5000;
long lastBlinkTime = 0;
//variables for delay for flashing lights
int flashInterval = 100;
long lastFlashTime = 0;
int timesFlashed = 0;
//variable for flashing lights
boolean hit = false;
void setup() {
Serial.begin(9600);//init Serial library (make sure Processing is sending data at the same baud rate)
servoX.attach(5);
servoY.attach(6);
//set pins as either an input or an output
pinMode(ldrLR, INPUT);
pinMode(ldrB, INPUT);
pinMode(laser, OUTPUT);
pinMode(headLights, OUTPUT);
pinMode(leftLights, OUTPUT);
pinMode(rightLights, OUTPUT);
pinMode(leftMotor1, OUTPUT);
pinMode(leftMotor2, OUTPUT);
pinMode(rightMotor1, OUTPUT);
pinMode(rightMotor2, OUTPUT);
sei(); //Enable global interrupts
EIMSK |= (1 << INT0); //Enable external interrupt INT0
EIMSK |= (1 << INT1); //Enable external interrupt INT1
EICRA |= (1 << ISC01); //Trigger INT0 on falling edge
digitalWrite(laser, HIGH); //default laser off
digitalWrite(headLights, HIGH); //default headlights on
digitalWrite(leftLights, LOW); //default leftlights off
digitalWrite(rightLights, LOW); //default rightlights off
//default values for servos
servoX.write(30);
servoY.write(30);
}
void loop() {
while(Serial.available() > 0) { //look for Serial data
int incoming = Serial.read(); //read and store the value
instruction[i] = incoming; //store the instruction in the array
//loop around the instruction array (expecting 10 characters)
for (j=0; j<10; j++)
{
//check that message is of form <XXXXXXXX>
if ((instruction[j] == '<') && (instruction[j+9] == '>'))
{
//check the message is for robot 2 <2...>
if (instruction[j+1] == '2')
{
//concatenate the two strings for x coordinates together
stringXCoord += instruction[j+3];
stringXCoord += instruction[j+4];
//make the string an integer
intXCoord = stringXCoord.toInt();
servoX.write(intXCoord); //write to the servo
//concatenate the two strings for y coordinates together
stringYCoord += instruction[j+5];
stringYCoord += instruction[j+6];
//make the string an integer
intYCoord = stringYCoord.toInt();
servoY.write(intYCoord); //write to the servo
//turn laser on
if (instruction[j+2] == 'F')
{
digitalWrite(laser, LOW);
}
//turn laser off
if (instruction[j+2] == 'f')
{
digitalWrite(laser, HIGH);
}
//control motors on robot - left motor forward
if (instruction[j+7] == 'F')
{
digitalWrite(leftMotor1, LOW);
digitalWrite(leftMotor2, HIGH);
}
//control motors on robot - left motor stop
if (instruction[j+7] == 'S')
{
digitalWrite(leftMotor1, LOW);
digitalWrite(leftMotor2, LOW);
}
//control motors on robot - left motor backward
if (instruction[j+7] == 'B')
{
digitalWrite(leftMotor1, HIGH);
digitalWrite(leftMotor2, LOW);
}
//control motors on robot - right motor forward
if (instruction[j+8] == 'F')
{
digitalWrite(rightMotor1, HIGH);
digitalWrite(rightMotor2, LOW);
}
//control motors on robot - right motor stop
if (instruction[j+8] == 'S')
{
digitalWrite(rightMotor1, LOW);
digitalWrite(rightMotor2, LOW);
}
//control motors on robot - right motor backward
if (instruction[j+8] == 'B')
{
digitalWrite(rightMotor1, LOW);
digitalWrite(rightMotor2, HIGH);
}
}
//reset variable for instruction array
i = -1;
}
}
for (j = 0; j<6; j++)
{
//if message is score packet for the Games Master
if ((instruction[j] == '[') && (instruction[j+5] == ']'))
{
//reset resend message to blank
resendMessage = "";
int k = 0;
//loop around the instruction message
for (k = j; k<(j+6); k++)
{
//concatenate all characters of message
resendMessage += instruction[k];
}
Serial.print(0); //flush the serial port
Serial.println(resendMessage); //send the message onto games master
}
}
//if an ldr has been hit
if (hit == true)
{
unsigned long flashMillis = millis();
//if delay between flashes has passed
if ((flashMillis - lastFlashTime) > flashInterval)
{
lastFlashTime = flashMillis;
if ((timesFlashed == 0) || (timesFlashed == 2) || (timesFlashed == 4) ||
(timesFlashed == 6) || (timesFlashed == 8) || (timesFlashed == 10)
|| (timesFlashed == 12) || (timesFlashed == 14) || (timesFlashed == 16)
|| (timesFlashed == 18))
{
digitalWrite(leftLights, LOW);
digitalWrite(rightLights, HIGH);
}
else if ((timesFlashed == 1) || (timesFlashed == 3) || (timesFlashed == 5) ||
(timesFlashed == 7) || (timesFlashed == 9) || (timesFlashed == 11)
|| (timesFlashed == 13) || (timesFlashed == 15) || (timesFlashed == 17)
|| (timesFlashed == 19))
{
digitalWrite(leftLights, HIGH);
digitalWrite(rightLights, LOW);
}
//turn lights back off
else if ((timesFlashed == 20))
{
digitalWrite(leftLights, LOW);
digitalWrite(rightLights, LOW);
hit = false;
timesFlashed = -1;
}
timesFlashed++;
}
}
//increment counter for instruction array
i++;
//reset variables used
stringXCoord = "";
stringYCoord = "";
intXCoord = 0;
intYCoord = 0;
}
//if an ldr has been hit
if (hit == true)
{
unsigned long flashMillis = millis();
//if delay between flashes has passed
if ((flashMillis - lastFlashTime) > flashInterval)
{
lastFlashTime = flashMillis;
if ((timesFlashed == 0) || (timesFlashed == 2) || (timesFlashed == 4) ||
(timesFlashed == 6) || (timesFlashed == 8) || (timesFlashed == 10)
|| (timesFlashed == 12) || (timesFlashed == 14) || (timesFlashed == 16)
|| (timesFlashed == 18))
{
digitalWrite(leftLights, LOW);
digitalWrite(rightLights, HIGH);
}
else if ((timesFlashed == 1) || (timesFlashed == 3) || (timesFlashed == 5) ||
(timesFlashed == 7) || (timesFlashed == 9) || (timesFlashed == 11)
|| (timesFlashed == 13) || (timesFlashed == 15) || (timesFlashed == 17)
|| (timesFlashed == 19))
{
digitalWrite(leftLights, HIGH);
digitalWrite(rightLights, LOW);
}
//turn lights back off
else if ((timesFlashed == 20))
{
digitalWrite(leftLights, LOW);
digitalWrite(rightLights, LOW);
hit = false;
timesFlashed = -1;
}
timesFlashed++;
}
}
}
//interrupt service routine for when the left/right LDR is hit
ISR(INT0_vect)
{
unsigned long hitMillis = millis();
//if (delayBetweenHits > 5)
if ((hitMillis - lastBlinkTime) > interval)
{
lastBlinkTime = hitMillis;
Serial.print(0);//flush the serial port
//print message to GM to increment Robot 1's score
Serial.println(GMscoreLR);
}
//variable to flash lights
hit = true;
}
//interrupt service routine for when the back LDR is hit
ISR(INT1_vect)
{
unsigned long hitMillis = millis();
//if (delayBetweenHits > 5)
if ((hitMillis - lastBlinkTime) > interval)
{
lastBlinkTime = hitMillis;
Serial.print(0);//flush the serial port
//print message to GM to increment Robot 1's score
Serial.println(GMscoreB);
}
//variable to flash lights
hit = true;
}
Final Processing Code for Games Master
import gifAnimation.*; //load animation library
import oscP5.*; // Load OSC P5 library
import netP5.*; // Load net P5 library
import processing.serial.*; // Load serial library
Serial arduinoPort; // Set arduinoPort as serial connection
OscP5 oscP5; // Set oscP5 as OSC connection
//addresses of iPads to send messages back
NetAddress iPad1;
NetAddress iPad2;
//background image on GUI
PImage bg;
//animation images
Gif player1Gif;
Gif player2Gif;
//variables used for sending instructions to robot 1
int xValue1 = 30;
int yValue1 = 30;
int laserFire1 =0;
float leftDrive1 = 0.5;
float rightDrive1 = 0.5;
char robotNo = 0;
//variables used for sending instructions to robot 2
int xValue2 = 30;
int yValue2 = 30;
int laserFire2 =0;
float leftDrive2 = 0.5;
float rightDrive2 = 0.5;
String oldMessage = "";
String newMessage = "";
String input = "";
//scores of robots
String score = "";
int robot1Score = 0;
int robot2Score = 0;
//Turns
boolean pOneTurn = true;
boolean pTwoTurn = false;
int timeLeft = 20;
//detecting sensor hits
int sideSensorHit1;
int rearSensorHit1;
int sideSensorHit2;
int rearSensorHit2;
int robotScore = 0;
//the score needed to win the game
int scoreToWin = 50;
//delays for updating sensors on GUIS
int sensorInterval = 5000;
long lastSensorTime = 0;
long sensorMillis;
long serialDelay = 0;
//delays for player turns
long playerMillis = 0;
long lastUpdate = 0;
boolean won = false;
void setup() {
size(960,600); // Processing screen size, dummy GUI for prototyping
bg = loadImage("Robot Skull.jpg"); //load background image
frameRate(100); //frame rate for animation
noStroke(); // We don’t want an outline or Stroke on our graphics
player1Gif = new Gif(this, "explosion_animation_gif.gif"); //load animation file
player1Gif.ignoreRepeat(); //ensure animation only occurs once
player2Gif = new Gif(this, "explosion_animation_gif.gif"); //load animation file
player2Gif.ignoreRepeat(); //ensure animation only occurs once
// Start oscP5, listening for incoming messages at port 8000
oscP5 = new OscP5(this,8000);
//set ipad addresses
iPad1 = new NetAddress("130.159.193.83",9000);
iPad2 = new NetAddress("130.159.193.160",9000);
// Set arduino to 9600 baud
arduinoPort = new Serial(this, Serial.list()[0], 9600);
}
//draw main GUI
void draw() {
background(bg);
textAlign(LEFT);
drawType1(width * 0.10);
textAlign(LEFT);
drawType2(width * 0.72);
drawTime(width * 0.10);
//draw animations
image(player1Gif, 10, height/2 - player1Gif.height/2);
image(player2Gif, 680, height/2 - player2Gif.height/2);
//count down time left for turns
if((millis() - playerMillis) > 1000){
if(won != true){
timeLeft = timeLeft - 1;
}
}
if(timeLeft < 0){
if(pOneTurn == true){
pOneTurn = false;
pTwoTurn = true;
arduinoPort.write(0);
arduinoPort.write("<1f3030SS>");
}else{
pOneTurn = true;
pTwoTurn = false;
arduinoPort.write(0);
arduinoPort.write("<2f3030SS>");
}
timeLeft = 20;
}
if((millis() - sensorMillis) >= sensorInterval){
sideSensorHit1 = 0;
sideSensorHit2 = 0;
rearSensorHit1 = 0;
rearSensorHit2 = 0;
}
playerMillis = millis();
//if player one has won
if(robot1Score >= scoreToWin) {
// update gui
won = true;
//draw that player one has won and flash animations
drawTypeWins1(width * 0.35);
player1Gif.loop();
player2Gif.loop();
}
//if player two has won
else if(robot2Score >= scoreToWin) {
//update gui
won = true;
//draw that player two has won and flash animations
drawTypeWins2(width * 0.35);
player1Gif.loop();
player2Gif.loop();
}
//update the gui on the ipads
updateIpads();
delay(1000);
}
//draw the time
void drawTime(float x)
{
textFont(createFont("Hall Fetica", 16));
text("Time: " + hour() + ":" + minute() + ":" + second() + " ", x, 20);
}
//draw laserbot 1 and its score
void drawType1(float x) {
textFont(createFont("Hall Fetica", 40));
line(x, 0, x, 65);
fill(255);
text("LaserBot 1", x, 95);
textFont(createFont("Hall Fetica", 28));
fill(209);
text("Score: " + robot1Score, x, 130);
}
//draw laserbot 2 and its score
void drawType2(float x) {
textFont(createFont("Hall Fetica", 40));
line(x, 0, x, 0);
fill(255);
text("LaserBot 2", x, 95);
textFont(createFont("Hall Fetica", 28));
fill(209);
text("Score: " + robot2Score, x, 130);
}
//draw that player 1 has won
void drawTypeWins1(float x) {
textFont(createFont("Hall Fetica", 40));
line(x, 0, x, 0);
fill(255);
text("Player One Wins", x, 570);
}
//draw that player 2 has won
void drawTypeWins2(float x) {
textFont(createFont("Hall Fetica", 40));
line(x, 0, x, 0);
fill(255);
text("Player Two Wins", x, 570);
}
//This runs whenever there is a new OSC message from an ipad
void oscEvent(OscMessage theOscMessage) {
// Creates a string out of the OSC message
String addr = theOscMessage.addrPattern();
if(!addr.equals("/ping")){
robotNo = addr.charAt(1);
//check which robot is wanting to send a message
if(robotNo == '1'){
OscMessage oneMessage = theOscMessage;
parseAndSendOne(oneMessage);
}else{
OscMessage twoMessage = theOscMessage;
parseAndSendTwo(twoMessage);
}
}
}
//method to send messages to robot one
void parseAndSendOne(OscMessage theOscMessage){
// Creates a string out of the OSC message
String addr = theOscMessage.addrPattern();
String restOfString = addr.substring(3);
if(restOfString.equals("fireButton")){
laserFire1 = int(theOscMessage.get(0).floatValue());
}
if(restOfString.equals("up")){ // Filters out
//
if(yValue1 != 50){ // Upper limit
yValue1 = yValue1 + int(theOscMessage.get(0).floatValue())*1; //5 degrees per button
}
}
if(restOfString.equals("down")){ // Filters out
//
if(yValue1 != 10){ // Lower limit
yValue1 = yValue1 - int(theOscMessage.get(0).floatValue())*1; //5 degrees per button
}
}
if(restOfString.equals("right")){ // Filters out
//
if(xValue1 != 50){ // Upper limit
xValue1 = xValue1 + int(theOscMessage.get(0).floatValue())*1; //5 degrees per button
}
}
if(restOfString.equals("left")){ // Filters out
//
if(xValue1 != 10){ // Lower limit
xValue1 = xValue1 - int(theOscMessage.get(0).floatValue())*1; //5 degrees per button
}
}
if(restOfString.equals("leftDrive")){ // Filters out
leftDrive1 = theOscMessage.get(0).floatValue();
}
if(restOfString.equals("rightDrive")){ // Filters out
rightDrive1 = theOscMessage.get(0).floatValue();
}
if(laserFire1 == 0){ // If led button 1 if off do....
newMessage = "<1f"+xValue1+yValue1;
}else{
newMessage = "<1F"+xValue1+yValue1;
}
//---------------------------------LEFT
if(leftDrive1 > 0.75){
newMessage = newMessage + "F";
}
else if(leftDrive1 < 0.25){
newMessage = newMessage + "B";
}
else if(leftDrive1 <= 0.75 && leftDrive1 >= 0.25 ){
newMessage = newMessage + "S";
}
//--------------------------------RIGHT
if(rightDrive1 > 0.75){
newMessage = newMessage + "F>";
}
else if(rightDrive1 < 0.25){
newMessage = newMessage + "B>";
}
else if(rightDrive1 <= 0.75 && rightDrive1 >= 0.25 ){
newMessage = newMessage + "S>";
}
if(won == false){
if(pOneTurn == true){
if(!oldMessage.equals(newMessage)){ // Check if message has changed.
arduinoPort.write(0); // FLUSH SERIAL PORT BEFORE SENDING TO AVOID CONFUSION
arduinoPort.write(newMessage); // send message
println(newMessage); // print to console
oldMessage = newMessage;
delay(50);
}
}
}
}
//method to send messages to robot two
void parseAndSendTwo(OscMessage theOscMessage){
// Creates a string out of the OSC message
String addr = theOscMessage.addrPattern();
String restOfString = addr.substring(3);
if(restOfString.equals("fireButton")){
laserFire2 = int(theOscMessage.get(0).floatValue());
}
if(restOfString.equals("up")){ // Filters out
//
if(yValue2 != 50){ // Upper limit
yValue2 = yValue2 + int(theOscMessage.get(0).floatValue())*1; //5 degrees per button
}
}
if(restOfString.equals("down")){ // Filters out
//
if(yValue2 != 10){ // Lower limit
yValue2 = yValue2 - int(theOscMessage.get(0).floatValue())*1; //5 degrees per button
}
}
if(restOfString.equals("right")){ // Filters out
//
if(xValue2 != 50){ // Upper limit
xValue2 = xValue2 + int(theOscMessage.get(0).floatValue())*1; //5 degrees per button
}
}
if(restOfString.equals("left")){ // Filters out
//
if(xValue2 != 10){ // Lower limit
xValue2 = xValue2 - int(theOscMessage.get(0).floatValue())*1; //5 degrees per button
}
}
if(restOfString.equals("leftDrive")){ // Filters out
leftDrive2 = theOscMessage.get(0).floatValue();
}
if(restOfString.equals("rightDrive")){ // Filters out
rightDrive2 = theOscMessage.get(0).floatValue();
}
if(laserFire2 == 0){ // If led button 1 if off do....
newMessage = "<2f"+xValue2+yValue2;
}else{
newMessage = "<2F"+xValue2+yValue2;
}
//---------------------------------LEFT
if(leftDrive2 > 0.75){
newMessage = newMessage + "F";
}
else if(leftDrive2 < 0.25){
newMessage = newMessage + "B";
}
else if(leftDrive2 < 0.75 && leftDrive2 > 0.25 ){
newMessage = newMessage + "S";
}
//--------------------------------RIGHT
if(rightDrive2 > 0.75){
newMessage = newMessage + "F>";
}
else if(rightDrive2 < 0.25){
newMessage = newMessage + "B>";
}
else if(rightDrive2 < 0.75 && rightDrive2 > 0.25 ){
newMessage = newMessage + "S>";
}
if(won == false){
if(pTwoTurn == true){
if(!oldMessage.equals(newMessage)){ // Check if message has changed.
arduinoPort.write(0); // FLUSH SERIAL PORT BEFORE SENDING TO AVOID CONFUSION
arduinoPort.write(newMessage); // send message
println(newMessage); // print to console
oldMessage = newMessage;
delay(50);
}
}
}
}
//method to update the gui on the ipads
void updateIpads(){
OscBundle updateBundle = new OscBundle();
//First iPad GUI update
OscMessage message = new OscMessage("/1/playerScore");
message.add(robot1Score);
updateBundle.add(message);
message.clear();
message = new OscMessage("/1/opponentScore");
message.add(robot2Score);
updateBundle.add(message);
message.clear();
message = new OscMessage("/1/ScoreToWin");
message.add(scoreToWin);
updateBundle.add(message);
message.clear();
message = new OscMessage("/1/TimeRemaining");
message.add(timeLeft);
updateBundle.add(message);
message.clear();
message = new OscMessage("/1/sideSensorLED");
message.add(sideSensorHit1);
updateBundle.add(message);
message.clear();
message = new OscMessage("/1/rearSensorLED");
message.add(rearSensorHit1);
updateBundle.add(message);
message.clear();
message = new OscMessage("/1/playerLED");
message.add(pOneTurn);
updateBundle.add(message);
message.clear();
message = new OscMessage("/1/opponentLED");
message.add(pTwoTurn);
updateBundle.add(message);
message.clear();
//sending update
oscP5.send(updateBundle, iPad1);
updateBundle.clear();
//iPad 2 update... same again
updateBundle = new OscBundle();
//First iPad GUI update
message = new OscMessage("/2/playerScore");
message.add(robot2Score);
updateBundle.add(message);
message.clear();
message = new OscMessage("/2/opponentScore");
message.add(robot1Score);
updateBundle.add(message);
message.clear();
message = new OscMessage("/2/ScoreToWin");
message.add(scoreToWin);
updateBundle.add(message);
message.clear();
message = new OscMessage("/2/TimeRemaining");
message.add(timeLeft);
updateBundle.add(message);
message.clear();
message = new OscMessage("/2/sideSensorLED");
message.add(sideSensorHit2);
updateBundle.add(message);
message.clear();
message = new OscMessage("/2/rearSensorLED");
message.add(rearSensorHit2);
updateBundle.add(message);
message.clear();
message = new OscMessage("/2/playerLED");
message.add(pTwoTurn);
updateBundle.add(message);
message.clear();
message = new OscMessage("/2/opponentLED");
message.add(pOneTurn);
updateBundle.add(message);
message.clear();
//sending update
oscP5.send(updateBundle, iPad2);
updateBundle.clear();
}
//method to parse any messages received from an arduino
void serialEvent(Serial arduinoPort)
//overrides serial event, so that Arduino can echo received messages
// (serial monitor in Arduino IDE not available as processing is using serial port)
{
input = arduinoPort.readStringUntil('\n');
print(input);
if (input != null){
//should be [
char bracketStart = input.charAt(1);
//should be G
char gamesMaster = input.charAt(2);
//should be either 1 or 2
char robotID = input.charAt(3);
if (bracketStart == '[')
{
if (gamesMaster == 'G')
{
char score1 = input.charAt(4);
char score2 = input.charAt(5);
score = "";
//concatenate the 2 numbers together for the score
score += score1;
score += score2;
//convert to an int
robotScore = Integer.parseInt(score);
//delay for sensor hit lights
sensorMillis = millis();
//delay for events
if (won == false){
if((millis() - serialDelay) > 5000){
//increment the correct robots score
if (robotID == '1')
{
robot1Score = robot1Score + robotScore;
//if score is 5, side sensor has been hit
if (robotScore == 5)
{
sideSensorHit2 = 1;
}
//if score is 10, rear sensor has been hit
else if (robotScore == 10)
{
rearSensorHit2 = 1;
}
//play animation of explosion
player2Gif.play();
}
else if (robotID == '2')
{
robot2Score = robot2Score + robotScore;
//if score is 5, side sensor has been hit
if (robotScore == 5)
{
sideSensorHit1 = 1;
}
//if score is 10, rear sensor has been hit
else if (robotScore == 10)
{
rearSensorHit1 = 1;
}
//play animation of explosion
player1Gif.play();
}
}
}
}
}
input = "";
serialDelay = millis();
}
}